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