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