platform/
flash.rs

1use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
2
3pub struct AsyncFlash<T>(pub T);
4
5pub trait UnlockFlash: ReadNorFlash {
6    type Unlocked<'a>: NorFlash<Error = Self::Error>
7    where
8        Self: 'a;
9    fn unlock(&mut self) -> Self::Unlocked<'_>;
10}
11
12impl<T: ReadNorFlash> ErrorType for AsyncFlash<T> {
13    type Error = T::Error;
14}
15
16impl<T: ReadNorFlash> embedded_storage_async::nor_flash::ReadNorFlash
17    for AsyncFlash<T>
18{
19    const READ_SIZE: usize = T::READ_SIZE;
20
21    async fn read(
22        &mut self,
23        offset: u32,
24        bytes: &mut [u8],
25    ) -> Result<(), Self::Error> {
26        self.0.read(offset, bytes)
27    }
28
29    fn capacity(&self) -> usize {
30        self.0.capacity()
31    }
32}
33
34impl<T: UnlockFlash> embedded_storage_async::nor_flash::NorFlash
35    for AsyncFlash<T>
36{
37    const WRITE_SIZE: usize = T::Unlocked::WRITE_SIZE;
38    const ERASE_SIZE: usize = T::Unlocked::ERASE_SIZE;
39
40    async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
41        self.0.unlock().erase(from, to)
42    }
43
44    async fn write(
45        &mut self,
46        offset: u32,
47        bytes: &[u8],
48    ) -> Result<(), Self::Error> {
49        self.0.unlock().write(offset, bytes)
50    }
51}