Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SPI error messages when using embedded_hal #140

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading