From bfb1ad505fbd982d400067f6635a17fd0936241c Mon Sep 17 00:00:00 2001 From: Federico Amedeo Izzo Date: Sun, 21 Feb 2021 14:13:32 +0100 Subject: [PATCH] Improved SPI Flash overwrite test --- tests/platform/overwriteExtFlash_MDx.c | 60 ++++++++++++++++++++------ 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/tests/platform/overwriteExtFlash_MDx.c b/tests/platform/overwriteExtFlash_MDx.c index e3861a68..4161a620 100644 --- a/tests/platform/overwriteExtFlash_MDx.c +++ b/tests/platform/overwriteExtFlash_MDx.c @@ -18,13 +18,15 @@ * along with this program; if not, see * ***************************************************************************/ + // ####### DISCLAIMER ######### + // This test overwrites a portion of the SPI flash memory + // Run this only if you know what you are doing! + #include #include #include #include "W25Qx.h" -uint8_t block[256] = {0}; - void printChunk(void *chunk) { uint8_t *ptr = ((uint8_t *) chunk); @@ -41,30 +43,62 @@ int main() W25Qx_init(); W25Qx_wakeup(); + uint8_t testData[16] = {0}; + uint8_t buffer[256] = {0}; + while(1) { getchar(); - // On UV380 flash at 0x5F60 there are 36032 bytes of 0xFF - uint32_t addr = 0x5F60; - printf("Read memory @ 0x5F60\r\n"); - W25Qx_readData(addr, block, 256); + // On UV380 flash at 0x6000 there are 36032 bytes of 0xFF + uint32_t addr = 0x6000; + printf("Read memory @ 0x%x\r\n", addr); + W25Qx_readData(addr, buffer, 256); for (int offset = 0; offset < 256; offset += 16) { printf("\r\n%lx: ", addr + offset); - printChunk(block + offset); + printChunk(buffer + offset); } - block[3] = 0xAA; - printf("\r\nWrite memory @ 0x5F60... "); - bool success = W25Qx_writeData(addr, block, 16); + // Prepare test data + for(int i = 0; i < 16; i++) + { + testData[i] = 'a' + (i % 16); + } + printf("\r\nOverwrite memory @ 0x6000... "); + bool success = W25Qx_writeData(addr, testData, 16); printf("%s\r\n", success ? "success" : "failed"); - printf("Read memory @ 0x5F60\r\n"); - W25Qx_readData(addr, block, 256); + printf("Read memory @ 0x%x\r\n", addr); + W25Qx_readData(addr, buffer, 256); for (int offset = 0; offset < 256; offset += 16) { printf("\r\n%lx: ", addr + offset); - printChunk(block + offset); + printChunk(buffer + offset); + } + + uint32_t blockAddr = addr / 4096 * 4096; + printf("\r\nErase memory @ 0x%x... ", blockAddr); + success = W25Qx_eraseSector(blockAddr); + printf("%s\r\n", success ? "success" : "failed"); + + printf("Read memory @ 0x%x\r\n", addr); + W25Qx_readData(addr, buffer, 256); + for (int offset = 0; offset < 256; offset += 16) + { + printf("\r\n%lx: ", addr + offset); + printChunk(buffer + offset); + } + + printf("\r\nWrite memory @ 0x%x... ", addr); + success = W25Qx_writePage(addr, testData, 16); + printf("%s\r\n", success ? "success" : "failed"); + + printf("Read memory @ 0x%x\r\n", addr); + W25Qx_readData(addr, buffer, 256); + for (int offset = 0; offset < 256; offset += 16) + { + printf("\r\n%lx: ", addr + offset); + printChunk(buffer + offset); } }