Skip to content

Commit

Permalink
equeue - avoid Kernel::get_ms_count from IRQ
Browse files Browse the repository at this point in the history
Kernel::get_ms_count is documented as not working from IRQ.

In RTOS builds it can return misleading answers - see
ARM-software/CMSIS_5#625

In non-RTOS builds, it can trigger an assert, as it upsets the
sleep logic.

Modified code is still not ideal - could be improved further if
there was a fast path for "post now" that didn't bother looking
at timers (both at post time and dispatch time).
  • Loading branch information
kjbracey authored and geky committed Aug 4, 2019
1 parent 0d67610 commit 203057c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@
using namespace mbed;

// Ticker operations
#if MBED_CONF_RTOS_PRESENT
#if MBED_CONF_RTOS_API_PRESENT

#include "rtos/Kernel.h"
#include "platform/mbed_os_timer.h"

unsigned equeue_tick() {
return osKernelGetTickCount();
// 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
// us out of sleep - the RTOS will not have updated its
// ticks yet.
if (core_util_is_isr_active()) {
// And the documentation further says that this
// should not be called from critical sections, for
// performance reasons, but I don't have a good
// current alternative!
return mbed::internal::os_timer->get_time() / 1000;
} else {
return rtos::Kernel::get_ms_count();
}
}

#else
Expand Down

0 comments on commit 203057c

Please sign in to comment.