mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-26 09:41:01 +00:00
add support for embedded platforms with millisecond clock
This commit is contained in:
parent
3c7c8efaca
commit
0d384c4d89
59
include/btstack/hal_time_ms.h
Normal file
59
include/btstack/hal_time_ms.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2014 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* 4. Any redistribution, use, or modification is done solely for
|
||||
* personal benefit and not for any commercial purpose or for
|
||||
* monetary gain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
||||
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* Please inquire about commercial licensing options at
|
||||
* contact@bluekitchen-gmbh.com
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* hal_time_ms.h
|
||||
*
|
||||
* Hardware abstraction layer for system clock with millisecond resolution
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __HAL_TIME_MS_H
|
||||
#define __HAL_TIME_MS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32_t hal_time_ms(void);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // __HAL_TIME_MS_H
|
@ -75,8 +75,8 @@ typedef struct timer {
|
||||
#ifdef HAVE_TIME
|
||||
struct timeval timeout; // <-- next timeout
|
||||
#endif
|
||||
#ifdef HAVE_TICK
|
||||
uint32_t timeout; // timeout in system ticks
|
||||
#if defined(HAVE_TICK) || defined(HAVE_TIME_MS)
|
||||
uint32_t timeout; // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS)
|
||||
#endif
|
||||
void (*process)(struct timer *ts); // <-- do processing
|
||||
} timer_source_t;
|
||||
|
@ -63,10 +63,24 @@
|
||||
|
||||
#include <stddef.h> // NULL
|
||||
|
||||
#ifdef HAVE_TIME_MS
|
||||
#include <btstack/hal_time_ms.h>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_TICK) && defined(HAVE_TIME_MS)
|
||||
#error "Please specify either HAVE_TICK or HAVE_TIME_MS"
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_TICK) || defined(HAVE_TIME_MS)
|
||||
#define TIMER_SUPPORT
|
||||
#endif
|
||||
|
||||
// the run loop
|
||||
static linked_list_t data_sources;
|
||||
|
||||
#ifdef TIMER_SUPPORT
|
||||
static linked_list_t timers;
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_TICK
|
||||
static uint32_t system_ticks;
|
||||
@ -96,13 +110,16 @@ static void embedded_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
|
||||
// time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
|
||||
ts->timeout = system_ticks + 1 + ticks;
|
||||
#endif
|
||||
#ifdef HAVE_TIME_MS
|
||||
ts->timeout = hal_time_ms() + timeout_in_ms + 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Add timer to run_loop (keep list sorted)
|
||||
*/
|
||||
static void embedded_add_timer(timer_source_t *ts){
|
||||
#ifdef HAVE_TICK
|
||||
#ifdef TIMER_SUPPORT
|
||||
linked_item_t *it;
|
||||
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
||||
// don't add timer that's already in there
|
||||
@ -116,8 +133,6 @@ static void embedded_add_timer(timer_source_t *ts){
|
||||
}
|
||||
ts->item.next = it->next;
|
||||
it->next = (linked_item_t *) ts;
|
||||
// log_info("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
||||
// embedded_dump_timer();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -125,8 +140,7 @@ static void embedded_add_timer(timer_source_t *ts){
|
||||
* Remove timer from run loop
|
||||
*/
|
||||
static int embedded_remove_timer(timer_source_t *ts){
|
||||
#ifdef HAVE_TICK
|
||||
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
||||
#ifdef TIMER_SUPPORT
|
||||
return linked_list_remove(&timers, (linked_item_t *) ts);
|
||||
#else
|
||||
return 0;
|
||||
@ -134,7 +148,7 @@ static int embedded_remove_timer(timer_source_t *ts){
|
||||
}
|
||||
|
||||
static void embedded_dump_timer(void){
|
||||
#ifdef HAVE_TICK
|
||||
#ifdef TIMER_SUPPORT
|
||||
#ifdef ENABLE_LOG_INFO
|
||||
linked_item_t *it;
|
||||
int i = 0;
|
||||
@ -160,10 +174,16 @@ void embedded_execute_once(void) {
|
||||
}
|
||||
|
||||
#ifdef HAVE_TICK
|
||||
uint32_t now = system_ticks;
|
||||
#endif
|
||||
#ifdef HAVE_TIME_MS
|
||||
uint32_t now = hal_time_ms();
|
||||
#endif
|
||||
#ifdef TIMER_SUPPORT
|
||||
// process timers
|
||||
while (timers) {
|
||||
timer_source_t *ts = (timer_source_t *) timers;
|
||||
if (ts->timeout > system_ticks) break;
|
||||
if (ts->timeout > now) break;
|
||||
run_loop_remove_timer(ts);
|
||||
ts->process(ts);
|
||||
}
|
||||
@ -223,8 +243,11 @@ static void embedded_init(void){
|
||||
|
||||
data_sources = NULL;
|
||||
|
||||
#ifdef HAVE_TICK
|
||||
#ifdef TIMER_SUPPORT
|
||||
timers = NULL;
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_TICK
|
||||
system_ticks = 0;
|
||||
hal_tick_init();
|
||||
hal_tick_set_handler(&embedded_tick_handler);
|
||||
|
Loading…
x
Reference in New Issue
Block a user