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
This commit is contained in:
David A. Mellis 2012-06-06 06:45:08 -07:00
commit a363686a31
1 changed files with 16 additions and 11 deletions

View File

@ -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);
}