1 | /*
|
---|
2 | * Copyright (C) 2009-2012 by Matthias Ringwald
|
---|
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 MATTHIAS RINGWALD 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 btstack@ringwald.ch
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * run_loop_embedded.c
|
---|
39 | *
|
---|
40 | * For this run loop, we assume that there's no global way to wait for a list
|
---|
41 | * of data sources to get ready. Instead, each data source has to queried
|
---|
42 | * individually. Calling ds->isReady() before calling ds->process() doesn't
|
---|
43 | * make sense, so we just poll each data source round robin.
|
---|
44 | *
|
---|
45 | * To support an idle state, where an MCU could go to sleep, the process function
|
---|
46 | * has to return if it has to called again as soon as possible
|
---|
47 | *
|
---|
48 | * After calling process() on every data source and evaluating the pending timers,
|
---|
49 | * the idle hook gets called if no data source did indicate that it needs to be
|
---|
50 | * called right away.
|
---|
51 | *
|
---|
52 | */
|
---|
53 |
|
---|
54 |
|
---|
55 | #include <btstack/run_loop.h>
|
---|
56 | #include <btstack/linked_list.h>
|
---|
57 | #include <btstack/hal_tick.h>
|
---|
58 | #include <btstack/hal_cpu.h>
|
---|
59 |
|
---|
60 | #include "run_loop_private.h"
|
---|
61 | #include "debug.h"
|
---|
62 |
|
---|
63 | #include <stddef.h> // NULL
|
---|
64 |
|
---|
65 | // the run loop
|
---|
66 | static linked_list_t data_sources;
|
---|
67 |
|
---|
68 | static linked_list_t timers;
|
---|
69 |
|
---|
70 | #ifdef HAVE_TICK
|
---|
71 | static uint32_t system_ticks;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | static int trigger_event_received = 0;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Add data_source to run_loop
|
---|
78 | */
|
---|
79 | static void embedded_add_data_source(data_source_t *ds){
|
---|
80 | linked_list_add(&data_sources, (linked_item_t *) ds);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Remove data_source from run loop
|
---|
85 | */
|
---|
86 | static int embedded_remove_data_source(data_source_t *ds){
|
---|
87 | return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
---|
88 | }
|
---|
89 |
|
---|
90 | // set timer
|
---|
91 | static void embedded_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
|
---|
92 | #ifdef HAVE_TICK
|
---|
93 | uint32_t ticks = 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 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Add timer to run_loop (keep list sorted)
|
---|
102 | */
|
---|
103 | static void embedded_add_timer(timer_source_t *ts){
|
---|
104 | #ifdef HAVE_TICK
|
---|
105 | linked_item_t *it;
|
---|
106 | for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
---|
107 | // don't add timer that's already in there
|
---|
108 | if ((timer_source_t *) it->next == ts){
|
---|
109 | log_error( "run_loop_timer_add error: timer to add already in list!");
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | if (ts->timeout < ((timer_source_t *) it->next)->timeout) {
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | ts->item.next = it->next;
|
---|
117 | it->next = (linked_item_t *) ts;
|
---|
118 | // log_info("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
---|
119 | // embedded_dump_timer();
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Remove timer from run loop
|
---|
125 | */
|
---|
126 | static int embedded_remove_timer(timer_source_t *ts){
|
---|
127 | #ifdef HAVE_TICK
|
---|
128 | // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
---|
129 | return linked_list_remove(&timers, (linked_item_t *) ts);
|
---|
130 | #else
|
---|
131 | return 0;
|
---|
132 | #endif
|
---|
133 | }
|
---|
134 |
|
---|
135 | static void embedded_dump_timer(void){
|
---|
136 | #ifdef HAVE_TICK
|
---|
137 | #ifdef ENABLE_LOG_INFO
|
---|
138 | linked_item_t *it;
|
---|
139 | int i = 0;
|
---|
140 | for (it = (linked_item_t *) timers; it ; it = it->next){
|
---|
141 | timer_source_t *ts = (timer_source_t*) it;
|
---|
142 | log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
|
---|
143 | }
|
---|
144 | #endif
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Execute run_loop once
|
---|
150 | */
|
---|
151 | void embedded_execute_once(void) {
|
---|
152 | data_source_t *ds;
|
---|
153 |
|
---|
154 | // process data sources
|
---|
155 | data_source_t *next;
|
---|
156 | for (ds = (data_source_t *) data_sources; ds != NULL ; ds = next){
|
---|
157 | next = (data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
|
---|
158 | ds->process(ds);
|
---|
159 | }
|
---|
160 |
|
---|
161 | #ifdef HAVE_TICK
|
---|
162 | // process timers
|
---|
163 | while (timers) {
|
---|
164 | timer_source_t *ts = (timer_source_t *) timers;
|
---|
165 | if (ts->timeout > system_ticks) break;
|
---|
166 | run_loop_remove_timer(ts);
|
---|
167 | ts->process(ts);
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
|
---|
172 | hal_cpu_disable_irqs();
|
---|
173 | if (trigger_event_received){
|
---|
174 | trigger_event_received = 0;
|
---|
175 | hal_cpu_enable_irqs();
|
---|
176 | } else {
|
---|
177 | hal_cpu_enable_irqs_and_sleep();
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Execute run_loop
|
---|
183 | */
|
---|
184 | static void embedded_execute(void) {
|
---|
185 | while (1) {
|
---|
186 | embedded_execute_once();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | #ifdef HAVE_TICK
|
---|
191 | static void embedded_tick_handler(void){
|
---|
192 | system_ticks++;
|
---|
193 | trigger_event_received = 1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | uint32_t embedded_get_ticks(void){
|
---|
197 | return system_ticks;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void embedded_set_ticks(uint32_t ticks){
|
---|
201 | system_ticks = ticks;
|
---|
202 | }
|
---|
203 |
|
---|
204 | uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){
|
---|
205 | return time_in_ms / hal_tick_get_tick_period_in_ms();
|
---|
206 | }
|
---|
207 |
|
---|
208 | uint32_t embedded_get_time_ms(void){
|
---|
209 | return system_ticks * hal_tick_get_tick_period_in_ms();
|
---|
210 | }
|
---|
211 |
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * trigger run loop iteration
|
---|
216 | */
|
---|
217 | void embedded_trigger(void){
|
---|
218 | trigger_event_received = 1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | static void embedded_init(void){
|
---|
222 |
|
---|
223 | data_sources = NULL;
|
---|
224 |
|
---|
225 | #ifdef HAVE_TICK
|
---|
226 | timers = NULL;
|
---|
227 | system_ticks = 0;
|
---|
228 | hal_tick_init();
|
---|
229 | hal_tick_set_handler(&embedded_tick_handler);
|
---|
230 | #endif
|
---|
231 | }
|
---|
232 |
|
---|
233 | const run_loop_t run_loop_embedded = {
|
---|
234 | &embedded_init,
|
---|
235 | &embedded_add_data_source,
|
---|
236 | &embedded_remove_data_source,
|
---|
237 | &embedded_set_timer,
|
---|
238 | &embedded_add_timer,
|
---|
239 | &embedded_remove_timer,
|
---|
240 | &embedded_execute_once,
|
---|
241 | &embedded_dump_timer,
|
---|
242 | };
|
---|