aboutsummaryrefslogtreecommitdiff
path: root/libraries/SPI
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/SPI')
-rw-r--r--libraries/SPI/SPI.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp
index 42915df..5e48073 100644
--- a/libraries/SPI/SPI.cpp
+++ b/libraries/SPI/SPI.cpp
@@ -14,27 +14,32 @@
SPIClass SPI;
void SPIClass::begin() {
- // Set direction register for SCK and MOSI pin.
- // MISO pin automatically overrides to INPUT.
+
+ // Set SS to high so a connected chip will be "deselected" by default
+ digitalWrite(SS, HIGH);
+
// 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
+ // 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);
+
+ // Set direction register for SCK and MOSI pin.
+ // MISO pin automatically overrides to INPUT.
+ // By doing this AFTER enabling SPI, we avoid accidentally
+ // clocking in a single bit since the lines go directly
+ // from "input" to SPI control.
+ // http://code.google.com/p/arduino/issues/detail?id=888
+ pinMode(SCK, OUTPUT);
+ pinMode(MOSI, OUTPUT);
}
+
void SPIClass::end() {
SPCR &= ~_BV(SPE);
}