Skip to content

Commit

Permalink
Improve SPI error messages when using embedded_hal
Browse files Browse the repository at this point in the history
  • Loading branch information
andber1 committed Mar 18, 2024
1 parent 77ec109 commit b13a645
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/spi/hal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use super::{super::hal::Delay, Error, Spi};

#[cfg(feature = "embedded-hal")]
Expand Down Expand Up @@ -113,44 +111,27 @@ impl<B: embedded_hal::spi::SpiBus<u8>> SimpleHalSpiDevice<B> {
}

#[cfg(feature = "embedded-hal")]
impl<B: embedded_hal::spi::SpiBus<u8>> embedded_hal::spi::SpiDevice<u8> for SimpleHalSpiDevice<B> {
impl<B: embedded_hal::spi::SpiBus<u8>> embedded_hal::spi::SpiDevice<u8> for SimpleHalSpiDevice<B>
where
Error: From<<B as embedded_hal::spi::ErrorType>::Error>,
{
fn transaction(
&mut self,
operations: &mut [embedded_hal::spi::Operation<'_, u8>],
) -> Result<(), Error> {
for op in operations {
match op {
embedded_hal::spi::Operation::Read(read) => {
self.bus.read(read).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice read transaction error",
))
})?;
self.bus.read(read)?;
}
embedded_hal::spi::Operation::Write(write) => {
self.bus.write(write).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice write transaction error",
))
})?;
self.bus.write(write)?;
}
embedded_hal::spi::Operation::Transfer(read, write) => {
self.bus.transfer(read, write).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice read/write transaction error",
))
})?;
self.bus.transfer(read, write)?;
}
embedded_hal::spi::Operation::TransferInPlace(words) => {
self.bus.transfer_in_place(words).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice in-place read/write transaction error",
))
})?;
self.bus.transfer_in_place(words)?;
}
embedded_hal::spi::Operation::DelayNs(us) => {
embedded_hal::delay::DelayNs::delay_us(&mut Delay::new(), *us);
Expand Down

0 comments on commit b13a645

Please sign in to comment.