hal: add optional defmt support.

This commit is contained in:
Dario Nieuwenhuis 2023-08-06 18:56:21 +02:00
parent a94b507108
commit 9fd6ebcfb0
10 changed files with 47 additions and 1 deletions

View File

@ -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 }

View File

@ -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"]

View File

@ -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;

View File

@ -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),
@ -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]

View File

@ -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 }

View File

@ -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<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,

View File

@ -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]),

View File

@ -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;

View File

@ -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.

View File

@ -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<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.
///