xref: /btstack/platform/embedded/btstack_run_loop_embedded.c (revision 1a6822024624aab466a9aff5f67325f81ed6d5e2)
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 /*
39  *  btstack_run_loop_embedded.c
40  *
41  *  For this run loop, we assume that there's no global way to wait for a list
42  *  of data sources to get ready. Instead, each data source has to queried
43  *  individually. Calling ds->isReady() before calling ds->process() doesn't
44  *  make sense, so we just poll each data source round robin.
45  *
46  *  To support an idle state, where an MCU could go to sleep, the process function
47  *  has to return if it has to called again as soon as possible
48  *
49  *  After calling process() on every data source and evaluating the pending timers,
50  *  the idle hook gets called if no data source did indicate that it needs to be
51  *  called right away.
52  *
53  */
54 
55 
56 #include "btstack_run_loop.h"
57 #include "btstack_run_loop_embedded.h"
58 #include "btstack_linked_list.h"
59 #include "hal_tick.h"
60 #include "hal_cpu.h"
61 
62 #include "btstack_debug.h"
63 
64 #include <stddef.h> // NULL
65 
66 #ifdef HAVE_EMBEDDED_TIME_MS
67 #include "hal_time_ms.h"
68 #endif
69 
70 #if defined(HAVE_EMBEDDED_TICK) && defined(HAVE_EMBEDDED_TIME_MS)
71 #error "Please specify either HAVE_EMBEDDED_TICK or HAVE_EMBEDDED_TIME_MS"
72 #endif
73 
74 #if defined(HAVE_EMBEDDED_TICK) || defined(HAVE_EMBEDDED_TIME_MS)
75 #define TIMER_SUPPORT
76 #endif
77 
78 static const btstack_run_loop_t btstack_run_loop_embedded;
79 
80 // the run loop
81 static btstack_linked_list_t data_sources;
82 
83 #ifdef TIMER_SUPPORT
84 static btstack_linked_list_t timers;
85 #endif
86 
87 #ifdef HAVE_EMBEDDED_TICK
88 static volatile uint32_t system_ticks;
89 #endif
90 
91 static int trigger_event_received = 0;
92 
93 /**
94  * Add data_source to run_loop
95  */
96 static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){
97     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
98 }
99 
100 /**
101  * Remove data_source from run loop
102  */
103 static int btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){
104     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
105 }
106 
107 // set timer
108 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
109 #ifdef HAVE_EMBEDDED_TICK
110     uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms);
111     if (ticks == 0) ticks++;
112     // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
113     ts->timeout = system_ticks + 1 + ticks;
114 #endif
115 #ifdef HAVE_EMBEDDED_TIME_MS
116     ts->timeout = hal_time_ms() + timeout_in_ms + 1;
117 #endif
118 }
119 
120 // under the assumption that a tick value is +/- 2^30 away from now, calculate the upper bits of the tick value
121 static int btstack_run_loop_embedded_reconstruct_higher_bits(uint32_t now, uint32_t ticks){
122     int32_t delta = ticks - now;
123     if (delta >= 0){
124         if (ticks >= now) {
125             return 0;
126         } else {
127             return 1;
128         }
129     } else {
130         if (ticks < now) {
131             return 0;
132         } else {
133             return -1;
134         }
135     }
136 }
137 
138 /**
139  * Add timer to run_loop (keep list sorted)
140  */
141 static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){
142 #ifdef TIMER_SUPPORT
143 
144 #ifdef HAVE_EMBEDDED_TICK
145     uint32_t now = system_ticks;
146 #endif
147 #ifdef HAVE_EMBEDDED_TIME_MS
148     uint32_t now = hal_time_ms();
149 #endif
150 
151     uint32_t new_low = ts->timeout;
152     int     new_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, new_low);
153 
154     btstack_linked_item_t *it;
155     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
156         // don't add timer that's already in there
157         if ((btstack_timer_source_t *) it->next == ts){
158             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
159             return;
160         }
161         uint32_t next_low  = ((btstack_timer_source_t *) it->next)->timeout;
162         int      next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low);
163         if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){
164             break;
165         }
166     }
167     ts->item.next = it->next;
168     it->next = (btstack_linked_item_t *) ts;
169 #endif
170 }
171 
172 /**
173  * Remove timer from run loop
174  */
175 static int btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){
176 #ifdef TIMER_SUPPORT
177     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
178 #else
179     return 0;
180 #endif
181 }
182 
183 static void btstack_run_loop_embedded_dump_timer(void){
184 #ifdef TIMER_SUPPORT
185 #ifdef ENABLE_LOG_INFO
186     btstack_linked_item_t *it;
187     int i = 0;
188     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
189         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
190         log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
191     }
192 #endif
193 #endif
194 }
195 
196 static void btstack_run_loop_embedded_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
197     ds->flags |= callback_types;
198 }
199 
200 static void btstack_run_loop_embedded_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
201     ds->flags &= ~callback_types;
202 }
203 
204 /**
205  * Execute run_loop once
206  */
207 void btstack_run_loop_embedded_execute_once(void) {
208     btstack_data_source_t *ds;
209 
210     // process data sources
211     btstack_data_source_t *next;
212     for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){
213         next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
214         if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
215             ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
216         }
217     }
218 
219 #ifdef TIMER_SUPPORT
220 
221 #ifdef HAVE_EMBEDDED_TICK
222     uint32_t now = system_ticks;
223 #endif
224 #ifdef HAVE_EMBEDDED_TIME_MS
225     uint32_t now = hal_time_ms();
226 #endif
227 
228     // process timers
229     while (timers) {
230         btstack_timer_source_t *ts = (btstack_timer_source_t *) timers;
231         uint32_t timeout_low = ts->timeout;
232         int      timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low);
233         if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break;
234         btstack_run_loop_remove_timer(ts);
235         ts->process(ts);
236     }
237 #endif
238 
239     // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
240     hal_cpu_disable_irqs();
241     if (trigger_event_received){
242         trigger_event_received = 0;
243         hal_cpu_enable_irqs();
244     } else {
245         hal_cpu_enable_irqs_and_sleep();
246     }
247 }
248 
249 /**
250  * Execute run_loop
251  */
252 static void btstack_run_loop_embedded_execute(void) {
253     while (1) {
254         btstack_run_loop_embedded_execute_once();
255     }
256 }
257 
258 #ifdef HAVE_EMBEDDED_TICK
259 static void btstack_run_loop_embedded_tick_handler(void){
260     system_ticks++;
261     trigger_event_received = 1;
262 }
263 
264 uint32_t btstack_run_loop_embedded_get_ticks(void){
265     return system_ticks;
266 }
267 
268 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){
269     return time_in_ms / hal_tick_get_tick_period_in_ms();
270 }
271 #endif
272 
273 static uint32_t btstack_run_loop_embedded_get_time_ms(void){
274 #ifdef HAVE_EMBEDDED_TIME_MS
275     return hal_time_ms();
276 #endif
277 #ifdef HAVE_EMBEDDED_TICK
278     return system_ticks * hal_tick_get_tick_period_in_ms();
279 #endif
280     return 0;
281 }
282 
283 
284 /**
285  * trigger run loop iteration
286  */
287 void btstack_run_loop_embedded_trigger(void){
288     trigger_event_received = 1;
289 }
290 
291 static void btstack_run_loop_embedded_init(void){
292     data_sources = NULL;
293 
294 #ifdef TIMER_SUPPORT
295     timers = NULL;
296 #endif
297 
298 #ifdef HAVE_EMBEDDED_TICK
299     system_ticks = 0;
300     hal_tick_init();
301     hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler);
302 #endif
303 }
304 
305 /**
306  * Provide btstack_run_loop_embedded instance
307  */
308 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){
309     return &btstack_run_loop_embedded;
310 }
311 
312 static const btstack_run_loop_t btstack_run_loop_embedded = {
313     &btstack_run_loop_embedded_init,
314     &btstack_run_loop_embedded_add_data_source,
315     &btstack_run_loop_embedded_remove_data_source,
316     &btstack_run_loop_embedded_enable_data_source_callbacks,
317     &btstack_run_loop_embedded_disable_data_source_callbacks,
318     &btstack_run_loop_embedded_set_timer,
319     &btstack_run_loop_embedded_add_timer,
320     &btstack_run_loop_embedded_remove_timer,
321     &btstack_run_loop_embedded_execute,
322     &btstack_run_loop_embedded_dump_timer,
323     &btstack_run_loop_embedded_get_time_ms,
324 };
325