Skip to content

Commit 4ebb5c1

Browse files
bors[bot]mattico
andauthored
Merge #304
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>
2 parents 64fd2d9 + dc36288 commit 4ebb5c1

File tree

20 files changed

+91
-4
lines changed

20 files changed

+91
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- stm32h7b0
2424
- stm32h735
2525
env: # Peripheral Feature flags
26-
FLAGS: rt,xspi,sdmmc,sdmmc-fatfs,fmc,usb_hs,rtc,ethernet,ltdc,crc,rand
26+
FLAGS: rt,xspi,sdmmc,sdmmc-fatfs,fmc,usb_hs,rtc,ethernet,ltdc,crc,rand,defmt
2727

2828
steps:
2929
- uses: actions/checkout@v2

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ readme = "README.md"
2121
exclude = [".gitignore"]
2222

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

2727
[dependencies]
2828
embedded-hal = "0.2.6"
2929
embedded-dma = "0.1.2"
3030
cortex-m = "^0.7.1"
31+
defmt = { version = ">=0.2.0,<0.4", optional = true }
3132
stm32h7 = { version = "^0.14.0", default-features = false }
3233
void = { version = "1.0.2", default-features = false }
3334
cast = { version = "0.3.0", default-features = false }

src/adc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct Adc<ADC, ED> {
8888
//
8989
// Refer to RM0433 Rev 6 - Chapter 24.4.13
9090
#[derive(Clone, Copy, Debug, PartialEq)]
91+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9192
#[allow(non_camel_case_types)]
9293
pub enum AdcSampleTime {
9394
/// 1.5 cycles sampling time
@@ -134,6 +135,7 @@ impl From<AdcSampleTime> for u8 {
134135
///
135136
/// Only values in range of 0..=15 are allowed.
136137
#[derive(Clone, Copy, Debug, PartialEq)]
138+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
137139
pub struct AdcLshift(u8);
138140

139141
impl AdcLshift {
@@ -155,6 +157,7 @@ impl AdcLshift {
155157
}
156158

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

160163
impl AdcCalOffset {
@@ -164,6 +167,7 @@ impl AdcCalOffset {
164167
}
165168

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

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

333+
#[cfg(feature = "defmt")]
334+
impl defmt::Format for StoredConfig {
335+
fn format(&self, fmt: defmt::Formatter) {
336+
defmt::write!(
337+
fmt,
338+
"StoredConfig({:?}, {:?}, {:?})",
339+
self.0,
340+
defmt::Debug2Format(&self.1),
341+
self.2
342+
)
343+
}
344+
}
345+
329346
/// Get and check the adc_ker_ck_input
330347
fn check_clock(prec: &impl AdcClkSelGetter, clocks: &CoreClocks) -> Hertz {
331348
// Select Kernel Clock

src/crc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ mod macros {
161161
/// A polynomial being even means that the least significant bit is `0`
162162
/// in the polynomial's normal representation.
163163
#[derive(Copy, Clone, Debug, PartialEq)]
164+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
164165
pub struct Polynomial(Poly);
165166

166167
impl Polynomial {
@@ -252,6 +253,7 @@ impl Default for Polynomial {
252253

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

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

304308
/// CRC unit configuration.
305309
#[derive(Clone, Debug, PartialEq)]
310+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
306311
pub struct Config {
307312
poly: Polynomial,
308313
initial: u32,

src/dma/bdma.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl Instance for BDMA2 {
8787

8888
/// BDMA interrupts
8989
#[derive(Debug, Clone, Copy)]
90+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9091
pub struct BdmaInterrupts {
9192
transfer_complete: bool,
9293
transfer_error: bool,
@@ -95,6 +96,7 @@ pub struct BdmaInterrupts {
9596

9697
/// Contains the complete set of configuration for a DMA stream.
9798
#[derive(Debug, Default, Clone, Copy)]
99+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
98100
pub struct BdmaConfig {
99101
pub(crate) priority: config::Priority,
100102
pub(crate) memory_increment: bool,

src/dma/dma.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl Instance for DMA2 {
7373

7474
/// DMA interrupts
7575
#[derive(Debug, Clone, Copy)]
76+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7677
pub struct DmaInterrupts {
7778
transfer_complete: bool,
7879
transfer_error: bool,
@@ -83,6 +84,7 @@ pub struct DmaInterrupts {
8384

8485
/// Contains configuration for a DMA stream
8586
#[derive(Debug, Clone, Copy)]
87+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8688
pub struct DmaConfig {
8789
pub(crate) priority: config::Priority,
8890
pub(crate) memory_increment: bool,

src/dma/mdma.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl Instance for MDMA {
9494

9595
/// MDMA Stream Transfer Requests
9696
#[derive(Debug, Clone, Copy)]
97+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9798
pub enum MdmaTransferRequest {
9899
Dma1Tcif0 = 0,
99100
Dma1Tcif1,
@@ -161,6 +162,7 @@ pub enum MdmaTransferRequest {
161162

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

207209
/// MDMA increment mode
208210
#[derive(Debug, Clone, Copy, PartialEq)]
211+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
209212
pub enum MdmaIncrement {
210213
Fixed,
211214
/// Increment by one source/destination element each element
@@ -262,9 +265,20 @@ impl fmt::Debug for MdmaBurstSize {
262265
}
263266
}
264267
}
268+
#[cfg(feature = "defmt")]
269+
impl defmt::Format for MdmaBurstSize {
270+
fn format(&self, fmt: defmt::Formatter) {
271+
if self.0 > 0 {
272+
defmt::write!(fmt, "{=u32}", 1 << self.0);
273+
} else {
274+
defmt::intern!("single").format(fmt);
275+
}
276+
}
277+
}
265278

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

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

311326
/// MDMA interrupts
312327
#[derive(Debug, Clone, Copy)]
328+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
313329
pub struct MdmaInterrupts {
314330
transfer_complete: bool,
315331
transfer_error: bool,
@@ -320,6 +336,7 @@ pub struct MdmaInterrupts {
320336

321337
/// Contains the complete set of configuration for a MDMA stream.
322338
#[derive(Debug, Default, Clone, Copy)]
339+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
323340
pub struct MdmaConfig {
324341
pub(crate) priority: config::Priority,
325342
pub(crate) destination_increment: MdmaIncrement,

src/dma/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ use traits::{
117117

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

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

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

144147
impl Direction for PeripheralToMemory {
@@ -153,6 +156,7 @@ impl Direction for PeripheralToMemory {
153156

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

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

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

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

237243
/// Which DMA buffer is in use.
238244
#[derive(Debug, Clone, Copy, PartialEq)]
245+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
239246
pub enum CurrentBuffer {
240247
/// The first buffer (m0ar).
241248
Buffer0 = 0,
@@ -264,6 +271,7 @@ pub mod config {
264271
/// priority over the stream with the higher number. For example, Stream 2
265272
/// takes priority over Stream 4.
266273
#[derive(Debug, Clone, Copy)]
274+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
267275
pub enum Priority {
268276
/// Low priority.
269277
Low,
@@ -293,6 +301,7 @@ pub mod config {
293301

294302
/// The level to fill the fifo to before performing the transaction.
295303
#[derive(Debug, Clone, Copy)]
304+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
296305
pub enum FifoThreshold {
297306
/// 1/4 full.
298307
QuarterFull,
@@ -318,6 +327,7 @@ pub mod config {
318327
/// How burst transfers are done, requires fifo enabled. Check datasheet for
319328
/// valid combinations.
320329
#[derive(Debug, Clone, Copy)]
330+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
321331
pub enum BurstMode {
322332
/// Single transfer, no burst.
323333
NoBurst,

src/i2c.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub enum Stop {
5454

5555
/// I2C error
5656
#[derive(Debug)]
57+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5758
#[non_exhaustive]
5859
pub enum Error {
5960
/// Bus error
@@ -96,6 +97,7 @@ where
9697
}
9798

9899
#[derive(Debug)]
100+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
99101
pub struct I2c<I2C> {
100102
i2c: I2C,
101103
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@
4747
//! * [Cyclic Redundancy Check (CRC)](crate::crc) Feature gate `crc`
4848
//! * [Random Number Generator](crate::rng) ([rand_core::RngCore](rand_core::RngCore) is implemented under the `rand` feature gate)
4949
//! * [System Window Watchdog](crate::watchdog)
50+
//!
51+
//! Cargo Features
52+
//!
53+
//! * [`defmt`](https://defmt.ferrous-systems.com/) formatting for some types can be enabled with the feature `defmt`.
5054
5155
#![cfg_attr(not(test), no_std)]
5256
#![allow(non_camel_case_types)]
5357

5458
extern crate paste;
5559

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

5964
#[cfg(not(feature = "device-selected"))]

0 commit comments

Comments
 (0)