Skip to content

Commit

Permalink
Fix gte errors (GrepitAB#43)
Browse files Browse the repository at this point in the history
This change fixes some issues where watermark sizes were interpreted the
wrong way. Before this commit, watermarks for Rx FIFOs and Tx Event FIFO
were clamped to the maximum permissible watermark. But the behavior
according to the users manual is that watermark sizes over the
maximum permissible will disable the corresponding interrupt.

P.31 and p.34 of the M_CAN users manual v330 states that Rx FIFO
Watermarks, FnWM, can take values 1-64 inclusive. Any value greater than
64 is interpreted as "Watermark interrupt disabled".

P.44 of the M_CAN users manual v330 states that the Tx Event FIFO
Watermark, EFWM, can take values 1-32 inclusive, Any value greater than
32 is interpreted as "Watermark interrupt disabled".

This change makes the implementation consistent with the users manual on
those specifics.

A value greater than the maximum will be changed to zero for
consistency. The bit ranges are 6 bits wide for the Tx Event and
7 bits wide for the Rx configs but the u8 datatype is used in the
software interface. That u8 will be masked and shifted so that f.ex.
writing 129 to either bit range will actually set the value to 1, not
consistent with the user manual.
Clamping to any value over the maximum seems arbitrary, and for that
reason values larger than the maximum permissible are set to zero, which
also disables the interrupt.
  • Loading branch information
evading authored Oct 24, 2023
1 parent 60bd364 commit 39c0726
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions mcan/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Tagging in git follows a pattern: `mcan/<version>`.

## [Unreleased]
- Fix some issues with watermark sizes for Rx FIFOs and Tx Event FIFO (#43)
- Add `Can::aux::initialization_mode` (#41)
- Adhere to `filter_map_bool_then` clippy lint (#42)

Expand Down
21 changes: 12 additions & 9 deletions mcan/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ impl<'a, Id: mcan_core::CanId, D: mcan_core::Dependencies<Id>, C: Capacities>
reg.rxf0.c.modify(|_, w| {
let w = w.fom().bit(config.rx_fifo_0.mode.into());
let mut watermark = config.rx_fifo_0.watermark;
// According to the spec, any value >= 64 is interpreted as watermark disabled
if watermark >= 64 {
watermark = 64;
// According to the spec, any value > 64 is interpreted as watermark interrupt
// disabled, as is 0.
if watermark > 64 {
watermark = 0;
}
// Safety: The value is sanitized before the write
unsafe { w.fwm().bits(watermark) }
Expand All @@ -351,9 +352,10 @@ impl<'a, Id: mcan_core::CanId, D: mcan_core::Dependencies<Id>, C: Capacities>
reg.rxf1.c.modify(|_, w| {
let w = w.fom().bit(config.rx_fifo_1.mode.into());
let mut watermark = config.rx_fifo_1.watermark;
// According to the spec, any value >= 64 is interpreted as watermark disabled
if watermark >= 64 {
watermark = 64;
// According to the spec, any value > 64 is interpreted as watermark interrupt
// disabled, as is 0.
if watermark > 64 {
watermark = 0;
}
// Safety: The value is sanitized before the write
unsafe { w.fwm().bits(watermark) }
Expand All @@ -366,9 +368,10 @@ impl<'a, Id: mcan_core::CanId, D: mcan_core::Dependencies<Id>, C: Capacities>
// Configure Tx Event Fifo
reg.txefc.modify(|_, w| {
let mut watermark = config.tx.tx_event_fifo_watermark;
// According to the spec, any value >= 32 is interpreted as watermark disabled
if watermark >= 32 {
watermark = 32;
// According to the spec, any value > 32 is interpreted as watermark interrupt
// disabled, as is 0.
if watermark > 32 {
watermark = 0;
}
// Safety: The value is sanitized before the write
unsafe { w.efwm().bits(watermark) }
Expand Down

0 comments on commit 39c0726

Please sign in to comment.