xref: /btstack/platform/embedded/btstack_run_loop_embedded.c (revision 9f1d8eee3da7ea3eb34ff486475434e06110d7f9)
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_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 int trigger_event_received = 0;
89 
90 // set timer
91 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
92 #ifdef HAVE_EMBEDDED_TICK
93     uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms);
94     if (ticks == 0) ticks++;
95     // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one
96     ts->timeout = system_ticks + 1 + ticks;
97 #endif
98 #ifdef HAVE_EMBEDDED_TIME_MS
99     ts->timeout = hal_time_ms() + timeout_in_ms + 1;
100 #endif
101 }
102 
103 /**
104  * Execute run_loop once
105  */
106 void btstack_run_loop_embedded_execute_once(void) {
107 
108     // poll data sources
109     btstack_run_loop_base_poll_data_sources();
110 
111 #ifdef TIMER_SUPPORT
112 
113 #ifdef HAVE_EMBEDDED_TICK
114     uint32_t now = system_ticks;
115 #endif
116 #ifdef HAVE_EMBEDDED_TIME_MS
117     uint32_t now = hal_time_ms();
118 #endif
119 
120     // process timers
121     btstack_run_loop_base_process_timers(now);
122 #endif
123 
124     // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
125     hal_cpu_disable_irqs();
126     if (trigger_event_received){
127         trigger_event_received = 0;
128         hal_cpu_enable_irqs();
129     } else {
130         hal_cpu_enable_irqs_and_sleep();
131     }
132 }
133 
134 /**
135  * Execute run_loop
136  */
137 static void btstack_run_loop_embedded_execute(void) {
138     while (true) {
139         btstack_run_loop_embedded_execute_once();
140     }
141 }
142 
143 #ifdef HAVE_EMBEDDED_TICK
144 static void btstack_run_loop_embedded_tick_handler(void){
145     system_ticks++;
146     trigger_event_received = 1;
147 }
148 
149 uint32_t btstack_run_loop_embedded_get_ticks(void){
150     return system_ticks;
151 }
152 
153 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){
154     return time_in_ms / hal_tick_get_tick_period_in_ms();
155 }
156 #endif
157 
158 static uint32_t btstack_run_loop_embedded_get_time_ms(void){
159 #if   defined(HAVE_EMBEDDED_TIME_MS)
160     return hal_time_ms();
161 #elif defined(HAVE_EMBEDDED_TICK)
162     return system_ticks * hal_tick_get_tick_period_in_ms();
163 #else
164     return 0;
165 #endif
166 }
167 
168 
169 /**
170  * trigger run loop iteration
171  */
172 void btstack_run_loop_embedded_trigger(void){
173     trigger_event_received = 1;
174 }
175 
176 static void btstack_run_loop_embedded_init(void){
177     btstack_run_loop_base_init();
178 
179 #ifdef HAVE_EMBEDDED_TICK
180     system_ticks = 0;
181     hal_tick_init();
182     hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler);
183 #endif
184 }
185 
186 /**
187  * Provide btstack_run_loop_embedded instance
188  */
189 
190 static const btstack_run_loop_t btstack_run_loop_embedded = {
191     &btstack_run_loop_embedded_init,
192     &btstack_run_loop_base_add_data_source,
193     &btstack_run_loop_base_remove_data_source,
194     &btstack_run_loop_base_enable_data_source_callbacks,
195     &btstack_run_loop_base_disable_data_source_callbacks,
196     &btstack_run_loop_embedded_set_timer,
197     &btstack_run_loop_base_add_timer,
198     &btstack_run_loop_base_remove_timer,
199     &btstack_run_loop_embedded_execute,
200     &btstack_run_loop_base_dump_timer,
201     &btstack_run_loop_embedded_get_time_ms,
202 };
203 
204 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){
205     return &btstack_run_loop_embedded;
206 }
207 
208