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 /* 39 * btstack_run_loop_embedded.c 40 * 41 * For this run loop, we assume that there's no global way to wait for a list 42 * of data sources to get ready. Instead, each data source has to queried 43 * individually. Calling ds->isReady() before calling ds->process() doesn't 44 * make sense, so we just poll each data source round robin. 45 * 46 * To support an idle state, where an MCU could go to sleep, the process function 47 * has to return if it has to called again as soon as possible 48 * 49 * After calling process() on every data source and evaluating the pending timers, 50 * the idle hook gets called if no data source did indicate that it needs to be 51 * called right away. 52 * 53 */ 54 55 56 #include "btstack_run_loop.h" 57 #include "btstack_run_loop_embedded.h" 58 #include "btstack_linked_list.h" 59 #include "hal_tick.h" 60 #include "hal_cpu.h" 61 62 #include "btstack_debug.h" 63 64 #include <stddef.h> // NULL 65 66 #ifdef HAVE_TIME_MS 67 #include "hal_time_ms.h" 68 #endif 69 70 #if defined(HAVE_TICK) && defined(HAVE_TIME_MS) 71 #error "Please specify either HAVE_TICK or HAVE_TIME_MS" 72 #endif 73 74 #if defined(HAVE_TICK) || defined(HAVE_TIME_MS) 75 #define TIMER_SUPPORT 76 #endif 77 78 static const btstack_run_loop_t btstack_run_loop_embedded; 79 80 // the run loop 81 static btstack_linked_list_t data_sources; 82 83 #ifdef TIMER_SUPPORT 84 static btstack_linked_list_t timers; 85 #endif 86 87 #ifdef HAVE_TICK 88 static volatile uint32_t system_ticks; 89 #endif 90 91 static int trigger_event_received = 0; 92 93 /** 94 * Add data_source to run_loop 95 */ 96 static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){ 97 btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); 98 } 99 100 /** 101 * Remove data_source from run loop 102 */ 103 static int btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){ 104 return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); 105 } 106 107 // set timer 108 static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 109 #ifdef HAVE_TICK 110 uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms); 111 if (ticks == 0) ticks++; 112 // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one 113 ts->timeout = system_ticks + 1 + ticks; 114 #endif 115 #ifdef HAVE_TIME_MS 116 ts->timeout = hal_time_ms() + timeout_in_ms + 1; 117 #endif 118 } 119 120 /** 121 * Add timer to run_loop (keep list sorted) 122 */ 123 static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ 124 #ifdef TIMER_SUPPORT 125 btstack_linked_item_t *it; 126 for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ 127 // don't add timer that's already in there 128 if ((btstack_timer_source_t *) it->next == ts){ 129 log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 130 return; 131 } 132 if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) { 133 break; 134 } 135 } 136 ts->item.next = it->next; 137 it->next = (btstack_linked_item_t *) ts; 138 #endif 139 } 140 141 /** 142 * Remove timer from run loop 143 */ 144 static int btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){ 145 #ifdef TIMER_SUPPORT 146 return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); 147 #else 148 return 0; 149 #endif 150 } 151 152 static void btstack_run_loop_embedded_dump_timer(void){ 153 #ifdef TIMER_SUPPORT 154 #ifdef ENABLE_LOG_INFO 155 btstack_linked_item_t *it; 156 int i = 0; 157 for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ 158 btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 159 log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); 160 } 161 #endif 162 #endif 163 } 164 165 /** 166 * Execute run_loop once 167 */ 168 void btstack_run_loop_embedded_execute_once(void) { 169 btstack_data_source_t *ds; 170 171 // process data sources 172 btstack_data_source_t *next; 173 for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ 174 next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself 175 ds->process(ds); 176 } 177 178 #ifdef HAVE_TICK 179 uint32_t now = system_ticks; 180 #endif 181 #ifdef HAVE_TIME_MS 182 uint32_t now = hal_time_ms(); 183 #endif 184 #ifdef TIMER_SUPPORT 185 // process timers 186 while (timers) { 187 btstack_timer_source_t *ts = (btstack_timer_source_t *) timers; 188 if (ts->timeout > now) break; 189 btstack_run_loop_remove_timer(ts); 190 ts->process(ts); 191 } 192 #endif 193 194 // disable IRQs and check if run loop iteration has been requested. if not, go to sleep 195 hal_cpu_disable_irqs(); 196 if (trigger_event_received){ 197 trigger_event_received = 0; 198 hal_cpu_enable_irqs(); 199 } else { 200 hal_cpu_enable_irqs_and_sleep(); 201 } 202 } 203 204 /** 205 * Execute run_loop 206 */ 207 static void btstack_run_loop_embedded_execute(void) { 208 while (1) { 209 btstack_run_loop_embedded_execute_once(); 210 } 211 } 212 213 #ifdef HAVE_TICK 214 static void btstack_run_loop_embedded_tick_handler(void){ 215 system_ticks++; 216 trigger_event_received = 1; 217 } 218 219 uint32_t btstack_run_loop_embedded_get_ticks(void){ 220 return system_ticks; 221 } 222 223 uint32_t btstack_run_loop_embedded_ticks_for_ms(uint32_t time_in_ms){ 224 return time_in_ms / hal_tick_get_tick_period_in_ms(); 225 } 226 #endif 227 228 static uint32_t btstack_run_loop_embedded_get_time_ms(void){ 229 #ifdef HAVE_TIME_MS 230 return hal_time_ms(); 231 #endif 232 #ifdef HAVE_TICK 233 return system_ticks * hal_tick_get_tick_period_in_ms(); 234 #endif 235 return 0; 236 } 237 238 239 /** 240 * trigger run loop iteration 241 */ 242 void btstack_run_loop_embedded_trigger(void){ 243 trigger_event_received = 1; 244 } 245 246 static void btstack_run_loop_embedded_init(void){ 247 data_sources = NULL; 248 249 #ifdef TIMER_SUPPORT 250 timers = NULL; 251 #endif 252 253 #ifdef HAVE_TICK 254 system_ticks = 0; 255 hal_tick_init(); 256 hal_tick_set_handler(&btstack_run_loop_embedded_tick_handler); 257 #endif 258 } 259 260 /** 261 * Provide btstack_run_loop_embedded instance 262 */ 263 const btstack_run_loop_t * btstack_run_loop_embedded_get_instance(void){ 264 return &btstack_run_loop_embedded; 265 } 266 267 static const btstack_run_loop_t btstack_run_loop_embedded = { 268 &btstack_run_loop_embedded_init, 269 &btstack_run_loop_embedded_add_data_source, 270 &btstack_run_loop_embedded_remove_data_source, 271 &btstack_run_loop_embedded_set_timer, 272 &btstack_run_loop_embedded_add_timer, 273 &btstack_run_loop_embedded_remove_timer, 274 &btstack_run_loop_embedded_execute, 275 &btstack_run_loop_embedded_dump_timer, 276 &btstack_run_loop_embedded_get_time_ms, 277 }; 278