From 9f42df27c74d1ec736e7b944736e08ff84f11fbf Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Thu, 16 Jun 2011 15:17:57 +0000 Subject: [PATCH] moved posix time handler code to run_loop_posix.c, implemented support for tick based timers for EMBEDDED --- include/btstack/run_loop.h | 5 ++++ src/run_loop.c | 49 -------------------------------- src/run_loop_embedded.c | 58 +++++++++++++++++++++++++------------- src/run_loop_posix.c | 45 +++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 69 deletions(-) diff --git a/include/btstack/run_loop.h b/include/btstack/run_loop.h index 429e2877f..31d9ad4d0 100644 --- a/include/btstack/run_loop.h +++ b/include/btstack/run_loop.h @@ -41,6 +41,8 @@ #include +#include + #ifdef HAVE_TIME #include #endif @@ -65,6 +67,9 @@ typedef struct timer { linked_item_t item; #ifdef HAVE_TIME struct timeval timeout; // <-- next timeout +#endif +#ifdef EMBEDDED + uint32_t timeout; // timeout in system ticks #endif void (*process)(struct timer *ts); // <-- do processing } timer_source_t; diff --git a/src/run_loop.c b/src/run_loop.c index c9c2c3b44..13b3d97bb 100644 --- a/src/run_loop.c +++ b/src/run_loop.c @@ -144,52 +144,3 @@ void run_loop_init(RUN_LOOP_TYPE type){ the_run_loop->init(); } -// set timer -void run_loop_set_timer(timer_source_t *a, int timeout_in_ms){ -#ifdef HAVE_TIME - gettimeofday(&a->timeout, NULL); - a->timeout.tv_sec += timeout_in_ms / 1000; - a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000; - if (a->timeout.tv_usec > 1000000) { - a->timeout.tv_usec -= 1000000; - a->timeout.tv_sec++; - } -#endif -} - -#ifdef HAVE_TIME -// compare timers - NULL is assumed to be before the Big Bang -// pre: 0 <= tv_usec < 1000000 -int run_loop_timeval_compare(struct timeval *a, struct timeval *b){ - if (!a && !b) return 0; - if (!a) return -1; - if (!b) return 1; - - if (a->tv_sec < b->tv_sec) { - return -1; - } - if (a->tv_sec > b->tv_sec) { - return 1; - } - - if (a->tv_usec < b->tv_usec) { - return -1; - } - if (a->tv_usec > b->tv_usec) { - return 1; - } - - return 0; - -} - -// compare timers - NULL is assumed to be before the Big Bang -// pre: 0 <= tv_usec < 1000000 -int run_loop_timer_compare(timer_source_t *a, timer_source_t *b){ - if (!a && !b) return 0; - if (!a) return -1; - if (!b) return 1; - return run_loop_timeval_compare(&a->timeout, &b->timeout); -} -#endif - diff --git a/src/run_loop_embedded.c b/src/run_loop_embedded.c index 6c1b665a1..f5009a224 100644 --- a/src/run_loop_embedded.c +++ b/src/run_loop_embedded.c @@ -42,17 +42,20 @@ #include #include +#include #include "run_loop_private.h" #include "debug.h" #include // NULL -// #define HAVE_TIME - // the run loop static linked_list_t data_sources; + +#ifdef EMBEDDED static linked_list_t timers; +static uint32_t system_ticks; +#endif /** * Add data_source to run_loop @@ -72,10 +75,10 @@ int embedded_remove_data_source(data_source_t *ds){ * Add timer to run_loop (keep list sorted) */ void embedded_add_timer(timer_source_t *ts){ -#ifdef HAVE_TIME +#ifdef EMBEDDED linked_item_t *it; for (it = (linked_item_t *) &timers; it->next ; it = it->next){ - if (run_loop_timer_compare( (timer_source_t *) it->next, ts) >= 0) { + if (ts->timeout < ((timer_source_t *) it->next)->timeout) { break; } } @@ -90,14 +93,16 @@ void embedded_add_timer(timer_source_t *ts){ * Remove timer from run loop */ int embedded_remove_timer(timer_source_t *ts){ -#ifdef HAVE_TIME +#ifdef EMBEDDED // log_dbg("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec); return linked_list_remove(&timers, (linked_item_t *) ts); +#else + return 0; #endif } -void embedded_dump_timer(void){ -#ifndef EMBEDDED +void embedded_dump_timer(){ +#if 0 linked_item_t *it; int i = 0; for (it = (linked_item_t *) timers; it ; it = it->next){ @@ -110,13 +115,9 @@ void embedded_dump_timer(void){ /** * Execute run_loop */ -void embedded_execute(void) { +void embedded_execute() { data_source_t *ds; -#ifdef HAVE_TIME - timer_source_t *ts; - struct timeval current_tv; -#endif - + while (1) { // process data sources @@ -126,14 +127,11 @@ void embedded_execute(void) { ds->process(ds); } -#ifdef HAVE_TIME +#ifdef EMBEDDED // process timers - // pre: 0 <= tv_usec < 1000000 while (timers) { - gettimeofday(¤t_tv, NULL); - ts = (timer_source_t *) timers; - if (ts->timeout.tv_sec > current_tv.tv_sec) break; - if (ts->timeout.tv_sec == current_tv.tv_sec && ts->timeout.tv_usec > current_tv.tv_usec) break; + timer_source_t *ts = (timer_source_t *) timers; + if (ts->timeout > system_ticks) break; run_loop_remove_timer(ts); ts->process(ts); } @@ -142,9 +140,29 @@ void embedded_execute(void) { } } -void embedded_init(void){ +#ifdef EMBEDDED +static void embedded_tick_handler(void){ + system_ticks++; +} + +// set timer +void run_loop_set_timer(timer_source_t *ts, int timeout_in_ms){ + int ticks = timeout_in_ms / hal_tick_get_tick_period_in_ms(); + if (ticks == 0) ticks++; + ts->timeout = system_ticks + ticks; +} +#endif + +void embedded_init(){ + data_sources = NULL; + +#ifdef EMBEDDED timers = NULL; + system_ticks = 0; + hal_tick_init(); + hal_tick_set_handler(&embedded_tick_handler); +#endif } const run_loop_t run_loop_embedded = { diff --git a/src/run_loop_posix.c b/src/run_loop_posix.c index 6e089bf86..7cbb62a27 100644 --- a/src/run_loop_posix.c +++ b/src/run_loop_posix.c @@ -184,6 +184,51 @@ void posix_execute(void) { } } +// set timer +void run_loop_set_timer(timer_source_t *a, int timeout_in_ms){ + gettimeofday(&a->timeout, NULL); + a->timeout.tv_sec += timeout_in_ms / 1000; + a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000; + if (a->timeout.tv_usec > 1000000) { + a->timeout.tv_usec -= 1000000; + a->timeout.tv_sec++; + } +} + +// compare timers - NULL is assumed to be before the Big Bang +// pre: 0 <= tv_usec < 1000000 +int run_loop_timeval_compare(struct timeval *a, struct timeval *b){ + if (!a && !b) return 0; + if (!a) return -1; + if (!b) return 1; + + if (a->tv_sec < b->tv_sec) { + return -1; + } + if (a->tv_sec > b->tv_sec) { + return 1; + } + + if (a->tv_usec < b->tv_usec) { + return -1; + } + if (a->tv_usec > b->tv_usec) { + return 1; + } + + return 0; + +} + +// compare timers - NULL is assumed to be before the Big Bang +// pre: 0 <= tv_usec < 1000000 +int run_loop_timer_compare(timer_source_t *a, timer_source_t *b){ + if (!a && !b) return 0; + if (!a) return -1; + if (!b) return 1; + return run_loop_timeval_compare(&a->timeout, &b->timeout); +} + void posix_init(void){ data_sources = NULL; timers = NULL;