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.h" 59 #include "btstack_run_loop_embedded.h" 60 #include "btstack_linked_list.h" 61 #include "hal_tick.h" 62 #include "hal_cpu.h" 63 64 #include "btstack_debug.h" 65 66 #include <stddef.h> // NULL 67 68 #ifdef HAVE_EMBEDDED_TIME_MS 69 #include "hal_time_ms.h" 70 #endif 71 72 #if defined(HAVE_EMBEDDED_TICK) && defined(HAVE_EMBEDDED_TIME_MS) 73 #error "Please specify either HAVE_EMBEDDED_TICK or HAVE_EMBEDDED_TIME_MS" 74 #endif 75 76 #if defined(HAVE_EMBEDDED_TICK) || defined(HAVE_EMBEDDED_TIME_MS) 77 #define TIMER_SUPPORT 78 #endif 79 80 static const btstack_run_loop_t btstack_run_loop_embedded; 81 82 // the run loop 83 static btstack_linked_list_t data_sources; 84 85 #ifdef TIMER_SUPPORT 86 static btstack_linked_list_t timers; 87 #endif 88 89 #ifdef HAVE_EMBEDDED_TICK 90 static volatile uint32_t system_ticks; 91 #endif 92 93 static int trigger_event_received = 0; 94 95 /** 96 * Add data_source to run_loop 97 */ 98 static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){ 99 btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); 100 } 101 102 /** 103 * Remove data_source from run loop 104 */ 105 static int btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){ 106 return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); 107 } 108 109 // set timer 110 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 111 #ifdef HAVE_EMBEDDED_TICK 112 uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms); 113 if (ticks == 0) ticks++; 114 // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one 115 ts->timeout = system_ticks + 1 + ticks; 116 #endif 117 #ifdef HAVE_EMBEDDED_TIME_MS 118 ts->timeout = hal_time_ms() + timeout_in_ms + 1; 119 #endif 120 } 121 122 // under the assumption that a tick value is +/- 2^30 away from now, calculate the upper bits of the tick value 123 static int btstack_run_loop_embedded_reconstruct_higher_bits(uint32_t now, uint32_t ticks){ 124 int32_t delta = ticks - now; 125 if (delta >= 0){ 126 if (ticks >= now) { 127 return 0; 128 } else { 129 return 1; 130 } 131 } else { 132 if (ticks < now) { 133 return 0; 134 } else { 135 return -1; 136 } 137 } 138 } 139 140 /** 141 * Add timer to run_loop (keep list sorted) 142 */ 143 static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ 144 #ifdef TIMER_SUPPORT 145 146 #ifdef HAVE_EMBEDDED_TICK 147 uint32_t now = system_ticks; 148 #endif 149 #ifdef HAVE_EMBEDDED_TIME_MS 150 uint32_t now = hal_time_ms(); 151 #endif 152 153 uint32_t new_low = ts->timeout; 154 int new_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, new_low); 155 156 btstack_linked_item_t *it; 157 for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ 158 // don't add timer that's already in there 159 if ((btstack_timer_source_t *) it->next == ts){ 160 log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 161 return; 162 } 163 uint32_t next_low = ((btstack_timer_source_t *) it->next)->timeout; 164 int next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low); 165 if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){ 166 break; 167 } 168 } 169 ts->item.next = it->next; 170 it->next = (btstack_linked_item_t *) ts; 171 #endif 172 } 173 174 /** 175 * Remove timer from run loop 176 */ 177 static int btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){ 178 #ifdef TIMER_SUPPORT 179 return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); 180 #else 181 return 0; 182 #endif 183 } 184 185 static void btstack_run_loop_embedded_dump_timer(void){ 186 #ifdef TIMER_SUPPORT 187 #ifdef ENABLE_LOG_INFO 188 btstack_linked_item_t *it; 189 int i = 0; 190 for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ 191 btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 192 log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); 193 } 194 #endif 195 #endif 196 } 197 198 static void btstack_run_loop_embedded_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 199 ds->flags |= callback_types; 200 } 201 202 static void btstack_run_loop_embedded_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 203 ds->flags &= ~callback_types; 204 } 205 206 /** 207 * Execute run_loop once 208 */ 209 void btstack_run_loop_embedded_execute_once(void) { 210 btstack_data_source_t *ds; 211 212 // process data sources 213 btstack_data_source_t *next; 214 for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ 215 next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself 216 if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ 217 ds->process(ds, DATA_SOURCE_CALLBACK_POLL); 218 } 219 } 220 221 #ifdef TIMER_SUPPORT 222 223 #ifdef HAVE_EMBEDDED_TICK 224 uint32_t now = system_ticks; 225 #endif 226 #ifdef HAVE_EMBEDDED_TIME_MS 227 uint32_t now = hal_time_ms(); 228 #endif 229 230 // process timers 231 while (timers) { 232 btstack_timer_source_t *ts = (btstack_timer_source_t *) timers; 233 uint32_t timeout_low = ts->timeout; 234 int timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low); 235 if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break; 236 btstack_run_loop_embedded_remove_timer(ts); 237 ts->process(ts); 238 } 239 #endif 240 241 // disable IRQs and check if run loop iteration has been requested. if not, go to sleep 242 hal_cpu_disable_irqs(); 243 if (trigger_event_received){ 244 trigger_event_received = 0; 245 hal_cpu_enable_irqs(); 246 } else { 247 hal_cpu_enable_irqs_and_sleep(); 248 } 249 } 250 251 /** 252 * Execute run_loop 253 */ 254 static void btstack_run_loop_embedded_execute(void) { 255 while (1) { 256 btstack_run_loop_embedded_execute_once(); 257 } 258 } 259 260 #ifdef HAVE_EMBEDDED_TICK 261 static void btstack_run_loop_embedded_tick_handler(void){ 262 system_ticks++; 263 trigger_event_received = 1; 264 } 265 266 uint32_t btstack_run_loop_embedded_get_ticks(void){ 267 return system_ticks; 268 } 269 270 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){ 271 return time_in_ms / hal_tick_get_tick_period_in_ms(); 272 } 273 #endif 274 275 static uint32_t btstack_run_loop_embedded_get_time_ms(void){ 276 #if defined(HAVE_EMBEDDED_TIME_MS) 277 return hal_time_ms(); 278 #elif defined(HAVE_EMBEDDED_TICK) 279 return system_ticks * hal_tick_get_tick_period_in_ms(); 280 #else 281 return 0; 282 #endif 283 } 284 285 286 /** 287 * trigger run loop iteration 288 */ 289 void btstack_run_loop_embedded_trigger(void){ 290 trigger_event_received = 1; 291 } 292 293 static void btstack_run_loop_embedded_init(void){ 294 data_sources = NULL; 295 296 #ifdef TIMER_SUPPORT 297 timers = NULL; 298 #endif 299 300 #ifdef HAVE_EMBEDDED_TICK 301 system_ticks = 0; 302 hal_tick_init(); 303 hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler); 304 #endif 305 } 306 307 /** 308 * Provide btstack_run_loop_embedded instance 309 */ 310 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){ 311 return &btstack_run_loop_embedded; 312 } 313 314 static const btstack_run_loop_t btstack_run_loop_embedded = { 315 &btstack_run_loop_embedded_init, 316 &btstack_run_loop_embedded_add_data_source, 317 &btstack_run_loop_embedded_remove_data_source, 318 &btstack_run_loop_embedded_enable_data_source_callbacks, 319 &btstack_run_loop_embedded_disable_data_source_callbacks, 320 &btstack_run_loop_embedded_set_timer, 321 &btstack_run_loop_embedded_add_timer, 322 &btstack_run_loop_embedded_remove_timer, 323 &btstack_run_loop_embedded_execute, 324 &btstack_run_loop_embedded_dump_timer, 325 &btstack_run_loop_embedded_get_time_ms, 326 }; 327