aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/asynch
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-04-06 22:25:24 +0200
committerDario Nieuwenhuis <[email protected]>2023-04-06 22:41:50 +0200
commitbe37eee13dbd7833e0d74ea57d31d3e5c58cd47f (patch)
tree3e1d5a59409ea06fe34d97fdaf45642683332638 /embassy-embedded-hal/src/shared_bus/asynch
parentf3ec6080bf9a39d9819195861e7b41e8a2081600 (diff)
Update embedded-hal crates.
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/asynch')
-rw-r--r--embassy-embedded-hal/src/shared_bus/asynch/i2c.rs42
-rw-r--r--embassy-embedded-hal/src/shared_bus/asynch/spi.rs171
2 files changed, 164 insertions, 49 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
index c5e1fd415..829554045 100644
--- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
+++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs
@@ -54,35 +54,35 @@ where
54 M: RawMutex + 'static, 54 M: RawMutex + 'static,
55 BUS: i2c::I2c + 'static, 55 BUS: i2c::I2c + 'static,
56{ 56{
57 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { 57 async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
58 let mut bus = self.bus.lock().await; 58 let mut bus = self.bus.lock().await;
59 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?; 59 bus.read(address, read).await.map_err(I2cDeviceError::I2c)?;
60 Ok(()) 60 Ok(())
61 } 61 }
62 62
63 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { 63 async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
64 let mut bus = self.bus.lock().await; 64 let mut bus = self.bus.lock().await;
65 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?; 65 bus.write(address, write).await.map_err(I2cDeviceError::I2c)?;
66 Ok(()) 66 Ok(())
67 } 67 }
68 68
69 async fn write_read<'a>( 69 async fn write_read(
70 &'a mut self, 70 &mut self,
71 address: u8, 71 address: u8,
72 wr_buffer: &'a [u8], 72 write: &[u8],
73 rd_buffer: &'a mut [u8], 73 read: &mut [u8],
74 ) -> Result<(), I2cDeviceError<BUS::Error>> { 74 ) -> Result<(), I2cDeviceError<BUS::Error>> {
75 let mut bus = self.bus.lock().await; 75 let mut bus = self.bus.lock().await;
76 bus.write_read(address, wr_buffer, rd_buffer) 76 bus.write_read(address, write, read)
77 .await 77 .await
78 .map_err(I2cDeviceError::I2c)?; 78 .map_err(I2cDeviceError::I2c)?;
79 Ok(()) 79 Ok(())
80 } 80 }
81 81
82 async fn transaction<'a, 'b>( 82 async fn transaction(
83 &'a mut self, 83 &mut self,
84 address: u8, 84 address: u8,
85 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 85 operations: &mut [embedded_hal_async::i2c::Operation<'_>],
86 ) -> Result<(), I2cDeviceError<BUS::Error>> { 86 ) -> Result<(), I2cDeviceError<BUS::Error>> {
87 let _ = address; 87 let _ = address;
88 let _ = operations; 88 let _ = operations;
@@ -121,25 +121,25 @@ where
121 M: RawMutex + 'static, 121 M: RawMutex + 'static,
122 BUS: i2c::I2c + SetConfig + 'static, 122 BUS: i2c::I2c + SetConfig + 'static,
123{ 123{
124 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { 124 async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
125 let mut bus = self.bus.lock().await; 125 let mut bus = self.bus.lock().await;
126 bus.set_config(&self.config); 126 bus.set_config(&self.config);
127 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?; 127 bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
128 Ok(()) 128 Ok(())
129 } 129 }
130 130
131 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { 131 async fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
132 let mut bus = self.bus.lock().await; 132 let mut bus = self.bus.lock().await;
133 bus.set_config(&self.config); 133 bus.set_config(&self.config);
134 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?; 134 bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
135 Ok(()) 135 Ok(())
136 } 136 }
137 137
138 async fn write_read<'a>( 138 async fn write_read(
139 &'a mut self, 139 &mut self,
140 address: u8, 140 address: u8,
141 wr_buffer: &'a [u8], 141 wr_buffer: &[u8],
142 rd_buffer: &'a mut [u8], 142 rd_buffer: &mut [u8],
143 ) -> Result<(), I2cDeviceError<BUS::Error>> { 143 ) -> Result<(), I2cDeviceError<BUS::Error>> {
144 let mut bus = self.bus.lock().await; 144 let mut bus = self.bus.lock().await;
145 bus.set_config(&self.config); 145 bus.set_config(&self.config);
@@ -149,11 +149,7 @@ where
149 Ok(()) 149 Ok(())
150 } 150 }
151 151
152 async fn transaction<'a, 'b>( 152 async fn transaction(&mut self, address: u8, operations: &mut [i2c::Operation<'_>]) -> Result<(), Self::Error> {
153 &'a mut self,
154 address: u8,
155 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
156 ) -> Result<(), I2cDeviceError<BUS::Error>> {
157 let _ = address; 153 let _ = address;
158 let _ = operations; 154 let _ = operations;
159 todo!() 155 todo!()
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
index d25716655..b5549a6cd 100644
--- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
+++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
@@ -25,12 +25,11 @@
25//! let spi_dev2 = SpiDevice::new(spi_bus, cs_pin2); 25//! let spi_dev2 = SpiDevice::new(spi_bus, cs_pin2);
26//! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128); 26//! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128);
27//! ``` 27//! ```
28use core::future::Future;
29 28
30use embassy_sync::blocking_mutex::raw::RawMutex; 29use embassy_sync::blocking_mutex::raw::RawMutex;
31use embassy_sync::mutex::Mutex; 30use embassy_sync::mutex::Mutex;
32use embedded_hal_1::digital::OutputPin; 31use embedded_hal_1::digital::OutputPin;
33use embedded_hal_1::spi::ErrorType; 32use embedded_hal_1::spi::Operation;
34use embedded_hal_async::spi; 33use embedded_hal_async::spi;
35 34
36use crate::shared_bus::SpiDeviceError; 35use crate::shared_bus::SpiDeviceError;
@@ -57,33 +56,92 @@ where
57 type Error = SpiDeviceError<BUS::Error, CS::Error>; 56 type Error = SpiDeviceError<BUS::Error, CS::Error>;
58} 57}
59 58
60unsafe impl<M, BUS, CS> spi::SpiDevice for SpiDevice<'_, M, BUS, CS> 59impl<M, BUS, CS> spi::SpiDeviceRead for SpiDevice<'_, M, BUS, CS>
61where 60where
62 M: RawMutex + 'static, 61 M: RawMutex,
63 BUS: spi::SpiBusFlush + 'static, 62 BUS: spi::SpiBusRead,
64 CS: OutputPin, 63 CS: OutputPin,
65{ 64{
66 type Bus = BUS; 65 async fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
66 let mut bus = self.bus.lock().await;
67 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
68
69 let op_res: Result<(), BUS::Error> = try {
70 for buf in operations {
71 bus.read(buf).await?;
72 }
73 };
74
75 // On failure, it's important to still flush and deassert CS.
76 let flush_res = bus.flush().await;
77 let cs_res = self.cs.set_high();
67 78
68 async fn transaction<R, F, Fut>(&mut self, f: F) -> Result<R, Self::Error> 79 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
69 where 80 flush_res.map_err(SpiDeviceError::Spi)?;
70 F: FnOnce(*mut Self::Bus) -> Fut, 81 cs_res.map_err(SpiDeviceError::Cs)?;
71 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>>, 82
72 { 83 Ok(op_res)
84 }
85}
86
87impl<M, BUS, CS> spi::SpiDeviceWrite for SpiDevice<'_, M, BUS, CS>
88where
89 M: RawMutex,
90 BUS: spi::SpiBusWrite,
91 CS: OutputPin,
92{
93 async fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
73 let mut bus = self.bus.lock().await; 94 let mut bus = self.bus.lock().await;
74 self.cs.set_low().map_err(SpiDeviceError::Cs)?; 95 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
75 96
76 let f_res = f(&mut *bus).await; 97 let op_res: Result<(), BUS::Error> = try {
98 for buf in operations {
99 bus.write(buf).await?;
100 }
101 };
77 102
78 // On failure, it's important to still flush and deassert CS. 103 // On failure, it's important to still flush and deassert CS.
79 let flush_res = bus.flush().await; 104 let flush_res = bus.flush().await;
80 let cs_res = self.cs.set_high(); 105 let cs_res = self.cs.set_high();
81 106
82 let f_res = f_res.map_err(SpiDeviceError::Spi)?; 107 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
83 flush_res.map_err(SpiDeviceError::Spi)?; 108 flush_res.map_err(SpiDeviceError::Spi)?;
84 cs_res.map_err(SpiDeviceError::Cs)?; 109 cs_res.map_err(SpiDeviceError::Cs)?;
85 110
86 Ok(f_res) 111 Ok(op_res)
112 }
113}
114
115impl<M, BUS, CS> spi::SpiDevice for SpiDevice<'_, M, BUS, CS>
116where
117 M: RawMutex,
118 BUS: spi::SpiBus,
119 CS: OutputPin,
120{
121 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> {
122 let mut bus = self.bus.lock().await;
123 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
124
125 let op_res: Result<(), BUS::Error> = try {
126 for op in operations {
127 match op {
128 Operation::Read(buf) => bus.read(buf).await?,
129 Operation::Write(buf) => bus.write(buf).await?,
130 Operation::Transfer(read, write) => bus.transfer(read, write).await?,
131 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?,
132 }
133 }
134 };
135
136 // On failure, it's important to still flush and deassert CS.
137 let flush_res = bus.flush().await;
138 let cs_res = self.cs.set_high();
139
140 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
141 flush_res.map_err(SpiDeviceError::Spi)?;
142 cs_res.map_err(SpiDeviceError::Cs)?;
143
144 Ok(op_res)
87 } 145 }
88} 146}
89 147
@@ -114,33 +172,94 @@ where
114 type Error = SpiDeviceError<BUS::Error, CS::Error>; 172 type Error = SpiDeviceError<BUS::Error, CS::Error>;
115} 173}
116 174
117unsafe impl<M, BUS, CS> spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> 175impl<M, BUS, CS> spi::SpiDeviceWrite for SpiDeviceWithConfig<'_, M, BUS, CS>
176where
177 M: RawMutex,
178 BUS: spi::SpiBusWrite + SetConfig,
179 CS: OutputPin,
180{
181 async fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
182 let mut bus = self.bus.lock().await;
183 bus.set_config(&self.config);
184 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
185
186 let op_res: Result<(), BUS::Error> = try {
187 for buf in operations {
188 bus.write(buf).await?;
189 }
190 };
191
192 // On failure, it's important to still flush and deassert CS.
193 let flush_res = bus.flush().await;
194 let cs_res = self.cs.set_high();
195
196 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
197 flush_res.map_err(SpiDeviceError::Spi)?;
198 cs_res.map_err(SpiDeviceError::Cs)?;
199
200 Ok(op_res)
201 }
202}
203
204impl<M, BUS, CS> spi::SpiDeviceRead for SpiDeviceWithConfig<'_, M, BUS, CS>
118where 205where
119 M: RawMutex + 'static, 206 M: RawMutex,
120 BUS: spi::SpiBusFlush + SetConfig + 'static, 207 BUS: spi::SpiBusRead + SetConfig,
121 CS: OutputPin, 208 CS: OutputPin,
122{ 209{
123 type Bus = BUS; 210 async fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
211 let mut bus = self.bus.lock().await;
212 bus.set_config(&self.config);
213 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
214
215 let op_res: Result<(), BUS::Error> = try {
216 for buf in operations {
217 bus.read(buf).await?;
218 }
219 };
220
221 // On failure, it's important to still flush and deassert CS.
222 let flush_res = bus.flush().await;
223 let cs_res = self.cs.set_high();
224
225 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
226 flush_res.map_err(SpiDeviceError::Spi)?;
227 cs_res.map_err(SpiDeviceError::Cs)?;
228
229 Ok(op_res)
230 }
231}
124 232
125 async fn transaction<R, F, Fut>(&mut self, f: F) -> Result<R, Self::Error> 233impl<M, BUS, CS> spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS>
126 where 234where
127 F: FnOnce(*mut Self::Bus) -> Fut, 235 M: RawMutex,
128 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>>, 236 BUS: spi::SpiBus + SetConfig,
129 { 237 CS: OutputPin,
238{
239 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> {
130 let mut bus = self.bus.lock().await; 240 let mut bus = self.bus.lock().await;
131 bus.set_config(&self.config); 241 bus.set_config(&self.config);
132 self.cs.set_low().map_err(SpiDeviceError::Cs)?; 242 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
133 243
134 let f_res = f(&mut *bus).await; 244 let op_res: Result<(), BUS::Error> = try {
245 for op in operations {
246 match op {
247 Operation::Read(buf) => bus.read(buf).await?,
248 Operation::Write(buf) => bus.write(buf).await?,
249 Operation::Transfer(read, write) => bus.transfer(read, write).await?,
250 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?,
251 }
252 }
253 };
135 254
136 // On failure, it's important to still flush and deassert CS. 255 // On failure, it's important to still flush and deassert CS.
137 let flush_res = bus.flush().await; 256 let flush_res = bus.flush().await;
138 let cs_res = self.cs.set_high(); 257 let cs_res = self.cs.set_high();
139 258
140 let f_res = f_res.map_err(SpiDeviceError::Spi)?; 259 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
141 flush_res.map_err(SpiDeviceError::Spi)?; 260 flush_res.map_err(SpiDeviceError::Spi)?;
142 cs_res.map_err(SpiDeviceError::Cs)?; 261 cs_res.map_err(SpiDeviceError::Cs)?;
143 262
144 Ok(f_res) 263 Ok(op_res)
145 } 264 }
146} 265}