From 06d75ad60691d2a826004ae53b2ca51d95998355 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Wed, 17 Feb 2021 11:39:52 +0100 Subject: [PATCH] Implemented sector erase and page write into W25Qx driver --- platform/drivers/NVM/W25Qx.c | 107 +++++++++++++++++++++++++++-- platform/drivers/NVM/W25Qx.h | 22 ++++++ tests/platform/dumpExtFlash_MDx.c | 8 +-- tests/platform/writeExtFlash_MDx.c | 85 +++++++++++++++++++++++ 4 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 tests/platform/writeExtFlash_MDx.c diff --git a/platform/drivers/NVM/W25Qx.c b/platform/drivers/NVM/W25Qx.c index 4b120155..9466e40f 100644 --- a/platform/drivers/NVM/W25Qx.c +++ b/platform/drivers/NVM/W25Qx.c @@ -24,11 +24,16 @@ #include #include #include +#include -#define CMD_READ 0x03 /* Read data */ -#define CMD_RSEC 0x48 /* Read security register */ -#define CMD_WKUP 0xAB /* Release power down */ -#define CMD_PDWN 0xB9 /* Power down */ +#define CMD_WRITE 0x02 /* Read data */ +#define CMD_READ 0x03 /* Read data */ +#define CMD_RDSTA 0x05 /* Read status register */ +#define CMD_WREN 0x06 /* Write enable */ +#define CMD_ESECT 0x20 /* Erase 4kB sector */ +#define CMD_RSECR 0x48 /* Read security register */ +#define CMD_WKUP 0xAB /* Release power down */ +#define CMD_PDWN 0xB9 /* Power down */ /* * Target-specific SPI interface functions, their implementation can be found @@ -84,7 +89,7 @@ ssize_t W25Qx_readSecurityRegister(uint32_t addr, void* buf, size_t len) } gpio_clearPin(FLASH_CS); - (void) spiFlash_SendRecv(CMD_RSEC); /* Command */ + (void) spiFlash_SendRecv(CMD_RSECR); /* Command */ (void) spiFlash_SendRecv((addr >> 16) & 0xFF); /* Address high */ (void) spiFlash_SendRecv((addr >> 8) & 0xFF); /* Address middle */ (void) spiFlash_SendRecv(addr & 0xFF); /* Address low */ @@ -115,3 +120,95 @@ void W25Qx_readData(uint32_t addr, void* buf, size_t len) gpio_setPin(FLASH_CS); } + +bool W25Qx_eraseSector(uint32_t addr) +{ + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_WREN); /* Write enable */ + gpio_setPin(FLASH_CS); + + delayUs(5); + + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_ESECT); /* Command */ + (void) spiFlash_SendRecv((addr >> 16) & 0xFF); /* Address high */ + (void) spiFlash_SendRecv((addr >> 8) & 0xFF); /* Address middle */ + (void) spiFlash_SendRecv(addr & 0xFF); /* Address low */ + gpio_setPin(FLASH_CS); + + /* + * Wait till erase terminates. + * Timeout after 500ms, at 250us per tick + */ + uint16_t timeout = 2000; + while(timeout > 0) + { + delayUs(250); + timeout--; + + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_RDSTA); /* Read status */ + uint8_t status = spiFlash_SendRecv(0x00); + gpio_setPin(FLASH_CS); + + /* If busy flag is low, we're done */ + if((status & 0x01) == 0) return true; + } + + /* If we get here, we had a timeout */ + return false; +} + +ssize_t W25Qx_writePage(uint32_t addr, void* buf, size_t len) +{ + /* Keep 256-byte boundary to avoid wrap-around when writing */ + size_t addrRange = addr & 0x0001FF; + size_t writeLen = len; + if((addrRange + len) > 0x100) + { + writeLen = 0x100 - addrRange; + } + + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_WREN); /* Write enable */ + gpio_setPin(FLASH_CS); + + delayUs(5); + + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_WRITE); /* Command */ + (void) spiFlash_SendRecv((addr >> 16) & 0xFF); /* Address high */ + (void) spiFlash_SendRecv((addr >> 8) & 0xFF); /* Address middle */ + (void) spiFlash_SendRecv(addr & 0xFF); /* Address low */ + + for(size_t i = 0; i < writeLen; i++) + { + uint8_t value = ((uint8_t *) buf)[i]; + (void) spiFlash_SendRecv(value); + } + + gpio_setPin(FLASH_CS); + + /* + * Wait till write terminates. + * Timeout after 500ms, at 250us per tick + */ + uint16_t timeout = 2000; + while(timeout > 0) + { + delayUs(250); + timeout--; + + gpio_clearPin(FLASH_CS); + (void) spiFlash_SendRecv(CMD_RDSTA); /* Read status */ + uint8_t status = spiFlash_SendRecv(0x00); + gpio_setPin(FLASH_CS); + + /* If busy flag is low, we're done */ + if((status & 0x01) == 0) return ((ssize_t) writeLen); + } + + /* If we get here, we had a timeout */ + return -1; +} + diff --git a/platform/drivers/NVM/W25Qx.h b/platform/drivers/NVM/W25Qx.h index 740df873..f19cd1a8 100644 --- a/platform/drivers/NVM/W25Qx.h +++ b/platform/drivers/NVM/W25Qx.h @@ -22,6 +22,7 @@ #define W25Qx_H #include +#include #include /** @@ -76,4 +77,25 @@ ssize_t W25Qx_readSecurityRegister(uint32_t addr, void *buf, size_t len); */ void W25Qx_readData(uint32_t addr, void *buf, size_t len); +/** + * Erase a 4kB sector. + * Function returns when erase process terminated. + * + * @param addr: sector address. + * @return true on success, false on failure. + */ +bool W25Qx_eraseSector(uint32_t addr); + +/** + * Write data to a 256-byte flash memory page. + * NOTE: if data size goes beyond the 256 byte boundary, length will be truncated + * to the one reaching the end of the page. + * + * @param addr: start address for write operation. + * @param buf: pointer to data buffer. + * @param len: number of bytes to written. + * @return: -1 on error, the number of bytes effectively written otherwise. + */ +ssize_t W25Qx_writePage(uint32_t addr, void *buf, size_t len); + #endif /* W25Qx_H */ diff --git a/tests/platform/dumpExtFlash_MDx.c b/tests/platform/dumpExtFlash_MDx.c index beb0b015..aa8eaeaf 100644 --- a/tests/platform/dumpExtFlash_MDx.c +++ b/tests/platform/dumpExtFlash_MDx.c @@ -21,7 +21,7 @@ #include #include #include -#include "extFlash_MDx.h" +#include "W25Qx.h" void printChunk(void *chunk) { @@ -36,8 +36,8 @@ void printChunk(void *chunk) int main() { - extFlash_init(); - extFlash_wakeup(); + W25Qx_init(); + W25Qx_wakeup(); while(1) { @@ -46,7 +46,7 @@ int main() for(uint32_t addr = 0; addr < 0xFFFFFF; addr += 16) { uint8_t buf[16]; - (void) extFlash_readData(addr, buf, 16); + (void) W25Qx_readData(addr, buf, 16); printf("\r\n%lx: ", addr); printChunk(buf); } diff --git a/tests/platform/writeExtFlash_MDx.c b/tests/platform/writeExtFlash_MDx.c new file mode 100644 index 00000000..10e486ab --- /dev/null +++ b/tests/platform/writeExtFlash_MDx.c @@ -0,0 +1,85 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Frederik Saraci IU2NRO, * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#include +#include +#include +#include "W25Qx.h" + + +static const uint32_t sector_address = 0; +static const uint32_t block_address = 0; + + +uint8_t block[256] = {0}; + +void printChunk(void *chunk) +{ + uint8_t *ptr = ((uint8_t *) chunk); + for(size_t i = 0; i < 16; i++) printf("%02x ", ptr[i]); + for(size_t i = 0; i < 16; i++) + { + if((ptr[i] > 0x22) && (ptr[i] < 0x7f)) printf("%c", ptr[i]); + else printf("."); + } +} + +int main() +{ + W25Qx_init(); + W25Qx_wakeup(); + + while(1) + { + getchar(); + + printf("Attempting write... "); + + for(size_t i = 0; i < 256; i++) + { + block[i] = 'a' + (i % 16); + } + + ssize_t rv = W25Qx_writePage(block_address, block, 256); + printf("%ld\r\n", rv); + + for(uint32_t pos = 0; pos < 0xFF; pos += 16) + { + uint8_t buf[16]; + (void) W25Qx_readData(block_address + pos, buf, 16); + printf("\r\n%02lx: ", pos); + printChunk(buf); + } + + printf("\r\n\r\nAttempting erase... "); + bool ok = W25Qx_eraseSector(sector_address); + printf("%d\r\n", ok); + + for(uint32_t pos = 0; pos < 0xFF; pos += 16) + { + uint8_t buf[16]; + (void) W25Qx_readData(block_address + pos, buf, 16); + printf("\r\n%02lx: ", pos); + printChunk(buf); + } + } + + return 0; +}