Skip to content

Commit a41c123

Browse files
committed
fix SPI init issues as described http://code.google.com/p/arduino/issues/detail?id=888. Tested using qt1110 (mode 3) on a mega and WS5100 (webserver example sketch) chips on a etherten
1 parent 308d907 commit a41c123

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

libraries/SPI/SPI.cpp

+18-11
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,34 @@
1414
SPIClass SPI;
1515

1616
void SPIClass::begin() {
17-
// Set direction register for SCK and MOSI pin.
18-
// MISO pin automatically overrides to INPUT.
17+
18+
// Set SS to high because many users use this as chip-select
19+
// and most chips "select" when SS is high. If your chip
20+
// does not, you can set it back to LOW after begin() is called.
21+
digitalWrite(SS, HIGH);
22+
1923
// When the SS pin is set as OUTPUT, it can be used as
2024
// a general purpose output port (it doesn't influence
2125
// SPI operations).
22-
23-
pinMode(SCK, OUTPUT);
24-
pinMode(MOSI, OUTPUT);
2526
pinMode(SS, OUTPUT);
26-
27-
digitalWrite(SCK, LOW);
28-
digitalWrite(MOSI, LOW);
29-
digitalWrite(SS, HIGH);
3027

31-
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
32-
// automatically switches to Slave, so the data direction of
28+
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
29+
// automatically switches to Slave, so the data direction of
3330
// the SS pin MUST be kept as OUTPUT.
3431
SPCR |= _BV(MSTR);
3532
SPCR |= _BV(SPE);
33+
34+
// Set direction register for SCK and MOSI pin.
35+
// MISO pin automatically overrides to INPUT.
36+
// By doing this AFTER enabling SPI, we avoid accidentally
37+
// clocking in a single bit since the lines go directly
38+
// from "input" to SPI control.
39+
// http://code.google.com/p/arduino/issues/detail?id=888
40+
pinMode(SCK, OUTPUT);
41+
pinMode(MOSI, OUTPUT);
3642
}
3743

44+
3845
void SPIClass::end() {
3946
SPCR &= ~_BV(SPE);
4047
}

0 commit comments

Comments
 (0)