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