1 /* 2 * Copyright (C) 2017 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_freertos.c 40 * 41 * Run loop on dedicated thread on FreeRTOS 42 * The run loop is triggered from other task/ISRs either via Event Groups 43 * or Task Notifications if HAVE_FREERTOS_TASK_NOTIFICATIONS is defined 44 */ 45 46 #define BTSTACK_FILE__ "btstack_run_loop_freertos.c" 47 48 #include <stddef.h> // NULL 49 50 #include "btstack_linked_list.h" 51 #include "btstack_debug.h" 52 #include "btstack_run_loop_freertos.h" 53 #include "hal_time_ms.h" 54 55 // some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..) 56 // wih this flag, the headers are properly found 57 58 #ifdef HAVE_FREERTOS_INCLUDE_PREFIX 59 #include "freertos/FreeRTOS.h" 60 #include "freertos/task.h" 61 #include "freertos/queue.h" 62 #include "freertos/event_groups.h" 63 #else 64 #include "FreeRTOS.h" 65 #include "task.h" 66 #include "queue.h" 67 #include "event_groups.h" 68 #endif 69 70 typedef struct function_call { 71 void (*fn)(void * arg); 72 void * arg; 73 } function_call_t; 74 75 static const btstack_run_loop_t btstack_run_loop_freertos; 76 77 static QueueHandle_t btstack_run_loop_queue; 78 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 79 static TaskHandle_t btstack_run_loop_task; 80 #else 81 static EventGroupHandle_t btstack_run_loop_event_group; 82 #endif 83 84 // bit 0 event group reserved to wakeup run loop 85 #define EVENT_GROUP_FLAG_RUN_LOOP 1 86 87 // the run loop 88 static btstack_linked_list_t timers; 89 static btstack_linked_list_t data_sources; 90 91 static uint32_t btstack_run_loop_freertos_get_time_ms(void){ 92 return hal_time_ms(); 93 } 94 95 // set timer 96 static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 97 ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; 98 } 99 100 /** 101 * Add timer to run_loop (keep list sorted) 102 */ 103 static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){ 104 btstack_linked_item_t *it; 105 for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ 106 // don't add timer that's already in there 107 if ((btstack_timer_source_t *) it->next == ts){ 108 log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 109 return; 110 } 111 if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) { 112 break; 113 } 114 } 115 ts->item.next = it->next; 116 it->next = (btstack_linked_item_t *) ts; 117 } 118 119 /** 120 * Remove timer from run loop 121 */ 122 static int btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){ 123 return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); 124 } 125 126 static void btstack_run_loop_freertos_dump_timer(void){ 127 #ifdef ENABLE_LOG_INFO 128 btstack_linked_item_t *it; 129 int i = 0; 130 for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ 131 btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 132 log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); 133 } 134 #endif 135 } 136 137 // schedules execution from regular thread 138 void btstack_run_loop_freertos_trigger(void){ 139 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 140 xTaskNotify(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits); 141 #else 142 xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 143 #endif 144 } 145 146 void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){ 147 function_call_t message; 148 message.fn = fn; 149 message.arg = arg; 150 BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY); 151 if (res != pdTRUE){ 152 log_error("Failed to post fn %p", fn); 153 } 154 btstack_run_loop_freertos_trigger(); 155 } 156 157 #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1) 158 void btstack_run_loop_freertos_trigger_from_isr(void){ 159 BaseType_t xHigherPriorityTaskWoken; 160 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 161 xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken); 162 if (xHigherPriorityTaskWoken) { 163 #ifdef ESP_PLATFORM 164 portYIELD_FROM_ISR(); 165 #else 166 portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); 167 #endif 168 } 169 #else 170 xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken); 171 #endif 172 } 173 174 void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){ 175 function_call_t message; 176 message.fn = fn; 177 message.arg = arg; 178 BaseType_t xHigherPriorityTaskWoken; 179 xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken); 180 btstack_run_loop_freertos_trigger_from_isr(); 181 } 182 #endif 183 184 /** 185 * Execute run_loop 186 */ 187 static void btstack_run_loop_freertos_execute(void) { 188 log_debug("RL: execute"); 189 190 while (1) { 191 192 // process data sources 193 btstack_data_source_t *ds; 194 btstack_data_source_t *next; 195 for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ 196 next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself 197 if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ 198 ds->process(ds, DATA_SOURCE_CALLBACK_POLL); 199 } 200 } 201 202 // process registered function calls on run loop thread 203 while (1){ 204 function_call_t message = { NULL, NULL }; 205 BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0); 206 if (res == pdFALSE) break; 207 if (message.fn){ 208 message.fn(message.arg); 209 } 210 } 211 212 // process timers and get et next timeout 213 uint32_t timeout_ms = portMAX_DELAY; 214 log_debug("RL: portMAX_DELAY %u", portMAX_DELAY); 215 while (timers) { 216 btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; 217 uint32_t now = btstack_run_loop_freertos_get_time_ms(); 218 log_debug("RL: now %u, expires %u", now, ts->timeout); 219 if (ts->timeout > now){ 220 timeout_ms = ts->timeout - now; 221 break; 222 } 223 // remove timer before processing it to allow handler to re-register with run loop 224 btstack_run_loop_freertos_remove_timer(ts); 225 log_debug("RL: first timer %p", ts->process); 226 ts->process(ts); 227 } 228 229 // wait for timeout or event group/task notification 230 log_debug("RL: wait with timeout %u", (int) timeout_ms); 231 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 232 xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms)); 233 #else 234 xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms)); 235 #endif 236 } 237 } 238 239 static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){ 240 btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); 241 } 242 243 static int btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){ 244 return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); 245 } 246 247 static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 248 ds->flags |= callback_types; 249 } 250 251 static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 252 ds->flags &= ~callback_types; 253 } 254 255 static void btstack_run_loop_freertos_init(void){ 256 timers = NULL; 257 258 // queue to receive events: up to 2 calls from transport, up to 3 for app 259 btstack_run_loop_queue = xQueueCreate(20, sizeof(function_call_t)); 260 261 #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS 262 // event group to wake run loop 263 btstack_run_loop_event_group = xEventGroupCreate(); 264 #endif 265 266 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 267 btstack_run_loop_task = xTaskGetCurrentTaskHandle(); 268 log_info("run loop task %p", btstack_run_loop_task); 269 #endif 270 271 log_info("run loop init, queue item size %u", (int) sizeof(function_call_t)); 272 } 273 274 /** 275 * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 276 */ 277 const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){ 278 return &btstack_run_loop_freertos; 279 } 280 281 static const btstack_run_loop_t btstack_run_loop_freertos = { 282 &btstack_run_loop_freertos_init, 283 &btstack_run_loop_freertos_add_data_source, 284 &btstack_run_loop_freertos_remove_data_source, 285 &btstack_run_loop_freertos_enable_data_source_callbacks, 286 &btstack_run_loop_freertos_disable_data_source_callbacks, 287 &btstack_run_loop_freertos_set_timer, 288 &btstack_run_loop_freertos_add_timer, 289 &btstack_run_loop_freertos_remove_timer, 290 &btstack_run_loop_freertos_execute, 291 &btstack_run_loop_freertos_dump_timer, 292 &btstack_run_loop_freertos_get_time_ms, 293 }; 294