Implemented sector erase and page write into W25Qx driver

This commit is contained in:
Silvano Seva 2021-02-17 11:39:52 +01:00
parent 4d66d8b1af
commit 06d75ad606
4 changed files with 213 additions and 9 deletions

View File

@ -24,11 +24,16 @@
#include <string.h>
#include <hwconfig.h>
#include <interfaces/gpio.h>
#include <interfaces/delays.h>
#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;
}

View File

@ -22,6 +22,7 @@
#define W25Qx_H
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
/**
@ -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 */

View File

@ -21,7 +21,7 @@
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#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);
}

View File

@ -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 <http://www.gnu.org/licenses/> *
***************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#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;
}