Skip to content

Commit 3fc7a0e

Browse files
author
Jonas Schievink
committed
Mask out invalid bits when setting BTR
1 parent 7e50e9a commit 3fc7a0e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5-
No changes.
5+
### Fixes
6+
7+
* Mask out all reserved bits in `set_bit_timing` before writing the register.
68

79
## [0.7.0 - 2022-05-30](https://github.com/stm32-rs/bxcan/releases/tag/v0.7.0)
810

src/lib.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,7 @@ impl<I: Instance> CanConfig<'_, I> {
265265
/// Then copy the `CAN_BUS_TIME` register value from the table and pass it as the `btr`
266266
/// parameter to this method.
267267
pub fn set_bit_timing(self, btr: u32) -> Self {
268-
let can = self.can.registers();
269-
can.btr.modify(|r, w| unsafe {
270-
let mode_bits = r.bits() & 0xC000_0000;
271-
w.bits(mode_bits | btr)
272-
});
268+
self.can.set_bit_timing(btr);
273269
self
274270
}
275271

@@ -367,12 +363,8 @@ impl<I: Instance> CanBuilder<I> {
367363
///
368364
/// Then copy the `CAN_BUS_TIME` register value from the table and pass it as the `btr`
369365
/// parameter to this method.
370-
pub fn set_bit_timing(self, btr: u32) -> Self {
371-
let can = self.can.registers();
372-
can.btr.modify(|r, w| unsafe {
373-
let mode_bits = r.bits() & 0xC000_0000;
374-
w.bits(mode_bits | btr)
375-
});
366+
pub fn set_bit_timing(mut self, btr: u32) -> Self {
367+
self.can.set_bit_timing(btr);
376368
self
377369
}
378370

@@ -479,6 +471,17 @@ where
479471
unsafe { &*I::REGISTERS }
480472
}
481473

474+
fn set_bit_timing(&mut self, btr: u32) {
475+
// Mask of all non-reserved BTR bits, except the mode flags.
476+
const MASK: u32 = 0x037F_03FF;
477+
478+
let can = self.registers();
479+
can.btr.modify(|r, w| unsafe {
480+
let mode_bits = r.bits() & 0xC000_0000;
481+
w.bits(mode_bits | (btr & MASK))
482+
});
483+
}
484+
482485
/// Returns a reference to the peripheral instance.
483486
///
484487
/// This allows accessing HAL-specific data stored in the instance type.

0 commit comments

Comments
 (0)