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_run_loop_freertos.h" 51 52 #include "btstack_run_loop_base.h" 53 #include "btstack_linked_list.h" 54 #include "btstack_debug.h" 55 #include "btstack_util.h" 56 #include "hal_time_ms.h" 57 58 // some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..) 59 // wih this flag, the headers are properly found 60 61 #ifdef HAVE_FREERTOS_INCLUDE_PREFIX 62 #include "freertos/FreeRTOS.h" 63 #include "freertos/task.h" 64 #include "freertos/queue.h" 65 #include "freertos/event_groups.h" 66 #else 67 #include "FreeRTOS.h" 68 #include "task.h" 69 #include "queue.h" 70 #include "event_groups.h" 71 #endif 72 73 typedef struct function_call { 74 void (*fn)(void * arg); 75 void * arg; 76 } function_call_t; 77 78 // pick allocation style, prefer static 79 #if( configSUPPORT_STATIC_ALLOCATION == 1 ) 80 #define USE_STATIC_ALLOC 81 #elif( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) 82 // ok, nothing to do 83 #else 84 #error "Either configSUPPORT_STATIC_ALLOCATION or configSUPPORT_DYNAMIC_ALLOCATION in FreeRTOSConfig.h must be 1" 85 #endif 86 87 // queue to receive events: up to 2 calls from transport, rest for app 88 #define RUN_LOOP_QUEUE_LENGTH 20 89 #define RUN_LOOP_QUEUE_ITEM_SIZE sizeof(function_call_t) 90 91 #ifdef USE_STATIC_ALLOC 92 static StaticQueue_t btstack_run_loop_queue_object; 93 static uint8_t btstack_run_loop_queue_storage[ RUN_LOOP_QUEUE_LENGTH * RUN_LOOP_QUEUE_ITEM_SIZE ]; 94 #endif 95 96 static QueueHandle_t btstack_run_loop_queue; 97 static TaskHandle_t btstack_run_loop_task; 98 99 #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS 100 static EventGroupHandle_t btstack_run_loop_event_group; 101 #endif 102 103 // bit 0 event group reserved to wakeup run loop 104 #define EVENT_GROUP_FLAG_RUN_LOOP 1 105 106 // the run loop 107 static bool run_loop_exit_requested; 108 109 static uint32_t btstack_run_loop_freertos_get_time_ms(void){ 110 return hal_time_ms(); 111 } 112 113 // set timer 114 static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 115 ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; 116 } 117 118 // schedules execution from regular thread 119 void btstack_run_loop_freertos_trigger(void){ 120 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 121 xTaskNotify(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits); 122 #else 123 xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 124 #endif 125 } 126 127 void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){ 128 129 // directly call function if already on btstack task 130 if (xTaskGetCurrentTaskHandle() == btstack_run_loop_task){ 131 (*fn)(arg); 132 return; 133 } 134 135 function_call_t message; 136 message.fn = fn; 137 message.arg = arg; 138 BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY); 139 if (res != pdTRUE){ 140 log_error("Failed to post fn %p", fn); 141 } 142 btstack_run_loop_freertos_trigger(); 143 } 144 145 #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1) 146 void btstack_run_loop_freertos_trigger_from_isr(void){ 147 BaseType_t xHigherPriorityTaskWoken; 148 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 149 xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken); 150 if (xHigherPriorityTaskWoken) { 151 #ifdef ESP_PLATFORM 152 portYIELD_FROM_ISR(); 153 #else 154 portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); 155 #endif 156 } 157 #else 158 xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken); 159 #endif 160 } 161 162 void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){ 163 function_call_t message; 164 message.fn = fn; 165 message.arg = arg; 166 BaseType_t xHigherPriorityTaskWoken; 167 xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken); 168 btstack_run_loop_freertos_trigger_from_isr(); 169 } 170 #endif 171 172 void btstack_run_loop_freertos_trigger_exit(void){ 173 run_loop_exit_requested = true; 174 } 175 176 /** 177 * Execute run_loop 178 */ 179 static void btstack_run_loop_freertos_execute(void) { 180 log_debug("RL: execute"); 181 182 run_loop_exit_requested = false; 183 184 while (true) { 185 186 // process data sources 187 btstack_run_loop_base_poll_data_sources(); 188 189 // process registered function calls on run loop thread 190 while (true){ 191 function_call_t message = { NULL, NULL }; 192 BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0); 193 if (res == pdFALSE) break; 194 if (message.fn){ 195 message.fn(message.arg); 196 } 197 } 198 199 // process timers 200 uint32_t now = btstack_run_loop_freertos_get_time_ms(); 201 btstack_run_loop_base_process_timers(now); 202 203 // exit triggered by btstack_run_loop_freertos_trigger_exit (from data source, timer, run on main thread) 204 if (run_loop_exit_requested) break; 205 206 // wait for timeout or event group/task notification 207 int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now); 208 209 uint32_t timeout_ms = portMAX_DELAY; 210 if (timeout_next_timer_ms >= 0){ 211 timeout_ms = (uint32_t) timeout_next_timer_ms; 212 } 213 214 log_debug("RL: wait with timeout %u", (int) timeout_ms); 215 #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 216 xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms)); 217 #else 218 xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms)); 219 #endif 220 } 221 } 222 223 static void btstack_run_loop_freertos_init(void){ 224 btstack_run_loop_base_init(); 225 226 #ifdef USE_STATIC_ALLOC 227 btstack_run_loop_queue = xQueueCreateStatic(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE, btstack_run_loop_queue_storage, &btstack_run_loop_queue_object); 228 #else 229 btstack_run_loop_queue = xQueueCreate(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE); 230 #endif 231 232 #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS 233 // event group to wake run loop 234 btstack_run_loop_event_group = xEventGroupCreate(); 235 #endif 236 237 // task to handle to optimize 'run on main thread' 238 btstack_run_loop_task = xTaskGetCurrentTaskHandle(); 239 240 log_info("run loop init, task %p, queue item size %u", btstack_run_loop_task, (int) sizeof(function_call_t)); 241 } 242 243 /** 244 * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 245 */ 246 247 static const btstack_run_loop_t btstack_run_loop_freertos = { 248 &btstack_run_loop_freertos_init, 249 &btstack_run_loop_base_add_data_source, 250 &btstack_run_loop_base_remove_data_source, 251 &btstack_run_loop_base_enable_data_source_callbacks, 252 &btstack_run_loop_base_disable_data_source_callbacks, 253 &btstack_run_loop_freertos_set_timer, 254 &btstack_run_loop_base_add_timer, 255 &btstack_run_loop_base_remove_timer, 256 &btstack_run_loop_freertos_execute, 257 &btstack_run_loop_base_dump_timer, 258 &btstack_run_loop_freertos_get_time_ms, 259 }; 260 261 const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){ 262 return &btstack_run_loop_freertos; 263 } 264