Add SPI Flash write data implementation and test
This commit is contained in:
parent
0b14e36bdb
commit
bf8a5cf87d
|
|
@ -212,3 +212,56 @@ ssize_t W25Qx_writePage(uint32_t addr, void* buf, size_t len)
|
|||
return -1;
|
||||
}
|
||||
|
||||
bool W25Qx_writeData(uint32_t addr, void* buf, size_t len)
|
||||
{
|
||||
// Fail if we are trying to write more than 4K bytes
|
||||
if(len > 4096)
|
||||
return false;
|
||||
// Fail if we are trying to write across 4K blocks,
|
||||
// this would erase two 4K blocks for one write, which is not good for flash life
|
||||
// We calculate block address using integer division of start and end address
|
||||
uint32_t startBlockAddr = addr / 4096 * 4096;
|
||||
uint32_t endBlockAddr = (addr + len - 1) / 4096 * 4096;
|
||||
if(endBlockAddr != startBlockAddr)
|
||||
return false;
|
||||
|
||||
// Read data from memory to check if it's already correct
|
||||
// Allocate buffer for storing data read from memory
|
||||
uint8_t *flashData;
|
||||
flashData = (uint8_t *) malloc(len);
|
||||
W25Qx_readData(addr, flashData, len);
|
||||
// If data in flash corresponds to the passed data, do not perform the write
|
||||
if(memcmp(buf, flashData, len) == 0)
|
||||
{
|
||||
// Free the buffer buffer
|
||||
free(flashData);
|
||||
return true;
|
||||
}
|
||||
// Free the flash data buffer
|
||||
free(flashData);
|
||||
|
||||
// Perform the actual read-erase-write of flash data
|
||||
// Allocate 4096 bytes block for storing flash block to be erased
|
||||
uint8_t *flashBlock;
|
||||
flashBlock = (uint8_t *) malloc(4096);
|
||||
// Read the 4K block from flash
|
||||
W25Qx_readData(startBlockAddr, flashBlock, 4096);
|
||||
uint32_t blockOffset = addr % 4096;
|
||||
// Overwrite changed portion
|
||||
memcpy(flashBlock + blockOffset, buf, len);
|
||||
// Erase the 4K block
|
||||
if(!W25Qx_eraseSector(startBlockAddr))
|
||||
{
|
||||
// The erase operation failed, return failure
|
||||
free(flashBlock);
|
||||
return false;
|
||||
}
|
||||
// Write back the modified 4K block in 256 bytes chunks
|
||||
for(uint32_t offset = 0; offset < 4096; offset += 256)
|
||||
{
|
||||
W25Qx_writePage(startBlockAddr + offset, flashBlock[offset], 256);
|
||||
}
|
||||
// Free the 4K buffer
|
||||
free(flashBlock);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,4 +98,19 @@ bool W25Qx_eraseSector(uint32_t addr);
|
|||
*/
|
||||
ssize_t W25Qx_writePage(uint32_t addr, void *buf, size_t len);
|
||||
|
||||
/**
|
||||
* Write data to flash memory.
|
||||
* Copies the 4K block to a memory buffer
|
||||
* Overwrites the specified part
|
||||
* Writes back the 4K block at chunks of 256Bytes.
|
||||
* The write is not performed if the destination content matches the source
|
||||
* Maximum write size = 4096 bytes.
|
||||
* This function fails if you are trying to write across 4K blocks
|
||||
*
|
||||
* @param addr: start address for read operation.
|
||||
* @param buf: pointer to a buffer where data is written to.
|
||||
* @param len: number of bytes to read.
|
||||
*/
|
||||
bool W25Qx_writeData(uint32_t addr, void *buf, size_t len);
|
||||
|
||||
#endif /* W25Qx_H */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
/***************************************************************************
|
||||
* 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"
|
||||
|
||||
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(".");
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
W25Qx_init();
|
||||
W25Qx_wakeup();
|
||||
|
||||
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, 16);
|
||||
printChunk(block);
|
||||
|
||||
block[3] = 0xAA;
|
||||
printf("Write memory @ 0x5F60... ");
|
||||
bool success = W25Qx_writeData(addr, block, 16);
|
||||
printf("%s\r\n", success ? "success" : "failed");
|
||||
|
||||
printf("Read memory @ 0x5F60\r\n");
|
||||
W25Qx_readData(addr, block, 16);
|
||||
printChunk(block);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue