xref: /btstack/platform/embedded/btstack_run_loop_embedded.c (revision 030a86615cb7fc6ca2fda1b977dba29bb4102723)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "btstack_run_loop_embedded.c"
39 
40 /*
41  *  btstack_run_loop_embedded.c
42  *
43  *  For this run loop, we assume that there's no global way to wait for a list
44  *  of data sources to get ready. Instead, each data source has to queried
45  *  individually. Calling ds->isReady() before calling ds->process() doesn't
46  *  make sense, so we just poll each data source round robin.
47  *
48  *  To support an idle state, where an MCU could go to sleep, the process function
49  *  has to return if it has to called again as soon as possible
50  *
51  *  After calling process() on every data source and evaluating the pending timers,
52  *  the idle hook gets called if no data source did indicate that it needs to be
53  *  called right away.
54  *
55  */
56 
57 
58 #include "btstack_run_loop.h"
59 #include "btstack_run_loop_embedded.h"
60 #include "btstack_linked_list.h"
61 #include "btstack_util.h"
62 #include "hal_tick.h"
63 #include "hal_cpu.h"
64 
65 #include "btstack_debug.h"
66 
67 #include <stddef.h> // NULL
68 
69 #ifdef HAVE_EMBEDDED_TIME_MS
70 #include "hal_time_ms.h"
71 #endif
72 
73 #if defined(HAVE_EMBEDDED_TICK) && defined(HAVE_EMBEDDED_TIME_MS)
74 #error "Please specify either HAVE_EMBEDDED_TICK or HAVE_EMBEDDED_TIME_MS"
75 #endif
76 
77 #if defined(HAVE_EMBEDDED_TICK) || defined(HAVE_EMBEDDED_TIME_MS)
78 #define TIMER_SUPPORT
79 #endif
80 
81 static const btstack_run_loop_t btstack_run_loop_embedded;
82 
83 // the run loop
84 static btstack_linked_list_t data_sources;
85 
86 #ifdef TIMER_SUPPORT
87 static btstack_linked_list_t timers;
88 #endif
89 
90 #ifdef HAVE_EMBEDDED_TICK
91 static volatile uint32_t system_ticks;
92 #endif
93 
94 static int trigger_event_received = 0;
95 
96 /**
97  * Add data_source to run_loop
98  */
99 static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){
100     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
101 }
102 
103 /**
104  * Remove data_source from run loop
105  */
106 static int btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){
107     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
108 }
109 
110 // set timer
111 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
112 #ifdef HAVE_EMBEDDED_TICK
113     uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms);
114     if (ticks == 0) ticks++;
115     // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
116     ts->timeout = system_ticks + 1 + ticks;
117 #endif
118 #ifdef HAVE_EMBEDDED_TIME_MS
119     ts->timeout = hal_time_ms() + timeout_in_ms + 1;
120 #endif
121 }
122 
123 /**
124  * Add timer to run_loop (keep list sorted)
125  */
126 static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){
127 #ifdef TIMER_SUPPORT
128     btstack_linked_item_t *it;
129     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
130         // don't add timer that's already in there
131         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
132         if (next == ts){
133             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
134             return;
135         }
136         // exit if new timeout before list timeout
137         int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
138         if (delta < 0) break;
139     }
140 
141     ts->item.next = it->next;
142     it->next = (btstack_linked_item_t *) ts;
143 #endif
144 }
145 
146 /**
147  * Remove timer from run loop
148  */
149 static int btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){
150 #ifdef TIMER_SUPPORT
151     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
152 #else
153     return 0;
154 #endif
155 }
156 
157 static void btstack_run_loop_embedded_dump_timer(void){
158 #ifdef TIMER_SUPPORT
159 #ifdef ENABLE_LOG_INFO
160     btstack_linked_item_t *it;
161     int i = 0;
162     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
163         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
164         log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
165     }
166 #endif
167 #endif
168 }
169 
170 static void btstack_run_loop_embedded_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
171     ds->flags |= callback_types;
172 }
173 
174 static void btstack_run_loop_embedded_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
175     ds->flags &= ~callback_types;
176 }
177 
178 /**
179  * Execute run_loop once
180  */
181 void btstack_run_loop_embedded_execute_once(void) {
182     btstack_data_source_t *ds;
183 
184     // process data sources
185     btstack_data_source_t *next;
186     for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){
187         next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
188         if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
189             ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
190         }
191     }
192 
193 #ifdef TIMER_SUPPORT
194 
195 #ifdef HAVE_EMBEDDED_TICK
196     uint32_t now = system_ticks;
197 #endif
198 #ifdef HAVE_EMBEDDED_TIME_MS
199     uint32_t now = hal_time_ms();
200 #endif
201 
202     // process timers
203     while (timers) {
204         btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
205         int32_t delta = btstack_time_delta(ts->timeout, now);
206         if (delta > 0) break;
207 
208         btstack_run_loop_embedded_remove_timer(ts);
209         ts->process(ts);
210     }
211 #endif
212 
213     // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
214     hal_cpu_disable_irqs();
215     if (trigger_event_received){
216         trigger_event_received = 0;
217         hal_cpu_enable_irqs();
218     } else {
219         hal_cpu_enable_irqs_and_sleep();
220     }
221 }
222 
223 /**
224  * Execute run_loop
225  */
226 static void btstack_run_loop_embedded_execute(void) {
227     while (1) {
228         btstack_run_loop_embedded_execute_once();
229     }
230 }
231 
232 #ifdef HAVE_EMBEDDED_TICK
233 static void btstack_run_loop_embedded_tick_handler(void){
234     system_ticks++;
235     trigger_event_received = 1;
236 }
237 
238 uint32_t btstack_run_loop_embedded_get_ticks(void){
239     return system_ticks;
240 }
241 
242 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){
243     return time_in_ms / hal_tick_get_tick_period_in_ms();
244 }
245 #endif
246 
247 static uint32_t btstack_run_loop_embedded_get_time_ms(void){
248 #if   defined(HAVE_EMBEDDED_TIME_MS)
249     return hal_time_ms();
250 #elif defined(HAVE_EMBEDDED_TICK)
251     return system_ticks * hal_tick_get_tick_period_in_ms();
252 #else
253     return 0;
254 #endif
255 }
256 
257 
258 /**
259  * trigger run loop iteration
260  */
261 void btstack_run_loop_embedded_trigger(void){
262     trigger_event_received = 1;
263 }
264 
265 static void btstack_run_loop_embedded_init(void){
266     data_sources = NULL;
267 
268 #ifdef TIMER_SUPPORT
269     timers = NULL;
270 #endif
271 
272 #ifdef HAVE_EMBEDDED_TICK
273     system_ticks = 0;
274     hal_tick_init();
275     hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler);
276 #endif
277 }
278 
279 /**
280  * Provide btstack_run_loop_embedded instance
281  */
282 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){
283     return &btstack_run_loop_embedded;
284 }
285 
286 static const btstack_run_loop_t btstack_run_loop_embedded = {
287     &btstack_run_loop_embedded_init,
288     &btstack_run_loop_embedded_add_data_source,
289     &btstack_run_loop_embedded_remove_data_source,
290     &btstack_run_loop_embedded_enable_data_source_callbacks,
291     &btstack_run_loop_embedded_disable_data_source_callbacks,
292     &btstack_run_loop_embedded_set_timer,
293     &btstack_run_loop_embedded_add_timer,
294     &btstack_run_loop_embedded_remove_timer,
295     &btstack_run_loop_embedded_execute,
296     &btstack_run_loop_embedded_dump_timer,
297     &btstack_run_loop_embedded_get_time_ms,
298 };
299