64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
use alloc::sync::Arc;
|
|
use core::cell::RefCell;
|
|
use core::ops::{Deref, DerefMut};
|
|
use embassy_sync::blocking_mutex::CriticalSectionMutex;
|
|
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
|
|
use embedded_storage::ReadStorage;
|
|
use esp_storage::{FlashStorage, FlashStorageError};
|
|
|
|
#[derive(Clone)]
|
|
pub struct MutexFlashStorage {
|
|
pub(crate) inner: Arc<CriticalSectionMutex<RefCell<FlashStorage<'static>>>>,
|
|
}
|
|
|
|
impl ReadStorage for MutexFlashStorage {
|
|
type Error = FlashStorageError;
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStorageError> {
|
|
self.inner
|
|
.lock(|f| ReadStorage::read(f.borrow_mut().deref_mut(), offset, bytes))
|
|
}
|
|
|
|
fn capacity(&self) -> usize {
|
|
self.inner
|
|
.lock(|f| ReadStorage::capacity(f.borrow().deref()))
|
|
}
|
|
}
|
|
|
|
impl embedded_storage::Storage for MutexFlashStorage {
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
NorFlash::write(self, offset, bytes)
|
|
}
|
|
}
|
|
|
|
impl ErrorType for MutexFlashStorage {
|
|
type Error = FlashStorageError;
|
|
}
|
|
|
|
impl ReadNorFlash for MutexFlashStorage {
|
|
const READ_SIZE: usize = 1;
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
ReadStorage::read(self, offset, bytes)
|
|
}
|
|
|
|
fn capacity(&self) -> usize {
|
|
ReadStorage::capacity(self)
|
|
}
|
|
}
|
|
|
|
impl NorFlash for MutexFlashStorage {
|
|
const WRITE_SIZE: usize = 1;
|
|
const ERASE_SIZE: usize = 4096;
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
self.inner
|
|
.lock(|f| NorFlash::erase(f.borrow_mut().deref_mut(), from, to))
|
|
}
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
self.inner
|
|
.lock(|f| NorFlash::write(f.borrow_mut().deref_mut(), offset, bytes))
|
|
}
|
|
}
|