From deed665e02d2d7a96b6d20b60c387383e49be277 Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Tue, 20 Jul 2021 14:10:28 -0400 Subject: [PATCH] add found() and regionIsEmpty() --- SPIFlash.cpp | 32 ++++++++++++++++++++++++++++---- SPIFlash.h | 4 +++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/SPIFlash.cpp b/SPIFlash.cpp index 296e100..47c7d41 100644 --- a/SPIFlash.cpp +++ b/SPIFlash.cpp @@ -45,8 +45,6 @@ uint8_t SPIFlash::UNIQUEID[8]; /// Release Power-down / Device ID (ABh), per section 8.2.19 of the W25X40CL datasheet. /// This means after using the sleep() function of this library, wake() must be the first /// function called. If other commands are used, the flash chip will ignore the commands. -/// The risk is that the code can hang if there is noise/static on the MISO line, -/// specifically noise/static that would evaluate to HIGH or 1. /// Constructor. JedecID is optional but recommended, since this will ensure that the device is present and has a valid response /// get this from the datasheet of your flash chip @@ -302,8 +300,34 @@ void SPIFlash::blockErase64K(uint32_t addr) { unselect(); } +/// found() - checks there is a FLASH chip by checking the deviceID repeatedly - should be a consistent value +uint8_t SPIFlash::found() { + uint16_t deviceID=0; + wakeup(); //if sleep() was previously called, wakeup() is required or it's non responsive + for (uint8_t i=0;i<10;i++) { + uint16_t idNow = readDeviceId(); + if (idNow==0 || idNow==0xffff || (i>0 && idNow != deviceID)) { + deviceID=0; + break; + } + deviceID=idNow; + } + if (deviceID==0) { //NO FLASH CHIP FOUND, ABORTING + return false; + } + return true; +} + +///regionIsEmpty() - check a random flashmem byte array is all clear and can be written to (ie. it's all 0xff) +uint8_t SPIFlash::regionIsEmpty(uint32_t startAddress, uint8_t length) { + uint8_t flashBuf[length]; + readBytes(startAddress, flashBuf, length); + for (uint8_t i=0;i