Skip to content

Commit

Permalink
Remove direct use of us ticker from platform
Browse files Browse the repository at this point in the history
Update platform code to use the ticker common layer rather than using
HAL us ticker directly. This both ensures that the underlying ticker
is properly initialized and that the value read is in microseconds with
full 32-bit range.
  • Loading branch information
c1728p9 authored and 0xc0170 committed Sep 29, 2017
1 parent 8183749 commit 114d60c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions platform/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,8 @@ void operator delete[](void *ptr)
extern "C" clock_t clock()
{
_mutex->lock();
clock_t t = us_ticker_read();
clock_t t = ticker_read(get_us_ticker_data());
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
_mutex->unlock();
return t;
}

5 changes: 3 additions & 2 deletions platform/mbed_wait_api_no_rtos.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ void wait_ms(int ms) {
}

void wait_us(int us) {
uint32_t start = us_ticker_read();
while ((us_ticker_read() - start) < (uint32_t)us);
const ticker_data_t *const ticker = get_us_ticker_data();
uint32_t start = ticker_read(ticker);
while ((ticker_read(ticker) - start) < (uint32_t)us);
}

#endif // #ifndef MBED_CONF_RTOS_PRESENT
Expand Down
9 changes: 7 additions & 2 deletions platform/mbed_wait_api_rtos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "hal/us_ticker_api.h"
#include "rtos/rtos.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_sleep.h"

void wait(float s) {
wait_us(s * 1000000.0f);
Expand All @@ -32,15 +33,19 @@ void wait_ms(int ms) {
}

void wait_us(int us) {
uint32_t start = us_ticker_read();
const ticker_data_t *const ticker = get_us_ticker_data();

uint32_t start = ticker_read(ticker);
// Use the RTOS to wait for millisecond delays if possible
int ms = us / 1000;
if ((ms > 0) && core_util_are_interrupts_enabled()) {
sleep_manager_lock_deep_sleep();
Thread::wait((uint32_t)ms);
sleep_manager_unlock_deep_sleep();
}
// Use busy waiting for sub-millisecond delays, or for the whole
// interval if interrupts are not enabled
while ((us_ticker_read() - start) < (uint32_t)us);
while ((ticker_read(ticker) - start) < (uint32_t)us);
}

#endif // #if MBED_CONF_RTOS_PRESENT
Expand Down

0 comments on commit 114d60c

Please sign in to comment.