xref: /btstack/platform/embedded/btstack_run_loop_embedded.c (revision 8613e0eb5b9621b90c6c50e3d739bfa544af1c63)
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 // the run loop
82 static btstack_linked_list_t data_sources;
83 
84 #ifdef TIMER_SUPPORT
85 static btstack_linked_list_t timers;
86 #endif
87 
88 #ifdef HAVE_EMBEDDED_TICK
89 static volatile uint32_t system_ticks;
90 #endif
91 
92 static int trigger_event_received = 0;
93 
94 /**
95  * Add data_source to run_loop
96  */
97 static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){
98     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
99 }
100 
101 /**
102  * Remove data_source from run loop
103  */
104 static bool btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){
105     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
106 }
107 
108 // set timer
109 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
110 #ifdef HAVE_EMBEDDED_TICK
111     uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms);
112     if (ticks == 0) ticks++;
113     // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
114     ts->timeout = system_ticks + 1 + ticks;
115 #endif
116 #ifdef HAVE_EMBEDDED_TIME_MS
117     ts->timeout = hal_time_ms() + timeout_in_ms + 1;
118 #endif
119 }
120 
121 /**
122  * Add timer to run_loop (keep list sorted)
123  */
124 static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){
125 #ifdef TIMER_SUPPORT
126     btstack_linked_item_t *it;
127     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
128         // don't add timer that's already in there
129         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
130         if (next == ts){
131             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
132             return;
133         }
134         // exit if new timeout before list timeout
135         int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
136         if (delta < 0) break;
137     }
138 
139     ts->item.next = it->next;
140     it->next = (btstack_linked_item_t *) ts;
141 #endif
142 }
143 
144 /**
145  * Remove timer from run loop
146  */
147 static bool btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){
148 #ifdef TIMER_SUPPORT
149     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
150 #else
151     return 0;
152 #endif
153 }
154 
155 static void btstack_run_loop_embedded_dump_timer(void){
156 #ifdef TIMER_SUPPORT
157 #ifdef ENABLE_LOG_INFO
158     btstack_linked_item_t *it;
159     int i = 0;
160     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
161         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
162         log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
163     }
164 #endif
165 #endif
166 }
167 
168 static void btstack_run_loop_embedded_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
169     ds->flags |= callback_types;
170 }
171 
172 static void btstack_run_loop_embedded_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
173     ds->flags &= ~callback_types;
174 }
175 
176 /**
177  * Execute run_loop once
178  */
179 void btstack_run_loop_embedded_execute_once(void) {
180     btstack_data_source_t *ds;
181 
182     // process data sources
183     btstack_data_source_t *next;
184     for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){
185         next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
186         if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
187             ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
188         }
189     }
190 
191 #ifdef TIMER_SUPPORT
192 
193 #ifdef HAVE_EMBEDDED_TICK
194     uint32_t now = system_ticks;
195 #endif
196 #ifdef HAVE_EMBEDDED_TIME_MS
197     uint32_t now = hal_time_ms();
198 #endif
199 
200     // process timers
201     while (timers) {
202         btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
203         int32_t delta = btstack_time_delta(ts->timeout, now);
204         if (delta > 0) break;
205 
206         btstack_run_loop_embedded_remove_timer(ts);
207         ts->process(ts);
208     }
209 #endif
210 
211     // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
212     hal_cpu_disable_irqs();
213     if (trigger_event_received){
214         trigger_event_received = 0;
215         hal_cpu_enable_irqs();
216     } else {
217         hal_cpu_enable_irqs_and_sleep();
218     }
219 }
220 
221 /**
222  * Execute run_loop
223  */
224 static void btstack_run_loop_embedded_execute(void) {
225     while (true) {
226         btstack_run_loop_embedded_execute_once();
227     }
228 }
229 
230 #ifdef HAVE_EMBEDDED_TICK
231 static void btstack_run_loop_embedded_tick_handler(void){
232     system_ticks++;
233     trigger_event_received = 1;
234 }
235 
236 uint32_t btstack_run_loop_embedded_get_ticks(void){
237     return system_ticks;
238 }
239 
240 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){
241     return time_in_ms / hal_tick_get_tick_period_in_ms();
242 }
243 #endif
244 
245 static uint32_t btstack_run_loop_embedded_get_time_ms(void){
246 #if   defined(HAVE_EMBEDDED_TIME_MS)
247     return hal_time_ms();
248 #elif defined(HAVE_EMBEDDED_TICK)
249     return system_ticks * hal_tick_get_tick_period_in_ms();
250 #else
251     return 0;
252 #endif
253 }
254 
255 
256 /**
257  * trigger run loop iteration
258  */
259 void btstack_run_loop_embedded_trigger(void){
260     trigger_event_received = 1;
261 }
262 
263 static void btstack_run_loop_embedded_init(void){
264     data_sources = NULL;
265 
266 #ifdef TIMER_SUPPORT
267     timers = NULL;
268 #endif
269 
270 #ifdef HAVE_EMBEDDED_TICK
271     system_ticks = 0;
272     hal_tick_init();
273     hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler);
274 #endif
275 }
276 
277 /**
278  * Provide btstack_run_loop_embedded instance
279  */
280 
281 static const btstack_run_loop_t btstack_run_loop_embedded = {
282     &btstack_run_loop_embedded_init,
283     &btstack_run_loop_embedded_add_data_source,
284     &btstack_run_loop_embedded_remove_data_source,
285     &btstack_run_loop_embedded_enable_data_source_callbacks,
286     &btstack_run_loop_embedded_disable_data_source_callbacks,
287     &btstack_run_loop_embedded_set_timer,
288     &btstack_run_loop_embedded_add_timer,
289     &btstack_run_loop_embedded_remove_timer,
290     &btstack_run_loop_embedded_execute,
291     &btstack_run_loop_embedded_dump_timer,
292     &btstack_run_loop_embedded_get_time_ms,
293 };
294 
295 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){
296     return &btstack_run_loop_embedded;
297 }
298 
299