Skip to content

Commit

Permalink
Merge #304
Browse files Browse the repository at this point in the history
304: Add optional support for printing some types with defmt r=mattico a=mattico

> `defmt` ("de format", short for "deferred formatting") is a highly efficient logging framework that targets resource-constrained devices, like microcontrollers.

https://defmt.ferrous-systems.com/

I've been maintaining this support in my fork for some time, it hasn't been burdensome. defmt has become popular enough that I think it is worthwhile to upstream this. Supporting defmt formatting in this library makes it easier to print error messages (don't need to use `defmt::Debug2Format`) and eliminates the code bloat of the `core::fmt` machinery. We could similarly move `Debug` derives behind a feature flag to help people who are trying to eliminate `core::fmt`, but I don't particularly care.

Co-authored-by: Matt Ickstadt <mattico8@gmail.com>
  • Loading branch information
bors[bot] and mattico authored Jan 4, 2022
2 parents 64fd2d9 + dc36288 commit 4ebb5c1
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- stm32h7b0
- stm32h735
env: # Peripheral Feature flags
FLAGS: rt,xspi,sdmmc,sdmmc-fatfs,fmc,usb_hs,rtc,ethernet,ltdc,crc,rand
FLAGS: rt,xspi,sdmmc,sdmmc-fatfs,fmc,usb_hs,rtc,ethernet,ltdc,crc,rand,defmt

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ readme = "README.md"
exclude = [".gitignore"]

[package.metadata.docs.rs]
features = ["stm32h743v", "rt", "xspi", "sdmmc", "fmc", "usb_hs", "rtc", "ethernet", "ltdc", "crc", "rand"]
features = ["stm32h743v", "rt", "xspi", "sdmmc", "fmc", "usb_hs", "rtc", "ethernet", "ltdc", "crc", "rand", "defmt"]
targets = ["thumbv7em-none-eabihf"]

[dependencies]
embedded-hal = "0.2.6"
embedded-dma = "0.1.2"
cortex-m = "^0.7.1"
defmt = { version = ">=0.2.0,<0.4", optional = true }
stm32h7 = { version = "^0.14.0", default-features = false }
void = { version = "1.0.2", default-features = false }
cast = { version = "0.3.0", default-features = false }
Expand Down
17 changes: 17 additions & 0 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct Adc<ADC, ED> {
//
// Refer to RM0433 Rev 6 - Chapter 24.4.13
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[allow(non_camel_case_types)]
pub enum AdcSampleTime {
/// 1.5 cycles sampling time
Expand Down Expand Up @@ -134,6 +135,7 @@ impl From<AdcSampleTime> for u8 {
///
/// Only values in range of 0..=15 are allowed.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcLshift(u8);

impl AdcLshift {
Expand All @@ -155,6 +157,7 @@ impl AdcLshift {
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcCalOffset(u16);

impl AdcCalOffset {
Expand All @@ -164,6 +167,7 @@ impl AdcCalOffset {
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcCalLinear([u32; 6]);

impl AdcCalLinear {
Expand Down Expand Up @@ -326,6 +330,19 @@ pub trait AdcExt<ADC>: Sized {
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct StoredConfig(AdcSampleTime, Resolution, AdcLshift);

#[cfg(feature = "defmt")]
impl defmt::Format for StoredConfig {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(
fmt,
"StoredConfig({:?}, {:?}, {:?})",
self.0,
defmt::Debug2Format(&self.1),
self.2
)
}
}

/// Get and check the adc_ker_ck_input
fn check_clock(prec: &impl AdcClkSelGetter, clocks: &CoreClocks) -> Hertz {
// Select Kernel Clock
Expand Down
5 changes: 5 additions & 0 deletions src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ mod macros {
/// A polynomial being even means that the least significant bit is `0`
/// in the polynomial's normal representation.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Polynomial(Poly);

impl Polynomial {
Expand Down Expand Up @@ -252,6 +253,7 @@ impl Default for Polynomial {

/// Errors generated when trying to create invalid polynomials.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PolynomialError {
/// Tried to create an even polynomial.
/// The hardware CRC unit only supports odd polynomials.
Expand All @@ -274,6 +276,7 @@ impl fmt::Display for PolynomialError {

/// Internal representation of a polynomial.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
enum Poly {
/// 7-bit polynomial
B7(u8),
Expand All @@ -291,6 +294,7 @@ enum Poly {
/// 'reflection' it most likely wants [`BitReversal::Byte`] and output reversal
/// enabled.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum BitReversal {
/// Each input byte has its bits reversed. `0x1A2B3C4D` becomes `0x58D43CB2`.
Expand All @@ -303,6 +307,7 @@ pub enum BitReversal {

/// CRC unit configuration.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config {
poly: Polynomial,
initial: u32,
Expand Down
2 changes: 2 additions & 0 deletions src/dma/bdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Instance for BDMA2 {

/// BDMA interrupts
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BdmaInterrupts {
transfer_complete: bool,
transfer_error: bool,
Expand All @@ -95,6 +96,7 @@ pub struct BdmaInterrupts {

/// Contains the complete set of configuration for a DMA stream.
#[derive(Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BdmaConfig {
pub(crate) priority: config::Priority,
pub(crate) memory_increment: bool,
Expand Down
2 changes: 2 additions & 0 deletions src/dma/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Instance for DMA2 {

/// DMA interrupts
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DmaInterrupts {
transfer_complete: bool,
transfer_error: bool,
Expand All @@ -83,6 +84,7 @@ pub struct DmaInterrupts {

/// Contains configuration for a DMA stream
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DmaConfig {
pub(crate) priority: config::Priority,
pub(crate) memory_increment: bool,
Expand Down
17 changes: 17 additions & 0 deletions src/dma/mdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Instance for MDMA {

/// MDMA Stream Transfer Requests
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MdmaTransferRequest {
Dma1Tcif0 = 0,
Dma1Tcif1,
Expand Down Expand Up @@ -161,6 +162,7 @@ pub enum MdmaTransferRequest {

/// MDMA Source/Destination sizes
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MdmaSize {
/// Byte (8-bit)
Byte = 0,
Expand Down Expand Up @@ -206,6 +208,7 @@ impl MdmaSize {

/// MDMA increment mode
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MdmaIncrement {
Fixed,
/// Increment by one source/destination element each element
Expand Down Expand Up @@ -262,9 +265,20 @@ impl fmt::Debug for MdmaBurstSize {
}
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for MdmaBurstSize {
fn format(&self, fmt: defmt::Formatter) {
if self.0 > 0 {
defmt::write!(fmt, "{=u32}", 1 << self.0);
} else {
defmt::intern!("single").format(fmt);
}
}
}

/// MDMA Packing/Alignment mode
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MdmaPackingAlignment {
/// Source data is packed/unpacked into the destination data size
Packed,
Expand Down Expand Up @@ -292,6 +306,7 @@ impl Default for MdmaPackingAlignment {

/// MDMA trigger mode
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MdmaTrigger {
/// Each MDMA request triggers a buffer transfer
Buffer = 0b00,
Expand All @@ -310,6 +325,7 @@ impl Default for MdmaTrigger {

/// MDMA interrupts
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MdmaInterrupts {
transfer_complete: bool,
transfer_error: bool,
Expand All @@ -320,6 +336,7 @@ pub struct MdmaInterrupts {

/// Contains the complete set of configuration for a MDMA stream.
#[derive(Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MdmaConfig {
pub(crate) priority: config::Priority,
pub(crate) destination_increment: MdmaIncrement,
Expand Down
10 changes: 10 additions & 0 deletions src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ use traits::{

/// Errors.
#[derive(PartialEq, Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DMAError {
/// DMA not ready to change buffers.
NotReady,
Expand All @@ -128,6 +129,7 @@ pub enum DMAError {

/// Possible DMA's directions.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DmaDirection {
/// Memory to Memory transfer.
MemoryToMemory,
Expand All @@ -139,6 +141,7 @@ pub enum DmaDirection {

/// DMA from a peripheral to a memory location.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PeripheralToMemory;

impl Direction for PeripheralToMemory {
Expand All @@ -153,6 +156,7 @@ impl Direction for PeripheralToMemory {

/// DMA from one memory location to another memory location.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MemoryToMemory<T> {
_data: PhantomData<T>,
}
Expand All @@ -169,6 +173,7 @@ impl<T> Direction for MemoryToMemory<T> {

/// DMA from a memory location to a peripheral.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MemoryToPeripheral;

impl Direction for MemoryToPeripheral {
Expand Down Expand Up @@ -203,6 +208,7 @@ unsafe impl TargetAddress<Self> for MemoryToMemory<u32> {

/// How full the DMA stream's fifo is.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FifoLevel {
/// 0 < fifo_level < 1/4.
GtZeroLtQuarter,
Expand Down Expand Up @@ -236,6 +242,7 @@ impl From<u8> for FifoLevel {

/// Which DMA buffer is in use.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CurrentBuffer {
/// The first buffer (m0ar).
Buffer0 = 0,
Expand Down Expand Up @@ -264,6 +271,7 @@ pub mod config {
/// priority over the stream with the higher number. For example, Stream 2
/// takes priority over Stream 4.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Priority {
/// Low priority.
Low,
Expand Down Expand Up @@ -293,6 +301,7 @@ pub mod config {

/// The level to fill the fifo to before performing the transaction.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FifoThreshold {
/// 1/4 full.
QuarterFull,
Expand All @@ -318,6 +327,7 @@ pub mod config {
/// How burst transfers are done, requires fifo enabled. Check datasheet for
/// valid combinations.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BurstMode {
/// Single transfer, no burst.
NoBurst,
Expand Down
2 changes: 2 additions & 0 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum Stop {

/// I2C error
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Error {
/// Bus error
Expand Down Expand Up @@ -96,6 +97,7 @@ where
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct I2c<I2C> {
i2c: I2C,
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@
//! * [Cyclic Redundancy Check (CRC)](crate::crc) Feature gate `crc`
//! * [Random Number Generator](crate::rng) ([rand_core::RngCore](rand_core::RngCore) is implemented under the `rand` feature gate)
//! * [System Window Watchdog](crate::watchdog)
//!
//! Cargo Features
//!
//! * [`defmt`](https://defmt.ferrous-systems.com/) formatting for some types can be enabled with the feature `defmt`.
#![cfg_attr(not(test), no_std)]
#![allow(non_camel_case_types)]

extern crate paste;

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Never {}

#[cfg(not(feature = "device-selected"))]
Expand Down
1 change: 1 addition & 0 deletions src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub struct ActiveLow;

/// Whether a PWM signal is left-aligned, right-aligned, or center-aligned
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Alignment {
Left,
Right,
Expand Down
1 change: 1 addition & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::stm32::RNG;
use crate::time::Hertz;

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ErrorKind {
ClockError = 0,
SeedError = 1,
Expand Down
2 changes: 2 additions & 0 deletions src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum RtcClock {
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
/// An error preventing the RTC from initializing
pub enum InitError {
RtcNotRunning,
Expand All @@ -70,6 +71,7 @@ pub enum InitError {
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DstError {
ClockNotInitialized,
AlreadyDst,
Expand Down
1 change: 1 addition & 0 deletions src/sai/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub enum I2SSync {
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum I2SError {
NoChannelAvailable,
}
Expand Down
4 changes: 3 additions & 1 deletion src/sdmmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ pins! {

/// The signalling scheme used on the SDMMC bus
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Signalling {
SDR12,
SDR25,
Expand All @@ -232,6 +233,7 @@ impl Default for Signalling {
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
Timeout,
SoftwareTimeout,
Expand Down
Loading

0 comments on commit 4ebb5c1

Please sign in to comment.