aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-05-20 11:30:28 +0200
committerGitHub <[email protected]>2024-05-20 11:30:28 +0200
commit34a0f744566ec3224e909fb01cb7672b1dcbb93a (patch)
treebabd754d9f81d5ec779d5fbe4d8e38ab16928352
parent621dbecedac3e57a15f070343608e9d631b55f08 (diff)
parent78cdebbc9523a0db3654f56e1a5acf8015d69248 (diff)
Merge pull request #2893 from Ragarnoy/u16-spidevice
Make SpiDevice generic over Word size and implement u16 transfer
-rw-r--r--embassy-embedded-hal/src/shared_bus/asynch/spi.rs14
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/spi.rs55
2 files changed, 16 insertions, 53 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
index 9890f218d..30d4ecc36 100644
--- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
+++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs
@@ -55,13 +55,14 @@ where
55 type Error = SpiDeviceError<BUS::Error, CS::Error>; 55 type Error = SpiDeviceError<BUS::Error, CS::Error>;
56} 56}
57 57
58impl<M, BUS, CS> spi::SpiDevice for SpiDevice<'_, M, BUS, CS> 58impl<M, BUS, CS, Word> spi::SpiDevice<Word> for SpiDevice<'_, M, BUS, CS>
59where 59where
60 M: RawMutex, 60 M: RawMutex,
61 BUS: spi::SpiBus, 61 BUS: spi::SpiBus<Word>,
62 CS: OutputPin, 62 CS: OutputPin,
63 Word: Copy + 'static,
63{ 64{
64 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { 65 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
65 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) { 66 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
66 return Err(SpiDeviceError::DelayNotSupported); 67 return Err(SpiDeviceError::DelayNotSupported);
67 } 68 }
@@ -138,13 +139,14 @@ where
138 type Error = SpiDeviceError<BUS::Error, CS::Error>; 139 type Error = SpiDeviceError<BUS::Error, CS::Error>;
139} 140}
140 141
141impl<M, BUS, CS> spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> 142impl<M, BUS, CS, Word> spi::SpiDevice<Word> for SpiDeviceWithConfig<'_, M, BUS, CS>
142where 143where
143 M: RawMutex, 144 M: RawMutex,
144 BUS: spi::SpiBus + SetConfig, 145 BUS: spi::SpiBus<Word> + SetConfig,
145 CS: OutputPin, 146 CS: OutputPin,
147 Word: Copy + 'static,
146{ 148{
147 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { 149 async fn transaction(&mut self, operations: &mut [spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
148 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) { 150 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
149 return Err(SpiDeviceError::DelayNotSupported); 151 return Err(SpiDeviceError::DelayNotSupported);
150 } 152 }
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
index 801899f9f..eb9c0c4f4 100644
--- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
+++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs
@@ -48,13 +48,14 @@ 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::SpiDevice for SpiDevice<'_, M, BUS, CS> 51impl<BUS, M, CS, Word> embedded_hal_1::spi::SpiDevice<Word> for SpiDevice<'_, M, BUS, CS>
52where 52where
53 M: RawMutex, 53 M: RawMutex,
54 BUS: SpiBus, 54 BUS: SpiBus<Word>,
55 CS: OutputPin, 55 CS: OutputPin,
56 Word: Copy + 'static,
56{ 57{
57 fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { 58 fn transaction(&mut self, operations: &mut [embedded_hal_1::spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
58 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) { 59 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
59 return Err(SpiDeviceError::DelayNotSupported); 60 return Err(SpiDeviceError::DelayNotSupported);
60 } 61 }
@@ -90,47 +91,6 @@ where
90 } 91 }
91} 92}
92 93
93impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer<u8> for SpiDevice<'_, M, BUS, CS>
94where
95 M: RawMutex,
96 BUS: embedded_hal_02::blocking::spi::Transfer<u8, Error = BusErr>,
97 CS: OutputPin<Error = CsErr>,
98{
99 type Error = SpiDeviceError<BusErr, CsErr>;
100 fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
101 self.bus.lock(|bus| {
102 let mut bus = bus.borrow_mut();
103 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
104 let op_res = bus.transfer(words);
105 let cs_res = self.cs.set_high();
106 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
107 cs_res.map_err(SpiDeviceError::Cs)?;
108 Ok(op_res)
109 })
110 }
111}
112
113impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write<u8> for SpiDevice<'_, M, BUS, CS>
114where
115 M: RawMutex,
116 BUS: embedded_hal_02::blocking::spi::Write<u8, Error = BusErr>,
117 CS: OutputPin<Error = CsErr>,
118{
119 type Error = SpiDeviceError<BusErr, CsErr>;
120
121 fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
122 self.bus.lock(|bus| {
123 let mut bus = bus.borrow_mut();
124 self.cs.set_low().map_err(SpiDeviceError::Cs)?;
125 let op_res = bus.write(words);
126 let cs_res = self.cs.set_high();
127 let op_res = op_res.map_err(SpiDeviceError::Spi)?;
128 cs_res.map_err(SpiDeviceError::Cs)?;
129 Ok(op_res)
130 })
131 }
132}
133
134/// SPI device on a shared bus, with its own configuration. 94/// SPI device on a shared bus, with its own configuration.
135/// 95///
136/// This is like [`SpiDevice`], with an additional bus configuration that's applied 96/// This is like [`SpiDevice`], with an additional bus configuration that's applied
@@ -163,13 +123,14 @@ where
163 type Error = SpiDeviceError<BUS::Error, CS::Error>; 123 type Error = SpiDeviceError<BUS::Error, CS::Error>;
164} 124}
165 125
166impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> 126impl<BUS, M, CS, Word> embedded_hal_1::spi::SpiDevice<Word> for SpiDeviceWithConfig<'_, M, BUS, CS>
167where 127where
168 M: RawMutex, 128 M: RawMutex,
169 BUS: SpiBus + SetConfig, 129 BUS: SpiBus<Word> + SetConfig,
170 CS: OutputPin, 130 CS: OutputPin,
131 Word: Copy + 'static,
171{ 132{
172 fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { 133 fn transaction(&mut self, operations: &mut [embedded_hal_1::spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
173 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) { 134 if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
174 return Err(SpiDeviceError::DelayNotSupported); 135 return Err(SpiDeviceError::DelayNotSupported);
175 } 136 }