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