Skip to content

Commit 5ae1f90

Browse files
authored
Merge pull request #488 from ptpaterson/eh1_0_alpha-spi
implement embedded-hal aplha SPI traits
2 parents 9bc48de + 98daa7a commit 5ae1f90

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

rp2040-hal/src/spi.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,91 @@ macro_rules! impl_write {
230230
type Error = Infallible;
231231
}
232232

233-
/* disabled for now - nb was migrated to separate crate
233+
#[cfg(feature = "eh1_0_alpha")]
234+
impl<D: SpiDevice> eh1::SpiBusFlush for Spi<Enabled, D, $nr> {
235+
fn flush(&mut self) -> Result<(), Self::Error> {
236+
while self.is_busy() {}
237+
Ok(())
238+
}
239+
}
240+
241+
#[cfg(feature = "eh1_0_alpha")]
242+
impl<D: SpiDevice> eh1::SpiBusRead<$type> for Spi<Enabled, D, $nr> {
243+
fn read(&mut self, words: &mut [$type]) -> Result<(), Self::Error> {
244+
for word in words.iter_mut() {
245+
// write empty word
246+
while !self.is_writable() {}
247+
self.device
248+
.sspdr
249+
.write(|w| unsafe { w.data().bits(0) });
250+
251+
// read one word
252+
while !self.is_readable() {}
253+
*word = self.device.sspdr.read().data().bits() as $type;
254+
}
255+
Ok(())
256+
}
257+
}
258+
259+
#[cfg(feature = "eh1_0_alpha")]
260+
impl<D: SpiDevice> eh1::SpiBusWrite<$type> for Spi<Enabled, D, $nr> {
261+
fn write(&mut self, words: &[$type]) -> Result<(), Self::Error> {
262+
for word in words.iter() {
263+
// write one word
264+
while !self.is_writable() {}
265+
self.device
266+
.sspdr
267+
.write(|w| unsafe { w.data().bits(*word as u16) });
268+
269+
// drop read wordd
270+
while !self.is_readable() {}
271+
let _ = self.device.sspdr.read().data().bits();
272+
}
273+
Ok(())
274+
}
275+
}
276+
277+
#[cfg(feature = "eh1_0_alpha")]
278+
impl<D: SpiDevice> eh1::SpiBus<$type> for Spi<Enabled, D, $nr> {
279+
fn transfer(&mut self, read: &mut [$type], write: &[$type]) -> Result<(), Self::Error>{
280+
let len = read.len().max(write.len());
281+
for i in 0..len {
282+
// write one word. Send empty word if buffer is empty.
283+
let wb = write.get(i).copied().unwrap_or(0);
284+
while !self.is_writable() {}
285+
self.device
286+
.sspdr
287+
.write(|w| unsafe { w.data().bits(wb as u16) });
288+
289+
// read one word. Drop extra words if buffer is full.
290+
while !self.is_readable() {}
291+
let rb = self.device.sspdr.read().data().bits() as $type;
292+
if let Some(r) = read.get_mut(i) {
293+
*r = rb;
294+
}
295+
}
296+
297+
Ok(())
298+
}
299+
300+
fn transfer_in_place(&mut self, words: &mut [$type]) -> Result<(), Self::Error>{
301+
for word in words.iter_mut() {
302+
// write one word
303+
while !self.is_writable() {}
304+
self.device
305+
.sspdr
306+
.write(|w| unsafe { w.data().bits(*word as u16) });
307+
308+
// read one word
309+
while !self.is_readable() {}
310+
*word = self.device.sspdr.read().data().bits() as $type;
311+
}
312+
313+
Ok(())
314+
}
315+
}
316+
317+
/* disabled for now - nb was migrated to separate crate
234318
#[cfg(feature = "eh1_0_alpha")]
235319
impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> {
236320
fn read(&mut self) -> Result<$type, nb::Error<Infallible>> {

0 commit comments

Comments
 (0)