1 | use embassy_futures::yield_now; |
2 | |
3 | /// Wrapper that yields for each operation to the wrapped instance |
4 | /// |
5 | /// This can be used in combination with BlockingAsync<T> to enforce yields |
6 | /// between long running blocking operations. |
7 | pub struct YieldingAsync<T> { |
8 | wrapped: T, |
9 | } |
10 | |
11 | impl<T> YieldingAsync<T> { |
12 | /// Create a new instance of a wrapper that yields after each operation. |
13 | pub fn new(wrapped: T) -> Self { |
14 | Self { wrapped } |
15 | } |
16 | } |
17 | |
18 | // |
19 | // I2C implementations |
20 | // |
21 | impl<T> embedded_hal_1::i2c::ErrorType for YieldingAsync<T> |
22 | where |
23 | T: embedded_hal_1::i2c::ErrorType, |
24 | { |
25 | type Error = T::Error; |
26 | } |
27 | |
28 | impl<T> embedded_hal_async::i2c::I2c for YieldingAsync<T> |
29 | where |
30 | T: embedded_hal_async::i2c::I2c, |
31 | { |
32 | async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { |
33 | self.wrapped.read(address, read).await?; |
34 | yield_now().await; |
35 | Ok(()) |
36 | } |
37 | |
38 | async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { |
39 | self.wrapped.write(address, write).await?; |
40 | yield_now().await; |
41 | Ok(()) |
42 | } |
43 | |
44 | async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { |
45 | self.wrapped.write_read(address, write, read).await?; |
46 | yield_now().await; |
47 | Ok(()) |
48 | } |
49 | |
50 | async fn transaction( |
51 | &mut self, |
52 | address: u8, |
53 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], |
54 | ) -> Result<(), Self::Error> { |
55 | self.wrapped.transaction(address, operations).await?; |
56 | yield_now().await; |
57 | Ok(()) |
58 | } |
59 | } |
60 | |
61 | // |
62 | // SPI implementations |
63 | // |
64 | |
65 | impl<T> embedded_hal_async::spi::ErrorType for YieldingAsync<T> |
66 | where |
67 | T: embedded_hal_async::spi::ErrorType, |
68 | { |
69 | type Error = T::Error; |
70 | } |
71 | |
72 | impl<T, Word: 'static + Copy> embedded_hal_async::spi::SpiBus<Word> for YieldingAsync<T> |
73 | where |
74 | T: embedded_hal_async::spi::SpiBus<Word>, |
75 | { |
76 | async fn flush(&mut self) -> Result<(), Self::Error> { |
77 | self.wrapped.flush().await?; |
78 | yield_now().await; |
79 | Ok(()) |
80 | } |
81 | |
82 | async fn write(&mut self, data: &[Word]) -> Result<(), Self::Error> { |
83 | self.wrapped.write(data).await?; |
84 | yield_now().await; |
85 | Ok(()) |
86 | } |
87 | |
88 | async fn read(&mut self, data: &mut [Word]) -> Result<(), Self::Error> { |
89 | self.wrapped.read(data).await?; |
90 | yield_now().await; |
91 | Ok(()) |
92 | } |
93 | |
94 | async fn transfer(&mut self, read: &mut [Word], write: &[Word]) -> Result<(), Self::Error> { |
95 | self.wrapped.transfer(read, write).await?; |
96 | yield_now().await; |
97 | Ok(()) |
98 | } |
99 | |
100 | async fn transfer_in_place(&mut self, words: &mut [Word]) -> Result<(), Self::Error> { |
101 | self.wrapped.transfer_in_place(words).await?; |
102 | yield_now().await; |
103 | Ok(()) |
104 | } |
105 | } |
106 | |
107 | /// |
108 | /// NOR flash implementations |
109 | /// |
110 | impl<T: embedded_storage::nor_flash::ErrorType> embedded_storage::nor_flash::ErrorType for YieldingAsync<T> { |
111 | type Error = T::Error; |
112 | } |
113 | |
114 | impl<T: embedded_storage_async::nor_flash::ReadNorFlash> embedded_storage_async::nor_flash::ReadNorFlash |
115 | for YieldingAsync<T> |
116 | { |
117 | const READ_SIZE: usize = T::READ_SIZE; |
118 | |
119 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
120 | self.wrapped.read(offset, bytes).await?; |
121 | Ok(()) |
122 | } |
123 | |
124 | fn capacity(&self) -> usize { |
125 | self.wrapped.capacity() |
126 | } |
127 | } |
128 | |
129 | impl<T: embedded_storage_async::nor_flash::NorFlash> embedded_storage_async::nor_flash::NorFlash for YieldingAsync<T> { |
130 | const WRITE_SIZE: usize = T::WRITE_SIZE; |
131 | const ERASE_SIZE: usize = T::ERASE_SIZE; |
132 | |
133 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
134 | self.wrapped.write(offset, bytes).await?; |
135 | yield_now().await; |
136 | Ok(()) |
137 | } |
138 | |
139 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
140 | // Yield between each actual erase |
141 | for from: u32 in (from..to).step_by(T::ERASE_SIZE) { |
142 | let to: u32 = core::cmp::min(v1:from + T::ERASE_SIZE as u32, v2:to); |
143 | self.wrapped.erase(from, to).await?; |
144 | yield_now().await; |
145 | } |
146 | Ok(()) |
147 | } |
148 | } |
149 | |
150 | #[cfg (test)] |
151 | mod tests { |
152 | use embedded_storage_async::nor_flash::NorFlash; |
153 | |
154 | use super::*; |
155 | use crate::flash::mem_flash::MemFlash; |
156 | |
157 | #[futures_test::test] |
158 | async fn can_erase() { |
159 | let flash = MemFlash::<1024, 128, 4>::new(0x00); |
160 | let mut yielding = YieldingAsync::new(flash); |
161 | |
162 | yielding.erase(0, 256).await.unwrap(); |
163 | |
164 | let flash = yielding.wrapped; |
165 | assert_eq!(2, flash.erases.len()); |
166 | assert_eq!((0, 128), flash.erases[0]); |
167 | assert_eq!((128, 256), flash.erases[1]); |
168 | } |
169 | } |
170 | |