From d7220e1106acff55f8e32fa9f55be59209350167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 15 Apr 2017 13:57:02 +0200 Subject: [PATCH] make flash library more general purpose and optimize if --- lib/flash_internal.c | 102 ++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/lib/flash_internal.c b/lib/flash_internal.c index cf4dedf..f149cf0 100644 --- a/lib/flash_internal.c +++ b/lib/flash_internal.c @@ -34,16 +34,12 @@ bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size) { - // verify it's in the storage area - if (addressSTORAGE_END) { - return false; - } - if (buffer==NULL || size==0) { + // verify it's in the flash area and do other sanity checks + if (address(UINT32_MAX-size) || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024) || buffer==NULL || size==0) { return false; } - // copy data byte per byte - // a more efficient way would be to copy words, than the remaining bytes + // copy data byte per byte (a more efficient way would be to copy words, than the remaining bytes) for (size_t i=0; iSTORAGE_END) { + // verify it's in the flash area and do other sanity checks + if (address(UINT32_MAX-size) || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024) || buffer==NULL || size==0 || size%2) { return false; } - if (buffer==NULL || size==0) { - return false; - } - - uint8_t page[PAGE_SIZE]; // the complete page to write flash_unlock(); // unlock flash to be able to write it - // go through memory - while (size) { - uint32_t page_pre = address%PAGE_SIZE; // the beginning data size in the page - address -= page_pre; // go to beginning of the page - storage_read(address, &page[0], page_pre); // copy existing data - if (size>=PAGE_SIZE-page_pre) { // no need to read tailing page data - for (uint16_t i=0; i0 && page_i0 && address<(page_start+PAGE_SIZE)) { + flash_program_half_word(address, *((uint16_t*)(buffer))); + if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong + flash_lock(); // lock back flash to protect it + return false; + } + if (*((uint16_t*)address)!=*((uint16_t*)buffer)) { // verify the programmed data is right + flash_lock(); // lock back flash to protect it + return false; + } + buffer += 2; + address += 2; + size -= 2; } } - - address += PAGE_SIZE; // go to next page - } flash_lock(); // lock back flash to protect it