Skip to content

Commit c62d8d4

Browse files
committed
Merge pull request #583 from rgrover/master
HAL: NRF51 - fix for the case where there are multiple tickers firing (issue #539)
2 parents ad70273 + de35d0c commit c62d8d4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

libraries/mbed/common/us_ticker_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void us_ticker_irq_handler(void) {
3737
return;
3838
}
3939

40-
if ((int)(head->timestamp - us_ticker_read()) <= 0) {
40+
if ((int64_t)(head->timestamp - us_ticker_read()) <= 0) {
4141
// This event was in the past:
4242
// point to the following one and execute its handler
4343
ticker_event_t *p = head;
@@ -70,7 +70,7 @@ void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t
7070
ticker_event_t *prev = NULL, *p = head;
7171
while (p != NULL) {
7272
/* check if we come before p */
73-
if ((int)(timestamp - p->timestamp) <= 0) {
73+
if ((int64_t)(timestamp - p->timestamp) < 0) {
7474
break;
7575
}
7676
/* go to the next element */

libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ uint32_t us_ticker_read()
4242

4343
timestamp_t value;
4444
app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */
45-
return (uint32_t)((value * 1000000) / APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value.
45+
return ((value * 1000000) / (uint32_t)APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value.
4646
* This is only as precise as the 32khz low-freq
4747
* clock source, but could be adequate.*/
4848
}
@@ -78,15 +78,17 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
7878
uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL;
7979
uint32_t ticksToCount = (targetCounter >= currentCounter) ?
8080
(targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter);
81-
if (ticksToCount > 0) {
82-
uint32_t rc;
83-
rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/);
84-
if (rc != NRF_SUCCESS) {
85-
/* placeholder to do something to recover from error */
86-
return;
87-
}
88-
us_ticker_appTimerRunning = true;
81+
if (ticksToCount < APP_TIMER_MIN_TIMEOUT_TICKS) { /* Honour the minimum value of the timeout_ticks parameter of app_timer_start() */
82+
ticksToCount = APP_TIMER_MIN_TIMEOUT_TICKS;
83+
}
84+
85+
uint32_t rc;
86+
rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/);
87+
if (rc != NRF_SUCCESS) {
88+
/* placeholder to do something to recover from error */
89+
return;
8990
}
91+
us_ticker_appTimerRunning = true;
9092
}
9193

9294
void us_ticker_disable_interrupt(void)

0 commit comments

Comments
 (0)