implement embedded-hal aplha SPI traits

This commit is contained in:
Paul Paterson 2022-10-28 13:21:55 -04:00
parent 2fb3e2a7e9
commit 98daa7a25d

View file

@ -230,6 +230,90 @@ macro_rules! impl_write {
type Error = Infallible;
}
#[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::SpiBusFlush for Spi<Enabled, D, $nr> {
fn flush(&mut self) -> Result<(), Self::Error> {
while self.is_busy() {}
Ok(())
}
}
#[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::SpiBusRead<$type> for Spi<Enabled, D, $nr> {
fn read(&mut self, words: &mut [$type]) -> Result<(), Self::Error> {
for word in words.iter_mut() {
// write empty word
while !self.is_writable() {}
self.device
.sspdr
.write(|w| unsafe { w.data().bits(0) });
// read one word
while !self.is_readable() {}
*word = self.device.sspdr.read().data().bits() as $type;
}
Ok(())
}
}
#[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::SpiBusWrite<$type> for Spi<Enabled, D, $nr> {
fn write(&mut self, words: &[$type]) -> Result<(), Self::Error> {
for word in words.iter() {
// write one word
while !self.is_writable() {}
self.device
.sspdr
.write(|w| unsafe { w.data().bits(*word as u16) });
// drop read wordd
while !self.is_readable() {}
let _ = self.device.sspdr.read().data().bits();
}
Ok(())
}
}
#[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::SpiBus<$type> for Spi<Enabled, D, $nr> {
fn transfer(&mut self, read: &mut [$type], write: &[$type]) -> Result<(), Self::Error>{
let len = read.len().max(write.len());
for i in 0..len {
// write one word. Send empty word if buffer is empty.
let wb = write.get(i).copied().unwrap_or(0);
while !self.is_writable() {}
self.device
.sspdr
.write(|w| unsafe { w.data().bits(wb as u16) });
// read one word. Drop extra words if buffer is full.
while !self.is_readable() {}
let rb = self.device.sspdr.read().data().bits() as $type;
if let Some(r) = read.get_mut(i) {
*r = rb;
}
}
Ok(())
}
fn transfer_in_place(&mut self, words: &mut [$type]) -> Result<(), Self::Error>{
for word in words.iter_mut() {
// write one word
while !self.is_writable() {}
self.device
.sspdr
.write(|w| unsafe { w.data().bits(*word as u16) });
// read one word
while !self.is_readable() {}
*word = self.device.sspdr.read().data().bits() as $type;
}
Ok(())
}
}
/* disabled for now - nb was migrated to separate crate
#[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> {