Storing data in flash

Thread Starter

Noobdesigns

Joined May 20, 2025
7
uint32_t storeLog(char* uid, uint32_t epochTimeLog, uint32_t currentLogStorageAddress)
{
static int32_t lastErasedSector = -1; // Keeps track of the last erased sector
uint8_t logToStore[8] = {0};
memcpy(&logToStore[0], uid, 4);
logToStore[4] = (epochTimeLog >> 24) & 0xFF;
logToStore[5] = (epochTimeLog >> 16) & 0xFF;
logToStore[6] = (epochTimeLog >> 8) & 0xFF;
logToStore[7] = epochTimeLog & 0xFF;
// 2. Erase sector only if not already erased
uint32_t sector = currentLogStorageAddress / SECTOR_SIZE;
if (sector != lastErasedSector) {
W25qxx_EraseSector(sector);
W25qxx_WaitForWriteEnd();
lastErasedSector = sector;
}
uint32_t page = (currentLogStorageAddress % SECTOR_SIZE) / PAGE_SIZE;
uint32_t offset = currentLogStorageAddress % PAGE_SIZE;
W25qxx_WritePage(logToStore, page, offset, 8);
W25qxx_WaitForWriteEnd();
W25qxx_Delay(10); // Extra margin

// 4. Verification
uint8_t verifyData[8] = {0};
W25qxx_ReadPage(verifyData, page, offset, 8);
printf("Read data from FM: ");
for(int i = 0; i < 8; i++) printf("%02X ", verifyData);
printf("\n");
return currentLogStorageAddress + 8;
}

The above code is storing data from sector 1 onwards, that is from address starting from 4096 onwards. I need to store the currentLogStorageAddress each time in sector 0 to know the last flashed address of sector 1. Please help me.
 
Top