Merge pull request #488 from ptpaterson/eh1_0_alpha-spi

implement embedded-hal aplha SPI traits
This commit is contained in:
Jan Niehusmann 2022-10-30 13:47:11 +01:00 committed by GitHub
commit 5ae1f90939
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -230,7 +230,91 @@ macro_rules! impl_write {
type Error = Infallible; type Error = Infallible;
} }
/* disabled for now - nb was migrated to separate crate #[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")] #[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> { impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> {
fn read(&mut self) -> Result<$type, nb::Error<Infallible>> { fn read(&mut self) -> Result<$type, nb::Error<Infallible>> {