Skip to content

Commit 0bbd0f6

Browse files
author
vchapuis
committed
feat(embedded-hal-bus): Spi - Add support for cs to clock delay
1 parent a0ccb65 commit 0bbd0f6

File tree

6 files changed

+109
-15
lines changed

6 files changed

+109
-15
lines changed

embedded-hal-bus/src/spi/atomic.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct AtomicDevice<'a, BUS, CS, D> {
3030
bus: &'a AtomicCell<BUS>,
3131
cs: CS,
3232
delay: D,
33+
/// Implementation of https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#cs-to-clock-delays
34+
cs_to_clock_delay_ns: u32,
3335
}
3436

3537
#[derive(Debug, Copy, Clone)]
@@ -50,12 +52,22 @@ impl<'a, BUS, CS, D> AtomicDevice<'a, BUS, CS, D> {
5052
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended
5153
/// to set the pin high the moment it's configured as an output, to avoid glitches.
5254
#[inline]
53-
pub fn new(bus: &'a AtomicCell<BUS>, mut cs: CS, delay: D) -> Result<Self, CS::Error>
55+
pub fn new(
56+
bus: &'a AtomicCell<BUS>,
57+
mut cs: CS,
58+
delay: D,
59+
cs_to_clock_delay_ns: u32,
60+
) -> Result<Self, CS::Error>
5461
where
5562
CS: OutputPin,
5663
{
5764
cs.set_high()?;
58-
Ok(Self { bus, cs, delay })
65+
Ok(Self {
66+
bus,
67+
cs,
68+
delay,
69+
cs_to_clock_delay_ns,
70+
})
5971
}
6072
}
6173

@@ -93,6 +105,7 @@ where
93105
bus,
94106
cs,
95107
delay: super::NoDelay,
108+
cs_to_clock_delay_ns: 0,
96109
})
97110
}
98111
}
@@ -134,7 +147,13 @@ where
134147

135148
let bus = unsafe { &mut *self.bus.bus.get() };
136149

137-
let result = transaction(operations, bus, &mut self.delay, &mut self.cs);
150+
let result = transaction(
151+
operations,
152+
bus,
153+
&mut self.delay,
154+
&mut self.cs,
155+
self.cs_to_clock_delay_ns,
156+
);
138157

139158
self.bus
140159
.busy

embedded-hal-bus/src/spi/critical_section.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct CriticalSectionDevice<'a, BUS, CS, D> {
2121
bus: &'a Mutex<RefCell<BUS>>,
2222
cs: CS,
2323
delay: D,
24+
/// Implementation of https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#cs-to-clock-delays
25+
cs_to_clock_delay_ns: u32,
2426
}
2527

2628
impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D> {
@@ -29,12 +31,22 @@ impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D> {
2931
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended
3032
/// to set the pin high the moment it's configured as an output, to avoid glitches.
3133
#[inline]
32-
pub fn new(bus: &'a Mutex<RefCell<BUS>>, mut cs: CS, delay: D) -> Result<Self, CS::Error>
34+
pub fn new(
35+
bus: &'a Mutex<RefCell<BUS>>,
36+
mut cs: CS,
37+
delay: D,
38+
cs_to_clock_delay_ns: u32,
39+
) -> Result<Self, CS::Error>
3340
where
3441
CS: OutputPin,
3542
{
3643
cs.set_high()?;
37-
Ok(Self { bus, cs, delay })
44+
Ok(Self {
45+
bus,
46+
cs,
47+
delay,
48+
cs_to_clock_delay_ns,
49+
})
3850
}
3951
}
4052

@@ -68,6 +80,7 @@ impl<'a, BUS, CS> CriticalSectionDevice<'a, BUS, CS, super::NoDelay> {
6880
bus,
6981
cs,
7082
delay: super::NoDelay,
83+
cs_to_clock_delay_ns: 0,
7184
})
7285
}
7386
}
@@ -91,7 +104,13 @@ where
91104
critical_section::with(|cs| {
92105
let bus = &mut *self.bus.borrow_ref_mut(cs);
93106

94-
transaction(operations, bus, &mut self.delay, &mut self.cs)
107+
transaction(
108+
operations,
109+
bus,
110+
&mut self.delay,
111+
&mut self.cs,
112+
self.cs_to_clock_delay_ns,
113+
)
95114
})
96115
}
97116
}

embedded-hal-bus/src/spi/exclusive.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct ExclusiveDevice<BUS, CS, D> {
2020
bus: BUS,
2121
cs: CS,
2222
delay: D,
23+
/// Implementation of https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#cs-to-clock-delays
24+
cs_to_clock_delay_ns: u32,
2325
}
2426

2527
impl<BUS, CS, D> ExclusiveDevice<BUS, CS, D> {
@@ -28,12 +30,17 @@ impl<BUS, CS, D> ExclusiveDevice<BUS, CS, D> {
2830
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended
2931
/// to set the pin high the moment it's configured as an output, to avoid glitches.
3032
#[inline]
31-
pub fn new(bus: BUS, mut cs: CS, delay: D) -> Result<Self, CS::Error>
33+
pub fn new(bus: BUS, mut cs: CS, delay: D, cs_to_clock_delay_ns: u32) -> Result<Self, CS::Error>
3234
where
3335
CS: OutputPin,
3436
{
3537
cs.set_high()?;
36-
Ok(Self { bus, cs, delay })
38+
Ok(Self {
39+
bus,
40+
cs,
41+
delay,
42+
cs_to_clock_delay_ns,
43+
})
3744
}
3845

3946
/// Returns a reference to the underlying bus object.
@@ -79,6 +86,7 @@ impl<BUS, CS> ExclusiveDevice<BUS, CS, super::NoDelay> {
7986
bus,
8087
cs,
8188
delay: super::NoDelay,
89+
cs_to_clock_delay_ns: 0,
8290
})
8391
}
8492
}
@@ -99,7 +107,13 @@ where
99107
{
100108
#[inline]
101109
fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> {
102-
transaction(operations, &mut self.bus, &mut self.delay, &mut self.cs)
110+
transaction(
111+
operations,
112+
&mut self.bus,
113+
&mut self.delay,
114+
&mut self.cs,
115+
self.cs_to_clock_delay_ns,
116+
)
103117
}
104118
}
105119

embedded-hal-bus/src/spi/mutex.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct MutexDevice<'a, BUS, CS, D> {
1919
bus: &'a Mutex<BUS>,
2020
cs: CS,
2121
delay: D,
22+
/// Implementation of https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#cs-to-clock-delays
23+
cs_to_clock_delay_ns: u32,
2224
}
2325

2426
impl<'a, BUS, CS, D> MutexDevice<'a, BUS, CS, D> {
@@ -27,12 +29,22 @@ impl<'a, BUS, CS, D> MutexDevice<'a, BUS, CS, D> {
2729
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended
2830
/// to set the pin high the moment it's configured as an output, to avoid glitches.
2931
#[inline]
30-
pub fn new(bus: &'a Mutex<BUS>, mut cs: CS, delay: D) -> Result<Self, CS::Error>
32+
pub fn new(
33+
bus: &'a Mutex<BUS>,
34+
mut cs: CS,
35+
delay: D,
36+
cs_to_clock_delay_ns: u32,
37+
) -> Result<Self, CS::Error>
3138
where
3239
CS: OutputPin,
3340
{
3441
cs.set_high()?;
35-
Ok(Self { bus, cs, delay })
42+
Ok(Self {
43+
bus,
44+
cs,
45+
delay,
46+
cs_to_clock_delay_ns,
47+
})
3648
}
3749
}
3850

@@ -66,6 +78,7 @@ impl<'a, BUS, CS> MutexDevice<'a, BUS, CS, super::NoDelay> {
6678
bus,
6779
cs,
6880
delay: super::NoDelay,
81+
cs_to_clock_delay_ns: 0,
6982
})
7083
}
7184
}
@@ -88,6 +101,12 @@ where
88101
fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> {
89102
let bus = &mut *self.bus.lock().unwrap();
90103

91-
transaction(operations, bus, &mut self.delay, &mut self.cs)
104+
transaction(
105+
operations,
106+
bus,
107+
&mut self.delay,
108+
&mut self.cs,
109+
self.cs_to_clock_delay_ns,
110+
)
92111
}
93112
}

embedded-hal-bus/src/spi/refcell.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub struct RefCellDevice<'a, BUS, CS, D> {
1818
bus: &'a RefCell<BUS>,
1919
cs: CS,
2020
delay: D,
21+
/// Implementation of https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#cs-to-clock-delays
22+
cs_to_clock_delay_ns: u32,
2123
}
2224

2325
impl<'a, BUS, CS, D> RefCellDevice<'a, BUS, CS, D> {
@@ -26,12 +28,22 @@ impl<'a, BUS, CS, D> RefCellDevice<'a, BUS, CS, D> {
2628
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended
2729
/// to set the pin high the moment it's configured as an output, to avoid glitches.
2830
#[inline]
29-
pub fn new(bus: &'a RefCell<BUS>, mut cs: CS, delay: D) -> Result<Self, CS::Error>
31+
pub fn new(
32+
bus: &'a RefCell<BUS>,
33+
mut cs: CS,
34+
delay: D,
35+
cs_to_clock_delay_ns: u32,
36+
) -> Result<Self, CS::Error>
3037
where
3138
CS: OutputPin,
3239
{
3340
cs.set_high()?;
34-
Ok(Self { bus, cs, delay })
41+
Ok(Self {
42+
bus,
43+
cs,
44+
delay,
45+
cs_to_clock_delay_ns,
46+
})
3547
}
3648
}
3749

@@ -65,6 +77,7 @@ impl<'a, BUS, CS> RefCellDevice<'a, BUS, CS, super::NoDelay> {
6577
bus,
6678
cs,
6779
delay: super::NoDelay,
80+
cs_to_clock_delay_ns: 0,
6881
})
6982
}
7083
}
@@ -87,6 +100,12 @@ where
87100
fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> {
88101
let bus = &mut *self.bus.borrow_mut();
89102

90-
transaction(operations, bus, &mut self.delay, &mut self.cs)
103+
transaction(
104+
operations,
105+
bus,
106+
&mut self.delay,
107+
&mut self.cs,
108+
self.cs_to_clock_delay_ns,
109+
)
91110
}
92111
}

embedded-hal-bus/src/spi/shared.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn transaction<Word, BUS, CS, D>(
1111
bus: &mut BUS,
1212
delay: &mut D,
1313
cs: &mut CS,
14+
cs_to_clock_delay_ns: u32,
1415
) -> Result<(), DeviceError<BUS::Error, CS::Error>>
1516
where
1617
BUS: SpiBus<Word> + ErrorType,
@@ -19,6 +20,9 @@ where
1920
Word: Copy,
2021
{
2122
cs.set_low().map_err(DeviceError::Cs)?;
23+
if cs_to_clock_delay_ns > 0 {
24+
delay.delay_ns(cs_to_clock_delay_ns);
25+
}
2226

2327
let op_res = operations.iter_mut().try_for_each(|op| match op {
2428
Operation::Read(buf) => bus.read(buf),

0 commit comments

Comments
 (0)