diff --git a/cores/nRF5/flash/flash_cache.c b/cores/nRF5/flash/flash_cache.c index 7f1a69a2..17fce728 100644 --- a/cores/nRF5/flash/flash_cache.c +++ b/cores/nRF5/flash/flash_cache.c @@ -107,3 +107,37 @@ void flash_cache_flush (flash_cache_t* fc) fc->cache_addr = FLASH_CACHE_INVALID_ADDR; } + +void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count) +{ + // overwrite with cache value if available + if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) && + !(addr < fc->cache_addr && addr + count <= fc->cache_addr) && + !(addr >= fc->cache_addr + FLASH_CACHE_PAGE_SIZE) ) + { + int dst_off = fc->cache_addr - addr; + int src_off = 0; + + if ( dst_off < 0 ) + { + src_off = -dst_off; + dst_off = 0; + } + + int cache_bytes = minof(FLASH_CACHE_PAGE_SIZE-src_off, count - dst_off); + + // start to cached + if ( dst_off ) fc->read(dst, addr, dst_off); + + // cached + memcpy(dst + dst_off, fc->cache_buf + src_off, cache_bytes); + + // cached to end + int copied = dst_off + cache_bytes; + if ( copied < count ) fc->read(dst + copied, addr + copied, count - copied); + } + else + { + memcpy(dst, (void*) addr, count); + } +} diff --git a/cores/nRF5/flash/flash_cache.h b/cores/nRF5/flash/flash_cache.h index eefdf6e1..7eb6fa06 100644 --- a/cores/nRF5/flash/flash_cache.h +++ b/cores/nRF5/flash/flash_cache.h @@ -60,6 +60,7 @@ extern "C" { uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count); void flash_cache_flush (flash_cache_t* fc); +void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count); #ifdef __cplusplus } diff --git a/cores/nRF5/flash/flash_nrf52.c b/cores/nRF5/flash/flash_nrf52.c index d8e81986..2d3ed736 100644 --- a/cores/nRF5/flash/flash_nrf52.c +++ b/cores/nRF5/flash/flash_nrf52.c @@ -82,7 +82,9 @@ uint32_t flash_nrf52_write (uint32_t dst, void const * src, uint32_t len) uint32_t flash_nrf52_read (void* dst, uint32_t src, uint32_t len) { - return fal_read(dst, src, len); + // return cache value if available + flash_cache_read(&_cache, dst, src, len); + return len; } bool flash_nrf52_erase(uint32_t addr)