Skip to content

Commit e95bf77

Browse files
authored
Merge branch 'master' into transceiver-delay-compensation
2 parents 3e0d9ab + 454e804 commit e95bf77

File tree

8 files changed

+55
-6
lines changed

8 files changed

+55
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
* Update MSRV to 1.60
6+
* Allow timestamp to be accessed in all modes
67
* Implement transceiver delay compensation configuration
78

89
## [v0.2.1] 2024-09-04

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ targets = ["thumbv7em-none-eabihf"]
2222
fdcan_g0_g4_l5 = [] # Peripheral map found on G0 G4 L5
2323
fdcan_h7 = [] # Peripheral map found on H7
2424

25+
defmt-03 = ["dep:defmt"]
26+
2527
[dependencies]
2628
bitflags = "1.3.2"
2729
paste = "1.0"
2830
vcell = "0.1.3"
2931
nb = "1.0.0"
3032
static_assertions = "1.1"
3133
volatile-register = "0.2.1"
34+
defmt = { version = "0.3.6", optional = true }
3235

3336
[dependencies.embedded-can-03]
3437
version = "0.3"

src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::num::{NonZeroU16, NonZeroU8};
1414
/// Then copy the `CAN_BUS_TIME` register value from the table and pass it as the `btr`
1515
/// parameter to this method.
1616
#[derive(Clone, Copy, Debug)]
17+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
1718
pub struct NominalBitTiming {
1819
/// Value by which the oscillator frequency is divided for generating the bit time quanta. The bit
1920
/// time is built up from a multiple of this quanta. Valid values are 1 to 512.
@@ -61,6 +62,7 @@ impl Default for NominalBitTiming {
6162
/// Configures the data bit timings for the FdCan Variable Bitrates.
6263
/// This is not used when frame_transmit is set to anything other than AllowFdCanAndBRS.
6364
#[derive(Clone, Copy, Debug)]
65+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
6466
pub struct DataBitTiming {
6567
/// Tranceiver Delay Compensation
6668
pub transceiver_delay_compensation: bool,
@@ -118,6 +120,7 @@ impl Default for DataBitTiming {
118120
/// or use Bit rate switching. But if this general setting does not allow
119121
/// that, only classic CAN is used instead.
120122
#[derive(Clone, Copy, Debug)]
123+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
121124
pub enum FrameTransmissionConfig {
122125
/// Only allow Classic CAN message Frames
123126
ClassicCanOnly,
@@ -129,6 +132,7 @@ pub enum FrameTransmissionConfig {
129132

130133
///
131134
#[derive(Clone, Copy, Debug)]
135+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
132136
pub enum ClockDivider {
133137
/// Divide by 1
134138
_1 = 0b0000,
@@ -166,6 +170,7 @@ pub enum ClockDivider {
166170

167171
/// Prescaler of the Timestamp counter
168172
#[derive(Clone, Copy, Debug)]
173+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
169174
pub enum TimestampPrescaler {
170175
/// 1
171176
_1 = 1,
@@ -203,6 +208,7 @@ pub enum TimestampPrescaler {
203208

204209
/// Selects the source of the Timestamp counter
205210
#[derive(Clone, Copy, Debug)]
211+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
206212
pub enum TimestampSource {
207213
/// The Timestamp counter is disabled
208214
None,
@@ -215,6 +221,7 @@ pub enum TimestampSource {
215221

216222
/// How to handle frames in the global filter
217223
#[derive(Clone, Copy, Debug)]
224+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
218225
pub enum NonMatchingFilter {
219226
/// Frames will go to Fifo0 when they do no match any specific filter
220227
IntoRxFifo0 = 0b00,
@@ -226,6 +233,7 @@ pub enum NonMatchingFilter {
226233

227234
/// How to handle frames which do not match a specific filter
228235
#[derive(Clone, Copy, Debug)]
236+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
229237
pub struct GlobalFilter {
230238
/// How to handle non-matching standard frames
231239
pub handle_standard_frames: NonMatchingFilter,
@@ -291,6 +299,7 @@ impl Default for GlobalFilter {
291299

292300
/// FdCan Config Struct
293301
#[derive(Clone, Copy, Debug)]
302+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
294303
pub struct FdCanConfig {
295304
/// Nominal Bit Timings
296305
pub nbtr: NominalBitTiming,

src/filter.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl ExtendedFilter {
106106

107107
/// Filter Type
108108
#[derive(Clone, Copy, Debug)]
109+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
109110
pub enum FilterType<ID, UNIT>
110111
where
111112
ID: Copy + Clone + core::fmt::Debug,
@@ -150,6 +151,7 @@ where
150151

151152
/// Filter Action
152153
#[derive(Clone, Copy, Debug)]
154+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
153155
pub enum Action {
154156
/// No Action
155157
Disable = 0b000,
@@ -182,6 +184,7 @@ impl From<Action> for crate::message_ram::enums::FilterElementConfig {
182184

183185
/// Filter
184186
#[derive(Clone, Copy, Debug)]
187+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
185188
pub struct Filter<ID, UNIT>
186189
where
187190
ID: Copy + Clone + core::fmt::Debug,
@@ -195,6 +198,7 @@ where
195198

196199
/// Standard Filter Slot
197200
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
201+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
198202
pub enum StandardFilterSlot {
199203
/// 0
200204
_0 = 0,
@@ -291,6 +295,7 @@ impl From<u8> for StandardFilterSlot {
291295

292296
/// Extended Filter Slot
293297
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
298+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
294299
pub enum ExtendedFilterSlot {
295300
/// 0
296301
_0 = 0,
@@ -327,6 +332,7 @@ impl From<u8> for ExtendedFilterSlot {
327332

328333
/// Enum over both Standard and Extended Filter ID's
329334
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
335+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
330336
pub enum FilterId {
331337
/// Standard Filter Slots
332338
Standard(StandardFilterSlot),

src/frame.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::message_ram::enums::{DataLength, FilterFrameMatch};
1313

1414
/// Type of Frame
1515
#[derive(Clone, Copy, Debug, PartialEq)]
16+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
1617
pub enum FrameFormat {
1718
/// Frame used by Classic CAN
1819
Standard = 0,
@@ -46,6 +47,7 @@ impl From<PacFrameFormat> for FrameFormat {
4647
/// This struct wraps the *arbitration field* and implements `PartialOrd` and `Ord` accordingly,
4748
/// ordering higher priorities greater than lower ones.
4849
#[derive(Debug, Copy, Clone)]
50+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
4951
pub struct FramePriority(pub(crate) IdReg);
5052

5153
/// Ordering is based on the Identifier and frame type (data vs. remote) and can be used to sort
@@ -72,6 +74,7 @@ impl Eq for FramePriority {}
7274

7375
/// Header of a transmit request
7476
#[derive(Debug, Copy, Clone)]
77+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
7578
pub struct TxFrameHeader {
7679
/// Length of the data in bytes
7780
pub len: u8,
@@ -139,6 +142,7 @@ impl From<&TxBufferElementHeader> for TxFrameHeader {
139142

140143
/// Header of a Received Frame
141144
#[derive(Debug, Copy, Clone)]
145+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
142146
pub struct RxFrameInfo {
143147
/// Length in bytes
144148
pub len: u8,

src/id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::message_ram::enums::{IdType, RemoteTransmissionRequest};
66

77
/// Standard 11-bit CAN Identifier (`0..=0x7FF`).
88
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
9+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
910
pub struct StandardId(u16);
1011

1112
impl StandardId {
@@ -52,6 +53,7 @@ impl From<StandardId> for IdType {
5253

5354
/// Extended 29-bit CAN Identifier (`0..=1FFF_FFFF`).
5455
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
56+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
5557
pub struct ExtendedId(u32);
5658

5759
impl ExtendedId {
@@ -105,6 +107,7 @@ impl From<ExtendedId> for IdType {
105107

106108
/// A CAN Identifier (standard or extended).
107109
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
110+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
108111
pub enum Id {
109112
/// Standard 11-bit Identifier (`0..=0x7FF`).
110113
Standard(StandardId),
@@ -148,6 +151,7 @@ impl From<Id> for IdType {
148151
/// have a higher priority than extended frames and data frames have a higher
149152
/// priority than remote frames.
150153
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
154+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
151155
pub(crate) struct IdReg(u32);
152156

153157
impl IdReg {

src/interrupt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ macro_rules! declare_interrupts {
2020
/// `[crate::config::FdCanConfig]` struct.
2121
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2222
#[non_exhaustive]
23+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
2324
pub enum Interrupt {
2425
$(
2526
#[doc = $doc]
@@ -30,6 +31,7 @@ macro_rules! declare_interrupts {
3031
paste::paste! {
3132
bitflags::bitflags! {
3233
/// A set of FdCAN interrupts.
34+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
3335
pub struct Interrupts: u32 {
3436
$(
3537
#[doc = $doc]
@@ -195,6 +197,7 @@ impl ops::BitOrAssign<Interrupt> for Interrupts {
195197
/// The events linked to these can be configured
196198
/// see `[config::FdCanConfig]`
197199
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
200+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
198201
pub enum InterruptLine {
199202
/// Interrupt Line 0
200203
_0 = 0,

src/lib.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub unsafe trait Instance: message_ram::Instance {
8787

8888
/// Indicates if an Receive Overflow has occurred
8989
#[derive(Clone, Copy, Debug)]
90+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
9091
pub enum ReceiveErrorOverflow {
9192
/// No overflow has occurred
9293
Normal(u8),
@@ -97,6 +98,7 @@ pub enum ReceiveErrorOverflow {
9798
///Error Counters
9899
#[derive(Clone, Copy, Debug)]
99100
#[non_exhaustive]
101+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
100102
pub struct ErrorCounters {
101103
/// General CAN error counter
102104
pub can_errors: u8,
@@ -116,6 +118,7 @@ enum LoopbackMode {
116118

117119
/// Bus Activity
118120
#[derive(Clone, Copy, Debug)]
121+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
119122
pub enum Activity {
120123
/// Node is Synchronizing
121124
Synchronizing = 0b00,
@@ -141,6 +144,7 @@ impl TryFrom<u8> for Activity {
141144

142145
/// Indicates the type of the last error which occurred on the CAN bus
143146
#[derive(Clone, Copy, Debug)]
147+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
144148
pub enum LastErrorCode {
145149
/// There has been no error since last read
146150
NoError = 0b000,
@@ -179,6 +183,7 @@ impl TryFrom<u8> for LastErrorCode {
179183
/// Some status indications regarding the FDCAN protocl
180184
#[derive(Clone, Copy, Debug)]
181185
#[non_exhaustive]
186+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
182187
pub struct ProtocolStatus {
183188
/// Type of current activity
184189
pub activity: Activity,
@@ -200,13 +205,16 @@ pub trait Transmit {}
200205
pub trait Receive {}
201206

202207
/// Allows for the FdCan Instance to be released or to enter ConfigMode
208+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
203209
pub struct PoweredDownMode;
204210
/// Allows for the configuration for the Instance
211+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
205212
pub struct ConfigMode;
206213
/// This mode can be used for a “Hot Selftest”, meaning the FDCAN can be tested without
207214
/// affecting a running CAN system connected to the FDCAN_TX and FDCAN_RX pins. In this
208215
/// mode, FDCAN_RX pin is disconnected from the FDCAN and FDCAN_TX pin is held
209216
/// recessive.
217+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
210218
pub struct InternalLoopbackMode;
211219
impl Transmit for InternalLoopbackMode {}
212220
impl Receive for InternalLoopbackMode {}
@@ -216,10 +224,12 @@ impl Receive for InternalLoopbackMode {}
216224
/// feedback from its transmit output to its receive input. The actual value of the FDCAN_RX
217225
/// input pin is disregarded by the FDCAN. The transmitted messages can be monitored at the
218226
/// FDCAN_TX transmit pin.
227+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
219228
pub struct ExternalLoopbackMode;
220229
impl Transmit for ExternalLoopbackMode {}
221230
impl Receive for ExternalLoopbackMode {}
222231
/// The normal use of the FdCan instance after configurations
232+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
223233
pub struct NormalOperationMode;
224234
impl Transmit for NormalOperationMode {}
225235
impl Receive for NormalOperationMode {}
@@ -229,6 +239,7 @@ impl Receive for NormalOperationMode {}
229239
/// send dominant bits, instead it waits for the occurrence of bus idle condition to resynchronize
230240
/// itself to the CAN communication. The error counters for transmit and receive are frozen while
231241
/// error logging (can_errors) is active. TODO: automatically enter in this mode?
242+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
232243
pub struct RestrictedOperationMode;
233244
impl Receive for RestrictedOperationMode {}
234245
/// In Bus monitoring mode (for more details refer to ISO11898-1, 10.12 Bus monitoring),
@@ -239,14 +250,17 @@ impl Receive for RestrictedOperationMode {}
239250
/// state. In Bus monitoring mode the TXBRP register is held in reset state. The Bus monitoring
240251
/// mode can be used to analyze the traffic on a CAN bus without affecting it by the transmission
241252
/// of dominant bits.
253+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
242254
pub struct BusMonitoringMode;
243255
impl Receive for BusMonitoringMode {}
244256
/// Test mode must be used for production tests or self test only. The software control for
245257
/// FDCAN_TX pin interferes with all CAN protocol functions. It is not recommended to use test
246258
/// modes for application.
259+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
247260
pub struct TestMode;
248261

249262
/// Interface to a FdCAN peripheral.
263+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
250264
pub struct FdCan<I: Instance, MODE> {
251265
control: FdCanControl<I, MODE>,
252266
}
@@ -528,6 +542,12 @@ where
528542
) -> Self {
529543
Self::create_can(t.0.config, t.0.instance)
530544
}
545+
546+
/// Returns the current FdCan timestamp counter
547+
#[inline]
548+
pub fn timestamp(&self) -> u16 {
549+
self.control.timestamp()
550+
}
531551
}
532552

533553
impl<I> FdCan<I, PoweredDownMode>
@@ -875,12 +895,6 @@ where
875895
.bit(filter.reject_remote_extended_frames)
876896
});
877897
}
878-
879-
/// Returns the current FdCan timestamp counter
880-
#[inline]
881-
pub fn timestamp(&self) -> u16 {
882-
self.control.timestamp()
883-
}
884898
}
885899

886900
impl<I> FdCan<I, InternalLoopbackMode>
@@ -1127,6 +1141,7 @@ where
11271141
/// FdCanControl Struct
11281142
/// Used to house some information during an FdCan split.
11291143
/// and can be used for some generic information retrieval during operation.
1144+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
11301145
pub struct FdCanControl<I, MODE>
11311146
where
11321147
I: Instance,
@@ -1192,6 +1207,7 @@ where
11921207
}
11931208

11941209
/// Interface to the CAN transmitter part.
1210+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
11951211
pub struct Tx<I, MODE> {
11961212
_can: PhantomData<I>,
11971213
_mode: PhantomData<MODE>,
@@ -1467,6 +1483,7 @@ impl FifoNr for Fifo1 {
14671483
/// Notes whether an overrun has occurred.
14681484
/// Since both arms contain T, this can be 'unwrap'ed without causing a panic.
14691485
#[derive(Clone, Copy, Debug)]
1486+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
14701487
pub enum ReceiveOverrun<T> {
14711488
/// No overrun has occured
14721489
NoOverrun(T),
@@ -1486,6 +1503,7 @@ impl<T> ReceiveOverrun<T> {
14861503
}
14871504

14881505
/// Interface to the CAN receiver part.
1506+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
14891507
pub struct Rx<I, MODE, FIFONR>
14901508
where
14911509
FIFONR: FifoNr,
@@ -1625,6 +1643,7 @@ where
16251643
/// These are used for the transmit queue
16261644
/// and the two Receive FIFOs
16271645
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
1646+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
16281647
pub enum Mailbox {
16291648
/// Transmit mailbox 0
16301649
_0 = 0,

0 commit comments

Comments
 (0)