2010-07-04 13:56:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* run_loop_embedded.c
|
|
|
|
*
|
|
|
|
* For this run loop, we assume that there's no global way to wait for a list
|
|
|
|
* of data sources to get ready. Instead, each data source has to queried
|
|
|
|
* individually. Calling ds->isReady() before calling ds->process() doesn't
|
|
|
|
* make sense, so we just poll each data source round robin.
|
|
|
|
*
|
2011-08-05 14:58:02 +00:00
|
|
|
* To support an idle state, where an MCU could go to sleep, the process function
|
|
|
|
* has to return if it has to called again as soon as possible
|
|
|
|
*
|
|
|
|
* After calling process() on every data source and evaluating the pending timers,
|
|
|
|
* the idle hook gets called if no data source did indicate that it needs to be
|
|
|
|
* called right away.
|
|
|
|
*
|
2010-07-04 13:56:43 +00:00
|
|
|
*/
|
|
|
|
|
2010-11-12 08:54:01 +00:00
|
|
|
|
2010-07-04 13:56:43 +00:00
|
|
|
#include <btstack/run_loop.h>
|
|
|
|
#include <btstack/linked_list.h>
|
2011-06-16 15:17:57 +00:00
|
|
|
#include <btstack/hal_tick.h>
|
2010-07-04 13:56:43 +00:00
|
|
|
|
|
|
|
#include "run_loop_private.h"
|
2010-07-05 20:49:14 +00:00
|
|
|
#include "debug.h"
|
2010-07-04 13:56:43 +00:00
|
|
|
|
2010-11-12 08:54:01 +00:00
|
|
|
#include <stddef.h> // NULL
|
|
|
|
|
2010-07-04 13:56:43 +00:00
|
|
|
// the run loop
|
|
|
|
static linked_list_t data_sources;
|
2011-06-16 15:17:57 +00:00
|
|
|
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2010-07-04 13:56:43 +00:00
|
|
|
static linked_list_t timers;
|
2011-06-16 15:17:57 +00:00
|
|
|
static uint32_t system_ticks;
|
|
|
|
#endif
|
2010-07-04 13:56:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data_source to run_loop
|
|
|
|
*/
|
|
|
|
void embedded_add_data_source(data_source_t *ds){
|
|
|
|
linked_list_add(&data_sources, (linked_item_t *) ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data_source from run loop
|
|
|
|
*/
|
|
|
|
int embedded_remove_data_source(data_source_t *ds){
|
|
|
|
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add timer to run_loop (keep list sorted)
|
|
|
|
*/
|
|
|
|
void embedded_add_timer(timer_source_t *ts){
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2010-07-04 13:56:43 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
2011-06-16 15:17:57 +00:00
|
|
|
if (ts->timeout < ((timer_source_t *) it->next)->timeout) {
|
2010-07-04 13:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts->item.next = it->next;
|
|
|
|
it->next = (linked_item_t *) ts;
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
2010-07-04 13:56:43 +00:00
|
|
|
// embedded_dump_timer();
|
2011-06-05 09:57:48 +00:00
|
|
|
#endif
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove timer from run loop
|
|
|
|
*/
|
|
|
|
int embedded_remove_timer(timer_source_t *ts){
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
2010-07-04 13:56:43 +00:00
|
|
|
return linked_list_remove(&timers, (linked_item_t *) ts);
|
2011-06-16 15:17:57 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
2011-06-05 09:57:48 +00:00
|
|
|
#endif
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 15:17:57 +00:00
|
|
|
void embedded_dump_timer(){
|
|
|
|
#if 0
|
2010-07-04 13:56:43 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
int i = 0;
|
|
|
|
for (it = (linked_item_t *) timers; it ; it = it->next){
|
|
|
|
timer_source_t *ts = (timer_source_t*) it;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout.tv_sec);
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
2010-08-03 19:47:36 +00:00
|
|
|
#endif
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute run_loop
|
|
|
|
*/
|
2011-06-16 15:17:57 +00:00
|
|
|
void embedded_execute() {
|
2010-07-04 13:56:43 +00:00
|
|
|
data_source_t *ds;
|
2011-06-16 15:17:57 +00:00
|
|
|
|
2010-07-04 13:56:43 +00:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
// process data sources
|
|
|
|
data_source_t *next;
|
|
|
|
for (ds = (data_source_t *) data_sources; ds != NULL ; ds = next){
|
|
|
|
next = (data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
|
|
|
|
ds->process(ds);
|
|
|
|
}
|
|
|
|
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2010-07-04 13:56:43 +00:00
|
|
|
// process timers
|
|
|
|
while (timers) {
|
2011-06-16 15:17:57 +00:00
|
|
|
timer_source_t *ts = (timer_source_t *) timers;
|
|
|
|
if (ts->timeout > system_ticks) break;
|
2010-07-04 13:56:43 +00:00
|
|
|
run_loop_remove_timer(ts);
|
|
|
|
ts->process(ts);
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-05 14:58:02 +00:00
|
|
|
// race condition.. poll data source, IRQ occurs -> data source gets ready -> IDLE hook makes MCU sleep?
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2011-06-16 15:17:57 +00:00
|
|
|
static void embedded_tick_handler(void){
|
|
|
|
system_ticks++;
|
|
|
|
}
|
|
|
|
|
2011-07-25 21:12:09 +00:00
|
|
|
uint32_t embedded_get_ticks(void){
|
|
|
|
return system_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){
|
2011-07-25 21:16:34 +00:00
|
|
|
return time_in_ms / hal_tick_get_tick_period_in_ms();
|
2011-07-25 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 15:17:57 +00:00
|
|
|
// set timer
|
2011-07-14 12:09:53 +00:00
|
|
|
void run_loop_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
|
2011-07-25 21:12:09 +00:00
|
|
|
uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms);
|
2011-06-16 15:17:57 +00:00
|
|
|
if (ticks == 0) ticks++;
|
|
|
|
ts->timeout = system_ticks + ticks;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void embedded_init(){
|
|
|
|
|
2010-07-04 13:56:43 +00:00
|
|
|
data_sources = NULL;
|
2011-06-16 15:17:57 +00:00
|
|
|
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2010-07-04 13:56:43 +00:00
|
|
|
timers = NULL;
|
2011-06-16 15:17:57 +00:00
|
|
|
system_ticks = 0;
|
|
|
|
hal_tick_init();
|
|
|
|
hal_tick_set_handler(&embedded_tick_handler);
|
|
|
|
#endif
|
2010-07-04 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 10:30:32 +00:00
|
|
|
run_loop_t run_loop_embedded = {
|
2010-07-04 13:56:43 +00:00
|
|
|
&embedded_init,
|
|
|
|
&embedded_add_data_source,
|
|
|
|
&embedded_remove_data_source,
|
|
|
|
&embedded_add_timer,
|
|
|
|
&embedded_remove_timer,
|
|
|
|
&embedded_execute,
|
|
|
|
&embedded_dump_timer
|
|
|
|
};
|