Wilson Tang
/
HSP_RPC_GUI
Fork of the offical HSP_RPC_GUI firmware
Fork of MAXREFDES100 firmware for MAX32620HSP
Diff: HSP/LoggingService/Logging_RPC.cpp
- Revision:
- 3:8e9b9f5818aa
- Parent:
- 2:d483f896c7a9
--- a/HSP/LoggingService/Logging_RPC.cpp Fri Apr 21 18:10:37 2017 -0500 +++ b/HSP/LoggingService/Logging_RPC.cpp Tue Apr 25 10:47:10 2017 -0500 @@ -37,6 +37,7 @@ #include "DataLoggingService.h" #include "Peripherals.h" #include "Logging.h" +#include "S25FS512.h" extern char loggingMissionCmds[4096]; uint32_t missionCmdIndex; @@ -161,31 +162,89 @@ return 0; } -#define SECTOR_SIZE_256K 0x10000 -#define PAGE_INC_256K 0x400 -#define SECTOR_SIZE_4K 0x1000 -#define PAGE_INC_4K 0x10 -#define TOTAL_SECTOR_NUMBER 263 - - -#define ADDRESS_INC_4K 0x1000 -#define ADDRESS_INC_32K 0x8000 -#define ADDRESS_INC_64K 0x10000 +// +// small helper function to determine if the X number of a page are FF (empty) +// +static bool isPartialPageEmpty(uint8_t *ptr, int count) { + int i; + for (i = 0; i < count; i++) { + if (ptr[i] != 0xFF) return false; + } + return true; +} -#define ADDRESS_4K_START 0x0 -#define ADDRESS_4K_END 0x8000 - -#define ADDRESS_32K_START 0x8000 -#define ADDRESS_32k_END 0x10000 - -#define ADDRESS_64K_START 0x10000 -#define ADDRESS_64k_END 0x2000000 - -#define SIZE_OF_EXTERNAL_FLASH 0x2000000 // 33,554,432 Bytes +// +// define the different sector sizes in flash +// +typedef enum { + e4K, + e32K, + e64K +} loggingFlashArea_t; //****************************************************************************** int Logging_EraseWrittenSectors(char argStrs[32][32], char replyStrs[32][32]) { - Peripherals::s25FS512()->bulkErase_Helper(); + uint32_t i; + uint8_t data[512]; + uint32_t address; + uint32_t pageNumber; + bool pageEmpty; + uint32_t pagesToScan; + loggingFlashArea_t currentArea; + uint32_t currentAreaSize; + //Peripherals::s25FS512()->bulkErase_Helper(); + + // + // quickly scan through the first few bytes of a page and continue this through a sector + // this will determine if the sector needs to be erased or can be skipped + // + currentArea = e4K; + address = 0; + // interate through the entire flash + while (address < SIZE_OF_EXTERNAL_FLASH) { + // determine the size of the current area + switch (currentArea) { + case e4K: + currentAreaSize = ADDRESS_INC_4K; + break; + case e32K: + currentAreaSize = ADDRESS_INC_32K; + break; + default: + currentAreaSize = ADDRESS_INC_64K; + break; + } + pagesToScan = currentAreaSize / SIZE_OF_PAGE; + pageNumber = address >> 8; + pageEmpty = true; + // scan the first few bytes in each page for this area + for (i = 0; i < pagesToScan; i++) { + Peripherals::s25FS512()->readPartialPage_Helper(pageNumber + i, data, 4); + pageEmpty = isPartialPageEmpty(data, 4); + if (pageEmpty == false) break; + } + // if we detected data then erase this sector + if (pageEmpty == false) { + switch (currentArea) { + case e4K: + Peripherals::s25FS512()->parameterSectorErase_Helper(address); + break; + default: + Peripherals::s25FS512()->sectorErase_Helper(address); + break; + } + } + // update the address with the current area size + address += currentAreaSize; + // determine the new area in flash + if (address < ADDRESS_4K_END) { + currentArea = e4K; + } else if (address == ADDRESS_32K_START) { + currentArea = e32K; + } else { + currentArea = e64K; + } + } return 0; }