Skip to main content

embedded_storage_file/
asyncronous.rs

1use embedded_storage::nor_flash::ErrorType as ErrorTypeSync;
2use embedded_storage::nor_flash::MultiwriteNorFlash as MultiwriteNorFlashSync;
3use embedded_storage::nor_flash::NorFlash as NorFlashSync;
4use embedded_storage::nor_flash::ReadNorFlash as ReadNorFlashSync;
5use embedded_storage_async::nor_flash::ErrorType as ErrorTypeAsync;
6use embedded_storage_async::nor_flash::MultiwriteNorFlash as MultiwriteNorFlashAsync;
7use embedded_storage_async::nor_flash::NorFlash as NorFlashAsync;
8use embedded_storage_async::nor_flash::ReadNorFlash as ReadNorFlashAsync;
9
10#[derive(Debug)]
11pub struct NorMemoryAsync<NMS> {
12    nor_memory_sync: NMS,
13}
14
15/// Asynchronous NOR flash memory interface from [`embedded_storage_async::nor_flash`].  
16/// it is a wrapper around a synchronous NOR flash memory.  
17/// Note: all operations are executed synchronously, prentending to be asynchronous.  
18impl<NMS> NorMemoryAsync<NMS> {
19    /// given a synchronous NOR flash memory, returns an asynchronous NOR flash memory.
20    pub fn new(nor_memory_sync: NMS) -> Self {
21        Self { nor_memory_sync }
22    }
23
24    /// returns the synchronous NOR flash memory back.
25    pub fn get_sync(self) -> NMS {
26        self.nor_memory_sync
27    }
28}
29
30impl<NMS> ErrorTypeAsync for NorMemoryAsync<NMS>
31where
32    NMS: ErrorTypeSync,
33{
34    type Error = NMS::Error;
35}
36
37impl<NMS> ReadNorFlashAsync for NorMemoryAsync<NMS>
38where
39    NMS: ReadNorFlashSync,
40{
41    const READ_SIZE: usize = NMS::READ_SIZE;
42
43    async fn read(&mut self, address: u32, buf: &mut [u8]) -> Result<(), Self::Error> {
44        async { self.nor_memory_sync.read(address, buf) }.await
45    }
46
47    fn capacity(&self) -> usize {
48        self.nor_memory_sync.capacity()
49    }
50}
51
52impl<NMS> NorFlashAsync for NorMemoryAsync<NMS>
53where
54    NMS: NorFlashSync,
55{
56    const WRITE_SIZE: usize = NMS::WRITE_SIZE;
57    const ERASE_SIZE: usize = NMS::ERASE_SIZE;
58
59    async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
60        async { self.nor_memory_sync.erase(from, to) }.await
61    }
62
63    async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
64        async { self.nor_memory_sync.write(offset, bytes) }.await
65    }
66}
67
68impl<NMS> MultiwriteNorFlashAsync for NorMemoryAsync<NMS> where NMS: MultiwriteNorFlashSync {}