embedded: streamline timer management using btstack_time_delta

This commit is contained in:
Matthias Ringwald 2019-08-04 21:51:19 +02:00
parent 2e331c2584
commit 030a86615c

View File

@ -58,6 +58,7 @@
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_embedded.h" #include "btstack_run_loop_embedded.h"
#include "btstack_linked_list.h" #include "btstack_linked_list.h"
#include "btstack_util.h"
#include "hal_tick.h" #include "hal_tick.h"
#include "hal_cpu.h" #include "hal_cpu.h"
@ -119,53 +120,24 @@ static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint
#endif #endif
} }
// under the assumption that a tick value is +/- 2^30 away from now, calculate the upper bits of the tick value
static int btstack_run_loop_embedded_reconstruct_higher_bits(uint32_t now, uint32_t ticks){
int32_t delta = ticks - now;
if (delta >= 0){
if (ticks >= now) {
return 0;
} else {
return 1;
}
} else {
if (ticks < now) {
return 0;
} else {
return -1;
}
}
}
/** /**
* Add timer to run_loop (keep list sorted) * Add timer to run_loop (keep list sorted)
*/ */
static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){
#ifdef TIMER_SUPPORT #ifdef TIMER_SUPPORT
#ifdef HAVE_EMBEDDED_TICK
uint32_t now = system_ticks;
#endif
#ifdef HAVE_EMBEDDED_TIME_MS
uint32_t now = hal_time_ms();
#endif
uint32_t new_low = ts->timeout;
int new_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, new_low);
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;
} }
uint32_t next_low = ((btstack_timer_source_t *) it->next)->timeout; // exit if new timeout before list timeout
int next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low); int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){ if (delta < 0) break;
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;
#endif #endif
@ -230,9 +202,9 @@ void btstack_run_loop_embedded_execute_once(void) {
// process timers // process timers
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 timeout_low = ts->timeout; int32_t delta = btstack_time_delta(ts->timeout, now);
int timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low); if (delta > 0) break;
if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break;
btstack_run_loop_embedded_remove_timer(ts); btstack_run_loop_embedded_remove_timer(ts);
ts->process(ts); ts->process(ts);
} }