Skip to content

Make defmt support opt-in #35

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

Merged
merged 1 commit into from
Jun 19, 2021
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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ jobs:
toolchain: ${{ matrix.rust }}
override: true
- name: Build
run: cargo build --all-targets
run: |
cargo build --all-targets
cargo build --all-targets --features unstable-defmt
- name: Run tests
run: cargo test

Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ maintenance = { status = "actively-developed" }

[dependencies]
bitflags = "1.2.1"
defmt = "0.2.0"
vcell = "0.1.2"
nb = "1.0.0"
embedded-can = "0.3"

[dependencies.defmt]
optional = true
version = "0.2.0"

[features]
unstable-defmt = ["defmt"]

[profile.test]
opt-level = "s"
# FIXME: Turning LTO off makes the testsuite executables 2.5x larger.
Expand Down
16 changes: 10 additions & 6 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
//! Filter bank API.

use core::marker::PhantomData;
use defmt::Format;

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

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

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

/// A 16-bit identifier mask.
#[derive(Debug, Copy, Clone, Format)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
pub struct Mask16 {
id: u16,
mask: u16,
}

/// A 32-bit identifier mask.
#[derive(Debug, Copy, Clone, Format)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
pub struct Mask32 {
id: u32,
mask: u32,
Expand Down Expand Up @@ -161,7 +164,8 @@ impl Mask32 {
}

/// The configuration of a filter bank.
#[derive(Debug, Copy, Clone, Format)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
pub enum BankConfig {
List16([ListEntry16; 4]),
List32([ListEntry32; 2]),
Expand Down
7 changes: 4 additions & 3 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ mod tests;

use core::cmp::Ordering;
use core::ops::{Deref, DerefMut};
use defmt::Format;

use crate::{Id, IdReg};

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

impl Eq for Data {}

impl Format for Data {
#[cfg(feature = "unstable-defmt")]
impl defmt::Format for Data {
fn format(&self, fmt: defmt::Formatter<'_>) {
self.as_ref().format(fmt)
}
Expand Down
5 changes: 2 additions & 3 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use core::ops;

use defmt::Format;

#[allow(unused_imports)] // for intra-doc links only
use crate::{Can, Rx};

Expand All @@ -19,7 +17,8 @@ use crate::{Can, Rx};
///
/// This means that some of the interrupts listed here will result in the same interrupt handler
/// being invoked.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Format)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Interrupt {
/// Fires the **TX** interrupt when one of the transmit mailboxes returns to empty state.
Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
//! - Currently, only RX FIFO 0 is supported, and FIFO 1 will not be used.
//! - Support for querying error states and handling error interrupts is incomplete.
//!
//! # Cargo Features
//!
//! | Feature | Description |
//! |---------|-------------|
//! | `unstable-defmt` | Implements [`defmt`]'s `Format` trait for the types in this crate.[^1] |
//!
//! [^1]: The specific version of defmt is unspecified and may be updated in a patch release.
//!
//! [`embedded-can`]: https://docs.rs/embedded-can
//! [`defmt`]: https://docs.rs/defmt

#![doc(html_root_url = "https://docs.rs/bxcan/0.5.1")]
// Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default
Expand All @@ -43,7 +52,6 @@ use core::cmp::{Ord, Ordering};
use core::convert::{Infallible, TryInto};
use core::marker::PhantomData;
use core::ptr::NonNull;
use defmt::Format;

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

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

impl IdReg {
Expand Down Expand Up @@ -876,7 +885,8 @@ where
}

/// The three transmit mailboxes
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Format)]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "unstable-defmt", derive(defmt::Format))]
pub enum Mailbox {
/// Transmit mailbox 0
Mailbox0 = 0,
Expand Down
5 changes: 4 additions & 1 deletion testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ name = "interrupts"
harness = false

[dependencies]
bxcan = { path = ".." }
cortex-m = "0.6.3"
cortex-m-rt = "0.6.13"
defmt = "0.2.0"
Expand All @@ -32,6 +31,10 @@ stm32f1 = { version = "0.12.1", features = ["stm32f107", "rt"] }
nb = "1.0.0"
irq = "0.2.3"

[dependencies.bxcan]
path = ".."
features = ["unstable-defmt"]

[features]
# set logging levels here
default = [
Expand Down