mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-18 05:42:49 +00:00
embedded: streamline timer management using btstack_time_delta
This commit is contained in:
parent
2e331c2584
commit
030a86615c
@ -58,6 +58,7 @@
|
||||
#include "btstack_run_loop.h"
|
||||
#include "btstack_run_loop_embedded.h"
|
||||
#include "btstack_linked_list.h"
|
||||
#include "btstack_util.h"
|
||||
#include "hal_tick.h"
|
||||
#include "hal_cpu.h"
|
||||
|
||||
@ -119,53 +120,24 @@ static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint
|
||||
#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)
|
||||
*/
|
||||
static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){
|
||||
#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;
|
||||
for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
|
||||
// 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!");
|
||||
return;
|
||||
}
|
||||
uint32_t next_low = ((btstack_timer_source_t *) it->next)->timeout;
|
||||
int next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low);
|
||||
if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){
|
||||
break;
|
||||
}
|
||||
// exit if new timeout before list timeout
|
||||
int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
|
||||
if (delta < 0) break;
|
||||
}
|
||||
|
||||
ts->item.next = it->next;
|
||||
it->next = (btstack_linked_item_t *) ts;
|
||||
#endif
|
||||
@ -229,10 +201,10 @@ void btstack_run_loop_embedded_execute_once(void) {
|
||||
|
||||
// process timers
|
||||
while (timers) {
|
||||
btstack_timer_source_t *ts = (btstack_timer_source_t *) timers;
|
||||
uint32_t timeout_low = ts->timeout;
|
||||
int timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low);
|
||||
if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break;
|
||||
btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
|
||||
int32_t delta = btstack_time_delta(ts->timeout, now);
|
||||
if (delta > 0) break;
|
||||
|
||||
btstack_run_loop_embedded_remove_timer(ts);
|
||||
ts->process(ts);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user