add flash cache read

This commit is contained in:
hathach committed 2018-10-19 14:32:03 +07:00
1 parent 191baf744a
commit dbe1d6a6d4
3 files changed
+38 -1

No files matched your search

+34
View File
@@ -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);
}
}
+1
View File
@@ -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
}
+3 -1
View File
@@ -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)