Skip to content

Commit 3555ada

Browse files
authored
Merge pull request #13867 from marcemmers/fix-systimer-overflow
SysTimer: Prevent unacknowledged tick overflow when halted for a while
2 parents a0e8ab9 + 8b7e7ee commit 3555ada

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

platform/include/platform/internal/SysTimer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class SysTimer: private mbed::TimerEvent, private mbed::NonCopyable<SysTimer<Per
191191
*/
192192
std::chrono::duration<int, period> unacknowledged_ticks() const
193193
{
194-
return std::chrono::duration<int, period>(core_util_atomic_load_u8(&_unacknowledged_ticks));
194+
return std::chrono::duration<int, period>(core_util_atomic_load_u32(&_unacknowledged_ticks));
195195
}
196196

197197
/** Get the current tick count
@@ -248,7 +248,7 @@ class SysTimer: private mbed::TimerEvent, private mbed::NonCopyable<SysTimer<Per
248248
const highres_time_point _epoch;
249249
highres_time_point _time;
250250
uint64_t _tick;
251-
uint8_t _unacknowledged_ticks;
251+
uint32_t _unacknowledged_ticks;
252252
bool _wake_time_set;
253253
bool _wake_time_passed;
254254
bool _wake_early;

platform/source/SysTimer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,15 @@ void SysTimer<Period, IRQ>::acknowledge_tick()
191191
{
192192
// Try to avoid missed ticks if OS's IRQ level is not keeping
193193
// up with our handler.
194-
// 8-bit counter to save space, and also make sure it we don't
195-
// try TOO hard to resync if something goes really awry -
196-
// resync will reset if the count hits 256.
197-
if (core_util_atomic_decr_u8(&_unacknowledged_ticks, 1) > 0) {
194+
// This value should not get large during normal operation.
195+
// However, when interrupts are not handled for a while
196+
// (e.g. by debug halt or large critical sections) this
197+
// number will get large very quickly. All these un-
198+
// acknowledged ticks need to be processed because otherwise
199+
// the OS timing will be off. Processing may take a while.
200+
// For more info see: https://github.com/ARMmbed/mbed-os/issues/13801
201+
202+
if (core_util_atomic_decr_u32(&_unacknowledged_ticks, 1) > 0) {
198203
_set_irq_pending();
199204
}
200205
}

0 commit comments

Comments
 (0)