From ea5e5ef995eef977562311ee46f06cb6f31c6877 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sun, 6 Aug 2023 18:56:21 +0200 Subject: [PATCH] hal: add optional defmt support. --- .github/workflows/test.yml | 5 +++++ embedded-hal-async/Cargo.toml | 4 ++++ embedded-hal-bus/Cargo.toml | 2 ++ embedded-hal-bus/src/lib.rs | 4 ++++ embedded-hal-bus/src/spi/mod.rs | 6 ++++++ embedded-hal/Cargo.toml | 3 +++ embedded-hal/src/digital.rs | 5 +++++ embedded-hal/src/i2c.rs | 6 ++++++ embedded-hal/src/lib.rs | 4 ++++ embedded-hal/src/pwm.rs | 4 ++++ embedded-hal/src/spi.rs | 10 +++++++++- 11 files changed, 52 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a2fa0d9d7..149bd1492 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,6 +24,8 @@ jobs: - thumbv6m-none-eabi - thumbv7m-none-eabi include: + - target: thumbv7m-none-eabi + features: defmt-03 - target: x86_64-unknown-linux-gnu features: std - target: x86_64-unknown-linux-gnu @@ -31,6 +33,9 @@ jobs: - target: x86_64-unknown-linux-gnu features: std,tokio-1,futures-03 rust: nightly + - target: x86_64-unknown-linux-gnu + features: std,tokio-1,futures-03,defmt-03 + rust: nightly steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@master diff --git a/embedded-hal-async/Cargo.toml b/embedded-hal-async/Cargo.toml index dbae01c7d..918514052 100644 --- a/embedded-hal-async/Cargo.toml +++ b/embedded-hal-async/Cargo.toml @@ -14,5 +14,9 @@ repository = "https://github.com/rust-embedded/embedded-hal" version = "0.2.0-alpha.2" rust-version = "1.65.0" +[features] +defmt-03 = ["dep:defmt-03", "embedded-hal/defmt-03"] + [dependencies] embedded-hal = { version = "=1.0.0-alpha.11", path = "../embedded-hal" } +defmt-03 = { package = "defmt", version = "0.3", optional = true } \ No newline at end of file diff --git a/embedded-hal-bus/Cargo.toml b/embedded-hal-bus/Cargo.toml index 66477fdfa..6edcdc75c 100644 --- a/embedded-hal-bus/Cargo.toml +++ b/embedded-hal-bus/Cargo.toml @@ -16,11 +16,13 @@ version = "0.1.0-alpha.3" [features] std = [] async = ["dep:embedded-hal-async"] +defmt-03 = ["dep:defmt-03", "embedded-hal/defmt-03", "embedded-hal-async?/defmt-03"] [dependencies] embedded-hal = { version = "=1.0.0-alpha.11", path = "../embedded-hal" } embedded-hal-async = { version = "=0.2.0-alpha.2", path = "../embedded-hal-async", optional = true } critical-section = { version = "1.0" } +defmt-03 = { package = "defmt", version = "0.3", optional = true } [package.metadata.docs.rs] features = ["std", "async"] diff --git a/embedded-hal-bus/src/lib.rs b/embedded-hal-bus/src/lib.rs index cdb668ae3..6feef548b 100644 --- a/embedded-hal-bus/src/lib.rs +++ b/embedded-hal-bus/src/lib.rs @@ -4,5 +4,9 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(feature = "async", feature(async_fn_in_trait, impl_trait_projections))] +// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`. +#[cfg(feature = "defmt-03")] +use defmt_03 as defmt; + pub mod i2c; pub mod spi; diff --git a/embedded-hal-bus/src/spi/mod.rs b/embedded-hal-bus/src/spi/mod.rs index 498dd1414..e3e888c8e 100644 --- a/embedded-hal-bus/src/spi/mod.rs +++ b/embedded-hal-bus/src/spi/mod.rs @@ -14,8 +14,12 @@ pub use mutex::*; mod critical_section; pub use self::critical_section::*; +#[cfg(feature = "defmt-03")] +use crate::defmt; + /// Error type for [`ExclusiveDevice`] operations. #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum DeviceError { /// An inner SPI bus operation failed Spi(BUS), @@ -37,6 +41,8 @@ where } /// Dummy `DelayUs` implementation that panics on use. +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub struct NoDelay; #[cold] diff --git a/embedded-hal/Cargo.toml b/embedded-hal/Cargo.toml index c70b3106a..ad9a0fdc3 100644 --- a/embedded-hal/Cargo.toml +++ b/embedded-hal/Cargo.toml @@ -14,3 +14,6 @@ name = "embedded-hal" readme = "README.md" repository = "https://github.com/rust-embedded/embedded-hal" version = "1.0.0-alpha.11" + +[dependencies] +defmt-03 = { package = "defmt", version = "0.3", optional = true } \ No newline at end of file diff --git a/embedded-hal/src/digital.rs b/embedded-hal/src/digital.rs index 677bb64d1..3f024ca2c 100644 --- a/embedded-hal/src/digital.rs +++ b/embedded-hal/src/digital.rs @@ -2,6 +2,9 @@ use core::{convert::From, ops::Not}; +#[cfg(feature = "defmt-03")] +use crate::defmt; + /// Error pub trait Error: core::fmt::Debug { /// Convert error to a generic error kind @@ -24,6 +27,7 @@ impl Error for core::convert::Infallible { /// free to define more specific or additional error types. However, by providing /// a mapping to these common errors, generic code can still react to them. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] #[non_exhaustive] pub enum ErrorKind { /// A different error occurred. The original error may contain more information. @@ -74,6 +78,7 @@ impl ErrorType for &mut T { /// assert_eq!(!state, PinState::High); /// ``` #[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum PinState { /// Low pin state Low, diff --git a/embedded-hal/src/i2c.rs b/embedded-hal/src/i2c.rs index 735408758..9195308b8 100644 --- a/embedded-hal/src/i2c.rs +++ b/embedded-hal/src/i2c.rs @@ -154,6 +154,9 @@ use crate::private; +#[cfg(feature = "defmt-03")] +use crate::defmt; + /// I2C error pub trait Error: core::fmt::Debug { /// Convert error to a generic I2C error kind @@ -176,6 +179,7 @@ impl Error for core::convert::Infallible { /// free to define more specific or additional error types. However, by providing /// a mapping to these common I2C errors, generic code can still react to them. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] #[non_exhaustive] pub enum ErrorKind { /// Bus error occurred. e.g. A START or a STOP condition is detected and is not @@ -199,6 +203,7 @@ pub enum ErrorKind { /// response was received to an address versus a no acknowledge to a data byte. /// Where it is not possible to differentiate, `Unknown` should be indicated. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum NoAcknowledgeSource { /// The device did not acknowledge its address. The device may be missing. Address, @@ -272,6 +277,7 @@ impl AddressMode for TenBitAddress {} /// /// Several operations can be combined as part of a transaction. #[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum Operation<'a> { /// Read data into the provided buffer Read(&'a mut [u8]), diff --git a/embedded-hal/src/lib.rs b/embedded-hal/src/lib.rs index c195bb2a1..f5eb76c32 100644 --- a/embedded-hal/src/lib.rs +++ b/embedded-hal/src/lib.rs @@ -15,3 +15,7 @@ mod private { impl Sealed for SevenBitAddress {} impl Sealed for TenBitAddress {} } + +// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`. +#[cfg(feature = "defmt-03")] +use defmt_03 as defmt; diff --git a/embedded-hal/src/pwm.rs b/embedded-hal/src/pwm.rs index 4c0f6ac60..3893eaade 100644 --- a/embedded-hal/src/pwm.rs +++ b/embedded-hal/src/pwm.rs @@ -1,5 +1,8 @@ //! Pulse Width Modulation (PWM) traits +#[cfg(feature = "defmt-03")] +use crate::defmt; + /// Error pub trait Error: core::fmt::Debug { /// Convert error to a generic error kind @@ -22,6 +25,7 @@ impl Error for core::convert::Infallible { /// free to define more specific or additional error types. However, by providing /// a mapping to these common errors, generic code can still react to them. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] #[non_exhaustive] pub enum ErrorKind { /// A different error occurred. The original error may contain more information. diff --git a/embedded-hal/src/spi.rs b/embedded-hal/src/spi.rs index 54bf8bcea..3813b9a6c 100644 --- a/embedded-hal/src/spi.rs +++ b/embedded-hal/src/spi.rs @@ -163,8 +163,12 @@ use core::fmt::Debug; +#[cfg(feature = "defmt-03")] +use crate::defmt; + /// Clock polarity #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum Polarity { /// Clock signal low when idle IdleLow, @@ -174,6 +178,7 @@ pub enum Polarity { /// Clock phase #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum Phase { /// Data in "captured" on the first clock transition CaptureOnFirstTransition, @@ -183,6 +188,7 @@ pub enum Phase { /// SPI mode #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub struct Mode { /// Clock polarity pub polarity: Polarity, @@ -236,6 +242,7 @@ impl Error for core::convert::Infallible { /// free to define more specific or additional error types. However, by providing /// a mapping to these common SPI errors, generic code can still react to them. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] #[non_exhaustive] pub enum ErrorKind { /// The peripheral receive buffer was overrun @@ -295,7 +302,8 @@ impl ErrorType for &mut T { /// SPI transaction operation. /// /// This allows composition of SPI operations into a single bus transaction -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum Operation<'a, Word: 'static> { /// Read data into the provided buffer. ///