|
| 1 | +use embedded_hal::delay::DelayNs; |
| 2 | +use embedded_hal::digital::OutputPin; |
| 3 | +use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice}; |
| 4 | +use mutex::{BlockingMutex, RawMutex}; |
| 5 | + |
| 6 | +use super::DeviceError; |
| 7 | +use crate::spi::shared::transaction; |
| 8 | + |
| 9 | +type Mutex<R, T> = BlockingMutex<R, T>; |
| 10 | + |
| 11 | +/// `mutex-traits`-based shared bus [`SpiDevice`] implementation. |
| 12 | +/// |
| 13 | +/// This allows for sharing an [`SpiBus`], obtaining multiple [`SpiDevice`] instances, |
| 14 | +/// each with its own `CS` pin. |
| 15 | +/// |
| 16 | +/// Whether a single bus can be used across multiple threads depends on which |
| 17 | +/// implementation of `RawMutex` is used. |
| 18 | +pub struct MutexTraitsDevice<'a, R, BUS, CS, D> { |
| 19 | + bus: &'a Mutex<R, BUS>, |
| 20 | + cs: CS, |
| 21 | + delay: D, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'a, R: RawMutex, BUS, CS, D> MutexTraitsDevice<'a, R, BUS, CS, D> { |
| 25 | + /// Create a new [`MutexTraitsDevice`]. |
| 26 | + /// |
| 27 | + /// This sets the `cs` pin high, and returns an error if that fails. It is recommended |
| 28 | + /// to set the pin high the moment it's configured as an output, to avoid glitches. |
| 29 | + #[inline] |
| 30 | + pub fn new(bus: &'a Mutex<R, BUS>, mut cs: CS, delay: D) -> Result<Self, CS::Error> |
| 31 | + where |
| 32 | + CS: OutputPin, |
| 33 | + { |
| 34 | + cs.set_high()?; |
| 35 | + Ok(Self { bus, cs, delay }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a, R: RawMutex, BUS, CS> MutexTraitsDevice<'a, R, BUS, CS, super::NoDelay> { |
| 40 | + /// Create a new [`MutexTraitsDevice`] without support for in-transaction delays. |
| 41 | + /// |
| 42 | + /// This sets the `cs` pin high, and returns an error if that fails. It is recommended |
| 43 | + /// to set the pin high the moment it's configured as an output, to avoid glitches. |
| 44 | + /// |
| 45 | + /// **Warning**: The returned instance *technically* doesn't comply with the `SpiDevice` |
| 46 | + /// contract, which mandates delay support. It is relatively rare for drivers to use |
| 47 | + /// in-transaction delays, so you might still want to use this method because it's more practical. |
| 48 | + /// |
| 49 | + /// Note that a future version of the driver might start using delays, causing your |
| 50 | + /// code to panic. This wouldn't be considered a breaking change from the driver side, because |
| 51 | + /// drivers are allowed to assume `SpiDevice` implementations comply with the contract. |
| 52 | + /// If you feel this risk outweighs the convenience of having `cargo` automatically upgrade |
| 53 | + /// the driver crate, you might want to pin the driver's version. |
| 54 | + /// |
| 55 | + /// # Panics |
| 56 | + /// |
| 57 | + /// The returned device will panic if you try to execute a transaction |
| 58 | + /// that contains any operations of type [`Operation::DelayNs`]. |
| 59 | + #[inline] |
| 60 | + pub fn new_no_delay(bus: &'a Mutex<R, BUS>, mut cs: CS) -> Result<Self, CS::Error> |
| 61 | + where |
| 62 | + CS: OutputPin, |
| 63 | + { |
| 64 | + cs.set_high()?; |
| 65 | + Ok(Self { |
| 66 | + bus, |
| 67 | + cs, |
| 68 | + delay: super::NoDelay, |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<R, BUS, CS, D> ErrorType for MutexTraitsDevice<'_, R, BUS, CS, D> |
| 74 | +where |
| 75 | + R: RawMutex, |
| 76 | + BUS: ErrorType, |
| 77 | + CS: OutputPin, |
| 78 | +{ |
| 79 | + type Error = DeviceError<BUS::Error, CS::Error>; |
| 80 | +} |
| 81 | + |
| 82 | +impl<Word: Copy + 'static, R, BUS, CS, D> SpiDevice<Word> for MutexTraitsDevice<'_, R, BUS, CS, D> |
| 83 | +where |
| 84 | + R: RawMutex, |
| 85 | + BUS: SpiBus<Word>, |
| 86 | + CS: OutputPin, |
| 87 | + D: DelayNs, |
| 88 | +{ |
| 89 | + #[inline] |
| 90 | + fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> { |
| 91 | + let bus = &mut *self.bus.lock(); |
| 92 | + |
| 93 | + transaction(operations, bus, &mut self.delay, &mut self.cs) |
| 94 | + } |
| 95 | +} |
0 commit comments