Skip to content

Commit 3450491

Browse files
committed
added 2 new constructors for ExclusiveDevice in embedded-hal-bus allowing to have devices without chip select namely new_no_cs and new_no_cs_no_delay
1 parent 055a619 commit 3450491

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

embedded-hal-bus/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ embedded-hal-async = { version = "1.0.0", path = "../embedded-hal-async", option
3737
critical-section = { version = "1.0" }
3838
defmt-03 = { package = "defmt", version = "0.3", optional = true }
3939
portable-atomic = {version = "1.3", default-features = false, optional = true, features = ["require-cas"]}
40+
unwrap-infallible = "0.1.5"
4041

4142
[package.metadata.docs.rs]
4243
features = ["std", "async"]

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! SPI bus sharing mechanisms.
22
33
use embedded_hal::delay::DelayNs;
4-
use embedded_hal::digital::OutputPin;
4+
use embedded_hal::digital::{NoPin, OutputPin};
55
use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice};
66
#[cfg(feature = "async")]
77
use embedded_hal_async::{
@@ -49,6 +49,15 @@ impl<BUS, CS, D> ExclusiveDevice<BUS, CS, D> {
4949
}
5050
}
5151

52+
use unwrap_infallible::UnwrapInfallible;
53+
54+
impl<BUS, D> ExclusiveDevice<BUS, NoPin, D> {
55+
/// Create a new [`ExclusiveDevice`] without a Chip Select (CS) pin.
56+
pub fn new_no_cs(bus: BUS, delay: D) -> Self {
57+
ExclusiveDevice::new(bus, NoPin, delay).unwrap_infallible()
58+
}
59+
}
60+
5261
impl<BUS, CS> ExclusiveDevice<BUS, CS, super::NoDelay> {
5362
/// Create a new [`ExclusiveDevice`] without support for in-transaction delays.
5463
///
@@ -83,6 +92,13 @@ impl<BUS, CS> ExclusiveDevice<BUS, CS, super::NoDelay> {
8392
}
8493
}
8594

95+
impl<BUS> ExclusiveDevice<BUS, NoPin, super::NoDelay> {
96+
/// Create a new [`ExclusiveDevice`] without a Chip Select (CS) pin.
97+
pub fn new_no_cs_no_delay(bus: BUS) -> Self {
98+
ExclusiveDevice::new_no_delay(bus, NoPin).unwrap_infallible()
99+
}
100+
}
101+
86102
impl<BUS, CS, D> ErrorType for ExclusiveDevice<BUS, CS, D>
87103
where
88104
BUS: ErrorType,

embedded-hal/src/digital.rs

+24
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,27 @@ impl<T: InputPin + ?Sized> InputPin for &mut T {
222222
T::is_low(self)
223223
}
224224
}
225+
226+
use core::convert::Infallible;
227+
use ErrorType as NoPinErrorType;
228+
229+
/// A dummy pin that does nothing.
230+
/// This can be used for devices that do not require a Chip Select (CS) pin.
231+
#[derive(Debug, Default, Copy, Clone)]
232+
pub struct NoPin;
233+
234+
// Implement `ErrorType` for NoPin (Explicitly Using `digital::ErrorType`)
235+
impl NoPinErrorType for NoPin {
236+
type Error = Infallible;
237+
}
238+
239+
// Implement `OutputPin`
240+
impl OutputPin for NoPin {
241+
fn set_low(&mut self) -> Result<(), Infallible> {
242+
Ok(())
243+
}
244+
245+
fn set_high(&mut self) -> Result<(), Infallible> {
246+
Ok(())
247+
}
248+
}

0 commit comments

Comments
 (0)