Skip to content

Commit 987dc68

Browse files
authored
Merge pull request #607 from 9names/embedded-hal-bus-atomic-device
Add embedded-hal-bus atomic-device features
2 parents f91fcbc + 2acbd77 commit 987dc68

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

embedded-hal-bus/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ repository = "https://github.com/rust-embedded/embedded-hal"
1515
version = "0.2.0"
1616

1717
[features]
18+
# Enable shared bus implementations using `std::sync::Mutex`, and implement `std::error::Error` for `DeviceError`
1819
std = ["alloc"]
20+
# Use `portable-atomic` to enable `atomic-device` on devices without native atomic CAS
21+
#
22+
# `portable-atomic` emulates atomic CAS functionality, allowing `embedded-hal-bus` to use `atomic-device` on hardware
23+
# that does not natively support atomic CAS. If you enable this, you must also add `portable-atomic` to your crate with
24+
# a feature flag such as `unsafe-assume-single-core` or `critical-section` to choose how atomic CAS is implemented.
25+
# See https://docs.rs/portable-atomic/1.7.0/portable_atomic/#optional-features for more info.
26+
portable-atomic = ["dep:portable-atomic"]
27+
# Enable `embedded-hal-async` support.
1928
async = ["dep:embedded-hal-async"]
29+
# Derive `defmt::Format` from `defmt` 0.3 for enums and structs. See https://github.com/knurling-rs/defmt for more info
2030
defmt-03 = ["dep:defmt-03", "embedded-hal/defmt-03", "embedded-hal-async?/defmt-03"]
2131
# Enables additional utilities requiring a global allocator.
2232
alloc = []
@@ -26,7 +36,7 @@ embedded-hal = { version = "1.0.0", path = "../embedded-hal" }
2636
embedded-hal-async = { version = "1.0.0", path = "../embedded-hal-async", optional = true }
2737
critical-section = { version = "1.0" }
2838
defmt-03 = { package = "defmt", version = "0.3", optional = true }
29-
portable-atomic = {version = "1", default-features = false}
39+
portable-atomic = {version = "1.3", default-features = false, optional = true, features = ["require-cas"]}
3040

3141
[package.metadata.docs.rs]
3242
features = ["std", "async"]

embedded-hal-bus/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ provides mechanisms to obtain multiple `I2c` instances out of a single `I2c` ins
3030

3131
## Optional Cargo features
3232

33-
- **`std`**: enable shared bus implementations using `std::sync::Mutex`, and implement
34-
`std::error::Error` for `DeviceError`.
3533
- **`async`**: enable `embedded-hal-async` support.
3634
- **`defmt-03`**: Derive `defmt::Format` from `defmt` 0.3 for enums and structs.
3735
- **`alloc`**: enable implementations using `alloc` (for instance, `spi::RcDevice`, which makes use of `alloc::rc::Rc`)
36+
- **`portable-atomic`**: Use `portable-atomic` to enable `atomic-device` on devices without native atomic CAS
37+
38+
`portable-atomic` emulates atomic CAS functionality, allowing `embedded-hal-bus` to use `atomic-device` on hardware
39+
that does not natively support atomic CAS. If you enable this, you must also add `portable-atomic` to your crate with
40+
a feature flag such as `unsafe-assume-single-core` or `critical-section` to choose how atomic CAS is implemented.
41+
See <https://docs.rs/portable-atomic/1.7.0/portable_atomic/#optional-features> for more info.
42+
- **`std`**: enable shared bus implementations using `std::sync::Mutex`, and implement
43+
`std::error::Error` for `DeviceError`.
3844

3945
## Minimum Supported Rust Version (MSRV)
4046

embedded-hal-bus/src/i2c/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ mod mutex;
88
pub use mutex::*;
99
mod critical_section;
1010
pub use self::critical_section::*;
11+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1112
mod atomic;
13+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1214
pub use atomic::*;
1315

1416
#[cfg(feature = "alloc")]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ pub use refcell::*;
1111
mod mutex;
1212
#[cfg(feature = "std")]
1313
pub use mutex::*;
14+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1415
mod atomic;
1516
mod critical_section;
1617
mod shared;
18+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1719
pub use atomic::*;
1820

1921
#[cfg(feature = "alloc")]

embedded-hal-bus/src/util.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
//! Utilities shared by all bus types.
22
3+
#[allow(unused_imports)]
34
use core::cell::UnsafeCell;
45

6+
#[cfg(not(feature = "portable-atomic"))]
7+
use core::sync::atomic::AtomicBool;
8+
#[cfg(feature = "portable-atomic")]
9+
use portable_atomic::AtomicBool;
10+
11+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
512
/// Cell type used by [`spi::AtomicDevice`](crate::spi::AtomicDevice) and [`i2c::AtomicDevice`](crate::i2c::AtomicDevice).
613
///
714
/// To use `AtomicDevice`, you must wrap the bus with this struct, and then
815
/// construct multiple `AtomicDevice` instances with references to it.
916
pub struct AtomicCell<BUS> {
1017
pub(crate) bus: UnsafeCell<BUS>,
11-
pub(crate) busy: portable_atomic::AtomicBool,
18+
pub(crate) busy: AtomicBool,
1219
}
13-
20+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1421
unsafe impl<BUS: Send> Send for AtomicCell<BUS> {}
22+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1523
unsafe impl<BUS: Send> Sync for AtomicCell<BUS> {}
1624

25+
#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
1726
impl<BUS> AtomicCell<BUS> {
1827
/// Create a new `AtomicCell`
1928
pub fn new(bus: BUS) -> Self {
2029
Self {
2130
bus: UnsafeCell::new(bus),
22-
busy: portable_atomic::AtomicBool::from(false),
31+
busy: AtomicBool::from(false),
2332
}
2433
}
2534
}

0 commit comments

Comments
 (0)