Skip to content

wakeup() calls command() with busyWait = false [otherwise, wakeup() hangs forever] #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions SPIFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
}
}
11 changes: 6 additions & 5 deletions SPIFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -90,9 +91,9 @@ class SPIFlash {
void end();
protected:
void select();
void unselect();
void unselect(int us = 0);
byte _slaveSelectPin;
uint16_t _jedecID;
};

#endif
#endif