diff --git a/SPIFlash.cpp b/SPIFlash.cpp index 20d1960..72bb808 100644 --- a/SPIFlash.cpp +++ b/SPIFlash.cpp @@ -37,11 +37,16 @@ void SPIFlash::select() { } /// UNselect the flash chip -void SPIFlash::unselect() { +void SPIFlash::unselect(int us) { digitalWrite(_slaveSelectPin, HIGH); + if (us > 0) { + delayMicroseconds(us); + } interrupts(); } + + /// setup SPI, read device ID etc... boolean SPIFlash::initialize() { @@ -119,7 +124,7 @@ void SPIFlash::readBytes(long addr, void* buf, word len) { } /// Send a command to the flash chip, pass TRUE for isWrite when its a write command -void SPIFlash::command(byte cmd, boolean isWrite){ +void SPIFlash::command(byte cmd, boolean isWrite, boolean busyWait){ #if defined(__AVR_ATmega32U4__) // Arduino Leonardo, MoteinoLeo DDRB |= B00000001; // Make sure the SS pin (PB0 - used by RFM12B on MoteinoLeo R1) is set as output HIGH! PORTB |= B00000001; @@ -129,8 +134,13 @@ void SPIFlash::command(byte cmd, boolean isWrite){ command(SPIFLASH_WRITEENABLE); // Write Enable unselect(); } - while(busy()); //wait for any write/erase to complete + + if (busyWait) { + while(busy()); //wait for any write/erase to complete + } + select(); + SPI.transfer(cmd); } @@ -216,16 +226,21 @@ void SPIFlash::blockErase32K(long addr) { } void SPIFlash::sleep() { - command(SPIFLASH_SLEEP); // Block Erase + command(SPIFLASH_SLEEP); // SLEEP unselect(); } void SPIFlash::wakeup() { - command(SPIFLASH_WAKE); // Block Erase - unselect(); + // not a write command + // do not wait for non-busy status because only SPIFLASH_WAKE command is accepted by the chip + // when in sleep mode + command(SPIFLASH_WAKE, false, false); + + // drive pin high for TRES1 microseconds + unselect(SPIFLASH_T_RES_1_US); } /// cleanup void SPIFlash::end() { SPI.end(); -} \ No newline at end of file +} diff --git a/SPIFlash.h b/SPIFlash.h index 20863c1..5df40fe 100644 --- a/SPIFlash.h +++ b/SPIFlash.h @@ -66,18 +66,19 @@ // Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf) // Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf) #define SPIFLASH_MACREAD 0x4B // read unique ID number (MAC) - +#define SPIFLASH_T_RES_1_US 3 + class SPIFlash { public: static byte UNIQUEID[8]; SPIFlash(byte slaveSelectPin, uint16_t jedecID=0); boolean initialize(); - void command(byte cmd, boolean isWrite=false); + void command(byte cmd, boolean isWrite=false, boolean busyWait=true); byte readStatus(); byte readByte(long addr); void readBytes(long addr, void* buf, word len); void writeByte(long addr, byte byt); - void writeBytes(long addr, const void* buf, uint16_t len); + void writeBytes(long addr, const void* buf, uint16_t len); // len must be between 1-256 boolean busy(); void chipErase(); void blockErase4K(long address); @@ -90,9 +91,9 @@ class SPIFlash { void end(); protected: void select(); - void unselect(); + void unselect(int us = 0); byte _slaveSelectPin; uint16_t _jedecID; }; -#endif \ No newline at end of file +#endif