embassy_embedded_hal/adapter/
blocking_async.rs

1/// Wrapper that implements async traits using blocking implementations.
2///
3/// This allows driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations.
4///
5/// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver.
6///
7/// Driver users are then free to choose which implementation that is available to them.
8pub struct BlockingAsync<T> {
9    wrapped: T,
10}
11
12impl<T> BlockingAsync<T> {
13    /// Create a new instance of a wrapper for a given peripheral.
14    pub fn new(wrapped: T) -> Self {
15        Self { wrapped }
16    }
17}
18
19//
20// I2C implementations
21//
22impl<T, E> embedded_hal_1::i2c::ErrorType for BlockingAsync<T>
23where
24    E: embedded_hal_1::i2c::Error + 'static,
25    T: embedded_hal_1::i2c::I2c<Error = E>,
26{
27    type Error = E;
28}
29
30impl<T, E> embedded_hal_async::i2c::I2c for BlockingAsync<T>
31where
32    E: embedded_hal_1::i2c::Error + 'static,
33    T: embedded_hal_1::i2c::I2c<Error = E>,
34{
35    async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
36        self.wrapped.read(address, read)
37    }
38
39    async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
40        self.wrapped.write(address, write)
41    }
42
43    async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
44        self.wrapped.write_read(address, write, read)
45    }
46
47    async fn transaction(
48        &mut self,
49        address: u8,
50        operations: &mut [embedded_hal_1::i2c::Operation<'_>],
51    ) -> Result<(), Self::Error> {
52        self.wrapped.transaction(address, operations)
53    }
54}
55
56//
57// SPI implementatinos
58//
59
60impl<T, E> embedded_hal_async::spi::ErrorType for BlockingAsync<T>
61where
62    E: embedded_hal_async::spi::Error,
63    T: embedded_hal_1::spi::SpiBus<Error = E>,
64{
65    type Error = E;
66}
67
68impl<T, E> embedded_hal_async::spi::SpiBus<u8> for BlockingAsync<T>
69where
70    E: embedded_hal_async::spi::Error,
71    T: embedded_hal_1::spi::SpiBus<Error = E>,
72{
73    async fn flush(&mut self) -> Result<(), Self::Error> {
74        Ok(())
75    }
76
77    async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
78        self.wrapped.write(data)?;
79        Ok(())
80    }
81
82    async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
83        self.wrapped.read(data)?;
84        Ok(())
85    }
86
87    async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
88        self.wrapped.transfer(read, write)?;
89        Ok(())
90    }
91
92    async fn transfer_in_place(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
93        self.wrapped.transfer_in_place(data)?;
94        Ok(())
95    }
96}
97
98/// NOR flash wrapper
99use embedded_storage::nor_flash::{ErrorType, MultiwriteNorFlash, NorFlash, ReadNorFlash};
100use embedded_storage_async::nor_flash::{
101    MultiwriteNorFlash as AsyncMultiwriteNorFlash, NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash,
102};
103
104impl<T> ErrorType for BlockingAsync<T>
105where
106    T: ErrorType,
107{
108    type Error = T::Error;
109}
110
111impl<T> AsyncNorFlash for BlockingAsync<T>
112where
113    T: NorFlash,
114{
115    const WRITE_SIZE: usize = <T as NorFlash>::WRITE_SIZE;
116    const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE;
117
118    async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
119        self.wrapped.write(offset, data)
120    }
121
122    async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
123        self.wrapped.erase(from, to)
124    }
125}
126
127impl<T> AsyncReadNorFlash for BlockingAsync<T>
128where
129    T: ReadNorFlash,
130{
131    const READ_SIZE: usize = <T as ReadNorFlash>::READ_SIZE;
132    async fn read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Self::Error> {
133        self.wrapped.read(address, data)
134    }
135
136    fn capacity(&self) -> usize {
137        self.wrapped.capacity()
138    }
139}
140
141impl<T> AsyncMultiwriteNorFlash for BlockingAsync<T> where T: MultiwriteNorFlash {}