diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/arch/arduino-nano-168/Makefile.inc | 4 | ||||
-rw-r--r-- | src/arch/arduino-nano/Makefile.inc | 4 | ||||
-rw-r--r-- | src/arch/arduino-nano/arch.cc | 8 | ||||
-rw-r--r-- | src/arch/arduino-nano/driver/spi.cc | 41 |
4 files changed, 55 insertions, 2 deletions
diff --git a/src/arch/arduino-nano-168/Makefile.inc b/src/arch/arduino-nano-168/Makefile.inc index 3b72c4d..f57914e 100644 --- a/src/arch/arduino-nano-168/Makefile.inc +++ b/src/arch/arduino-nano-168/Makefile.inc @@ -37,6 +37,10 @@ else ifneq ($(findstring i2c,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/i2c.cc endif +ifneq ($(findstring spi,${arch_drivers}), ) + CXX_TARGETS += src/arch/arduino-nano/driver/spi.cc +endif + ifneq ($(findstring stdin,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/stdin.cc endif diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc index 9e149ac..ebc77ea 100644 --- a/src/arch/arduino-nano/Makefile.inc +++ b/src/arch/arduino-nano/Makefile.inc @@ -37,6 +37,10 @@ else ifneq ($(findstring i2c,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/i2c.cc endif +ifneq ($(findstring spi,${arch_drivers}), ) + CXX_TARGETS += src/arch/arduino-nano/driver/spi.cc +endif + ifneq ($(findstring stdin,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/stdin.cc endif diff --git a/src/arch/arduino-nano/arch.cc b/src/arch/arduino-nano/arch.cc index 221ef5b..1023927 100644 --- a/src/arch/arduino-nano/arch.cc +++ b/src/arch/arduino-nano/arch.cc @@ -105,12 +105,16 @@ void Arch::idle(void) void Arch::delay_us(unsigned int const us) { - _delay_us(us); + for (unsigned int i = 0; i < us; i++) { + _delay_us(1); + } } void Arch::delay_ms(unsigned int const ms) { - _delay_ms(ms); + for (unsigned int i = 0; i < ms; i++) { + _delay_ms(1); + } } Arch arch; diff --git a/src/arch/arduino-nano/driver/spi.cc b/src/arch/arduino-nano/driver/spi.cc new file mode 100644 index 0000000..86aa311 --- /dev/null +++ b/src/arch/arduino-nano/driver/spi.cc @@ -0,0 +1,41 @@ +#include "driver/spi.h" +#include "driver/gpio.h" +#include "arch.h" +#include <avr/io.h> + +signed char SPI::setup() +{ + // configure SS as output to avoid unintened switches to slave mode + gpio.output(GPIO::pb2, 0); + // up to 1 MHz SPI clock +#if F_CPU == 16000000UL + SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0); +#elif F_CPU == 8000000UL + SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0); +#else + SPCR = _BV(SPE) | _BV(MSTR); +#endif +} + +signed char SPI::xmit( + unsigned char tx_len, unsigned char *tx_buf, + unsigned char rx_len, unsigned char *rx_buf) +{ + unsigned char i; + + while ((i < tx_len) || (i < rx_len)) { + if (i < tx_len) { + SPDR = tx_buf[i]; + } else { + SPDR = 0; + } + while (!(SPSR & _BV(SPIF))) ; + if (i < rx_len) { + rx_buf[i] = SPDR; + } + i++; + } + return 0; +} + +SPI spi; |