fix for the case where there are multiple tickers firing at nearly the same time

pull/583/head
Rohit Grover 2014-10-20 09:49:22 +01:00
parent 53ec8ba932
commit de35d0c84e
2 changed files with 13 additions and 11 deletions

View File

@ -37,7 +37,7 @@ void us_ticker_irq_handler(void) {
return; return;
} }
if ((int)(head->timestamp - us_ticker_read()) <= 0) { if ((int64_t)(head->timestamp - us_ticker_read()) <= 0) {
// This event was in the past: // This event was in the past:
// point to the following one and execute its handler // point to the following one and execute its handler
ticker_event_t *p = head; 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; ticker_event_t *prev = NULL, *p = head;
while (p != NULL) { while (p != NULL) {
/* check if we come before p */ /* check if we come before p */
if ((int)(timestamp - p->timestamp) <= 0) { if ((int64_t)(timestamp - p->timestamp) < 0) {
break; break;
} }
/* go to the next element */ /* go to the next element */

View File

@ -42,7 +42,7 @@ uint32_t us_ticker_read()
timestamp_t value; timestamp_t value;
app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */ 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 * This is only as precise as the 32khz low-freq
* clock source, but could be adequate.*/ * clock source, but could be adequate.*/
} }
@ -78,7 +78,10 @@ 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 targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL;
uint32_t ticksToCount = (targetCounter >= currentCounter) ? uint32_t ticksToCount = (targetCounter >= currentCounter) ?
(targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter); (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter);
if (ticksToCount > 0) { 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; uint32_t rc;
rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/);
if (rc != NRF_SUCCESS) { if (rc != NRF_SUCCESS) {
@ -86,7 +89,6 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
return; return;
} }
us_ticker_appTimerRunning = true; us_ticker_appTimerRunning = true;
}
} }
void us_ticker_disable_interrupt(void) void us_ticker_disable_interrupt(void)