From 2e4aafea4251bd10eef17320f1868be19a9c584d Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sun, 14 Oct 2018 23:12:49 +0100 Subject: [PATCH 1/3] Move flashmem.h into include folder --- Sming/Services/SpifFS/spiffs_config.h | 2 +- Sming/system/{ => include}/flashmem.h | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Sming/system/{ => include}/flashmem.h (100%) diff --git a/Sming/Services/SpifFS/spiffs_config.h b/Sming/Services/SpifFS/spiffs_config.h index 8a931a149a..caeff78c0b 100644 --- a/Sming/Services/SpifFS/spiffs_config.h +++ b/Sming/Services/SpifFS/spiffs_config.h @@ -11,7 +11,7 @@ // ----------- 8< ------------ #ifdef __ets__ #include - #include "../system/flashmem.h" + #include "flashmem.h" #else #include "spiffy_host.h" #endif /* __ets__ */ diff --git a/Sming/system/flashmem.h b/Sming/system/include/flashmem.h similarity index 100% rename from Sming/system/flashmem.h rename to Sming/system/include/flashmem.h From 0bc579edfaf55a0321a4fb2e5659c6c5887d4d53 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sun, 14 Oct 2018 23:23:04 +0100 Subject: [PATCH 2/3] Rewrite flashmem driver to cope with misaligned RAM buffers --- Sming/system/flashmem.c | 313 +++++++++++++++++++++----------- Sming/system/include/flashmem.h | 163 +++++++++++++---- 2 files changed, 329 insertions(+), 147 deletions(-) diff --git a/Sming/system/flashmem.c b/Sming/system/flashmem.c index 6c4f15b8fd..9040773213 100644 --- a/Sming/system/flashmem.c +++ b/Sming/system/flashmem.c @@ -1,102 +1,205 @@ -#include "flashmem.h" -#include +/**** + * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. + * Created 2015 by Skurydin Alexey + * http://github.com/SmingHub/Sming + * All files of the Sming Core are provided under the LGPL v3 license. + * + * Based on NodeMCU platform_flash + * https://github.com/nodemcu/nodemcu-firmware + * + * @author: 27/8/2018 - Mikee47 + * + * Rewritten to deal with misaligned RAM buffers. + * + ****/ -// Based on NodeMCU platform_flash -// https://github.com/nodemcu/nodemcu-firmware +#include "flashmem.h" +#include "espinc/peri.h" extern char _flash_code_end[]; -uint32_t flashmem_write( const void *from, uint32_t toaddr, uint32_t size ) +/* + * To ensure memory alignment a temporary buffer is used by flashmem_read and flashmem_write functions. + * + * The buffer must be an integer multiple of INTERNAL_FLASH_WRITE_UNIT_SIZE. + */ +#define FLASH_BUFFERS 32 + +/** @brief determines if the given value is aligned to a word (4-byte) boundary */ +#undef IS_ALIGNED +#define IS_ALIGNED(_x) (((uint32_t)(_x)&0x00000003) == 0) + +// Buffers need to be word aligned for flash access +#define __aligned __attribute__((aligned(4))) + +// If this module were in C++ we could use std::min +static inline uint32_t min(uint32_t a, uint32_t b) { - uint32_t temp, rest, ssize = size; - unsigned i; - char tmpdata[ INTERNAL_FLASH_WRITE_UNIT_SIZE ]; - const uint8_t *pfrom = ( const uint8_t* )from; - const uint32_t blksize = INTERNAL_FLASH_WRITE_UNIT_SIZE; - const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1; - - // Align the start - if( toaddr & blkmask ) - { - rest = toaddr & blkmask; - temp = toaddr & ~blkmask; // this is the actual aligned address - flashmem_read_internal( tmpdata, temp, blksize ); - for( i = rest; size && ( i < blksize ); i ++, size --, pfrom ++ ) - tmpdata[ i ] = *pfrom; - flashmem_write_internal( tmpdata, temp, blksize ); - if( size == 0 ) - return ssize; - toaddr = temp + blksize; - } - // The start address is now a multiple of blksize - // Compute how many bytes we can write as multiples of blksize - rest = size & blkmask; - temp = size & ~blkmask; - // Program the blocks now - if( temp ) - { - flashmem_write_internal( pfrom, toaddr, temp ); - toaddr += temp; - pfrom += temp; - } - // And the final part of a block if needed - if( rest ) - { - flashmem_read_internal( tmpdata, toaddr, blksize ); - for( i = 0; size && ( i < rest ); i ++, size --, pfrom ++ ) - tmpdata[ i ] = *pfrom; - flashmem_write_internal( tmpdata, toaddr, blksize ); - } - return ssize; + return (a < b) ? a : b; } -uint32_t flashmem_read( void *to, uint32_t fromaddr, uint32_t size ) +// For release builds, bypass internal flash read routines and go direct to SDK +#ifdef DEBUG_BUILD + +#define FLASHMEM_READ flashmem_read_internal +#define FLASHMEM_WRITE flashmem_write_internal + +#else + +static inline uint32_t _flashmem_write_internal(const void* from, uint32_t toaddr, uint32_t size) { - uint32_t temp, rest, ssize = size; - unsigned i; - char tmpdata[ INTERNAL_FLASH_READ_UNIT_SIZE ]; - uint8_t *pto = ( uint8_t* )to; - const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE; - const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1; - - // Align the start - if( fromaddr & blkmask ) - { - rest = fromaddr & blkmask; - temp = fromaddr & ~blkmask; // this is the actual aligned address - flashmem_read_internal( tmpdata, temp, blksize ); - for( i = rest; size && ( i < blksize ); i ++, size --, pto ++ ) - *pto = tmpdata[ i ]; - - if( size == 0 ) - return ssize; - fromaddr = temp + blksize; - } - // The start address is now a multiple of blksize - // Compute how many bytes we can read as multiples of blksize - rest = size & blkmask; - temp = size & ~blkmask; - // Program the blocks now - if( temp ) - { - flashmem_read_internal( pto, fromaddr, temp ); - fromaddr += temp; - pto += temp; - } - // And the final part of a block if needed - if( rest ) - { - flashmem_read_internal( tmpdata, fromaddr, blksize ); - for( i = 0; size && ( i < rest ); i ++, size --, pto ++ ) - *pto = tmpdata[ i ]; - } - return ssize; + SpiFlashOpResult r = spi_flash_write(toaddr, (uint32*)from, size); + return (SPI_FLASH_RESULT_OK == r) ? size : 0; } -bool flashmem_erase_sector( uint32_t sector_id ) +static inline uint32_t _flashmem_read_internal(void* to, uint32_t fromaddr, uint32_t size) { - WRITE_PERI_REG(0x60000914, 0x73); - return spi_flash_erase_sector( sector_id ) == SPI_FLASH_RESULT_OK; + SpiFlashOpResult r = spi_flash_read(fromaddr, (uint32*)to, size); + return (SPI_FLASH_RESULT_OK == r) ? size : 0; +} + +#define FLASHMEM_READ _flashmem_read_internal +#define FLASHMEM_WRITE _flashmem_write_internal + +#endif + + +uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) +{ + if(IS_ALIGNED(from) && IS_ALIGNED(toaddr) && IS_ALIGNED(size)) + return FLASHMEM_WRITE(from, toaddr, size); + + const uint32_t blksize = INTERNAL_FLASH_WRITE_UNIT_SIZE; + const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1; + + __aligned char tmpdata[FLASH_BUFFERS * INTERNAL_FLASH_WRITE_UNIT_SIZE]; + const uint8_t* pfrom = (const uint8_t*)from; + uint32_t remain = size; + + // Align the start + if(toaddr & blkmask) + { + uint32_t rest = toaddr & blkmask; + uint32_t addr_aligned = toaddr & ~blkmask; // this is the actual aligned address + + // Read existing unit and overlay with new data + if(FLASHMEM_READ(tmpdata, addr_aligned, blksize) != blksize) + return 0; + while(remain && rest < blksize) + { + tmpdata[rest++] = *pfrom++; + --remain; + } + + // Write the unit + uint32_t written = FLASHMEM_WRITE(tmpdata, addr_aligned, blksize); + if(written != blksize) + return written; + + if (remain == 0) + return size; + + toaddr = addr_aligned + blksize; + } + + // The start address is now a multiple of blksize + // Compute how many bytes we can write as multiples of blksize + uint32_t rest = remain & blkmask; + remain &= ~blkmask; + // Program the blocks now + while(remain) + { + unsigned count = min(remain, sizeof(tmpdata)); + memcpy(tmpdata, pfrom, count); + uint32_t written = FLASHMEM_WRITE(tmpdata, toaddr, count); + remain -= written; + if(written != count) + return size - remain; + toaddr += count; + pfrom += count; + } + + // And the final part of a block if needed + if(rest) + { + if(FLASHMEM_READ(tmpdata, toaddr, blksize) != blksize) + return size - remain; + unsigned i; + for(i = 0; i < rest; ++i) + tmpdata[i] = *pfrom++; + uint32_t written = FLASHMEM_WRITE(tmpdata, toaddr, blksize); + remain -= written; + if(written != blksize) + return size - remain; + } + + return size; +} + +uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) +{ + if(IS_ALIGNED(to) && IS_ALIGNED(fromaddr) && IS_ALIGNED(size)) + return FLASHMEM_READ(to, fromaddr, size); + + const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE; + const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1; + + __aligned char tmpdata[FLASH_BUFFERS * INTERNAL_FLASH_READ_UNIT_SIZE]; + uint8_t* pto = (uint8_t*)to; + uint32_t remain = size; + + // Align the start + if(fromaddr & blkmask) + { + uint32_t rest = fromaddr & blkmask; + uint32_t addr_aligned = fromaddr & ~blkmask; // this is the actual aligned address + if (FLASHMEM_READ(tmpdata, addr_aligned, blksize) != blksize) + return 0; + // memcpy(pto, &tmpdata[rest], std::min(blksize - rest, remain)) + while(remain && rest < blksize) + { + *pto++ = tmpdata[rest++]; + --remain; + } + if (remain == 0) + return size; + fromaddr = addr_aligned + blksize; + } + + // The start address is now a multiple of blksize + // Compute how many bytes we can read as multiples of blksize + uint32_t rest = remain & blkmask; + remain &= ~blkmask; + // Read the blocks now + while(remain) + { + unsigned count = min(remain, sizeof(tmpdata)); + uint32_t read = FLASHMEM_READ(tmpdata, fromaddr, count); + memcpy(pto, tmpdata, read); + remain -= read; + if(read != count) + return size - remain; + fromaddr += count; + pto += count; + } + + // And the final part of a block if needed + if(rest) + { + if(FLASHMEM_READ(tmpdata, fromaddr, blksize) != blksize) + return size - remain; + unsigned i; + for(i = 0; i < rest; ++i) + *pto++ = tmpdata[i]; + } + + return size; +} + +bool flashmem_erase_sector(uint32_t sector_id) +{ + WDT_FEED(); + return spi_flash_erase_sector(sector_id) == SPI_FLASH_RESULT_OK; } SPIFlashInfo flashmem_get_info() @@ -149,9 +252,7 @@ uint16_t flashmem_get_size_sectors() return flashmem_get_size_bytes() / SPI_FLASH_SEC_SIZE; } -// Helper function: find the flash sector in which an address resides -// Return the sector number, as well as the start and end address of the sector -uint32_t flashmem_find_sector( uint32_t address, uint32_t *pstart, uint32_t *pend ) +uint32_t flashmem_find_sector(uint32_t address, uint32_t *pstart, uint32_t *pend) { // All the sectors in the flash have the same size, so just align the address uint32_t sect_id = address / INTERNAL_FLASH_SECTOR_SIZE; @@ -172,19 +273,11 @@ uint32_t flashmem_get_sector_of_address( uint32_t addr ) uint32_t flashmem_write_internal( const void *from, uint32_t toaddr, uint32_t size ) { - SpiFlashOpResult r; - const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1; - uint32_t *apbuf = NULL; - if( ((uint32_t)from) & blkmask ){ - apbuf = (uint32_t *)malloc(size); - if(!apbuf) - return 0; - memcpy(apbuf, from, size); - } - WRITE_PERI_REG(0x60000914, 0x73); - r = spi_flash_write(toaddr, apbuf?(uint32 *)apbuf:(uint32 *)from, size); - if(apbuf) - free(apbuf); + assert(IS_ALIGNED(from) && IS_ALIGNED(toaddr) && IS_ALIGNED(size)); + + WDT_FEED(); + + SpiFlashOpResult r = spi_flash_write(toaddr, (uint32*)from, size); if(SPI_FLASH_RESULT_OK == r) return size; else{ @@ -195,9 +288,11 @@ uint32_t flashmem_write_internal( const void *from, uint32_t toaddr, uint32_t si uint32_t flashmem_read_internal( void *to, uint32_t fromaddr, uint32_t size ) { - SpiFlashOpResult r; - WRITE_PERI_REG(0x60000914, 0x73); - r = spi_flash_read(fromaddr, (uint32 *)to, size); + assert(IS_ALIGNED(to) && IS_ALIGNED(fromaddr) && IS_ALIGNED(size)); + + WDT_FEED(); + + SpiFlashOpResult r = spi_flash_read(fromaddr, (uint32*)to, size); if(SPI_FLASH_RESULT_OK == r) return size; else{ @@ -208,9 +303,9 @@ uint32_t flashmem_read_internal( void *to, uint32_t fromaddr, uint32_t size ) uint32_t flashmem_get_first_free_block_address() { - if (_flash_code_end == NULL) + if(_flash_code_end == NULL) { - debugf("_flash_code_end:%08x\n", (uint32_t)_flash_code_end); + debugf("_flash_code_end is null"); return 0; } diff --git a/Sming/system/include/flashmem.h b/Sming/system/include/flashmem.h index 79d804c58c..982763f630 100644 --- a/Sming/system/include/flashmem.h +++ b/Sming/system/include/flashmem.h @@ -1,5 +1,13 @@ -// Based on NodeMCU platform_flash -// https://github.com/nodemcu/nodemcu-firmware +/**** + * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. + * Created 2015 by Skurydin Alexey + * http://github.com/SmingHub/Sming + * All files of the Sming Core are provided under the LGPL v3 license. + * + * Based on NodeMCU platform_flash + * https://github.com/nodemcu/nodemcu-firmware + * + ****/ #ifndef SYSTEM_FLASHMEM_H_ #define SYSTEM_FLASHMEM_H_ @@ -10,65 +18,144 @@ extern "C" { #include -#define INTERNAL_FLASH_WRITE_UNIT_SIZE 4 -#define INTERNAL_FLASH_READ_UNIT_SIZE 4 +// Flash memory access must be aligned and in multiples of 4-byte words +#define INTERNAL_FLASH_WRITE_UNIT_SIZE 4 +#define INTERNAL_FLASH_READ_UNIT_SIZE 4 -#define FLASH_TOTAL_SEC_COUNT (flashmem_get_size_sectors()) +#define FLASH_TOTAL_SEC_COUNT (flashmem_get_size_sectors()) #define SYS_PARAM_SEC_COUNT 4 #define FLASH_WORK_SEC_COUNT (FLASH_TOTAL_SEC_COUNT - SYS_PARAM_SEC_COUNT) -#define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE -#define INTERNAL_FLASH_SIZE ( (FLASH_WORK_SEC_COUNT) * INTERNAL_FLASH_SECTOR_SIZE ) -#define INTERNAL_FLASH_START_ADDRESS 0x40200000 +#define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE +#define INTERNAL_FLASH_SIZE ((FLASH_WORK_SEC_COUNT)*INTERNAL_FLASH_SECTOR_SIZE) +#define INTERNAL_FLASH_START_ADDRESS 0x40200000 +/** @brief SPI Flash memory information block. + * Stored at the beginning of flash memory. + */ typedef struct { - uint8_t unknown0; - uint8_t unknown1; + uint8_t unknown0; + uint8_t unknown1; enum { - MODE_QIO = 0, - MODE_QOUT = 1, - MODE_DIO = 2, - MODE_DOUT = 15, - } mode : 8; + MODE_QIO = 0, + MODE_QOUT = 1, + MODE_DIO = 2, + MODE_DOUT = 15, + } mode : 8; enum { - SPEED_40MHZ = 0, - SPEED_26MHZ = 1, - SPEED_20MHZ = 2, - SPEED_80MHZ = 15, - } speed : 4; + SPEED_40MHZ = 0, + SPEED_26MHZ = 1, + SPEED_20MHZ = 2, + SPEED_80MHZ = 15, + } speed : 4; enum { - SIZE_4MBIT = 0, - SIZE_2MBIT = 1, - SIZE_8MBIT = 2, - SIZE_16MBIT = 3, - SIZE_32MBIT = 4, - } size : 4; + SIZE_4MBIT = 0, + SIZE_2MBIT = 1, + SIZE_8MBIT = 2, + SIZE_16MBIT = 3, + SIZE_32MBIT = 4, + } size : 4; } STORE_TYPEDEF_ATTR SPIFlashInfo; -extern uint32_t flashmem_write( const void *from, uint32_t toaddr, uint32_t size ); -extern uint32_t flashmem_read( void *to, uint32_t fromaddr, uint32_t size ); -extern bool flashmem_erase_sector( uint32_t sector_id ); +/** @brief obtain the flash memory address for a memory pointer + * @param memptr + * @retval uint32_t offset from start of flash memory + * @note if memptr is not in valid flash memory it will return an offset which exceeds + * the internal flash memory size. This is caught by constructors using getMaxSize() + * to provide a zero-length extent. + */ +static inline uint32_t flashmem_get_address(const void* memptr) +{ + return (uint32_t)memptr - INTERNAL_FLASH_START_ADDRESS; +} -extern SPIFlashInfo flashmem_get_info(); -extern uint8_t flashmem_get_size_type(); -extern uint32_t flashmem_get_size_bytes(); -extern uint16_t flashmem_get_size_sectors(); -uint32_t flashmem_find_sector( uint32_t address, uint32_t *pstart, uint32_t *pend ); -uint32_t flashmem_get_sector_of_address( uint32_t addr ); +/** @brief write a block of data to flash + * @param from buffer to obtain data from + * @param toaddr flash location to start writing + * @param size number of bytes to write + * @retval uint32_t number of bytes written + * @note none of the parameters need to be aligned + */ +uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size); -extern uint32_t flashmem_write_internal( const void *from, uint32_t toaddr, uint32_t size ); -extern uint32_t flashmem_read_internal( void *to, uint32_t fromaddr, uint32_t size ); +/** @brief read a block of data from flash + * @param to buffer to store data + * @param fromaddr flash location to start reading + * @param size number of bytes to read + * @retval uint32_t number of bytes written + * @note none of the parameters need to be aligned + */ +uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size); + +/** @brief Erase a single flash sector + * @param sector_id the sector to erase + * @retval true on success + */ +bool flashmem_erase_sector(uint32_t sector_id); + +/** @brief get flash memory information block + * @retval SPIFlashInfo information block + */ +SPIFlashInfo flashmem_get_info(); + +/** @brief Returns a number indicating the size of flash memory chip + * @retval see SpiFlashInfo.size field for possible values + */ +uint8_t flashmem_get_size_type(); + +/** @brief get the total flash memory size + * @retval uint32_t size in bytes + */ +uint32_t flashmem_get_size_bytes(); + +/** @brief get the total number of flash sectors + * @retval uint16_t sector count + */ +uint16_t flashmem_get_size_sectors(); + +/** @brief Helper function: find the flash sector in which an address resides + * @param address + * @param pstart OUT/OPTIONAL: start of sector containing the given address + * @param pend OUT/OPTIONAL: last address in sector + * @retval uint32_t sector number for the given address + * @note optional parameters may be null + */ +uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend); + +/** @brief Get sector number containing the given address + * @retval uint32_t sector number + */ +uint32_t flashmem_get_sector_of_address(uint32_t addr); + +/** @brief write to flash memory + * @param to buffer to read data from - MUST be word-aligned + * @param toaddr flash address (offset) to write to - MUST be word-aligned + * @param size number of bytes to write - MUST be word-aligned + * @retval number of bytes actually written + * @note All parameters MUST be aligned to 4-byte word boundaries, **including** the RAM pointer + */ +uint32_t flashmem_write_internal(const void* from, uint32_t toaddr, uint32_t size); + +/** @brief read from flash memory + * @param to buffer to store data - MUST be word-aligned + * @param fromaddr flash address (offset) to read from - MUST be word-aligned + * @param size number of bytes to read - MUST be word-aligned + * @retval number of bytes actually read + * @note All parameters MUST be aligned to 4-byte word boundaries, **including** the RAM pointer + */ +uint32_t flashmem_read_internal(void* to, uint32_t fromaddr, uint32_t size); /* * @brief Returns the address of the first free block on flash * @retval uint32_t the actual address on flash */ -extern uint32_t flashmem_get_first_free_block_address(); +uint32_t flashmem_get_first_free_block_address(); + #ifdef __cplusplus } From 0383705a92ec88d769230dbefe26b745488a475d Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 15 Oct 2018 23:25:27 +0100 Subject: [PATCH 3/3] Remove FLASHMEM_READ and FLASHMEM_WRITE macros and replace with regular flashmem_read_internal and flashmem_write_internal calls. These don't really do much as the compiler will optimise the internal functions in release mode anyway. --- Sming/system/flashmem.c | 48 ++++++++++------------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/Sming/system/flashmem.c b/Sming/system/flashmem.c index 9040773213..36157d0448 100644 --- a/Sming/system/flashmem.c +++ b/Sming/system/flashmem.c @@ -27,7 +27,7 @@ extern char _flash_code_end[]; /** @brief determines if the given value is aligned to a word (4-byte) boundary */ #undef IS_ALIGNED -#define IS_ALIGNED(_x) (((uint32_t)(_x)&0x00000003) == 0) +#define IS_ALIGNED(x) (((uint32_t)(x)&0x00000003) == 0) // Buffers need to be word aligned for flash access #define __aligned __attribute__((aligned(4))) @@ -38,36 +38,10 @@ static inline uint32_t min(uint32_t a, uint32_t b) return (a < b) ? a : b; } -// For release builds, bypass internal flash read routines and go direct to SDK -#ifdef DEBUG_BUILD - -#define FLASHMEM_READ flashmem_read_internal -#define FLASHMEM_WRITE flashmem_write_internal - -#else - -static inline uint32_t _flashmem_write_internal(const void* from, uint32_t toaddr, uint32_t size) -{ - SpiFlashOpResult r = spi_flash_write(toaddr, (uint32*)from, size); - return (SPI_FLASH_RESULT_OK == r) ? size : 0; -} - -static inline uint32_t _flashmem_read_internal(void* to, uint32_t fromaddr, uint32_t size) -{ - SpiFlashOpResult r = spi_flash_read(fromaddr, (uint32*)to, size); - return (SPI_FLASH_RESULT_OK == r) ? size : 0; -} - -#define FLASHMEM_READ _flashmem_read_internal -#define FLASHMEM_WRITE _flashmem_write_internal - -#endif - - uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) { if(IS_ALIGNED(from) && IS_ALIGNED(toaddr) && IS_ALIGNED(size)) - return FLASHMEM_WRITE(from, toaddr, size); + return flashmem_write_internal(from, toaddr, size); const uint32_t blksize = INTERNAL_FLASH_WRITE_UNIT_SIZE; const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1; @@ -83,7 +57,7 @@ uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) uint32_t addr_aligned = toaddr & ~blkmask; // this is the actual aligned address // Read existing unit and overlay with new data - if(FLASHMEM_READ(tmpdata, addr_aligned, blksize) != blksize) + if(flashmem_read_internal(tmpdata, addr_aligned, blksize) != blksize) return 0; while(remain && rest < blksize) { @@ -92,7 +66,7 @@ uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) } // Write the unit - uint32_t written = FLASHMEM_WRITE(tmpdata, addr_aligned, blksize); + uint32_t written = flashmem_write_internal(tmpdata, addr_aligned, blksize); if(written != blksize) return written; @@ -111,7 +85,7 @@ uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) { unsigned count = min(remain, sizeof(tmpdata)); memcpy(tmpdata, pfrom, count); - uint32_t written = FLASHMEM_WRITE(tmpdata, toaddr, count); + uint32_t written = flashmem_write_internal(tmpdata, toaddr, count); remain -= written; if(written != count) return size - remain; @@ -122,12 +96,12 @@ uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) // And the final part of a block if needed if(rest) { - if(FLASHMEM_READ(tmpdata, toaddr, blksize) != blksize) + if(flashmem_read_internal(tmpdata, toaddr, blksize) != blksize) return size - remain; unsigned i; for(i = 0; i < rest; ++i) tmpdata[i] = *pfrom++; - uint32_t written = FLASHMEM_WRITE(tmpdata, toaddr, blksize); + uint32_t written = flashmem_write_internal(tmpdata, toaddr, blksize); remain -= written; if(written != blksize) return size - remain; @@ -139,7 +113,7 @@ uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size) uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) { if(IS_ALIGNED(to) && IS_ALIGNED(fromaddr) && IS_ALIGNED(size)) - return FLASHMEM_READ(to, fromaddr, size); + return flashmem_read_internal(to, fromaddr, size); const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE; const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1; @@ -153,7 +127,7 @@ uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) { uint32_t rest = fromaddr & blkmask; uint32_t addr_aligned = fromaddr & ~blkmask; // this is the actual aligned address - if (FLASHMEM_READ(tmpdata, addr_aligned, blksize) != blksize) + if (flashmem_read_internal(tmpdata, addr_aligned, blksize) != blksize) return 0; // memcpy(pto, &tmpdata[rest], std::min(blksize - rest, remain)) while(remain && rest < blksize) @@ -174,7 +148,7 @@ uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) while(remain) { unsigned count = min(remain, sizeof(tmpdata)); - uint32_t read = FLASHMEM_READ(tmpdata, fromaddr, count); + uint32_t read = flashmem_read_internal(tmpdata, fromaddr, count); memcpy(pto, tmpdata, read); remain -= read; if(read != count) @@ -186,7 +160,7 @@ uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) // And the final part of a block if needed if(rest) { - if(FLASHMEM_READ(tmpdata, fromaddr, blksize) != blksize) + if(flashmem_read_internal(tmpdata, fromaddr, blksize) != blksize) return size - remain; unsigned i; for(i = 0; i < rest; ++i)