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