-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate identical SPI transaction impl
Consolidate three identical implementations of the `SpiDevice::transaction`. Fewer lines of code, faster compilation, fewer bugs...
- Loading branch information
Showing
6 changed files
with
52 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::spi::DeviceError; | ||
use embedded_hal::delay::DelayUs; | ||
use embedded_hal::digital::OutputPin; | ||
use embedded_hal::spi::{ErrorType, Operation, SpiBus}; | ||
|
||
/// Common implementation to perform a transaction against the device. | ||
#[inline] | ||
pub fn transaction<Word, BUS, CS, D>( | ||
operations: &mut [Operation<Word>], | ||
bus: &mut BUS, | ||
delay: &mut D, | ||
cs: &mut CS, | ||
) -> Result<(), DeviceError<BUS::Error, CS::Error>> | ||
where | ||
BUS: SpiBus<Word> + ErrorType, | ||
CS: OutputPin, | ||
D: DelayUs, | ||
Word: Copy, | ||
{ | ||
cs.set_low().map_err(DeviceError::Cs)?; | ||
|
||
let op_res = operations.iter_mut().try_for_each(|op| match op { | ||
Operation::Read(buf) => bus.read(buf), | ||
Operation::Write(buf) => bus.write(buf), | ||
Operation::Transfer(read, write) => bus.transfer(read, write), | ||
Operation::TransferInPlace(buf) => bus.transfer_in_place(buf), | ||
Operation::DelayUs(us) => { | ||
bus.flush()?; | ||
delay.delay_us(*us); | ||
Ok(()) | ||
} | ||
}); | ||
|
||
// On failure, it's important to still flush and deassert CS. | ||
let flush_res = bus.flush(); | ||
let cs_res = cs.set_high(); | ||
|
||
op_res.map_err(DeviceError::Spi)?; | ||
flush_res.map_err(DeviceError::Spi)?; | ||
cs_res.map_err(DeviceError::Cs)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters