From 1de2c10577a41376bfefc94cbd159ae27b77f163 Mon Sep 17 00:00:00 2001 From: Zou Hanya Date: Sun, 19 Nov 2017 12:02:59 +0900 Subject: [PATCH 1/3] Add missing break statements in _beginSPI() --- src/FLASHIO.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/FLASHIO.cpp b/src/FLASHIO.cpp index 5227e84..5146105 100755 --- a/src/FLASHIO.cpp +++ b/src/FLASHIO.cpp @@ -188,18 +188,22 @@ bool SPIFlash::_beginSPI(uint8_t opcode) { _nextByte(WRITE, opcode); _nextByte(WRITE, DUMMYBYTE); _transferAddress(); + break; case SECTORERASE: _nextByte(WRITE, opcode); _transferAddress(); + break; case BLOCK32ERASE: _nextByte(WRITE, opcode); _transferAddress(); + break; case BLOCK64ERASE: _nextByte(WRITE, opcode); _transferAddress(); + break; default: _nextByte(WRITE, opcode); From 9633c2fd197065a42e7eb77ddb51690ded23bee3 Mon Sep 17 00:00:00 2001 From: Zou Hanya Date: Sun, 19 Nov 2017 12:09:09 +0900 Subject: [PATCH 2/3] Fix timeout condition of erase commands --- src/FLASHIO.cpp | 2 +- src/SPIFlash.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FLASHIO.cpp b/src/FLASHIO.cpp index 5146105..147b5e4 100755 --- a/src/FLASHIO.cpp +++ b/src/FLASHIO.cpp @@ -401,7 +401,7 @@ bool SPIFlash::_notBusy(uint32_t timeout) { } _time++; } while ((micros() - _time) < timeout); - if ((micros() - _time) == timeout) { + if (timeout <= (micros() - _time)) { return false; } return true; diff --git a/src/SPIFlash.cpp b/src/SPIFlash.cpp index 6d5062b..c731188 100755 --- a/src/SPIFlash.cpp +++ b/src/SPIFlash.cpp @@ -730,7 +730,7 @@ bool SPIFlash::eraseSector(uint32_t _addr) { _beginSPI(SECTORERASE); //The address is transferred as a part of this function _endSPI(); - if(!_notBusy(500L)) { + if(!_notBusy(500 * 1000L)) { return false; //Datasheet says erasing a sector takes 400ms max } //_writeDisable(); @@ -753,7 +753,7 @@ bool SPIFlash::eraseBlock32K(uint32_t _addr) { _beginSPI(BLOCK32ERASE); _endSPI(); - if(!_notBusy(1*S)) { + if(!_notBusy(1000 * 1000L)) { return false; //Datasheet says erasing a sector takes 400ms max } _writeDisable(); @@ -777,7 +777,7 @@ bool SPIFlash::eraseBlock64K(uint32_t _addr) { _beginSPI(BLOCK64ERASE); _endSPI(); - if(!_notBusy(1200L)) { + if(!_notBusy(1200 * 1000L)) { return false; //Datasheet says erasing a sector takes 400ms max } #ifdef RUNDIAGNOSTIC From a179306c7a41649ecadce97b7d4ddc534d326ae7 Mon Sep 17 00:00:00 2001 From: Zou Hanya Date: Sun, 19 Nov 2017 12:41:30 +0900 Subject: [PATCH 3/3] Add error check after erase commands in TestFlash.ino --- examples/TestFlash/TestFlash.ino | 93 +++++++++++++++++--------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/examples/TestFlash/TestFlash.ino b/examples/TestFlash/TestFlash.ino index 1cc0977..03d7859 100755 --- a/examples/TestFlash/TestFlash.ino +++ b/examples/TestFlash/TestFlash.ino @@ -337,22 +337,25 @@ void loop() { } addr = Serial.parseInt(); Serial.println(addr); - flash.eraseSector(addr); - clearprintBuffer(); - sprintf(printBuffer, "A 4KB sector containing address %d has been erased", addr); - Serial.println(printBuffer); - printReadChoice(); - while (!Serial.available()) { - } - uint8_t choice = Serial.parseInt(); - Serial.println(choice); - if (choice == 1) { - printOutputChoice(); + if (flash.eraseSector(addr)) { + clearprintBuffer(); + sprintf(printBuffer, "A 4KB sector containing address %d has been erased", addr); + Serial.println(printBuffer); + printReadChoice(); while (!Serial.available()) { } - uint8_t outputType = Serial.parseInt(); - Serial.println(outputType); - printPage(addr, outputType); + uint8_t choice = Serial.parseInt(); + Serial.println(choice); + if (choice == 1) { + printOutputChoice(); + while (!Serial.available()) { + } + uint8_t outputType = Serial.parseInt(); + Serial.println(outputType); + printPage(addr, outputType); + } + } else { + Serial.println("Erasing sector failed"); } printLine(); printNextCMD(); @@ -368,22 +371,25 @@ void loop() { } addr = Serial.parseInt(); Serial.println(addr); - flash.eraseBlock32K(addr); - clearprintBuffer(); - sprintf(printBuffer, "A 32KB block containing address %d has been erased", addr); - Serial.println(printBuffer); - printReadChoice(); - while (!Serial.available()) { - } - uint8_t choice = Serial.parseInt(); - Serial.println(choice); - if (choice == 1) { - printOutputChoice(); + if (flash.eraseBlock32K(addr)) { + clearprintBuffer(); + sprintf(printBuffer, "A 32KB block containing address %d has been erased", addr); + Serial.println(printBuffer); + printReadChoice(); while (!Serial.available()) { } - uint8_t outputType = Serial.parseInt(); - Serial.println(outputType); - printPage(addr, outputType); + uint8_t choice = Serial.parseInt(); + Serial.println(choice); + if (choice == 1) { + printOutputChoice(); + while (!Serial.available()) { + } + uint8_t outputType = Serial.parseInt(); + Serial.println(outputType); + printPage(addr, outputType); + } + } else { + Serial.println("Erasing block 32K failed"); } printLine(); printNextCMD(); @@ -399,22 +405,25 @@ void loop() { } addr = Serial.parseInt(); Serial.println(addr); - flash.eraseBlock64K(addr); - clearprintBuffer(); - sprintf(printBuffer, "A 64KB block containing address %d has been erased", addr); - Serial.println(printBuffer); - printReadChoice(); - while (!Serial.available()) { - } - uint8_t choice = Serial.parseInt(); - Serial.println(choice); - if (choice == 1) { - printOutputChoice(); + if (flash.eraseBlock64K(addr)) { + clearprintBuffer(); + sprintf(printBuffer, "A 64KB block containing address %d has been erased", addr); + Serial.println(printBuffer); + printReadChoice(); while (!Serial.available()) { } - uint8_t outputType = Serial.parseInt(); - Serial.println(outputType); - printPage(addr, outputType); + uint8_t choice = Serial.parseInt(); + Serial.println(choice); + if (choice == 1) { + printOutputChoice(); + while (!Serial.available()) { + } + uint8_t outputType = Serial.parseInt(); + Serial.println(outputType); + printPage(addr, outputType); + } + } else { + Serial.println("Erasing block 64K failed"); } printLine(); printNextCMD();