diff --git a/memory.x b/memory.x index 03fcd44..213f9a1 100644 --- a/memory.x +++ b/memory.x @@ -1,6 +1,8 @@ MEMORY { - FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K + FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K + /* reserved for config data */ + CONFIG (rx) : ORIGIN = 0x8100000, LENGTH = 16K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 112K RAM2 (xrw) : ORIGIN = 0x2001C000, LENGTH = 16K RAM3 (xrw) : ORIGIN = 0x20020000, LENGTH = 64K diff --git a/src/flash_store.rs b/src/flash_store.rs index 100786c..f69e534 100644 --- a/src/flash_store.rs +++ b/src/flash_store.rs @@ -1,3 +1,4 @@ +use log::{info, error}; use stm32f4xx_hal::{ flash::{Error, FlashExt}, stm32::FLASH, @@ -18,11 +19,12 @@ impl StoreBackend for FlashBackend { type Data = [u8]; fn data(&self) -> &Self::Data { - self.flash.read() + &self.flash.read()[FLASH_SECTOR_OFFSET..(FLASH_SECTOR_OFFSET + FLASH_SECTOR_SIZE)] } type Error = Error; fn erase(&mut self) -> Result<(), Self::Error> { + info!("erasing store flash"); self.flash.unlocked().erase(FLASH_SECTOR) } @@ -41,5 +43,17 @@ pub type FlashStore = Store; pub fn store(flash: FLASH) -> FlashStore { let backend = FlashBackend { flash }; - FlashStore::new(backend) + let mut store = FlashStore::new(backend); + + // just try to read the store + match store.get_bytes_used() { + Ok(_) => {} + Err(e) => { + error!("corrupt store, erasing. error: {:?}", e); + let _ = store.erase() + .map_err(|e| error!("flash erase failed: {:?}", e)); + } + } + + store }