Skip to content

Commit bf2b8a1

Browse files
authored
Merge pull request #450 from rust-embedded/bus-defmt
hal: add optional defmt support.
2 parents 6d83939 + 3505e76 commit bf2b8a1

File tree

11 files changed

+56
-7
lines changed

11 files changed

+56
-7
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ jobs:
2121
- nightly
2222
target:
2323
- x86_64-unknown-linux-gnu
24-
- thumbv6m-none-eabi
2524
- thumbv7m-none-eabi
25+
features:
26+
- ''
2627
include:
27-
- target: x86_64-unknown-linux-gnu
28+
- rust: stable
29+
target: x86_64-unknown-linux-gnu
2830
features: std
29-
- target: x86_64-unknown-linux-gnu
31+
- rust: stable
32+
target: x86_64-unknown-linux-gnu
3033
features: alloc
31-
- target: x86_64-unknown-linux-gnu
32-
features: std,tokio-1,futures-03
33-
rust: nightly
34+
- rust: nightly
35+
target: x86_64-unknown-linux-gnu
36+
features: std,tokio-1,futures-03,defmt-03
3437
steps:
3538
- uses: actions/checkout@v3
3639
- uses: dtolnay/rust-toolchain@master

embedded-hal-async/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ repository = "https://github.com/rust-embedded/embedded-hal"
1414
version = "0.2.0-alpha.2"
1515
rust-version = "1.65.0"
1616

17+
[features]
18+
defmt-03 = ["dep:defmt-03", "embedded-hal/defmt-03"]
19+
1720
[dependencies]
1821
embedded-hal = { version = "=1.0.0-alpha.11", path = "../embedded-hal" }
22+
defmt-03 = { package = "defmt", version = "0.3", optional = true }

embedded-hal-bus/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ version = "0.1.0-alpha.3"
1616
[features]
1717
std = []
1818
async = ["dep:embedded-hal-async"]
19+
defmt-03 = ["dep:defmt-03", "embedded-hal/defmt-03", "embedded-hal-async?/defmt-03"]
1920

2021
[dependencies]
2122
embedded-hal = { version = "=1.0.0-alpha.11", path = "../embedded-hal" }
2223
embedded-hal-async = { version = "=0.2.0-alpha.2", path = "../embedded-hal-async", optional = true }
2324
critical-section = { version = "1.0" }
25+
defmt-03 = { package = "defmt", version = "0.3", optional = true }
2426

2527
[package.metadata.docs.rs]
2628
features = ["std", "async"]

embedded-hal-bus/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
#![cfg_attr(docsrs, feature(doc_cfg))]
55
#![cfg_attr(feature = "async", feature(async_fn_in_trait, impl_trait_projections))]
66

7+
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
8+
#[cfg(feature = "defmt-03")]
9+
use defmt_03 as defmt;
10+
711
pub mod i2c;
812
pub mod spi;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ pub use mutex::*;
1414
mod critical_section;
1515
pub use self::critical_section::*;
1616

17+
#[cfg(feature = "defmt-03")]
18+
use crate::defmt;
19+
1720
/// Error type for [`ExclusiveDevice`] operations.
1821
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
22+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
1923
pub enum DeviceError<BUS, CS> {
2024
/// An inner SPI bus operation failed
2125
Spi(BUS),
@@ -37,6 +41,8 @@ where
3741
}
3842

3943
/// Dummy `DelayUs` implementation that panics on use.
44+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
45+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
4046
pub struct NoDelay;
4147

4248
#[cold]

embedded-hal/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ name = "embedded-hal"
1414
readme = "README.md"
1515
repository = "https://github.com/rust-embedded/embedded-hal"
1616
version = "1.0.0-alpha.11"
17+
18+
[dependencies]
19+
defmt-03 = { package = "defmt", version = "0.3", optional = true }

embedded-hal/src/digital.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
use core::{convert::From, ops::Not};
44

5+
#[cfg(feature = "defmt-03")]
6+
use crate::defmt;
7+
58
/// Error
69
pub trait Error: core::fmt::Debug {
710
/// Convert error to a generic error kind
@@ -24,6 +27,7 @@ impl Error for core::convert::Infallible {
2427
/// free to define more specific or additional error types. However, by providing
2528
/// a mapping to these common errors, generic code can still react to them.
2629
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
30+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
2731
#[non_exhaustive]
2832
pub enum ErrorKind {
2933
/// A different error occurred. The original error may contain more information.
@@ -74,6 +78,7 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
7478
/// assert_eq!(!state, PinState::High);
7579
/// ```
7680
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
81+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
7782
pub enum PinState {
7883
/// Low pin state
7984
Low,

embedded-hal/src/i2c.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@
154154
155155
use crate::private;
156156

157+
#[cfg(feature = "defmt-03")]
158+
use crate::defmt;
159+
157160
/// I2C error
158161
pub trait Error: core::fmt::Debug {
159162
/// Convert error to a generic I2C error kind
@@ -176,6 +179,7 @@ impl Error for core::convert::Infallible {
176179
/// free to define more specific or additional error types. However, by providing
177180
/// a mapping to these common I2C errors, generic code can still react to them.
178181
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
182+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
179183
#[non_exhaustive]
180184
pub enum ErrorKind {
181185
/// Bus error occurred. e.g. A START or a STOP condition is detected and is not
@@ -199,6 +203,7 @@ pub enum ErrorKind {
199203
/// response was received to an address versus a no acknowledge to a data byte.
200204
/// Where it is not possible to differentiate, `Unknown` should be indicated.
201205
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
206+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
202207
pub enum NoAcknowledgeSource {
203208
/// The device did not acknowledge its address. The device may be missing.
204209
Address,
@@ -272,6 +277,7 @@ impl AddressMode for TenBitAddress {}
272277
///
273278
/// Several operations can be combined as part of a transaction.
274279
#[derive(Debug, PartialEq, Eq)]
280+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
275281
pub enum Operation<'a> {
276282
/// Read data into the provided buffer
277283
Read(&'a mut [u8]),

embedded-hal/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ mod private {
1515
impl Sealed for SevenBitAddress {}
1616
impl Sealed for TenBitAddress {}
1717
}
18+
19+
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
20+
#[cfg(feature = "defmt-03")]
21+
use defmt_03 as defmt;

embedded-hal/src/pwm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Pulse Width Modulation (PWM) traits
22
3+
#[cfg(feature = "defmt-03")]
4+
use crate::defmt;
5+
36
/// Error
47
pub trait Error: core::fmt::Debug {
58
/// Convert error to a generic error kind
@@ -22,6 +25,7 @@ impl Error for core::convert::Infallible {
2225
/// free to define more specific or additional error types. However, by providing
2326
/// a mapping to these common errors, generic code can still react to them.
2427
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
28+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
2529
#[non_exhaustive]
2630
pub enum ErrorKind {
2731
/// A different error occurred. The original error may contain more information.

0 commit comments

Comments
 (0)