freertos: handle time_ms overrun after 49 days

This commit is contained in:
Matthias Ringwald 2019-08-04 19:56:45 +02:00
parent b6fc147f78
commit 9155bf8c4c

View File

@ -47,9 +47,11 @@
#include <stddef.h> // NULL #include <stddef.h> // NULL
#include "btstack_run_loop_freertos.h"
#include "btstack_linked_list.h" #include "btstack_linked_list.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_run_loop_freertos.h" #include "btstack_util.h"
#include "hal_time_ms.h" #include "hal_time_ms.h"
// some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..) // some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..)
@ -104,13 +106,14 @@ static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){
btstack_linked_item_t *it; btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
// don't add timer that's already in there // don't add timer that's already in there
if ((btstack_timer_source_t *) it->next == ts){ btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
if (next == ts){
log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
return; return;
} }
if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) { // exit if new timeout before list timeout
break; int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
} if (delta < 0) break;
} }
ts->item.next = it->next; ts->item.next = it->next;
it->next = (btstack_linked_item_t *) ts; it->next = (btstack_linked_item_t *) ts;
@ -216,15 +219,16 @@ static void btstack_run_loop_freertos_execute(void) {
} }
} }
// process timers and get et next timeout // process timers and get next timeout
uint32_t timeout_ms = portMAX_DELAY; uint32_t timeout_ms = portMAX_DELAY;
log_debug("RL: portMAX_DELAY %u", portMAX_DELAY); log_debug("RL: portMAX_DELAY %u", portMAX_DELAY);
while (timers) { while (timers) {
btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
uint32_t now = btstack_run_loop_freertos_get_time_ms(); uint32_t now = btstack_run_loop_freertos_get_time_ms();
log_debug("RL: now %u, expires %u", now, ts->timeout); int32_t delta_ms = btstack_time_delta(ts->timeout, now);
if (ts->timeout > now){ log_debug("RL: now %u, expires %u -> delta %d", now, ts->timeout, delta_ms);
timeout_ms = ts->timeout - now; if (delta_ms > 0){
timeout_ms = delta_ms;
break; break;
} }
// remove timer before processing it to allow handler to re-register with run loop // remove timer before processing it to allow handler to re-register with run loop