Skip to content

Commit 7ad7d13

Browse files
committed
add DelayNs providers to spi devices
1 parent 9b78d21 commit 7ad7d13

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

src/spi/bus.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::convert::Infallible;
2+
use embedded_hal::delay::DelayNs;
23
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
34
pub use embedded_io::Write;
45

@@ -248,8 +249,11 @@ where
248249
PINS: Pins<SPI>,
249250
{
250251
/// Create a new [SpiExclusiveDevice] for exclusive use on this bus
251-
pub fn new_device(self, config: &SpiConfig) -> SpiExclusiveDevice<SPI, PINS> {
252-
SpiExclusiveDevice::new(self, config)
252+
pub fn new_device<D>(self, config: &SpiConfig, delay: D) -> SpiExclusiveDevice<SPI, PINS, D>
253+
where
254+
D: DelayNs,
255+
{
256+
SpiExclusiveDevice::new(self, config, delay)
253257
}
254258
}
255259

src/spi/exclusive_device.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
use core::convert::Infallible;
22

3-
use embedded_hal::spi::{ErrorType, Operation, SpiBus as HalBus, SpiDevice};
3+
use embedded_hal::{
4+
delay::DelayNs,
5+
spi::{ErrorType, Operation, SpiBus as HalBus, SpiDevice},
6+
};
47

58
use crate::spi::SpiConfig;
69

710
use super::{Pins, SpiBus, SpiX};
811

912
/// SPI exclusive device abstraction
10-
pub struct SpiExclusiveDevice<SPI, PINS> {
13+
pub struct SpiExclusiveDevice<SPI, PINS, D> {
1114
bus: SpiBus<SPI, PINS>,
15+
delay: D,
1216
}
1317

14-
impl<SPI, PINS> SpiExclusiveDevice<SPI, PINS>
18+
impl<SPI, PINS, D> SpiExclusiveDevice<SPI, PINS, D>
1519
where
1620
SPI: SpiX,
1721
PINS: Pins<SPI>,
22+
D: DelayNs,
1823
{
1924
/// Create [SpiExclusiveDevice] using the existing [SpiBus](super::SpiBus)
2025
/// with the given [SpiConfig]
21-
pub fn new(mut bus: SpiBus<SPI, PINS>, config: &SpiConfig) -> Self
26+
pub fn new(mut bus: SpiBus<SPI, PINS>, config: &SpiConfig, delay: D) -> Self
2227
where
2328
PINS: Pins<SPI>,
2429
{
2530
bus.configure(config, PINS::CS_INDEX);
2631

27-
Self { bus }
32+
Self { bus, delay }
2833
}
2934

3035
/// Releases the Bus back deconstructing it
@@ -45,30 +50,31 @@ where
4550
}
4651
}
4752

48-
impl<SPI, PINS> ErrorType for SpiExclusiveDevice<SPI, PINS>
53+
impl<SPI, PINS, D> ErrorType for SpiExclusiveDevice<SPI, PINS, D>
4954
where
5055
SPI: SpiX,
5156
PINS: Pins<SPI>,
5257
{
5358
type Error = Infallible;
5459
}
5560

56-
impl<SPI, PINS> SpiDevice for SpiExclusiveDevice<SPI, PINS>
61+
impl<SPI, PINS, D> SpiDevice for SpiExclusiveDevice<SPI, PINS, D>
5762
where
5863
SPI: SpiX,
5964
PINS: Pins<SPI>,
65+
D: DelayNs,
6066
{
6167
fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> {
6268
self.bus.start_frame();
6369

6470
for operation in operations {
6571
match operation {
66-
Operation::Read(words) => self.bus.read(words),
67-
Operation::Write(words) => self.bus.write(words),
68-
Operation::Transfer(read, write) => self.bus.transfer(read, write),
69-
Operation::TransferInPlace(words) => self.bus.transfer_in_place(words),
70-
Operation::DelayNs(_ns) => Ok(()), // TODO: NOOP?
71-
}?;
72+
Operation::Read(words) => self.bus.read(words)?,
73+
Operation::Write(words) => self.bus.write(words)?,
74+
Operation::Transfer(read, write) => self.bus.transfer(read, write)?,
75+
Operation::TransferInPlace(words) => self.bus.transfer_in_place(words)?,
76+
Operation::DelayNs(ns) => self.delay.delay_ns(*ns),
77+
}
7278
}
7379
self.bus.end_frame();
7480

src/spi/shared_bus.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::cell::RefCell;
22
use core::ops::Deref;
3+
use embedded_hal::delay::DelayNs;
34
use riscv::interrupt;
45

56
use super::{PinCS, PinsNoCS, SpiBus, SpiConfig, SpiSharedDevice, SpiX};
@@ -18,15 +19,17 @@ where
1819
}
1920

2021
/// Create a new shared device on this SPI bus.
21-
pub fn new_device<'bus, CS>(
22+
pub fn new_device<'bus, CS, D>(
2223
&'bus self,
2324
cs: CS,
2425
config: &SpiConfig,
25-
) -> SpiSharedDevice<'bus, SPI, PINS, CS>
26+
delay: D,
27+
) -> SpiSharedDevice<'bus, SPI, PINS, CS, D>
2628
where
2729
CS: PinCS<SPI>,
30+
D: DelayNs,
2831
{
29-
SpiSharedDevice::new(self, cs, config)
32+
SpiSharedDevice::new(self, cs, config, delay)
3033
}
3134
}
3235

src/spi/shared_device.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
use core::convert::Infallible;
22

3-
use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice};
3+
use embedded_hal::{
4+
delay::DelayNs,
5+
spi::{ErrorType, Operation, SpiBus, SpiDevice},
6+
};
47
use riscv::interrupt;
58

69
use super::{PinCS, Pins, PinsNoCS, SharedBus, SpiConfig, SpiX};
710

811
/// SPI shared device abstraction
9-
pub struct SpiSharedDevice<'bus, SPI, PINS, CS> {
12+
pub struct SpiSharedDevice<'bus, SPI, PINS, CS, D> {
1013
bus: &'bus SharedBus<SPI, PINS>,
1114
cs: CS,
1215
config: SpiConfig,
16+
delay: D,
1317
}
1418

15-
impl<'bus, SPI, PINS, CS> SpiSharedDevice<'bus, SPI, PINS, CS>
19+
impl<'bus, SPI, PINS, CS, D> SpiSharedDevice<'bus, SPI, PINS, CS, D>
1620
where
1721
SPI: SpiX,
1822
PINS: PinsNoCS<SPI>,
1923
CS: PinCS<SPI>,
24+
D: DelayNs,
2025
{
2126
/// Create shared [SpiSharedDevice] using the existing [SharedBus]
2227
/// and given [SpiConfig]. The config gets cloned.
23-
pub fn new(bus: &'bus SharedBus<SPI, PINS>, cs: CS, config: &SpiConfig) -> Self
28+
pub fn new(bus: &'bus SharedBus<SPI, PINS>, cs: CS, config: &SpiConfig, delay: D) -> Self
2429
where
2530
PINS: PinsNoCS<SPI>,
2631
{
2732
Self {
2833
bus,
2934
cs,
3035
config: config.clone(),
36+
delay,
3137
}
3238
}
3339

@@ -55,20 +61,22 @@ where
5561
}
5662
}
5763

58-
impl<SPI, PINS, CS> ErrorType for SpiSharedDevice<'_, SPI, PINS, CS>
64+
impl<SPI, PINS, CS, D> ErrorType for SpiSharedDevice<'_, SPI, PINS, CS, D>
5965
where
6066
SPI: SpiX,
6167
PINS: Pins<SPI>,
6268
CS: PinCS<SPI>,
69+
D: DelayNs,
6370
{
6471
type Error = Infallible;
6572
}
6673

67-
impl<SPI, PINS, CS> SpiDevice for SpiSharedDevice<'_, SPI, PINS, CS>
74+
impl<SPI, PINS, CS, D> SpiDevice for SpiSharedDevice<'_, SPI, PINS, CS, D>
6875
where
6976
SPI: SpiX,
7077
PINS: Pins<SPI>,
7178
CS: PinCS<SPI>,
79+
D: DelayNs,
7280
{
7381
fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Infallible> {
7482
interrupt::free(|| {
@@ -79,12 +87,12 @@ where
7987
bus.start_frame();
8088
for operation in operations {
8189
match operation {
82-
Operation::Read(words) => bus.read(words),
83-
Operation::Write(words) => bus.write(words),
84-
Operation::Transfer(read, write) => bus.transfer(read, write),
85-
Operation::TransferInPlace(words) => bus.transfer_in_place(words),
86-
Operation::DelayNs(_ns) => Ok(()), // TODO: NOOP?
87-
}?;
90+
Operation::Read(words) => bus.read(words)?,
91+
Operation::Write(words) => bus.write(words)?,
92+
Operation::Transfer(read, write) => bus.transfer(read, write)?,
93+
Operation::TransferInPlace(words) => bus.transfer_in_place(words)?,
94+
Operation::DelayNs(ns) => self.delay.delay_ns(*ns),
95+
};
8896
}
8997
bus.end_frame();
9098

0 commit comments

Comments
 (0)