Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hal: add optional defmt support. #450

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ jobs:
- nightly
target:
- x86_64-unknown-linux-gnu
- thumbv6m-none-eabi
- thumbv7m-none-eabi
features:
- ''
include:
- target: x86_64-unknown-linux-gnu
- rust: stable
target: x86_64-unknown-linux-gnu
features: std
- target: x86_64-unknown-linux-gnu
- rust: stable
target: x86_64-unknown-linux-gnu
features: alloc
- target: x86_64-unknown-linux-gnu
features: std,tokio-1,futures-03
rust: nightly
- rust: nightly
target: x86_64-unknown-linux-gnu
features: std,tokio-1,futures-03,defmt-03
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
Expand Down
4 changes: 4 additions & 0 deletions embedded-hal-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 2 additions & 0 deletions embedded-hal-bus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions embedded-hal-bus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions embedded-hal-bus/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BUS, CS> {
/// An inner SPI bus operation failed
Spi(BUS),
Expand All @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions embedded-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 5 additions & 0 deletions embedded-hal/src/digital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -74,6 +78,7 @@ impl<T: ErrorType + ?Sized> 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,
Expand Down
6 changes: 6 additions & 0 deletions embedded-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]),
Expand Down
4 changes: 4 additions & 0 deletions embedded-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions embedded-hal/src/pwm.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion embedded-hal/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -295,7 +302,8 @@ impl<T: ErrorType + ?Sized> 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.
///
Expand Down