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 BLUEKITCHEN
24 * GMBH 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_embedded.h"
59
60 #include "btstack_run_loop.h"
61 #include "btstack_linked_list.h"
62 #include "btstack_util.h"
63 #include "hal_tick.h"
64 #include "hal_cpu.h"
65
66 #include "btstack_debug.h"
67
68 #include <stddef.h> // NULL
69
70 #ifdef HAVE_EMBEDDED_TIME_MS
71 #include "hal_time_ms.h"
72 #endif
73
74 #if defined(HAVE_EMBEDDED_TICK) && defined(HAVE_EMBEDDED_TIME_MS)
75 #error "Please specify either HAVE_EMBEDDED_TICK or HAVE_EMBEDDED_TIME_MS"
76 #endif
77
78 #if defined(HAVE_EMBEDDED_TICK) || defined(HAVE_EMBEDDED_TIME_MS)
79 #define TIMER_SUPPORT
80 #endif
81
82 // the run loop
83
84 #ifdef HAVE_EMBEDDED_TICK
85 static volatile uint32_t system_ticks;
86 #endif
87
88 static bool trigger_event_received;
89
90 static bool run_loop_exit_requested;
91
92 // set timer
btstack_run_loop_embedded_set_timer(btstack_timer_source_t * ts,uint32_t timeout_in_ms)93 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
94 #ifdef HAVE_EMBEDDED_TICK
95 uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms);
96 if (ticks == 0) ticks++;
97 // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
98 ts->timeout = system_ticks + 1 + ticks;
99 #endif
100 #ifdef HAVE_EMBEDDED_TIME_MS
101 ts->timeout = hal_time_ms() + timeout_in_ms + 1;
102 #endif
103 }
104
105 /**
106 * Execute run_loop once
107 */
btstack_run_loop_embedded_execute_once(void)108 void btstack_run_loop_embedded_execute_once(void) {
109
110 // poll data sources
111 btstack_run_loop_base_poll_data_sources();
112
113 // execute callbacks
114 btstack_run_loop_base_execute_callbacks();
115
116 #ifdef TIMER_SUPPORT
117
118 #ifdef HAVE_EMBEDDED_TICK
119 uint32_t now = system_ticks;
120 #endif
121 #ifdef HAVE_EMBEDDED_TIME_MS
122 uint32_t now = hal_time_ms();
123 #endif
124
125 // process timers
126 btstack_run_loop_base_process_timers(now);
127 #endif
128
129 // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
130 hal_cpu_disable_irqs();
131 if (trigger_event_received){
132 trigger_event_received = false;
133 hal_cpu_enable_irqs();
134 } else {
135 hal_cpu_enable_irqs_and_sleep();
136 }
137 }
138
139 /**
140 * Execute run_loop
141 */
btstack_run_loop_embedded_execute(void)142 static void btstack_run_loop_embedded_execute(void) {
143 while (run_loop_exit_requested == false) {
144 btstack_run_loop_embedded_execute_once();
145 }
146 }
147
btstack_run_loop_embedded_trigger_exit(void)148 static void btstack_run_loop_embedded_trigger_exit(void){
149 run_loop_exit_requested = true;
150 }
151
152 #ifdef HAVE_EMBEDDED_TICK
btstack_run_loop_embedded_tick_handler(void)153 static void btstack_run_loop_embedded_tick_handler(void){
154 system_ticks++;
155 trigger_event_received = true;
156 }
157
btstack_run_loop_embedded_get_ticks(void)158 uint32_t btstack_run_loop_embedded_get_ticks(void){
159 return system_ticks;
160 }
161
btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms)162 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){
163 return time_in_ms / hal_tick_get_tick_period_in_ms();
164 }
165 #endif
166
btstack_run_loop_embedded_get_time_ms(void)167 static uint32_t btstack_run_loop_embedded_get_time_ms(void){
168 #if defined(HAVE_EMBEDDED_TIME_MS)
169 return hal_time_ms();
170 #elif defined(HAVE_EMBEDDED_TICK)
171 return system_ticks * hal_tick_get_tick_period_in_ms();
172 #else
173 return 0;
174 #endif
175 }
176
btstack_run_loop_embedded_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration)177 static void btstack_run_loop_embedded_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){
178 btstack_run_loop_base_add_callback(callback_registration);
179 trigger_event_received = true;
180 }
181
btstack_run_loop_embedded_poll_data_sources_from_irq(void)182 static void btstack_run_loop_embedded_poll_data_sources_from_irq(void){
183 trigger_event_received = true;
184 }
185
186 // @deprecated Use btstack_run_loop_poll_data_sources_from_irq() instead
btstack_run_loop_embedded_trigger(void)187 void btstack_run_loop_embedded_trigger(void){
188 btstack_run_loop_embedded_poll_data_sources_from_irq();
189 }
190
btstack_run_loop_embedded_init(void)191 static void btstack_run_loop_embedded_init(void){
192 btstack_run_loop_base_init();
193
194 #ifdef HAVE_EMBEDDED_TICK
195 system_ticks = 0;
196 hal_tick_init();
197 hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler);
198 #endif
199 }
200
201 /**
202 * Provide btstack_run_loop_embedded instance
203 */
btstack_run_loop_embedded_get_instance(void)204 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){
205 static const btstack_run_loop_t btstack_run_loop_embedded = {
206 &btstack_run_loop_embedded_init,
207 &btstack_run_loop_base_add_data_source,
208 &btstack_run_loop_base_remove_data_source,
209 &btstack_run_loop_base_enable_data_source_callbacks,
210 &btstack_run_loop_base_disable_data_source_callbacks,
211 &btstack_run_loop_embedded_set_timer,
212 &btstack_run_loop_base_add_timer,
213 &btstack_run_loop_base_remove_timer,
214 &btstack_run_loop_embedded_execute,
215 &btstack_run_loop_base_dump_timer,
216 &btstack_run_loop_embedded_get_time_ms,
217 &btstack_run_loop_embedded_poll_data_sources_from_irq,
218 &btstack_run_loop_embedded_execute_on_main_thread,
219 &btstack_run_loop_embedded_trigger_exit,
220 };
221
222 return &btstack_run_loop_embedded;
223 }
224
225