diff options
Diffstat (limited to 'embassy-embedded-hal/src/adapter/blocking_async.rs')
| -rw-r--r-- | embassy-embedded-hal/src/adapter/blocking_async.rs | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/adapter/blocking_async.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs new file mode 100644 index 000000000..b996d6a75 --- /dev/null +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | use embedded_hal_02::{blocking, serial}; | ||
| 2 | |||
| 3 | /// Wrapper that implements async traits using blocking implementations. | ||
| 4 | /// | ||
| 5 | /// This allows driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations. | ||
| 6 | /// | ||
| 7 | /// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver. | ||
| 8 | /// | ||
| 9 | /// Driver users are then free to choose which implementation that is available to them. | ||
| 10 | pub struct BlockingAsync<T> { | ||
| 11 | wrapped: T, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<T> BlockingAsync<T> { | ||
| 15 | /// Create a new instance of a wrapper for a given peripheral. | ||
| 16 | pub fn new(wrapped: T) -> Self { | ||
| 17 | Self { wrapped } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | // | ||
| 22 | // I2C implementations | ||
| 23 | // | ||
| 24 | impl<T, E> embedded_hal_1::i2c::ErrorType for BlockingAsync<T> | ||
| 25 | where | ||
| 26 | E: embedded_hal_1::i2c::Error + 'static, | ||
| 27 | T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>, | ||
| 28 | { | ||
| 29 | type Error = E; | ||
| 30 | } | ||
| 31 | |||
| 32 | impl<T, E> embedded_hal_async::i2c::I2c for BlockingAsync<T> | ||
| 33 | where | ||
| 34 | E: embedded_hal_1::i2c::Error + 'static, | ||
| 35 | T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>, | ||
| 36 | { | ||
| 37 | async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 38 | self.wrapped.read(address, read) | ||
| 39 | } | ||
| 40 | |||
| 41 | async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { | ||
| 42 | self.wrapped.write(address, write) | ||
| 43 | } | ||
| 44 | |||
| 45 | async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 46 | self.wrapped.write_read(address, write, read) | ||
| 47 | } | ||
| 48 | |||
| 49 | async fn transaction( | ||
| 50 | &mut self, | ||
| 51 | address: u8, | ||
| 52 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], | ||
| 53 | ) -> Result<(), Self::Error> { | ||
| 54 | let _ = address; | ||
| 55 | let _ = operations; | ||
| 56 | todo!() | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | // | ||
| 61 | // SPI implementatinos | ||
| 62 | // | ||
| 63 | |||
| 64 | impl<T, E> embedded_hal_async::spi::ErrorType for BlockingAsync<T> | ||
| 65 | where | ||
| 66 | E: embedded_hal_1::spi::Error, | ||
| 67 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 68 | { | ||
| 69 | type Error = E; | ||
| 70 | } | ||
| 71 | |||
| 72 | impl<T, E> embedded_hal_async::spi::SpiBus<u8> for BlockingAsync<T> | ||
| 73 | where | ||
| 74 | E: embedded_hal_1::spi::Error + 'static, | ||
| 75 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 76 | { | ||
| 77 | async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> { | ||
| 78 | // Ensure we write the expected bytes | ||
| 79 | for i in 0..core::cmp::min(read.len(), write.len()) { | ||
| 80 | read[i] = write[i].clone(); | ||
| 81 | } | ||
| 82 | self.wrapped.transfer(read)?; | ||
| 83 | Ok(()) | ||
| 84 | } | ||
| 85 | |||
| 86 | async fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Result<(), Self::Error> { | ||
| 87 | todo!() | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | impl<T, E> embedded_hal_async::spi::SpiBusFlush for BlockingAsync<T> | ||
| 92 | where | ||
| 93 | E: embedded_hal_1::spi::Error + 'static, | ||
| 94 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 95 | { | ||
| 96 | async fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 97 | Ok(()) | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | impl<T, E> embedded_hal_async::spi::SpiBusWrite<u8> for BlockingAsync<T> | ||
| 102 | where | ||
| 103 | E: embedded_hal_1::spi::Error + 'static, | ||
| 104 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 105 | { | ||
| 106 | async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { | ||
| 107 | self.wrapped.write(data)?; | ||
| 108 | Ok(()) | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | impl<T, E> embedded_hal_async::spi::SpiBusRead<u8> for BlockingAsync<T> | ||
| 113 | where | ||
| 114 | E: embedded_hal_1::spi::Error + 'static, | ||
| 115 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 116 | { | ||
| 117 | async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { | ||
| 118 | self.wrapped.transfer(data)?; | ||
| 119 | Ok(()) | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | // Uart implementatinos | ||
| 124 | impl<T, E> embedded_hal_1::serial::ErrorType for BlockingAsync<T> | ||
| 125 | where | ||
| 126 | T: serial::Read<u8, Error = E>, | ||
| 127 | E: embedded_hal_1::serial::Error + 'static, | ||
| 128 | { | ||
| 129 | type Error = E; | ||
| 130 | } | ||
| 131 | |||
| 132 | /// NOR flash wrapper | ||
| 133 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 134 | use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; | ||
| 135 | |||
| 136 | impl<T> ErrorType for BlockingAsync<T> | ||
| 137 | where | ||
| 138 | T: ErrorType, | ||
| 139 | { | ||
| 140 | type Error = T::Error; | ||
| 141 | } | ||
| 142 | |||
| 143 | impl<T> AsyncNorFlash for BlockingAsync<T> | ||
| 144 | where | ||
| 145 | T: NorFlash, | ||
| 146 | { | ||
| 147 | const WRITE_SIZE: usize = <T as NorFlash>::WRITE_SIZE; | ||
| 148 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; | ||
| 149 | |||
| 150 | async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { | ||
| 151 | self.wrapped.write(offset, data) | ||
| 152 | } | ||
| 153 | |||
| 154 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 155 | self.wrapped.erase(from, to) | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | impl<T> AsyncReadNorFlash for BlockingAsync<T> | ||
| 160 | where | ||
| 161 | T: ReadNorFlash, | ||
| 162 | { | ||
| 163 | const READ_SIZE: usize = <T as ReadNorFlash>::READ_SIZE; | ||
| 164 | async fn read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Self::Error> { | ||
| 165 | self.wrapped.read(address, data) | ||
| 166 | } | ||
| 167 | |||
| 168 | fn capacity(&self) -> usize { | ||
| 169 | self.wrapped.capacity() | ||
| 170 | } | ||
| 171 | } | ||
