Skip to content

Commit 163fb19

Browse files
committed
events: Don't use OsTimer in SysTick builds
Previous fix assumed OsTimer is in use - it is for tickless RTOS builds and non-RTOS builds, but non-tickless RTOS builds avoid it altogether and use the SysTick peripheral. Restore previous behaviour for those builds, and just always read the tick count - there is no downside to this when ticking. While in the code, make equeue_tick initialisation explicit. This was problem if the OsTimer is not yet initialised when th done while debugging - turns out not to be part of the fix, but it speeds up the runtime code.
1 parent ed4bf52 commit 163fb19

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

events/equeue/equeue.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer)
8080
q->slab.data = q->buffer;
8181

8282
q->queue = 0;
83+
equeue_tick_init();
8384
q->tick = equeue_tick();
8485
q->generation = 0;
8586
q->break_requested = false;

events/equeue/equeue_mbed.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ using namespace mbed;
3939
#include "rtos/Kernel.h"
4040
#include "platform/mbed_os_timer.h"
4141

42+
void equeue_tick_init()
43+
{
44+
#if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT
45+
mbed::internal::init_os_timer();
46+
#endif
47+
}
48+
4249
unsigned equeue_tick()
4350
{
51+
#if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT
4452
// It is not safe to call get_ms_count from ISRs, both
4553
// because documentation says so, and because it will give
4654
// a stale value from the RTOS if the interrupt has woken
@@ -55,6 +63,14 @@ unsigned equeue_tick()
5563
} else {
5664
return rtos::Kernel::get_ms_count();
5765
}
66+
#else
67+
// And this is the legacy behaviour - if running in
68+
// non-tickless mode, this works fine, despite Mbed OS
69+
// documentation saying no. (Most recent CMSIS-RTOS
70+
// permits `ososKernelGetTickCount` from IRQ, and our
71+
// `rtos::Kernel` wrapper copes too).
72+
return rtos::Kernel::get_ms_count();
73+
#endif
5874
}
5975

6076
#else
@@ -70,7 +86,6 @@ unsigned equeue_tick()
7086
#define ALIAS_TIMEOUT Timeout
7187
#endif
7288

73-
static bool equeue_tick_inited = false;
7489
static volatile unsigned equeue_minutes = 0;
7590
static unsigned equeue_timer[
7691
(sizeof(ALIAS_TIMER) + sizeof(unsigned) - 1) / sizeof(unsigned)];
@@ -83,7 +98,7 @@ static void equeue_tick_update()
8398
reinterpret_cast<ALIAS_TIMER *>(equeue_timer)->reset();
8499
}
85100

86-
static void equeue_tick_init()
101+
void equeue_tick_init()
87102
{
88103
MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(ALIAS_TIMER),
89104
"The equeue_timer buffer must fit the class Timer");
@@ -95,16 +110,10 @@ static void equeue_tick_init()
95110
equeue_minutes = 0;
96111
timer->start();
97112
ticker->attach_us(equeue_tick_update, 1000 << 16);
98-
99-
equeue_tick_inited = true;
100113
}
101114

102115
unsigned equeue_tick()
103116
{
104-
if (!equeue_tick_inited) {
105-
equeue_tick_init();
106-
}
107-
108117
unsigned minutes;
109118
unsigned ms;
110119

events/equeue/equeue_platform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extern "C" {
6464
// limited by the accuracy of this tick.
6565
//
6666
// Must intentionally overflow to 0 after 2^32-1
67+
void equeue_tick_init(void);
6768
unsigned equeue_tick(void);
6869

6970

events/equeue/equeue_posix.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626

2727
// Tick operations
28+
void equeue_tick_init(void)
29+
{
30+
}
31+
2832
unsigned equeue_tick(void)
2933
{
3034
struct timeval tv;

0 commit comments

Comments
 (0)