forked from M-Labs/thermostat
flash_store: get to a working state
This commit is contained in:
parent
880a887c40
commit
97813f917d
4
memory.x
4
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
|
||||
|
|
|
@ -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<FlashBackend>;
|
|||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue