Skip to content

Commit

Permalink
Merge #35 #36
Browse files Browse the repository at this point in the history
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 <jonasschievink@gmail.com>
  • Loading branch information
bors[bot] and jonas-schievink committed Jun 19, 2021
3 parents a5ca0e4 + ccf5932 + ff547ee commit e620841
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 45 deletions.
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
43 changes: 13 additions & 30 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 @@ -321,33 +330,6 @@ where
self.instance
}

/// Configure bit timings and silent/loop-back mode.
///
/// Acutal configuration happens on the `CanConfig` that is passed to the
/// closure. It must be done this way because those configuration bits can
/// only be set if the CAN controller is in a special init mode.
/// Puts the peripheral in sleep mode afterwards. `Can::enable()` must be
/// called to exit sleep mode and start reception and transmission.
pub fn configure<F>(&mut self, f: F)
where
F: FnOnce(&mut CanConfig<I>),
{
let can = self.registers();

// Enter init mode.
can.mcr
.modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
loop {
let msr = can.msr.read();
if msr.slak().bit_is_clear() && msr.inak().bit_is_set() {
break;
}
}

let mut config = CanConfig { _can: PhantomData };
f(&mut config);
}

/// Configure bit timings and silent/loop-back mode.
pub fn modify_config(&mut self) -> CanConfig<'_, I> {
let can = self.registers();
Expand Down Expand Up @@ -876,7 +858,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

0 comments on commit e620841

Please sign in to comment.