aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/adapter.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/adapter.rs')
-rw-r--r--embassy-embedded-hal/src/adapter.rs248
1 files changed, 248 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs
new file mode 100644
index 000000000..033efb7ef
--- /dev/null
+++ b/embassy-embedded-hal/src/adapter.rs
@@ -0,0 +1,248 @@
1use core::future::Future;
2use embedded_hal_02::blocking;
3use embedded_hal_02::serial;
4
5/// BlockingAsync is a wrapper that implements async traits using blocking peripherals. This allows
6/// driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations.
7///
8/// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver.
9///
10/// Driver users are then free to choose which implementation that is available to them.
11pub struct BlockingAsync<T> {
12 wrapped: T,
13}
14
15impl<T> BlockingAsync<T> {
16 /// Create a new instance of a wrapper for a given peripheral.
17 pub fn new(wrapped: T) -> Self {
18 Self { wrapped }
19 }
20}
21
22//
23// I2C implementations
24//
25impl<T, E> embedded_hal_1::i2c::ErrorType for BlockingAsync<T>
26where
27 E: embedded_hal_1::i2c::Error + 'static,
28 T: blocking::i2c::WriteRead<Error = E>
29 + blocking::i2c::Read<Error = E>
30 + blocking::i2c::Write<Error = E>,
31{
32 type Error = E;
33}
34
35impl<T, E> embedded_hal_async::i2c::I2c for BlockingAsync<T>
36where
37 E: embedded_hal_1::i2c::Error + 'static,
38 T: blocking::i2c::WriteRead<Error = E>
39 + blocking::i2c::Read<Error = E>
40 + blocking::i2c::Write<Error = E>,
41{
42 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
43 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
44 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
45
46 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
47 async move { self.wrapped.read(address, buffer) }
48 }
49
50 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
51 async move { self.wrapped.write(address, bytes) }
52 }
53
54 fn write_read<'a>(
55 &'a mut self,
56 address: u8,
57 bytes: &'a [u8],
58 buffer: &'a mut [u8],
59 ) -> Self::WriteReadFuture<'a> {
60 async move { self.wrapped.write_read(address, bytes, buffer) }
61 }
62
63 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
64
65 fn transaction<'a, 'b>(
66 &'a mut self,
67 address: u8,
68 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
69 ) -> Self::TransactionFuture<'a, 'b> {
70 let _ = address;
71 let _ = operations;
72 async move { todo!() }
73 }
74}
75
76//
77// SPI implementatinos
78//
79
80impl<T, E> embedded_hal_async::spi::ErrorType for BlockingAsync<T>
81where
82 E: embedded_hal_1::spi::Error,
83 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
84{
85 type Error = E;
86}
87
88impl<T, E> embedded_hal_async::spi::SpiBus<u8> for BlockingAsync<T>
89where
90 E: embedded_hal_1::spi::Error + 'static,
91 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
92{
93 type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
94
95 fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> {
96 async move {
97 // Ensure we write the expected bytes
98 for i in 0..core::cmp::min(read.len(), write.len()) {
99 read[i] = write[i].clone();
100 }
101 self.wrapped.transfer(read)?;
102 Ok(())
103 }
104 }
105
106 type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
107
108 fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
109 async move { todo!() }
110 }
111}
112
113impl<T, E> embedded_hal_async::spi::SpiBusFlush for BlockingAsync<T>
114where
115 E: embedded_hal_1::spi::Error + 'static,
116 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
117{
118 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
119
120 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
121 async move { Ok(()) }
122 }
123}
124
125impl<T, E> embedded_hal_async::spi::SpiBusWrite<u8> for BlockingAsync<T>
126where
127 E: embedded_hal_1::spi::Error + 'static,
128 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
129{
130 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
131
132 fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
133 async move {
134 self.wrapped.write(data)?;
135 Ok(())
136 }
137 }
138}
139
140impl<T, E> embedded_hal_async::spi::SpiBusRead<u8> for BlockingAsync<T>
141where
142 E: embedded_hal_1::spi::Error + 'static,
143 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
144{
145 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
146
147 fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
148 async move {
149 self.wrapped.transfer(data)?;
150 Ok(())
151 }
152 }
153}
154
155// Uart implementatinos
156impl<T, E> embedded_hal_1::serial::ErrorType for BlockingAsync<T>
157where
158 T: serial::Read<u8, Error = E>,
159 E: embedded_hal_1::serial::Error + 'static,
160{
161 type Error = E;
162}
163
164#[cfg(feature = "_todo_embedded_hal_serial")]
165impl<T, E> embedded_hal_async::serial::Read for BlockingAsync<T>
166where
167 T: serial::Read<u8, Error = E>,
168 E: embedded_hal_1::serial::Error + 'static,
169{
170 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
171 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
172 async move {
173 let mut pos = 0;
174 while pos < buf.len() {
175 match self.wrapped.read() {
176 Err(nb::Error::WouldBlock) => {}
177 Err(nb::Error::Other(e)) => return Err(e),
178 Ok(b) => {
179 buf[pos] = b;
180 pos += 1;
181 }
182 }
183 }
184 Ok(())
185 }
186 }
187}
188
189#[cfg(feature = "_todo_embedded_hal_serial")]
190impl<T, E> embedded_hal_async::serial::Write for BlockingAsync<T>
191where
192 T: blocking::serial::Write<u8, Error = E> + serial::Read<u8, Error = E>,
193 E: embedded_hal_1::serial::Error + 'static,
194{
195 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
196 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
197 async move { self.wrapped.bwrite_all(buf) }
198 }
199
200 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
201 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
202 async move { self.wrapped.bflush() }
203 }
204}
205
206/// NOR flash wrapper
207use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
208use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash};
209
210impl<T> ErrorType for BlockingAsync<T>
211where
212 T: ErrorType,
213{
214 type Error = T::Error;
215}
216
217impl<T> AsyncNorFlash for BlockingAsync<T>
218where
219 T: NorFlash,
220{
221 const WRITE_SIZE: usize = <T as NorFlash>::WRITE_SIZE;
222 const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE;
223
224 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
225 fn write<'a>(&'a mut self, offset: u32, data: &'a [u8]) -> Self::WriteFuture<'a> {
226 async move { self.wrapped.write(offset, data) }
227 }
228
229 type EraseFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
230 fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a> {
231 async move { self.wrapped.erase(from, to) }
232 }
233}
234
235impl<T> AsyncReadNorFlash for BlockingAsync<T>
236where
237 T: ReadNorFlash,
238{
239 const READ_SIZE: usize = <T as ReadNorFlash>::READ_SIZE;
240 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
241 fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
242 async move { self.wrapped.read(address, data) }
243 }
244
245 fn capacity(&self) -> usize {
246 self.wrapped.capacity()
247 }
248}