aboutsummaryrefslogtreecommitdiff
path: root/libraries/SPI/SPI.cpp
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@bug.st>2012-01-04 15:14:51 +0100
committerCristian Maglie <c.maglie@bug.st>2012-01-04 15:14:51 +0100
commit8c2b5b979a75d109ae7cc306afc50d7ffe1e0366 (patch)
tree19ac8d91c7209397e7dc890b432a254ae9a7cd62 /libraries/SPI/SPI.cpp
parentbe52c95820400d48e47615232d3c38236166fad3 (diff)
Moved libraries folder inside platform folder. Now libraries and examples are searched per board/platform
Diffstat (limited to 'libraries/SPI/SPI.cpp')
-rw-r--r--libraries/SPI/SPI.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp
new file mode 100644
index 0000000..42915df
--- /dev/null
+++ b/libraries/SPI/SPI.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
+ * SPI Master library for arduino.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ */
+
+#include "pins_arduino.h"
+#include "SPI.h"
+
+SPIClass SPI;
+
+void SPIClass::begin() {
+ // Set direction register for SCK and MOSI pin.
+ // MISO pin automatically overrides to INPUT.
+ // When the SS pin is set as OUTPUT, it can be used as
+ // a general purpose output port (it doesn't influence
+ // SPI operations).
+
+ pinMode(SCK, OUTPUT);
+ pinMode(MOSI, OUTPUT);
+ pinMode(SS, OUTPUT);
+
+ digitalWrite(SCK, LOW);
+ digitalWrite(MOSI, LOW);
+ digitalWrite(SS, HIGH);
+
+ // Warning: if the SS pin ever becomes a LOW INPUT then SPI
+ // automatically switches to Slave, so the data direction of
+ // the SS pin MUST be kept as OUTPUT.
+ SPCR |= _BV(MSTR);
+ SPCR |= _BV(SPE);
+}
+
+void SPIClass::end() {
+ SPCR &= ~_BV(SPE);
+}
+
+void SPIClass::setBitOrder(uint8_t bitOrder)
+{
+ if(bitOrder == LSBFIRST) {
+ SPCR |= _BV(DORD);
+ } else {
+ SPCR &= ~(_BV(DORD));
+ }
+}
+
+void SPIClass::setDataMode(uint8_t mode)
+{
+ SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
+}
+
+void SPIClass::setClockDivider(uint8_t rate)
+{
+ SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
+ SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
+}
+