Skip to content

Commit 0949164

Browse files
committed
events: Added better handling of desynchronized timers in platform layer
An odd bug (c0951c9) in the NCS36510 ticker code caused the timer/ticker classes to become desynchronized. Updated the handling to not assume the timers are perfectly in synch. This will increase the event's tolerance of less robust platforms.
1 parent c0951c9 commit 0949164

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

events/equeue/equeue_mbed.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626

2727
// Ticker operations
2828
static bool equeue_tick_inited = false;
29-
static unsigned equeue_minutes = 0;
29+
static volatile unsigned equeue_minutes = 0;
3030
static unsigned equeue_timer[
3131
(sizeof(Timer)+sizeof(unsigned)-1)/sizeof(unsigned)];
3232
static unsigned equeue_ticker[
3333
(sizeof(Ticker)+sizeof(unsigned)-1)/sizeof(unsigned)];
3434

3535
static void equeue_tick_update() {
36+
equeue_minutes += reinterpret_cast<Timer*>(equeue_timer)->read_ms();
3637
reinterpret_cast<Timer*>(equeue_timer)->reset();
37-
equeue_minutes += 1;
3838
}
3939

4040
static void equeue_tick_init() {
@@ -48,7 +48,7 @@ static void equeue_tick_init() {
4848
equeue_minutes = 0;
4949
reinterpret_cast<Timer*>(equeue_timer)->start();
5050
reinterpret_cast<Ticker*>(equeue_ticker)
51-
->attach_us(equeue_tick_update, (1 << 16)*1000);
51+
->attach_us(equeue_tick_update, 1000 << 16);
5252

5353
equeue_tick_inited = true;
5454
}
@@ -58,8 +58,15 @@ unsigned equeue_tick() {
5858
equeue_tick_init();
5959
}
6060

61-
unsigned equeue_ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
62-
return (equeue_minutes << 16) + equeue_ms;
61+
unsigned minutes;
62+
unsigned ms;
63+
64+
do {
65+
minutes = equeue_minutes;
66+
ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
67+
} while (minutes != equeue_minutes);
68+
69+
return minutes + ms;
6370
}
6471

6572

0 commit comments

Comments
 (0)