aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal')
-rw-r--r--embassy-embedded-hal/Cargo.toml2
-rw-r--r--embassy-embedded-hal/src/adapter.rs79
-rw-r--r--embassy-embedded-hal/src/lib.rs6
-rw-r--r--embassy-embedded-hal/src/shared_bus/asynch/i2c.rs107
-rw-r--r--embassy-embedded-hal/src/shared_bus/asynch/spi.rs70
5 files changed, 103 insertions, 161 deletions
diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml
index 85ee856a6..fa74be8c4 100644
--- a/embassy-embedded-hal/Cargo.toml
+++ b/embassy-embedded-hal/Cargo.toml
@@ -20,7 +20,7 @@ nightly = ["embedded-hal-async", "embedded-storage-async"]
20embassy-sync = { version = "0.1.0", path = "../embassy-sync" } 20embassy-sync = { version = "0.1.0", path = "../embassy-sync" }
21embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } 21embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
22embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } 22embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
23embedded-hal-async = { version = "=0.1.0-alpha.3", optional = true } 23embedded-hal-async = { version = "=0.2.0-alpha.0", optional = true }
24embedded-storage = "0.3.0" 24embedded-storage = "0.3.0"
25embedded-storage-async = { version = "0.3.0", optional = true } 25embedded-storage-async = { version = "0.3.0", optional = true }
26nb = "1.0.0" 26nb = "1.0.0"
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs
index 1c43f015f..3680984f1 100644
--- a/embassy-embedded-hal/src/adapter.rs
+++ b/embassy-embedded-hal/src/adapter.rs
@@ -38,32 +38,31 @@ where
38 E: embedded_hal_1::i2c::Error + 'static, 38 E: embedded_hal_1::i2c::Error + 'static,
39 T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>, 39 T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>,
40{ 40{
41 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 41 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), Self::Error> {
42 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 42 self.wrapped.read(address, buffer)
43 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
44
45 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
46 async move { self.wrapped.read(address, buffer) }
47 } 43 }
48 44
49 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { 45 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), Self::Error> {
50 async move { self.wrapped.write(address, bytes) } 46 self.wrapped.write(address, bytes)
51 } 47 }
52 48
53 fn write_read<'a>(&'a mut self, address: u8, bytes: &'a [u8], buffer: &'a mut [u8]) -> Self::WriteReadFuture<'a> { 49 async fn write_read<'a>(
54 async move { self.wrapped.write_read(address, bytes, buffer) } 50 &'a mut self,
51 address: u8,
52 bytes: &'a [u8],
53 buffer: &'a mut [u8],
54 ) -> Result<(), Self::Error> {
55 self.wrapped.write_read(address, bytes, buffer)
55 } 56 }
56 57
57 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; 58 async fn transaction<'a, 'b>(
58
59 fn transaction<'a, 'b>(
60 &'a mut self, 59 &'a mut self,
61 address: u8, 60 address: u8,
62 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 61 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
63 ) -> Self::TransactionFuture<'a, 'b> { 62 ) -> Result<(), Self::Error> {
64 let _ = address; 63 let _ = address;
65 let _ = operations; 64 let _ = operations;
66 async move { todo!() } 65 todo!()
67 } 66 }
68} 67}
69 68
@@ -84,23 +83,17 @@ where
84 E: embedded_hal_1::spi::Error + 'static, 83 E: embedded_hal_1::spi::Error + 'static,
85 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 84 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
86{ 85{
87 type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 86 async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> {
88 87 // Ensure we write the expected bytes
89 fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> { 88 for i in 0..core::cmp::min(read.len(), write.len()) {
90 async move { 89 read[i] = write[i].clone();
91 // Ensure we write the expected bytes
92 for i in 0..core::cmp::min(read.len(), write.len()) {
93 read[i] = write[i].clone();
94 }
95 self.wrapped.transfer(read)?;
96 Ok(())
97 } 90 }
91 self.wrapped.transfer(read)?;
92 Ok(())
98 } 93 }
99 94
100 type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 95 async fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Result<(), Self::Error> {
101 96 todo!()
102 fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
103 async move { todo!() }
104 } 97 }
105} 98}
106 99
@@ -109,10 +102,8 @@ where
109 E: embedded_hal_1::spi::Error + 'static, 102 E: embedded_hal_1::spi::Error + 'static,
110 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 103 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
111{ 104{
112 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 105 async fn flush(&mut self) -> Result<(), Self::Error> {
113 106 Ok(())
114 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
115 async move { Ok(()) }
116 } 107 }
117} 108}
118 109
@@ -121,13 +112,9 @@ where
121 E: embedded_hal_1::spi::Error + 'static, 112 E: embedded_hal_1::spi::Error + 'static,
122 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 113 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
123{ 114{
124 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 115 async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
125 116 self.wrapped.write(data)?;
126 fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { 117 Ok(())
127 async move {
128 self.wrapped.write(data)?;
129 Ok(())
130 }
131 } 118 }
132} 119}
133 120
@@ -136,13 +123,9 @@ where
136 E: embedded_hal_1::spi::Error + 'static, 123 E: embedded_hal_1::spi::Error + 'static,
137 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 124 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
138{ 125{
139 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 126 async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
140 127 self.wrapped.transfer(data)?;
141 fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { 128 Ok(())
142 async move {
143 self.wrapped.transfer(data)?;
144 Ok(())
145 }
146 } 129 }
147} 130}
148 131
@@ -192,7 +175,7 @@ where
192 } 175 }
193 176
194 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a; 177 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
195 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { 178 fn flush(&mut self) -> Result<(), Self::Error> {
196 async move { self.wrapped.bflush() } 179 async move { self.wrapped.bflush() }
197 } 180 }
198} 181}
diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs
index a12a3a3a0..8da042228 100644
--- a/embassy-embedded-hal/src/lib.rs
+++ b/embassy-embedded-hal/src/lib.rs
@@ -1,5 +1,9 @@
1#![cfg_attr(not(feature = "std"), no_std)] 1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] 2#![cfg_attr(
3 feature = "nightly",
4 feature(type_alias_impl_trait, async_fn_in_trait, impl_trait_projections)
5)]
6#![cfg_attr(feature = "nightly", allow(incomplete_features))]
3#![warn(missing_docs)] 7#![warn(missing_docs)]
4 8
5//! Utilities to use `embedded-hal` traits with Embassy. 9//! Utilities to use `embedded-hal` traits with Embassy.
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
index 0bc6afd98..c5e1fd415 100644
--- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
+++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
@@ -22,7 +22,6 @@
22//! let i2c_dev2 = I2cDevice::new(i2c_bus); 22//! let i2c_dev2 = I2cDevice::new(i2c_bus);
23//! let mpu = Mpu6050::new(i2c_dev2); 23//! let mpu = Mpu6050::new(i2c_dev2);
24//! ``` 24//! ```
25use core::future::Future;
26 25
27use embassy_sync::blocking_mutex::raw::RawMutex; 26use embassy_sync::blocking_mutex::raw::RawMutex;
28use embassy_sync::mutex::Mutex; 27use embassy_sync::mutex::Mutex;
@@ -55,53 +54,39 @@ where
55 M: RawMutex + 'static, 54 M: RawMutex + 'static,
56 BUS: i2c::I2c + 'static, 55 BUS: i2c::I2c + 'static,
57{ 56{
58 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 57 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
59 58 let mut bus = self.bus.lock().await;
60 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { 59 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
61 async move { 60 Ok(())
62 let mut bus = self.bus.lock().await;
63 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
64 Ok(())
65 }
66 } 61 }
67 62
68 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 63 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
69 64 let mut bus = self.bus.lock().await;
70 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { 65 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
71 async move { 66 Ok(())
72 let mut bus = self.bus.lock().await;
73 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
74 Ok(())
75 }
76 } 67 }
77 68
78 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 69 async fn write_read<'a>(
79
80 fn write_read<'a>(
81 &'a mut self, 70 &'a mut self,
82 address: u8, 71 address: u8,
83 wr_buffer: &'a [u8], 72 wr_buffer: &'a [u8],
84 rd_buffer: &'a mut [u8], 73 rd_buffer: &'a mut [u8],
85 ) -> Self::WriteReadFuture<'a> { 74 ) -> Result<(), I2cDeviceError<BUS::Error>> {
86 async move { 75 let mut bus = self.bus.lock().await;
87 let mut bus = self.bus.lock().await; 76 bus.write_read(address, wr_buffer, rd_buffer)
88 bus.write_read(address, wr_buffer, rd_buffer) 77 .await
89 .await 78 .map_err(I2cDeviceError::I2c)?;
90 .map_err(I2cDeviceError::I2c)?; 79 Ok(())
91 Ok(())
92 }
93 } 80 }
94 81
95 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; 82 async fn transaction<'a, 'b>(
96
97 fn transaction<'a, 'b>(
98 &'a mut self, 83 &'a mut self,
99 address: u8, 84 address: u8,
100 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 85 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
101 ) -> Self::TransactionFuture<'a, 'b> { 86 ) -> Result<(), I2cDeviceError<BUS::Error>> {
102 let _ = address; 87 let _ = address;
103 let _ = operations; 88 let _ = operations;
104 async move { todo!() } 89 todo!()
105 } 90 }
106} 91}
107 92
@@ -136,55 +121,41 @@ where
136 M: RawMutex + 'static, 121 M: RawMutex + 'static,
137 BUS: i2c::I2c + SetConfig + 'static, 122 BUS: i2c::I2c + SetConfig + 'static,
138{ 123{
139 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 124 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
140 125 let mut bus = self.bus.lock().await;
141 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { 126 bus.set_config(&self.config);
142 async move { 127 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
143 let mut bus = self.bus.lock().await; 128 Ok(())
144 bus.set_config(&self.config);
145 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
146 Ok(())
147 }
148 } 129 }
149 130
150 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 131 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
151 132 let mut bus = self.bus.lock().await;
152 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { 133 bus.set_config(&self.config);
153 async move { 134 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
154 let mut bus = self.bus.lock().await; 135 Ok(())
155 bus.set_config(&self.config);
156 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
157 Ok(())
158 }
159 } 136 }
160 137
161 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 138 async fn write_read<'a>(
162
163 fn write_read<'a>(
164 &'a mut self, 139 &'a mut self,
165 address: u8, 140 address: u8,
166 wr_buffer: &'a [u8], 141 wr_buffer: &'a [u8],
167 rd_buffer: &'a mut [u8], 142 rd_buffer: &'a mut [u8],
168 ) -> Self::WriteReadFuture<'a> { 143 ) -> Result<(), I2cDeviceError<BUS::Error>> {
169 async move { 144 let mut bus = self.bus.lock().await;
170 let mut bus = self.bus.lock().await; 145 bus.set_config(&self.config);
171 bus.set_config(&self.config); 146 bus.write_read(address, wr_buffer, rd_buffer)
172 bus.write_read(address, wr_buffer, rd_buffer) 147 .await
173 .await 148 .map_err(I2cDeviceError::I2c)?;
174 .map_err(I2cDeviceError::I2c)?; 149 Ok(())
175 Ok(())
176 }
177 } 150 }
178 151
179 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; 152 async fn transaction<'a, 'b>(
180
181 fn transaction<'a, 'b>(
182 &'a mut self, 153 &'a mut self,
183 address: u8, 154 address: u8,
184 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 155 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
185 ) -> Self::TransactionFuture<'a, 'b> { 156 ) -> Result<(), I2cDeviceError<BUS::Error>> {
186 let _ = address; 157 let _ = address;
187 let _ = operations; 158 let _ = operations;
188 async move { todo!() } 159 todo!()
189 } 160 }
190} 161}
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
index a3814d6d0..d25716655 100644
--- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
+++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
@@ -65,33 +65,25 @@ where
65{ 65{
66 type Bus = BUS; 66 type Bus = BUS;
67 67
68 type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a 68 async fn transaction<R, F, Fut>(&mut self, f: F) -> Result<R, Self::Error>
69 where 69 where
70 Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, 70 F: FnOnce(*mut Self::Bus) -> Fut,
71 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; 71 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>>,
72
73 fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
74 where
75 R: 'a,
76 F: FnOnce(*mut Self::Bus) -> Fut + 'a,
77 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a,
78 { 72 {
79 async move { 73 let mut bus = self.bus.lock().await;
80 let mut bus = self.bus.lock().await; 74 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
81 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
82 75
83 let f_res = f(&mut *bus).await; 76 let f_res = f(&mut *bus).await;
84 77
85 // On failure, it's important to still flush and deassert CS. 78 // On failure, it's important to still flush and deassert CS.
86 let flush_res = bus.flush().await; 79 let flush_res = bus.flush().await;
87 let cs_res = self.cs.set_high(); 80 let cs_res = self.cs.set_high();
88 81
89 let f_res = f_res.map_err(SpiDeviceError::Spi)?; 82 let f_res = f_res.map_err(SpiDeviceError::Spi)?;
90 flush_res.map_err(SpiDeviceError::Spi)?; 83 flush_res.map_err(SpiDeviceError::Spi)?;
91 cs_res.map_err(SpiDeviceError::Cs)?; 84 cs_res.map_err(SpiDeviceError::Cs)?;
92 85
93 Ok(f_res) 86 Ok(f_res)
94 }
95 } 87 }
96} 88}
97 89
@@ -130,33 +122,25 @@ where
130{ 122{
131 type Bus = BUS; 123 type Bus = BUS;
132 124
133 type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a 125 async fn transaction<R, F, Fut>(&mut self, f: F) -> Result<R, Self::Error>
134 where
135 Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a,
136 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a;
137
138 fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
139 where 126 where
140 R: 'a, 127 F: FnOnce(*mut Self::Bus) -> Fut,
141 F: FnOnce(*mut Self::Bus) -> Fut + 'a, 128 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>>,
142 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a,
143 { 129 {
144 async move { 130 let mut bus = self.bus.lock().await;
145 let mut bus = self.bus.lock().await; 131 bus.set_config(&self.config);
146 bus.set_config(&self.config); 132 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
147 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
148 133
149 let f_res = f(&mut *bus).await; 134 let f_res = f(&mut *bus).await;
150 135
151 // On failure, it's important to still flush and deassert CS. 136 // On failure, it's important to still flush and deassert CS.
152 let flush_res = bus.flush().await; 137 let flush_res = bus.flush().await;
153 let cs_res = self.cs.set_high(); 138 let cs_res = self.cs.set_high();
154 139
155 let f_res = f_res.map_err(SpiDeviceError::Spi)?; 140 let f_res = f_res.map_err(SpiDeviceError::Spi)?;
156 flush_res.map_err(SpiDeviceError::Spi)?; 141 flush_res.map_err(SpiDeviceError::Spi)?;
157 cs_res.map_err(SpiDeviceError::Cs)?; 142 cs_res.map_err(SpiDeviceError::Cs)?;
158 143
159 Ok(f_res) 144 Ok(f_res)
160 }
161 } 145 }
162} 146}