Skip to content

Commit

Permalink
events: Don't use OsTimer in SysTick builds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kjbracey authored and geky committed Aug 4, 2019
1 parent 203057c commit 58113eb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions equeue.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) {
q->slab.data = q->buffer;

q->queue = 0;
equeue_tick_init();
q->tick = equeue_tick();
q->generation = 0;
q->break_requested = false;
Expand Down
24 changes: 16 additions & 8 deletions equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ using namespace mbed;
#include "rtos/Kernel.h"
#include "platform/mbed_os_timer.h"

void equeue_tick_init() {
#if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT
mbed::internal::init_os_timer();
#endif
}

unsigned equeue_tick() {
#if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT
// It is not safe to call get_ms_count from ISRs, both
// because documentation says so, and because it will give
// a stale value from the RTOS if the interrupt has woken
Expand All @@ -42,6 +49,14 @@ unsigned equeue_tick() {
} else {
return rtos::Kernel::get_ms_count();
}
#else
// And this is the legacy behaviour - if running in
// non-tickless mode, this works fine, despite Mbed OS
// documentation saying no. (Most recent CMSIS-RTOS
// permits `ososKernelGetTickCount` from IRQ, and our
// `rtos::Kernel` wrapper copes too).
return rtos::Kernel::get_ms_count();
#endif
}

#else
Expand All @@ -57,7 +72,6 @@ unsigned equeue_tick() {
#define ALIAS_TIMEOUT Timeout
#endif

static bool equeue_tick_inited = false;
static volatile unsigned equeue_minutes = 0;
static unsigned equeue_timer[
(sizeof(ALIAS_TIMER)+sizeof(unsigned)-1)/sizeof(unsigned)];
Expand All @@ -69,7 +83,7 @@ static void equeue_tick_update() {
reinterpret_cast<ALIAS_TIMER*>(equeue_timer)->reset();
}

static void equeue_tick_init() {
void equeue_tick_init() {
MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(ALIAS_TIMER),
"The equeue_timer buffer must fit the class Timer");
MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(ALIAS_TICKER),
Expand All @@ -80,15 +94,9 @@ static void equeue_tick_init() {
equeue_minutes = 0;
timer->start();
ticker->attach_us(equeue_tick_update, 1000 << 16);

equeue_tick_inited = true;
}

unsigned equeue_tick() {
if (!equeue_tick_inited) {
equeue_tick_init();
}

unsigned minutes;
unsigned ms;

Expand Down
1 change: 1 addition & 0 deletions equeue_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern "C" {
// limited by the accuracy of this tick.
//
// Must intentionally overflow to 0 after 2^32-1
void equeue_tick_init(void);
unsigned equeue_tick(void);


Expand Down
3 changes: 3 additions & 0 deletions equeue_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@


// Tick operations
void equeue_tick_init(void) {
}

unsigned equeue_tick(void) {
struct timeval tv;
gettimeofday(&tv, 0);
Expand Down

0 comments on commit 58113eb

Please sign in to comment.