summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstevie67 <stevie@air-stevie.fritz.box>2014-01-04 03:02:32 +0100
committerstevie67 <stevie@air-stevie.fritz.box>2014-01-04 03:02:32 +0100
commit2c62e262b2c84b077355d67c6302bd02f6a28552 (patch)
treea3908cbb793ac6fa2e9ac640b3a121c403cd8f23
parentc8d1384fc0c7aafa5dee3445ece20f4e43dfa9c1 (diff)
Fix issue #62: Cache loses data
Use the storage cache not only for writing but also for reading. This avoids reading stale data and thus data loss.
-rw-r--r--stm/storage.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/stm/storage.c b/stm/storage.c
index 6878de22e..7342a53ac 100644
--- a/stm/storage.c
+++ b/stm/storage.c
@@ -49,6 +49,16 @@ static uint8_t *cache_get_addr_for_write(uint32_t flash_addr) {
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
}
+static uint8_t *cache_get_addr_for_read(uint32_t flash_addr) {
+ uint32_t flash_sector_start;
+ uint32_t flash_sector_size;
+ uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
+ if (cache_flash_sector_id == flash_sector_id)
+ return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
+ // not in cache, copy straight from flash
+ return (uint8_t*)flash_addr;
+}
+
void storage_init(void) {
if (!is_initialised) {
cache_flash_sector_id = 0;
@@ -131,8 +141,8 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
return true;
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
- // non-MBR block, just copy straight from flash
- uint8_t *src = (uint8_t*)FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE;
+ uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE;
+ uint8_t *src = cache_get_addr_for_read(flash_addr);
memcpy(dest, src, BLOCK_SIZE);
return true;