@@ -49,6 +49,18 @@ static uint8_t *cache_get_addr_for_write(uint32_t flash_addr) {
4949 return (uint8_t * )CACHE_MEM_START_ADDR + flash_addr - flash_sector_start ;
5050}
5151
52+ static uint8_t * cache_get_addr_for_read (uint32_t flash_addr ) {
53+ uint32_t flash_sector_start ;
54+ uint32_t flash_sector_size ;
55+ uint32_t flash_sector_id = flash_get_sector_info (flash_addr , & flash_sector_start , & flash_sector_size );
56+ if (cache_flash_sector_id == flash_sector_id ) {
57+ // in cache, copy from there
58+ return (uint8_t * )CACHE_MEM_START_ADDR + flash_addr - flash_sector_start ;
59+ }
60+ // not in cache, copy straight from flash
61+ return (uint8_t * )flash_addr ;
62+ }
63+
5264void storage_init (void ) {
5365 if (!is_initialised ) {
5466 cache_flash_sector_id = 0 ;
@@ -131,8 +143,9 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
131143 return true;
132144
133145 } else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS ) {
134- // non-MBR block, just copy straight from flash
135- uint8_t * src = (uint8_t * )FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK ) * BLOCK_SIZE ;
146+ // non-MBR block, get data from flash memory, possibly via cache
147+ uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK ) * BLOCK_SIZE ;
148+ uint8_t * src = cache_get_addr_for_read (flash_addr );
136149 memcpy (dest , src , BLOCK_SIZE );
137150 return true;
138151
0 commit comments