diff --git a/embedded-hal-bus/src/spi/atomic.rs b/embedded-hal-bus/src/spi/atomic.rs index 7d18e28c..ac605380 100644 --- a/embedded-hal-bus/src/spi/atomic.rs +++ b/embedded-hal-bus/src/spi/atomic.rs @@ -30,6 +30,8 @@ pub struct AtomicDevice<'a, BUS, CS, D> { bus: &'a AtomicCell, cs: CS, delay: D, + /// Implementation of + cs_to_clock_delay_ns: u32, } #[derive(Debug, Copy, Clone)] @@ -50,12 +52,22 @@ impl<'a, BUS, CS, D> AtomicDevice<'a, BUS, CS, D> { /// This sets the `cs` pin high, and returns an error if that fails. It is recommended /// to set the pin high the moment it's configured as an output, to avoid glitches. #[inline] - pub fn new(bus: &'a AtomicCell, mut cs: CS, delay: D) -> Result + pub fn new( + bus: &'a AtomicCell, + mut cs: CS, + delay: D, + cs_to_clock_delay_ns: u32, + ) -> Result where CS: OutputPin, { cs.set_high()?; - Ok(Self { bus, cs, delay }) + Ok(Self { + bus, + cs, + delay, + cs_to_clock_delay_ns, + }) } } @@ -93,6 +105,7 @@ where bus, cs, delay: super::NoDelay, + cs_to_clock_delay_ns: 0, }) } } @@ -134,7 +147,13 @@ where let bus = unsafe { &mut *self.bus.bus.get() }; - let result = transaction(operations, bus, &mut self.delay, &mut self.cs); + let result = transaction( + operations, + bus, + &mut self.delay, + &mut self.cs, + self.cs_to_clock_delay_ns, + ); self.bus .busy diff --git a/embedded-hal-bus/src/spi/critical_section.rs b/embedded-hal-bus/src/spi/critical_section.rs index 4c3a46eb..cb28cbd7 100644 --- a/embedded-hal-bus/src/spi/critical_section.rs +++ b/embedded-hal-bus/src/spi/critical_section.rs @@ -21,6 +21,8 @@ pub struct CriticalSectionDevice<'a, BUS, CS, D> { bus: &'a Mutex>, cs: CS, delay: D, + /// Implementation of + cs_to_clock_delay_ns: u32, } impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D> { @@ -29,12 +31,22 @@ impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D> { /// This sets the `cs` pin high, and returns an error if that fails. It is recommended /// to set the pin high the moment it's configured as an output, to avoid glitches. #[inline] - pub fn new(bus: &'a Mutex>, mut cs: CS, delay: D) -> Result + pub fn new( + bus: &'a Mutex>, + mut cs: CS, + delay: D, + cs_to_clock_delay_ns: u32, + ) -> Result where CS: OutputPin, { cs.set_high()?; - Ok(Self { bus, cs, delay }) + Ok(Self { + bus, + cs, + delay, + cs_to_clock_delay_ns, + }) } } @@ -68,6 +80,7 @@ impl<'a, BUS, CS> CriticalSectionDevice<'a, BUS, CS, super::NoDelay> { bus, cs, delay: super::NoDelay, + cs_to_clock_delay_ns: 0, }) } } @@ -91,7 +104,13 @@ where critical_section::with(|cs| { let bus = &mut *self.bus.borrow_ref_mut(cs); - transaction(operations, bus, &mut self.delay, &mut self.cs) + transaction( + operations, + bus, + &mut self.delay, + &mut self.cs, + self.cs_to_clock_delay_ns, + ) }) } } diff --git a/embedded-hal-bus/src/spi/exclusive.rs b/embedded-hal-bus/src/spi/exclusive.rs index 1599ae7a..26573d96 100644 --- a/embedded-hal-bus/src/spi/exclusive.rs +++ b/embedded-hal-bus/src/spi/exclusive.rs @@ -20,6 +20,8 @@ pub struct ExclusiveDevice { bus: BUS, cs: CS, delay: D, + /// Implementation of + cs_to_clock_delay_ns: u32, } impl ExclusiveDevice { @@ -28,12 +30,17 @@ impl ExclusiveDevice { /// This sets the `cs` pin high, and returns an error if that fails. It is recommended /// to set the pin high the moment it's configured as an output, to avoid glitches. #[inline] - pub fn new(bus: BUS, mut cs: CS, delay: D) -> Result + pub fn new(bus: BUS, mut cs: CS, delay: D, cs_to_clock_delay_ns: u32) -> Result where CS: OutputPin, { cs.set_high()?; - Ok(Self { bus, cs, delay }) + Ok(Self { + bus, + cs, + delay, + cs_to_clock_delay_ns, + }) } /// Returns a reference to the underlying bus object. @@ -79,6 +86,7 @@ impl ExclusiveDevice { bus, cs, delay: super::NoDelay, + cs_to_clock_delay_ns: 0, }) } } @@ -99,7 +107,13 @@ where { #[inline] fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> { - transaction(operations, &mut self.bus, &mut self.delay, &mut self.cs) + transaction( + operations, + &mut self.bus, + &mut self.delay, + &mut self.cs, + self.cs_to_clock_delay_ns, + ) } } diff --git a/embedded-hal-bus/src/spi/mutex.rs b/embedded-hal-bus/src/spi/mutex.rs index 83fe85d8..b4fc8946 100644 --- a/embedded-hal-bus/src/spi/mutex.rs +++ b/embedded-hal-bus/src/spi/mutex.rs @@ -19,6 +19,8 @@ pub struct MutexDevice<'a, BUS, CS, D> { bus: &'a Mutex, cs: CS, delay: D, + /// Implementation of + cs_to_clock_delay_ns: u32, } impl<'a, BUS, CS, D> MutexDevice<'a, BUS, CS, D> { @@ -27,12 +29,22 @@ impl<'a, BUS, CS, D> MutexDevice<'a, BUS, CS, D> { /// This sets the `cs` pin high, and returns an error if that fails. It is recommended /// to set the pin high the moment it's configured as an output, to avoid glitches. #[inline] - pub fn new(bus: &'a Mutex, mut cs: CS, delay: D) -> Result + pub fn new( + bus: &'a Mutex, + mut cs: CS, + delay: D, + cs_to_clock_delay_ns: u32, + ) -> Result where CS: OutputPin, { cs.set_high()?; - Ok(Self { bus, cs, delay }) + Ok(Self { + bus, + cs, + delay, + cs_to_clock_delay_ns, + }) } } @@ -66,6 +78,7 @@ impl<'a, BUS, CS> MutexDevice<'a, BUS, CS, super::NoDelay> { bus, cs, delay: super::NoDelay, + cs_to_clock_delay_ns: 0, }) } } @@ -88,6 +101,12 @@ where fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> { let bus = &mut *self.bus.lock().unwrap(); - transaction(operations, bus, &mut self.delay, &mut self.cs) + transaction( + operations, + bus, + &mut self.delay, + &mut self.cs, + self.cs_to_clock_delay_ns, + ) } } diff --git a/embedded-hal-bus/src/spi/refcell.rs b/embedded-hal-bus/src/spi/refcell.rs index 35bea03a..83d0a167 100644 --- a/embedded-hal-bus/src/spi/refcell.rs +++ b/embedded-hal-bus/src/spi/refcell.rs @@ -18,6 +18,8 @@ pub struct RefCellDevice<'a, BUS, CS, D> { bus: &'a RefCell, cs: CS, delay: D, + /// Implementation of + cs_to_clock_delay_ns: u32, } impl<'a, BUS, CS, D> RefCellDevice<'a, BUS, CS, D> { @@ -26,12 +28,22 @@ impl<'a, BUS, CS, D> RefCellDevice<'a, BUS, CS, D> { /// This sets the `cs` pin high, and returns an error if that fails. It is recommended /// to set the pin high the moment it's configured as an output, to avoid glitches. #[inline] - pub fn new(bus: &'a RefCell, mut cs: CS, delay: D) -> Result + pub fn new( + bus: &'a RefCell, + mut cs: CS, + delay: D, + cs_to_clock_delay_ns: u32, + ) -> Result where CS: OutputPin, { cs.set_high()?; - Ok(Self { bus, cs, delay }) + Ok(Self { + bus, + cs, + delay, + cs_to_clock_delay_ns, + }) } } @@ -65,6 +77,7 @@ impl<'a, BUS, CS> RefCellDevice<'a, BUS, CS, super::NoDelay> { bus, cs, delay: super::NoDelay, + cs_to_clock_delay_ns: 0, }) } } @@ -87,6 +100,12 @@ where fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> { let bus = &mut *self.bus.borrow_mut(); - transaction(operations, bus, &mut self.delay, &mut self.cs) + transaction( + operations, + bus, + &mut self.delay, + &mut self.cs, + self.cs_to_clock_delay_ns, + ) } } diff --git a/embedded-hal-bus/src/spi/shared.rs b/embedded-hal-bus/src/spi/shared.rs index 95730ba1..0e4ea33a 100644 --- a/embedded-hal-bus/src/spi/shared.rs +++ b/embedded-hal-bus/src/spi/shared.rs @@ -11,6 +11,7 @@ pub fn transaction( bus: &mut BUS, delay: &mut D, cs: &mut CS, + cs_to_clock_delay_ns: u32, ) -> Result<(), DeviceError> where BUS: SpiBus + ErrorType, @@ -19,6 +20,9 @@ where Word: Copy, { cs.set_low().map_err(DeviceError::Cs)?; + if cs_to_clock_delay_ns > 0 { + delay.delay_ns(cs_to_clock_delay_ns); + } let op_res = operations.iter_mut().try_for_each(|op| match op { Operation::Read(buf) => bus.read(buf),