Skip to content

Commit e620841

Browse files
Merge #35 #36
35: Make `defmt` support opt-in r=jonas-schievink a=jonas-schievink `defmt` is still evolving and making breaking changes, and it currently doesn't allow supporting multiple versions of it (knurling-rs/defmt#426). By making it opt-in and leaving its version unspecified, it should not stand in the way of bxcan 1.0 anymore. 36: Remove `Can::configure` r=jonas-schievink a=jonas-schievink Closes #31 Co-authored-by: Jonas Schievink <[email protected]>
3 parents a5ca0e4 + ccf5932 + ff547ee commit e620841

File tree

7 files changed

+43
-45
lines changed

7 files changed

+43
-45
lines changed

.github/workflows/ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ jobs:
3636
toolchain: ${{ matrix.rust }}
3737
override: true
3838
- name: Build
39-
run: cargo build --all-targets
39+
run: |
40+
cargo build --all-targets
41+
cargo build --all-targets --features unstable-defmt
4042
- name: Run tests
4143
run: cargo test
4244

Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ maintenance = { status = "actively-developed" }
2222

2323
[dependencies]
2424
bitflags = "1.2.1"
25-
defmt = "0.2.0"
2625
vcell = "0.1.2"
2726
nb = "1.0.0"
2827
embedded-can = "0.3"
2928

29+
[dependencies.defmt]
30+
optional = true
31+
version = "0.2.0"
32+
33+
[features]
34+
unstable-defmt = ["defmt"]
35+
3036
[profile.test]
3137
opt-level = "s"
3238
# FIXME: Turning LTO off makes the testsuite executables 2.5x larger.

src/filter.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
//! Filter bank API.
22
33
use core::marker::PhantomData;
4-
use defmt::Format;
54

65
use crate::pac::can::RegisterBlock;
76
use crate::{ExtendedId, FilterOwner, Id, Instance, MasterInstance, StandardId};
87

98
/// A 16-bit filter list entry.
109
///
1110
/// This can match data and remote frames using standard IDs.
12-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Format)]
11+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
12+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
1313
pub struct ListEntry16(u16);
1414

1515
/// A 32-bit filter list entry.
1616
///
1717
/// This can match data and remote frames using extended or standard IDs.
18-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Format)]
18+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
19+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
1920
pub struct ListEntry32(u32);
2021

2122
/// A 16-bit identifier mask.
22-
#[derive(Debug, Copy, Clone, Format)]
23+
#[derive(Debug, Copy, Clone)]
24+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
2325
pub struct Mask16 {
2426
id: u16,
2527
mask: u16,
2628
}
2729

2830
/// A 32-bit identifier mask.
29-
#[derive(Debug, Copy, Clone, Format)]
31+
#[derive(Debug, Copy, Clone)]
32+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
3033
pub struct Mask32 {
3134
id: u32,
3235
mask: u32,
@@ -161,7 +164,8 @@ impl Mask32 {
161164
}
162165

163166
/// The configuration of a filter bank.
164-
#[derive(Debug, Copy, Clone, Format)]
167+
#[derive(Debug, Copy, Clone)]
168+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
165169
pub enum BankConfig {
166170
List16([ListEntry16; 4]),
167171
List32([ListEntry32; 2]),

src/frame.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ mod tests;
33

44
use core::cmp::Ordering;
55
use core::ops::{Deref, DerefMut};
6-
use defmt::Format;
76

87
use crate::{Id, IdReg};
98

109
/// A CAN data or remote frame.
11-
#[derive(Clone, Debug, Eq, Format)]
10+
#[derive(Clone, Debug, Eq)]
11+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
1212
pub struct Frame {
1313
pub(crate) id: IdReg,
1414
pub(crate) data: Data,
@@ -272,7 +272,8 @@ impl PartialEq for Data {
272272

273273
impl Eq for Data {}
274274

275-
impl Format for Data {
275+
#[cfg(feature = "unstable-defmt")]
276+
impl defmt::Format for Data {
276277
fn format(&self, fmt: defmt::Formatter<'_>) {
277278
self.as_ref().format(fmt)
278279
}

src/interrupt.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
use core::ops;
44

5-
use defmt::Format;
6-
75
#[allow(unused_imports)] // for intra-doc links only
86
use crate::{Can, Rx};
97

@@ -19,7 +17,8 @@ use crate::{Can, Rx};
1917
///
2018
/// This means that some of the interrupts listed here will result in the same interrupt handler
2119
/// being invoked.
22-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Format)]
20+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
21+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
2322
#[non_exhaustive]
2423
pub enum Interrupt {
2524
/// Fires the **TX** interrupt when one of the transmit mailboxes returns to empty state.

src/lib.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@
1919
//! - Currently, only RX FIFO 0 is supported, and FIFO 1 will not be used.
2020
//! - Support for querying error states and handling error interrupts is incomplete.
2121
//!
22+
//! # Cargo Features
23+
//!
24+
//! | Feature | Description |
25+
//! |---------|-------------|
26+
//! | `unstable-defmt` | Implements [`defmt`]'s `Format` trait for the types in this crate.[^1] |
27+
//!
28+
//! [^1]: The specific version of defmt is unspecified and may be updated in a patch release.
29+
//!
2230
//! [`embedded-can`]: https://docs.rs/embedded-can
31+
//! [`defmt`]: https://docs.rs/defmt
2332
2433
#![doc(html_root_url = "https://docs.rs/bxcan/0.5.1")]
2534
// Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default
@@ -43,7 +52,6 @@ use core::cmp::{Ord, Ordering};
4352
use core::convert::{Infallible, TryInto};
4453
use core::marker::PhantomData;
4554
use core::ptr::NonNull;
46-
use defmt::Format;
4755

4856
use self::pac::generic::*; // To make the PAC extraction build
4957

@@ -119,7 +127,8 @@ pub enum Error {
119127
/// Lower identifier values have a higher priority. Additionally standard frames
120128
/// have a higher priority than extended frames and data frames have a higher
121129
/// priority than remote frames.
122-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Format)]
130+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
131+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
123132
struct IdReg(u32);
124133

125134
impl IdReg {
@@ -321,33 +330,6 @@ where
321330
self.instance
322331
}
323332

324-
/// Configure bit timings and silent/loop-back mode.
325-
///
326-
/// Acutal configuration happens on the `CanConfig` that is passed to the
327-
/// closure. It must be done this way because those configuration bits can
328-
/// only be set if the CAN controller is in a special init mode.
329-
/// Puts the peripheral in sleep mode afterwards. `Can::enable()` must be
330-
/// called to exit sleep mode and start reception and transmission.
331-
pub fn configure<F>(&mut self, f: F)
332-
where
333-
F: FnOnce(&mut CanConfig<I>),
334-
{
335-
let can = self.registers();
336-
337-
// Enter init mode.
338-
can.mcr
339-
.modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
340-
loop {
341-
let msr = can.msr.read();
342-
if msr.slak().bit_is_clear() && msr.inak().bit_is_set() {
343-
break;
344-
}
345-
}
346-
347-
let mut config = CanConfig { _can: PhantomData };
348-
f(&mut config);
349-
}
350-
351333
/// Configure bit timings and silent/loop-back mode.
352334
pub fn modify_config(&mut self) -> CanConfig<'_, I> {
353335
let can = self.registers();
@@ -876,7 +858,8 @@ where
876858
}
877859

878860
/// The three transmit mailboxes
879-
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Format)]
861+
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
862+
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
880863
pub enum Mailbox {
881864
/// Transmit mailbox 0
882865
Mailbox0 = 0,

testsuite/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ name = "interrupts"
1818
harness = false
1919

2020
[dependencies]
21-
bxcan = { path = ".." }
2221
cortex-m = "0.6.3"
2322
cortex-m-rt = "0.6.13"
2423
defmt = "0.2.0"
@@ -32,6 +31,10 @@ stm32f1 = { version = "0.12.1", features = ["stm32f107", "rt"] }
3231
nb = "1.0.0"
3332
irq = "0.2.3"
3433

34+
[dependencies.bxcan]
35+
path = ".."
36+
features = ["unstable-defmt"]
37+
3538
[features]
3639
# set logging levels here
3740
default = [

0 commit comments

Comments
 (0)