Unintrusive SPI transactions & misc fixes

This commit is contained in:
Felix Rusu 2014-07-29 17:10:06 -04:00
parent 43e0150b1b
commit 57715ad5c0
2 changed files with 20 additions and 20 deletions

View File

@ -32,38 +32,36 @@ SPIFlash::SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID) {
/// Select the flash chip /// Select the flash chip
void SPIFlash::select() { void SPIFlash::select() {
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(33000000, MSBFIRST, SPI_MODE0));
#else
noInterrupts(); noInterrupts();
#endif //save current SPI settings
_SPCR = SPCR;
_SPSR = SPSR;
//set FLASH chip SPI settings
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV4); //decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
SPI.begin();
digitalWrite(_slaveSelectPin, LOW); digitalWrite(_slaveSelectPin, LOW);
} }
/// UNselect the flash chip /// UNselect the flash chip
void SPIFlash::unselect() { void SPIFlash::unselect() {
digitalWrite(_slaveSelectPin, HIGH); digitalWrite(_slaveSelectPin, HIGH);
#ifdef SPI_HAS_TRANSACTION //restore SPI settings to what they were before talking to the FLASH chip
SPI.endTransaction(); SPCR = _SPCR;
#else SPSR = _SPSR;
interrupts(); interrupts();
#endif
} }
/// setup SPI, read device ID etc... /// setup SPI, read device ID etc...
boolean SPIFlash::initialize() boolean SPIFlash::initialize()
{ {
_SPCR = SPCR;
_SPSR = SPSR;
pinMode(_slaveSelectPin, OUTPUT); pinMode(_slaveSelectPin, OUTPUT);
#ifdef SPI_HAS_TRANSACTION
digitalWrite(_slaveSelectPin, HIGH);
#else
unselect(); unselect();
SPI.setDataMode(SPI_MODE0); wakeup();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2); //max speed, except on Due which can run at system clock speed
#endif
SPI.begin();
if (_jedecID == 0 || readDeviceId() == _jedecID) { if (_jedecID == 0 || readDeviceId() == _jedecID) {
command(SPIFLASH_STATUSWRITE, true); // Write Status Register command(SPIFLASH_STATUSWRITE, true); // Write Status Register
SPI.transfer(0); // Global Unprotect SPI.transfer(0); // Global Unprotect
@ -228,16 +226,16 @@ void SPIFlash::blockErase32K(long addr) {
} }
void SPIFlash::sleep() { void SPIFlash::sleep() {
command(SPIFLASH_SLEEP); // Block Erase command(SPIFLASH_SLEEP);
unselect(); unselect();
} }
void SPIFlash::wakeup() { void SPIFlash::wakeup() {
command(SPIFLASH_WAKE); // Block Erase command(SPIFLASH_WAKE);
unselect(); unselect();
} }
/// cleanup /// cleanup
void SPIFlash::end() { void SPIFlash::end() {
SPI.end(); SPI.end();
} }

View File

@ -93,6 +93,8 @@ protected:
void unselect(); void unselect();
byte _slaveSelectPin; byte _slaveSelectPin;
uint16_t _jedecID; uint16_t _jedecID;
byte _SPCR;
byte _SPSR;
}; };
#endif #endif