Skip to content

Commit

Permalink
spi: make Operation::DelayNs with nanosecond granularity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 28, 2023
1 parent 68ef494 commit a56e9b4
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions embedded-hal-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- delay: Rename `DelayUs` to `BasicDelay`
- delay: Add `BasicDelay::delay_ns()`
- delay: Add default impls of `delay_ms` and `delay_us` based on `delay_ns`.
- spi: Rename `Operation::DelayUs` to `Operation::DelayNs`, with microsecond precision.

## [v1.0.0-rc.1] - 2023-08-15

Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-bus/src/spi/critical_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a, BUS, CS> CriticalSectionDevice<'a, BUS, CS, super::NoDelay> {
/// # Panics
///
/// The returned device will panic if you try to execute a transaction
/// that contains any operations of type [`Operation::DelayUs`].
/// that contains any operations of type [`Operation::DelayNs`].
#[inline]
pub fn new_no_delay(bus: &'a Mutex<RefCell<BUS>>, cs: CS) -> Self {
Self {
Expand Down
11 changes: 6 additions & 5 deletions embedded-hal-bus/src/spi/exclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use embedded_hal::digital::OutputPin;
use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice};
#[cfg(feature = "async")]
use embedded_hal_async::{
delay::BasicDelay as AsyncDelayUs,
delay::BasicDelay as AsyncBasicDelay,
spi::{SpiBus as AsyncSpiBus, SpiDevice as AsyncSpiDevice},
};

use super::shared::transaction;
use super::DeviceError;

/// [`SpiDevice`] implementation with exclusive access to the bus (not shared).
Expand Down Expand Up @@ -47,7 +48,7 @@ impl<BUS, CS> ExclusiveDevice<BUS, CS, super::NoDelay> {
/// # Panics
///
/// The returned device will panic if you try to execute a transaction
/// that contains any operations of type `Operation::DelayUs`.
/// that contains any operations of type [`Operation::DelayNs`].
#[inline]
pub fn new_no_delay(bus: BUS, cs: CS) -> Self {
Self {
Expand Down Expand Up @@ -84,7 +85,7 @@ impl<Word: Copy + 'static, BUS, CS, D> AsyncSpiDevice<Word> for ExclusiveDevice<
where
BUS: AsyncSpiBus<Word>,
CS: OutputPin,
D: AsyncDelayUs,
D: AsyncBasicDelay,
{
#[inline]
async fn transaction(
Expand All @@ -100,10 +101,10 @@ where
Operation::Write(buf) => self.bus.write(buf).await,
Operation::Transfer(read, write) => self.bus.transfer(read, write).await,
Operation::TransferInPlace(buf) => self.bus.transfer_in_place(buf).await,
Operation::DelayUs(us) => match self.bus.flush().await {
Operation::DelayNs(ns) => match self.bus.flush().await {
Err(e) => Err(e),
Ok(()) => {
self.delay.delay_us(*us).await;
self.delay.delay_ns(*ns).await;
Ok(())
}
},
Expand Down
4 changes: 2 additions & 2 deletions embedded-hal-bus/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ where
}
}

/// Dummy `DelayUs` implementation that panics on use.
/// Dummy [`BasicDelay`](embedded_hal::delay::BasicDelay) implementation that panics on use.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct NoDelay;

#[cold]
fn no_delay_panic() {
panic!("You've tried to execute a SPI transaction containing a `Operation::Delay` in a `SpiDevice` created with `new_no_delay()`. Create it with `new()` instead, passing a `DelayUs` implementation.");
panic!("You've tried to execute a SPI transaction containing a `Operation::DelayNs` in a `SpiDevice` created with `new_no_delay()`. Create it with `new()` instead, passing a `BasicDelay` implementation.");
}

impl embedded_hal::delay::BasicDelay for NoDelay {
Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-bus/src/spi/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a, BUS, CS> MutexDevice<'a, BUS, CS, super::NoDelay> {
/// # Panics
///
/// The returned device will panic if you try to execute a transaction
/// that contains any operations of type `Operation::DelayUs`.
/// that contains any operations of type [`Operation::DelayNs`].
#[inline]
pub fn new_no_delay(bus: &'a Mutex<BUS>, cs: CS) -> Self {
Self {
Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-bus/src/spi/refcell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a, BUS, CS> RefCellDevice<'a, BUS, CS, super::NoDelay> {
/// # Panics
///
/// The returned device will panic if you try to execute a transaction
/// that contains any operations of type `Operation::DelayUs`.
/// that contains any operations of type [`Operation::DelayNs`].
#[inline]
pub fn new_no_delay(bus: &'a RefCell<BUS>, cs: CS) -> Self {
Self {
Expand Down
4 changes: 2 additions & 2 deletions embedded-hal-bus/src/spi/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ where
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) => {
Operation::DelayNs(ns) => {
bus.flush()?;
delay.delay_us(*us);
delay.delay_ns(*ns);
Ok(())
}
});
Expand Down
1 change: 1 addition & 0 deletions embedded-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- delay: Add `BasicDelay::delay_ns()`
- delay: Add default impls of `delay_ms` and `delay_us` based on `delay_ns`.
- delay: Make the default impl of `delay_ms` more efficient, it now does less calls to the underlying `delay_ns` (previously `delay_us`).
- spi: Rename `Operation::DelayUs` to `Operation::DelayNs`, with microsecond precision.

## [v1.0.0-rc.1] - 2023-08-15

Expand Down
4 changes: 2 additions & 2 deletions embedded-hal/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ pub enum Operation<'a, Word: 'static> {
///
/// Equivalent to [`SpiBus::transfer_in_place`].
TransferInPlace(&'a mut [Word]),
/// Delay for at least the specified number of microseconds.
DelayUs(u32),
/// Delay for at least the specified number of nanoseconds.
DelayNs(u32),
}

/// SPI device trait.
Expand Down

0 comments on commit a56e9b4

Please sign in to comment.