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