Skip to content

Commit dec5ea1

Browse files
authored
Merge pull request #771 from stm32-rs/monotonic-ccr
monotonics fix: CC1 instead of CC3
2 parents 81ea447 + 2b1402c commit dec5ea1

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- rust: nightly
4747
mcu: stm32f479
4848
experimental: true
49-
features: usb_fs,sdio-host,can,i2s,fsmc_lcd,rtic2,rtic-tim2
49+
features: usb_fs,sdio-host,can,i2s,fsmc_lcd,rtic2,rtic-tim3
5050

5151
steps:
5252
- uses: actions/checkout@v4

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
### Changed
1919

2020
- Use `stm32f4-staging` until `stm32f4` is released [#706]
21+
- RTIC2 monotonics fix: CC1 instead of CC3
2122
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
2223

2324
[#566]: https://github.com/stm32-rs/stm32f4xx-hal/pull/566

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ required-features = ["tim2", "rtic1"]
679679

680680
[[example]]
681681
name = "rtic2-tick"
682-
required-features = ["rtic2", "rtic-tim2"]
682+
required-features = ["rtic2", "rtic-tim3"]
683683

684684
[[example]]
685685
name = "rtic-usart-shell"

examples/rtic2-tick.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use stm32f4xx_hal::{
1010
pac,
1111
prelude::*,
1212
};
13-
type Mono = stm32f4xx_hal::timer::MonoTimerUs<pac::TIM2>;
13+
type Mono = stm32f4xx_hal::timer::MonoTimerUs<pac::TIM3>;
1414

1515
// Uncomment if use SysTick as monotonic timer
1616
//use rtic_monotonics::systick::prelude::*;
@@ -35,8 +35,8 @@ mod app {
3535
let rcc = ctx.device.RCC.constrain();
3636
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
3737

38-
// Create TIM2 monotonic and initialize timer queue
39-
ctx.device.TIM2.monotonic_us(&mut ctx.core.NVIC, &clocks);
38+
// Create TIM3 monotonic and initialize timer queue
39+
ctx.device.TIM3.monotonic_us(&mut ctx.core.NVIC, &clocks);
4040

4141
// Uncomment if use SysTick as monotonic timer
4242
//Mono::start(ctx.core.SYST, 48_000_000);

src/timer/monotonics.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ macro_rules! __internal_create_stm32_timer_interrupt {
139139
}
140140

141141
macro_rules! make_timer {
142-
($tim: ident, $timer:ident, $bits:ident, $overflow:ident, $tq:ident$(, doc: ($($doc:tt)*))?) => {
142+
($tim: ident, $timer:ident, $overflow:ident, $tq:ident$(, doc: ($($doc:tt)*))?) => {
143143
static $overflow: AtomicU64 = AtomicU64::new(0);
144144
static $tq: TimerQueue<MonoTimerBackend<pac::$timer>> = TimerQueue::new();
145145

@@ -164,10 +164,14 @@ macro_rules! make_timer {
164164
self.tim.dier().modify(|_, w| w.uie().set_bit());
165165

166166
// Configure and enable half-period interrupt
167-
self.tim
168-
.ccr(2)
169-
.write(|w| w.ccr().set(($bits::MAX - ($bits::MAX >> 1)).into()));
170-
self.tim.dier().modify(|_, w| w.cc3ie().set_bit());
167+
self.tim.ccr1().write(|w| {
168+
w.ccr().set(
169+
(<pac::$timer as General>::Width::MAX
170+
- (<pac::$timer as General>::Width::MAX >> 1))
171+
.into(),
172+
)
173+
});
174+
self.tim.dier().modify(|_, w| w.cc1ie().set_bit());
171175

172176
// Trigger an update event to load the prescaler value to the clock.
173177
self.tim.egr().write(|w| w.ug().set_bit());
@@ -206,23 +210,24 @@ macro_rules! make_timer {
206210
fn now() -> Self::Ticks {
207211
calculate_now(
208212
|| $overflow.load(Ordering::Relaxed),
209-
|| Self::tim().cnt().read().bits(),
213+
|| Self::tim().cnt().read().cnt().bits(),
210214
)
211215
}
212216

213217
fn set_compare(instant: Self::Ticks) {
214218
let now = Self::now();
215219

216220
// Since the timer may or may not overflow based on the requested compare val, we check how many ticks are left.
217-
// `wrapping_sup` takes care of the u64 integer overflow special case.
218-
let val = if instant.wrapping_sub(now) <= ($bits::MAX as u64) {
219-
instant as $bits
220-
} else {
221-
// In the past or will overflow
222-
0
223-
};
224-
225-
Self::tim().ccr(1).write(|r| r.ccr().set(val.into()));
221+
// `wrapping_sub` takes care of the u64 integer overflow special case.
222+
let val =
223+
if instant.wrapping_sub(now) <= (<pac::$timer as General>::Width::MAX as u64) {
224+
instant as <pac::$timer as General>::Width
225+
} else {
226+
// In the past or will overflow
227+
0
228+
};
229+
230+
Self::tim().ccr2().write(|r| r.ccr().set(val.into()));
226231
}
227232

228233
fn clear_compare_flag() {
@@ -249,8 +254,8 @@ macro_rules! make_timer {
249254
assert!(prev % 2 == 1, "Monotonic must have missed an interrupt!");
250255
}
251256
// Half period
252-
if Self::tim().sr().read().cc3if().bit_is_set() {
253-
Self::tim().sr().modify(|_, w| w.cc3if().clear_bit());
257+
if Self::tim().sr().read().cc1if().bit_is_set() {
258+
Self::tim().sr().modify(|_, w| w.cc1if().clear_bit());
254259
let prev = $overflow.fetch_add(1, Ordering::Relaxed);
255260
assert!(prev % 2 == 0, "Monotonic must have missed an interrupt!");
256261
}
@@ -264,16 +269,16 @@ macro_rules! make_timer {
264269
}
265270

266271
#[cfg(all(feature = "tim2", feature = "rtic-tim2"))]
267-
make_timer!(tim2, TIM2, u32, TIMER2_OVERFLOWS, TIMER2_TQ);
272+
make_timer!(tim2, TIM2, TIMER2_OVERFLOWS, TIMER2_TQ);
268273

269274
#[cfg(all(feature = "tim3", feature = "rtic-tim3"))]
270-
make_timer!(tim3, TIM3, u16, TIMER3_OVERFLOWS, TIMER3_TQ);
275+
make_timer!(tim3, TIM3, TIMER3_OVERFLOWS, TIMER3_TQ);
271276

272277
#[cfg(all(feature = "tim4", feature = "rtic-tim4"))]
273-
make_timer!(tim4, TIM4, u16, TIMER4_OVERFLOWS, TIMER4_TQ);
278+
make_timer!(tim4, TIM4, TIMER4_OVERFLOWS, TIMER4_TQ);
274279

275280
#[cfg(all(feature = "tim5", feature = "rtic-tim5"))]
276-
make_timer!(tim5, TIM5, u16, TIMER5_OVERFLOWS, TIMER5_TQ);
281+
make_timer!(tim5, TIM5, TIMER5_OVERFLOWS, TIMER5_TQ);
277282

278283
pub trait Irq {
279284
const IRQ: pac::Interrupt;

0 commit comments

Comments
 (0)