diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | include/arch/arduino-nano/driver/timer.h | 28 | ||||
-rw-r--r-- | src/arch/arduino-nano-168/Makefile.inc | 74 | ||||
l--------- | src/arch/arduino-nano-168/arch.cc | 1 | ||||
l--------- | src/arch/arduino-nano-168/driver | 1 | ||||
-rw-r--r-- | src/arch/arduino-nano/Makefile.inc | 9 | ||||
-rw-r--r-- | src/arch/arduino-nano/driver/timer.cc | 3 | ||||
-rw-r--r-- | src/driver/soft_i2c.cc | 162 |
8 files changed, 283 insertions, 0 deletions
@@ -56,6 +56,11 @@ ifeq (${softi2c_pullup}, 1) COMMON_FLAGS += -DSOFTI2C_PULLUP endif +ifeq (${softi2c_timer}, 1) + arch_drivers += ,timer + COMMON_FLAGS += -DSOFTI2C_TIMER +endif + ifeq (${kout_nop}, 1) COMMON_FLAGS += -DKOUT_NOP endif diff --git a/include/arch/arduino-nano/driver/timer.h b/include/arch/arduino-nano/driver/timer.h new file mode 100644 index 0000000..267cb1d --- /dev/null +++ b/include/arch/arduino-nano/driver/timer.h @@ -0,0 +1,28 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +#define ON_TIMER_INTERRUPT ISR(TIMER0_COMPA_vect) + +class Timer { + private: + Timer(const Timer ©); + + + public: + Timer() {} + + inline void setup(unsigned char const frequency) { + OCR0A = frequency ? 255 / frequency : 1; + TCCR0A = _BV(WGM01); + } + inline void start(unsigned char const interrupt) { + TCNT0 = 0; + TCCR0B = _BV(CS01) | _BV(CS00); + if (interrupt) { + TIMSK0 = _BV(OCIE0A); + } + } + inline void stop() { TCCR0B = 0; TIMSK0 = 0; } +}; + +extern Timer timer; diff --git a/src/arch/arduino-nano-168/Makefile.inc b/src/arch/arduino-nano-168/Makefile.inc new file mode 100644 index 0000000..de4ac8b --- /dev/null +++ b/src/arch/arduino-nano-168/Makefile.inc @@ -0,0 +1,74 @@ +# vim:ft=make + +MCU = atmega168 +PORT = /dev/ttyUSB0 +BAUD = 19200 + +COMMON_FLAGS += -Werror=overflow +COMMON_FLAGS += -mmcu=${MCU} -DF_CPU=16000000UL -DMULTIPASS_ARCH_arduino_nano +COMMON_FLAGS += -flto +COMMON_FLAGS += -DMULTIPASS_ARCH_HAS_I2C + +CC = avr-gcc +CXX = avr-g++ +NM = avr-nm +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump + +ifeq (${aspectc}, 1) + CXX = ag++ -r build/repo.acp -v 0 --c_compiler avr-g++ -p . --Xcompiler +endif + +TARGETS += src/arch/arduino-nano/arch.cc +TARGETS += src/arch/arduino-nano/driver/gpio.cc +TARGETS += src/arch/arduino-nano/driver/stdout.cc +TARGETS += src/arch/arduino-nano/driver/uptime.cc + +ifneq ($(findstring softi2c,${drivers}), ) +else ifneq ($(findstring i2c,${arch_drivers}), ) + TARGETS += src/arch/arduino-nano/driver/i2c.cc +endif + +ifneq ($(findstring stdin,${arch_drivers}), ) + TARGETS += src/arch/arduino-nano/driver/stdin.cc +endif + +ifneq ($(findstring timer,${arch_drivers}), ) + TARGETS += src/arch/arduino-nano/driver/timer.cc +endif + +ifneq (${i2c_freq}, ) + COMMON_FLAGS += -DF_I2C=${i2c_freq} +endif + +ifneq (${timer_freq}, ) + COMMON_FLAGS += -DF_TIMER=${timer_freq} +endif + +OBJECTS = ${TARGETS:.cc=.o} + +.cc.o: + ${CXX} ${INCLUDES} ${COMMON_FLAGS} ${CXXFLAGS} -c -o $@ ${@:.o=.cc} + +build/system.elf: ${OBJECTS} + ${CXX} ${COMMON_FLAGS} ${CXXFLAGS} -Wl,--gc-sections -o $@ ${OBJECTS} + avr-size --format=avr --mcu=${MCU} $@ + +build/system.hex: build/system.elf + ${OBJCOPY} -O ihex ${@:.hex=.elf} $@ + +program: build/system.hex + avrdude -p ${MCU} -c arduino -P ${PORT} -b ${BAUD} -U flash:w:build/system.hex + +arch_clean: + rm -f ${OBJECTS} build/system.hex + +monitor: + screen ${PORT} 115200 + +arch_help: + @echo "arduino-nano specific flags:" + @echo " PORT = ${PORT}" + @echo " BAUD = ${BAUD} (only used for programming)" + +.PHONY: arch_clean arch_help monitor program diff --git a/src/arch/arduino-nano-168/arch.cc b/src/arch/arduino-nano-168/arch.cc new file mode 120000 index 0000000..808ad49 --- /dev/null +++ b/src/arch/arduino-nano-168/arch.cc @@ -0,0 +1 @@ +../arduino-nano/arch.cc
\ No newline at end of file diff --git a/src/arch/arduino-nano-168/driver b/src/arch/arduino-nano-168/driver new file mode 120000 index 0000000..d402f72 --- /dev/null +++ b/src/arch/arduino-nano-168/driver @@ -0,0 +1 @@ +../arduino-nano/driver
\ No newline at end of file diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc index 18a6563..d4d5be7 100644 --- a/src/arch/arduino-nano/Makefile.inc +++ b/src/arch/arduino-nano/Makefile.inc @@ -4,6 +4,7 @@ MCU = atmega328p PORT = /dev/ttyUSB0 BAUD = 57600 +COMMON_FLAGS += -Werror=overflow COMMON_FLAGS += -mmcu=${MCU} -DF_CPU=16000000UL -DMULTIPASS_ARCH_arduino_nano COMMON_FLAGS += -flto COMMON_FLAGS += -DMULTIPASS_ARCH_HAS_I2C @@ -32,10 +33,18 @@ ifneq ($(findstring stdin,${arch_drivers}), ) TARGETS += src/arch/arduino-nano/driver/stdin.cc endif +ifneq ($(findstring timer,${arch_drivers}), ) + TARGETS += src/arch/arduino-nano/driver/timer.cc +endif + ifneq (${i2c_freq}, ) COMMON_FLAGS += -DF_I2C=${i2c_freq} endif +ifneq (${timer_freq}, ) + COMMON_FLAGS += -DF_TIMER=${timer_freq} +endif + OBJECTS = ${TARGETS:.cc=.o} .cc.o: diff --git a/src/arch/arduino-nano/driver/timer.cc b/src/arch/arduino-nano/driver/timer.cc new file mode 100644 index 0000000..4f2d6d1 --- /dev/null +++ b/src/arch/arduino-nano/driver/timer.cc @@ -0,0 +1,3 @@ +#include "driver/timer.h" + +Timer timer; diff --git a/src/driver/soft_i2c.cc b/src/driver/soft_i2c.cc index 33f77ab..99a88aa 100644 --- a/src/driver/soft_i2c.cc +++ b/src/driver/soft_i2c.cc @@ -2,6 +2,13 @@ #include "driver/gpio.h" #include "arch.h" +#ifdef SOFTI2C_TIMER +#ifdef TIMER_CYCLES +#error "SOFTI2C_TIMER and TIMER_CYCLES are mutually exclusive" +#endif +#include "driver/timer.h" +#endif + #ifdef SOFTI2C_PULLUP #define SDA_HIGH gpio.input(sda, 1) #define SDA_LOW gpio.output(sda, 0) @@ -14,6 +21,8 @@ #define SCL_LOW gpio.output(scl) #endif +#ifndef SOFTI2C_TIMER + signed char SoftI2C::setup() { SDA_HIGH; @@ -136,6 +145,159 @@ signed char SoftI2C::xmit(unsigned char address, return 0; } +#else + +#ifndef F_I2C +#define F_I2C 100000 +#endif + +volatile unsigned char timer_done = 0; + +inline void await_timer() +{ + timer_done = 0; + timer.start(1); + while (!timer_done) { + arch.idle(); + } + timer.stop(); +} + +signed char SoftI2C::setup() +{ + SDA_HIGH; + SCL_HIGH; + /* + * I2C frequency is the time between two SCL low->high transitions + * (or high->low, whatever you prefer). For the timer, we need to set the + * time between SCL low->high and the following high->low transition + * (and vice versa), which is twice the desired I2C frequency. Also, + * timer.setup wants kHz and not Hz, so we have + * Timer Freq [kHz] = I2C Freq [Hz] * 2 / 1000 + */ + timer.setup(F_I2C / 500); + return 0; +} + +void SoftI2C::start() +{ + SDA_HIGH; + SCL_HIGH; + await_timer(); + SDA_LOW; + await_timer(); + SCL_LOW; + await_timer(); +} + +void SoftI2C::stop() +{ + SCL_LOW; + SDA_LOW; + await_timer(); + SCL_HIGH; + await_timer(); + SDA_HIGH; +} + +bool SoftI2C::tx(unsigned char byte) +{ + unsigned char got_ack = 0; + for (unsigned char i = 0; i <= 8; i++) { + if ((byte & 0x80) || (i == 8)) { + SDA_HIGH; + } else { + SDA_LOW; + } + byte <<= 1; + SCL_HIGH; + await_timer(); + while (!gpio.read(scl)) ; + if (i == 8) { + if (!gpio.read(sda)) { + got_ack = 1; + } + } + SCL_LOW; + await_timer(); + } + return got_ack; +} + +unsigned char SoftI2C::rx(bool send_ack) +{ + unsigned char byte = 0; + SDA_HIGH; + for (unsigned char i = 0; i <= 8; i++) { + SCL_HIGH; + await_timer(); + while (!gpio.read(scl)) ; + if ((i < 8) && gpio.read(sda)) { + byte |= 1 << (7 - i); + } + SCL_LOW; + await_timer(); + if ((i == 7) && send_ack) { + SDA_LOW; + } else if ((i == 8) && send_ack) { + SDA_HIGH; + } + } + return byte; +} + +void SoftI2C::scan(unsigned int *results) +{ + unsigned char i2caddr; + for (unsigned char address = 0; address < 128; address++) { + + i2caddr = (address << 1) | 0; + + start(); + + if (tx(i2caddr)) { + results[address / (8 * sizeof(unsigned int))] |= 1 << (address % (8 * sizeof(unsigned int))); + stop(); + } + } + stop(); +} + +signed char SoftI2C::xmit(unsigned char address, + unsigned char tx_len, unsigned char *tx_buf, + unsigned char rx_len, unsigned char *rx_buf) +{ + unsigned char i; + + if (tx_len) { + start(); + tx((address << 1) | 0); + + for (i = 0; i < tx_len; i++) { + tx(tx_buf[i]); + } + } + if (rx_len) { + start(); + tx((address << 1) | 1); + + for (i = 1; i <= rx_len; i++) { + rx_buf[i-1] = rx((i < rx_len) * 1); + } + } + + stop(); + + return 0; +} + +ON_TIMER_INTERRUPT +{ + timer_done = 1; +} + +#endif + #ifdef MULTIPASS_ARCH_esp8266 SoftI2C i2c(GPIO::d7, GPIO::d8); #elif MULTIPASS_ARCH_arduino_nano |