From de35d0c84e6416fcf9bdf4b4ec6d11aee86d4b7d Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Mon, 20 Oct 2014 09:49:22 +0100 Subject: [PATCH] fix for the case where there are multiple tickers firing at nearly the same time --- libraries/mbed/common/us_ticker_api.c | 4 ++-- .../TARGET_MCU_NRF51822/us_ticker.c | 20 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libraries/mbed/common/us_ticker_api.c b/libraries/mbed/common/us_ticker_api.c index 8218b9ccda..f307c8add7 100644 --- a/libraries/mbed/common/us_ticker_api.c +++ b/libraries/mbed/common/us_ticker_api.c @@ -37,7 +37,7 @@ void us_ticker_irq_handler(void) { return; } - if ((int)(head->timestamp - us_ticker_read()) <= 0) { + if ((int64_t)(head->timestamp - us_ticker_read()) <= 0) { // This event was in the past: // point to the following one and execute its handler ticker_event_t *p = head; @@ -70,7 +70,7 @@ void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t ticker_event_t *prev = NULL, *p = head; while (p != NULL) { /* check if we come before p */ - if ((int)(timestamp - p->timestamp) <= 0) { + if ((int64_t)(timestamp - p->timestamp) < 0) { break; } /* go to the next element */ diff --git a/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c b/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c index 5b56afb6a0..d544106d25 100644 --- a/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c +++ b/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c @@ -42,7 +42,7 @@ uint32_t us_ticker_read() timestamp_t value; app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */ - return (uint32_t)((value * 1000000) / APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value. + return ((value * 1000000) / (uint32_t)APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value. * This is only as precise as the 32khz low-freq * clock source, but could be adequate.*/ } @@ -78,15 +78,17 @@ void us_ticker_set_interrupt(timestamp_t timestamp) uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL; uint32_t ticksToCount = (targetCounter >= currentCounter) ? (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter); - if (ticksToCount > 0) { - uint32_t rc; - rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); - if (rc != NRF_SUCCESS) { - /* placeholder to do something to recover from error */ - return; - } - us_ticker_appTimerRunning = true; + if (ticksToCount < APP_TIMER_MIN_TIMEOUT_TICKS) { /* Honour the minimum value of the timeout_ticks parameter of app_timer_start() */ + ticksToCount = APP_TIMER_MIN_TIMEOUT_TICKS; } + + uint32_t rc; + rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); + if (rc != NRF_SUCCESS) { + /* placeholder to do something to recover from error */ + return; + } + us_ticker_appTimerRunning = true; } void us_ticker_disable_interrupt(void)