Skip to content

Commit 9fd6ebc

Browse files
committed
hal: add optional defmt support.
1 parent a94b507 commit 9fd6ebc

File tree

10 files changed

+47
-1
lines changed

10 files changed

+47
-1
lines changed

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.

embedded-hal/src/spi.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,12 @@
163163
164164
use core::fmt::Debug;
165165

166+
#[cfg(feature = "defmt-03")]
167+
use crate::defmt;
168+
166169
/// Clock polarity
167170
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
171+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
168172
pub enum Polarity {
169173
/// Clock signal low when idle
170174
IdleLow,
@@ -174,6 +178,7 @@ pub enum Polarity {
174178

175179
/// Clock phase
176180
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
181+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
177182
pub enum Phase {
178183
/// Data in "captured" on the first clock transition
179184
CaptureOnFirstTransition,
@@ -183,6 +188,7 @@ pub enum Phase {
183188

184189
/// SPI mode
185190
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
191+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
186192
pub struct Mode {
187193
/// Clock polarity
188194
pub polarity: Polarity,
@@ -236,6 +242,7 @@ impl Error for core::convert::Infallible {
236242
/// free to define more specific or additional error types. However, by providing
237243
/// a mapping to these common SPI errors, generic code can still react to them.
238244
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
245+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
239246
#[non_exhaustive]
240247
pub enum ErrorKind {
241248
/// The peripheral receive buffer was overrun
@@ -295,7 +302,8 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
295302
/// SPI transaction operation.
296303
///
297304
/// This allows composition of SPI operations into a single bus transaction
298-
#[derive(Debug, PartialEq)]
305+
#[derive(Debug, PartialEq, Eq)]
306+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
299307
pub enum Operation<'a, Word: 'static> {
300308
/// Read data into the provided buffer.
301309
///

0 commit comments

Comments
 (0)