Skip to content

Commit a363686

Browse files
committed
Merge pull request #95 from gandrewstone/master
Allow the SPI library to support different modes Avoid spurious transitions on the SPI lines by activating the SPI bus before setting MOSI and SCK as outputs. That way they go straight to the correct values. Also, set SS HIGH before setting it as an OUTPUT, so the slave device isn't temporarily activated. http://code.google.com/p/arduino/issues/detail?id=888
2 parents 002baa7 + 4e8fffa commit a363686

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

libraries/SPI/SPI.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,32 @@
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 so a connected chip will be "deselected" by default
19+
digitalWrite(SS, HIGH);
20+
1921
// When the SS pin is set as OUTPUT, it can be used as
2022
// a general purpose output port (it doesn't influence
2123
// SPI operations).
22-
23-
pinMode(SCK, OUTPUT);
24-
pinMode(MOSI, OUTPUT);
2524
pinMode(SS, OUTPUT);
26-
27-
digitalWrite(SCK, LOW);
28-
digitalWrite(MOSI, LOW);
29-
digitalWrite(SS, HIGH);
3025

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

42+
3843
void SPIClass::end() {
3944
SPCR &= ~_BV(SPE);
4045
}

0 commit comments

Comments
 (0)