implement simple card storage retreival
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
use embedded_storage::nor_flash::NorFlash;
|
||||
use embedded_storage::nor_flash::ReadNorFlash;
|
||||
use esp_storage::{FlashStorage, FlashStorageError};
|
||||
|
||||
pub type StoreError = FlashStorageError;
|
||||
|
||||
const FLASH_ADDR: u32 = 0x9000;
|
||||
|
||||
const INITIALIZATION_SIZE: usize = 0x1004;
|
||||
const FLASH_INITIALIZE_MAGIC: u32 = 0xde6de6de;
|
||||
|
||||
pub struct Store {
|
||||
pub storage: FlashStorage<'static>,
|
||||
}
|
||||
|
||||
fn initialize_flash(storage: &mut FlashStorage) -> Result<(), StoreError> {
|
||||
let zeros: [u8; INITIALIZATION_SIZE] = [0; INITIALIZATION_SIZE];
|
||||
storage.write(FLASH_ADDR, &zeros)?;
|
||||
storage.write(FLASH_ADDR, &FLASH_INITIALIZE_MAGIC.to_ne_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub fn new(flash: esp_hal::peripherals::FLASH<'static>) -> Result<Self, StoreError> {
|
||||
let mut storage = FlashStorage::new(flash);
|
||||
|
||||
let mut magic: [u8; 4] = [0, 0, 0, 0];
|
||||
storage.read(FLASH_ADDR, &mut magic)?;
|
||||
|
||||
if (u32::from_ne_bytes(magic)) != FLASH_INITIALIZE_MAGIC {
|
||||
initialize_flash(&mut storage)?;
|
||||
}
|
||||
|
||||
Ok(Self { storage })
|
||||
}
|
||||
|
||||
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), StoreError> {
|
||||
self.storage.read(FLASH_ADDR + offset, bytes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user