summaryrefslogtreecommitdiff
path: root/src/arch/arduino-nano/driver
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2019-12-20 12:58:04 +0100
committerDaniel Friesel <daniel.friesel@uos.de>2019-12-20 12:58:04 +0100
commit0f422f5332f8868aee277cc621e2f1633506b1c3 (patch)
treee32eceadbd74c97bb7e1b48e7f6dd54c7b2bbf24 /src/arch/arduino-nano/driver
parent664c1b6c157cb6f8dc87ea0c7931589306fc10eb (diff)
add untested Arduino Nano SPI driver
Diffstat (limited to 'src/arch/arduino-nano/driver')
-rw-r--r--src/arch/arduino-nano/driver/spi.cc41
1 files changed, 41 insertions, 0 deletions
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;