aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/blocking
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking')
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/spi.rs120
1 files changed, 15 insertions, 105 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
index 22e013be9..6d03d6263 100644
--- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
+++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
@@ -22,7 +22,7 @@ use core::cell::RefCell;
22use embassy_sync::blocking_mutex::raw::RawMutex; 22use embassy_sync::blocking_mutex::raw::RawMutex;
23use embassy_sync::blocking_mutex::Mutex; 23use embassy_sync::blocking_mutex::Mutex;
24use embedded_hal_1::digital::OutputPin; 24use embedded_hal_1::digital::OutputPin;
25use embedded_hal_1::spi::{self, Operation, SpiBus, SpiBusRead, SpiBusWrite}; 25use embedded_hal_1::spi::{self, Operation, SpiBus};
26 26
27use crate::shared_bus::SpiDeviceError; 27use crate::shared_bus::SpiDeviceError;
28use crate::SetConfig; 28use crate::SetConfig;
@@ -48,58 +48,6 @@ where
48 type Error = SpiDeviceError<BUS::Error, CS::Error>; 48 type Error = SpiDeviceError<BUS::Error, CS::Error>;
49} 49}
50 50
51impl<BUS, M, CS> embedded_hal_1::spi::SpiDeviceRead for SpiDevice<'_, M, BUS, CS>
52where
53 M: RawMutex,
54 BUS: SpiBusRead,
55 CS: OutputPin,
56{
57 fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
58 self.bus.lock(|bus| {
59 let mut bus = bus.borrow_mut();
60 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
61
62 let op_res = operations.iter_mut().try_for_each(|buf| bus.read(buf));
63
64 // On failure, it's important to still flush and deassert CS.
65 let flush_res = bus.flush();
66 let cs_res = self.cs.set_high();
67
68 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
69 flush_res.map_err(SpiDeviceError::Spi)?;
70 cs_res.map_err(SpiDeviceError::Cs)?;
71
72 Ok(op_res)
73 })
74 }
75}
76
77impl<BUS, M, CS> embedded_hal_1::spi::SpiDeviceWrite for SpiDevice<'_, M, BUS, CS>
78where
79 M: RawMutex,
80 BUS: SpiBusWrite,
81 CS: OutputPin,
82{
83 fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
84 self.bus.lock(|bus| {
85 let mut bus = bus.borrow_mut();
86 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
87
88 let op_res = operations.iter().try_for_each(|buf| bus.write(buf));
89
90 // On failure, it's important to still flush and deassert CS.
91 let flush_res = bus.flush();
92 let cs_res = self.cs.set_high();
93
94 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
95 flush_res.map_err(SpiDeviceError::Spi)?;
96 cs_res.map_err(SpiDeviceError::Cs)?;
97
98 Ok(op_res)
99 })
100 }
101}
102
103impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDevice<'_, M, BUS, CS> 51impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDevice<'_, M, BUS, CS>
104where 52where
105 M: RawMutex, 53 M: RawMutex,
@@ -116,6 +64,13 @@ where
116 Operation::Write(buf) => bus.write(buf), 64 Operation::Write(buf) => bus.write(buf),
117 Operation::Transfer(read, write) => bus.transfer(read, write), 65 Operation::Transfer(read, write) => bus.transfer(read, write),
118 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf), 66 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf),
67 #[cfg(not(feature = "time"))]
68 Operation::DelayUs(_) => Err(SpiDeviceError::DelayUsNotSupported),
69 #[cfg(feature = "time")]
70 Operation::DelayUs(us) => {
71 embassy_time::block_for(embassy_time::Duration::from_micros(*us as _));
72 Ok(())
73 }
119 }); 74 });
120 75
121 // On failure, it's important to still flush and deassert CS. 76 // On failure, it's important to still flush and deassert CS.
@@ -199,58 +154,6 @@ where
199 type Error = SpiDeviceError<BUS::Error, CS::Error>; 154 type Error = SpiDeviceError<BUS::Error, CS::Error>;
200} 155}
201 156
202impl<BUS, M, CS> embedded_hal_1::spi::SpiDeviceRead for SpiDeviceWithConfig<'_, M, BUS, CS>
203where
204 M: RawMutex,
205 BUS: SpiBusRead + SetConfig,
206 CS: OutputPin,
207{
208 fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
209 self.bus.lock(|bus| {
210 let mut bus = bus.borrow_mut();
211 bus.set_config(&self.config);
212 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
213
214 let op_res = operations.iter_mut().try_for_each(|buf| bus.read(buf));
215
216 // On failure, it's important to still flush and deassert CS.
217 let flush_res = bus.flush();
218 let cs_res = self.cs.set_high();
219
220 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
221 flush_res.map_err(SpiDeviceError::Spi)?;
222 cs_res.map_err(SpiDeviceError::Cs)?;
223 Ok(op_res)
224 })
225 }
226}
227
228impl<BUS, M, CS> embedded_hal_1::spi::SpiDeviceWrite for SpiDeviceWithConfig<'_, M, BUS, CS>
229where
230 M: RawMutex,
231 BUS: SpiBusWrite + SetConfig,
232 CS: OutputPin,
233{
234 fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
235 self.bus.lock(|bus| {
236 let mut bus = bus.borrow_mut();
237 bus.set_config(&self.config);
238 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
239
240 let op_res = operations.iter().try_for_each(|buf| bus.write(buf));
241
242 // On failure, it's important to still flush and deassert CS.
243 let flush_res = bus.flush();
244 let cs_res = self.cs.set_high();
245
246 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
247 flush_res.map_err(SpiDeviceError::Spi)?;
248 cs_res.map_err(SpiDeviceError::Cs)?;
249 Ok(op_res)
250 })
251 }
252}
253
254impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> 157impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS>
255where 158where
256 M: RawMutex, 159 M: RawMutex,
@@ -268,6 +171,13 @@ where
268 Operation::Write(buf) => bus.write(buf), 171 Operation::Write(buf) => bus.write(buf),
269 Operation::Transfer(read, write) => bus.transfer(read, write), 172 Operation::Transfer(read, write) => bus.transfer(read, write),
270 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf), 173 Operation::TransferInPlace(buf) => bus.transfer_in_place(buf),
174 #[cfg(not(feature = "time"))]
175 Operation::DelayUs(_) => Err(SpiDeviceError::DelayUsNotSupported),
176 #[cfg(feature = "time")]
177 Operation::DelayUs(us) => {
178 embassy_time::block_for(embassy_time::Duration::from_micros(*us as _));
179 Ok(())
180 }
271 }); 181 });
272 182
273 // On failure, it's important to still flush and deassert CS. 183 // On failure, it's important to still flush and deassert CS.