Skip to content

Commit 48861ae

Browse files
committed
Fix timeout logic when tick_count is wrapped
1 parent 653dc8f commit 48861ae

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

freertos-rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The freertos-build crate can be used to build and link FreeRTOS from source insi
1010
name = "freertos-next"
1111
readme = "README.md"
1212
repository = "https://github.com/mcu-rust/FreeRTOS"
13-
version = "0.6.0"
13+
version = "0.6.1"
1414

1515

1616
[lib]

freertos-rust/src/os_traits_impls.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,25 @@ mod sys_tick_timeout {
316316

317317
fn add(sys_tick: u32, count: u32, tick: u64) -> (u32, u32) {
318318
let reload = (SYST::get_reload() + 1) as u64;
319-
let mut diff_count = tick / reload;
319+
let diff_count = tick / reload;
320320
let diff_sys_tick = (tick - diff_count * reload) as u32;
321+
let mut diff_count = diff_count as u32;
321322
let new_sys_tick = if diff_sys_tick > sys_tick {
322323
diff_count += 1;
323324
sys_tick + reload as u32 - diff_sys_tick
324325
} else {
325326
sys_tick - diff_sys_tick
326327
};
327-
(new_sys_tick, count.wrapping_add(diff_count as u32))
328+
(new_sys_tick, count.wrapping_add(diff_count))
328329
}
329330
}
330331

331332
impl TimeoutState for FreeRtosTimeoutState {
332333
/// Can be reused without calling `restart()`.
333334
fn timeout(&mut self) -> bool {
334335
let (sys_tick, count) = Self::now();
335-
if count > self.count || (count == self.count && sys_tick < self.sys_tick) {
336+
let diff = (count as i32).wrapping_sub(self.count as i32);
337+
if diff > 1 || (diff == 0 && sys_tick < self.sys_tick) {
336338
(self.sys_tick, self.count) =
337339
Self::add(self.sys_tick, self.count, self.timeout_tick);
338340
true

0 commit comments

Comments
 (0)