17d5f5399SMatthias Ringwald /* 27d5f5399SMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH 37d5f5399SMatthias Ringwald * 47d5f5399SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 57d5f5399SMatthias Ringwald * modification, are permitted provided that the following conditions 67d5f5399SMatthias Ringwald * are met: 77d5f5399SMatthias Ringwald * 87d5f5399SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 97d5f5399SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 107d5f5399SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 117d5f5399SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 127d5f5399SMatthias Ringwald * documentation and/or other materials provided with the distribution. 137d5f5399SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 147d5f5399SMatthias Ringwald * contributors may be used to endorse or promote products derived 157d5f5399SMatthias Ringwald * from this software without specific prior written permission. 167d5f5399SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 177d5f5399SMatthias Ringwald * personal benefit and not for any commercial purpose or for 187d5f5399SMatthias Ringwald * monetary gain. 197d5f5399SMatthias Ringwald * 207d5f5399SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 217d5f5399SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 227d5f5399SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 237d5f5399SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 247d5f5399SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 257d5f5399SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 267d5f5399SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 277d5f5399SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 287d5f5399SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 297d5f5399SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 307d5f5399SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 317d5f5399SMatthias Ringwald * SUCH DAMAGE. 327d5f5399SMatthias Ringwald * 337d5f5399SMatthias Ringwald * Please inquire about commercial licensing options at 347d5f5399SMatthias Ringwald * [email protected] 357d5f5399SMatthias Ringwald * 367d5f5399SMatthias Ringwald */ 377d5f5399SMatthias Ringwald 387d5f5399SMatthias Ringwald /* 397d5f5399SMatthias Ringwald * btstack_run_loop_freertos.c 407d5f5399SMatthias Ringwald * 417d5f5399SMatthias Ringwald * Run loop on dedicated thread on FreeRTOS 42828fd804SMatthias Ringwald * The run loop is triggered from other task/ISRs either via Event Groups 43828fd804SMatthias Ringwald * or Task Notifications if HAVE_FREERTOS_TASK_NOTIFICATIONS is defined 447d5f5399SMatthias Ringwald */ 457d5f5399SMatthias Ringwald 46e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_run_loop_freertos.c" 470c2f4953SMatthias Ringwald 487d5f5399SMatthias Ringwald #include <stddef.h> // NULL 497d5f5399SMatthias Ringwald 509155bf8cSMatthias Ringwald #include "btstack_run_loop_freertos.h" 519155bf8cSMatthias Ringwald 527d5f5399SMatthias Ringwald #include "btstack_linked_list.h" 537d5f5399SMatthias Ringwald #include "btstack_debug.h" 549155bf8cSMatthias Ringwald #include "btstack_util.h" 556d23ba05SMatthias Ringwald #include "hal_time_ms.h" 567d5f5399SMatthias Ringwald 576d23ba05SMatthias Ringwald // some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..) 586d23ba05SMatthias Ringwald // wih this flag, the headers are properly found 597d5f5399SMatthias Ringwald 606d23ba05SMatthias Ringwald #ifdef HAVE_FREERTOS_INCLUDE_PREFIX 617d5f5399SMatthias Ringwald #include "freertos/FreeRTOS.h" 627d5f5399SMatthias Ringwald #include "freertos/task.h" 637d5f5399SMatthias Ringwald #include "freertos/queue.h" 647d5f5399SMatthias Ringwald #include "freertos/event_groups.h" 656d23ba05SMatthias Ringwald #else 666d23ba05SMatthias Ringwald #include "FreeRTOS.h" 676d23ba05SMatthias Ringwald #include "task.h" 686d23ba05SMatthias Ringwald #include "queue.h" 696d23ba05SMatthias Ringwald #include "event_groups.h" 706d23ba05SMatthias Ringwald #endif 717d5f5399SMatthias Ringwald 727d5f5399SMatthias Ringwald typedef struct function_call { 737d5f5399SMatthias Ringwald void (*fn)(void * arg); 747d5f5399SMatthias Ringwald void * arg; 757d5f5399SMatthias Ringwald } function_call_t; 767d5f5399SMatthias Ringwald 770c9cde95SMatthias Ringwald // pick allocation style, prefer static 780c9cde95SMatthias Ringwald #if( configSUPPORT_STATIC_ALLOCATION == 1 ) 790c9cde95SMatthias Ringwald #define USE_STATIC_ALLOC 800c9cde95SMatthias Ringwald #elif( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) 810c9cde95SMatthias Ringwald // ok, nothing to do 820c9cde95SMatthias Ringwald #else 830c9cde95SMatthias Ringwald #error "Either configSUPPORT_STATIC_ALLOCATION or configSUPPORT_DYNAMIC_ALLOCATION in FreeRTOSConfig.h must be 1" 840c9cde95SMatthias Ringwald #endif 850c9cde95SMatthias Ringwald 860c9cde95SMatthias Ringwald // queue to receive events: up to 2 calls from transport, rest for app 870c9cde95SMatthias Ringwald #define RUN_LOOP_QUEUE_LENGTH 20 880c9cde95SMatthias Ringwald #define RUN_LOOP_QUEUE_ITEM_SIZE sizeof(function_call_t) 890c9cde95SMatthias Ringwald 900c9cde95SMatthias Ringwald #ifdef USE_STATIC_ALLOC 910c9cde95SMatthias Ringwald static StaticQueue_t btstack_run_loop_queue_object; 920c9cde95SMatthias Ringwald static uint8_t btstack_run_loop_queue_storage[ RUN_LOOP_QUEUE_LENGTH * RUN_LOOP_QUEUE_ITEM_SIZE ]; 930c9cde95SMatthias Ringwald #endif 940c9cde95SMatthias Ringwald 957d5f5399SMatthias Ringwald static QueueHandle_t btstack_run_loop_queue; 96828fd804SMatthias Ringwald static TaskHandle_t btstack_run_loop_task; 97f51d404eSMatthias Ringwald 98f51d404eSMatthias Ringwald #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS 997d5f5399SMatthias Ringwald static EventGroupHandle_t btstack_run_loop_event_group; 100828fd804SMatthias Ringwald #endif 1017d5f5399SMatthias Ringwald 1027d5f5399SMatthias Ringwald // bit 0 event group reserved to wakeup run loop 1037d5f5399SMatthias Ringwald #define EVENT_GROUP_FLAG_RUN_LOOP 1 1047d5f5399SMatthias Ringwald 1057d5f5399SMatthias Ringwald // the run loop 1069dc32eb4SMatthias Ringwald static bool run_loop_exit_requested; 1077d5f5399SMatthias Ringwald 1087d5f5399SMatthias Ringwald static uint32_t btstack_run_loop_freertos_get_time_ms(void){ 1097d5f5399SMatthias Ringwald return hal_time_ms(); 1107d5f5399SMatthias Ringwald } 1117d5f5399SMatthias Ringwald 1127d5f5399SMatthias Ringwald // set timer 1137d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 1147d5f5399SMatthias Ringwald ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; 1157d5f5399SMatthias Ringwald } 1167d5f5399SMatthias Ringwald 1177d5f5399SMatthias Ringwald // schedules execution from regular thread 1187d5f5399SMatthias Ringwald void btstack_run_loop_freertos_trigger(void){ 119828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 120828fd804SMatthias Ringwald xTaskNotify(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits); 121828fd804SMatthias Ringwald #else 1227d5f5399SMatthias Ringwald xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 123828fd804SMatthias Ringwald #endif 1247d5f5399SMatthias Ringwald } 1257d5f5399SMatthias Ringwald 1267d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){ 127f51d404eSMatthias Ringwald 128f51d404eSMatthias Ringwald // directly call function if already on btstack task 129f51d404eSMatthias Ringwald if (xTaskGetCurrentTaskHandle() == btstack_run_loop_task){ 130f51d404eSMatthias Ringwald (*fn)(arg); 131f51d404eSMatthias Ringwald return; 132f51d404eSMatthias Ringwald } 133f51d404eSMatthias Ringwald 1347d5f5399SMatthias Ringwald function_call_t message; 1357d5f5399SMatthias Ringwald message.fn = fn; 1367d5f5399SMatthias Ringwald message.arg = arg; 1377d5f5399SMatthias Ringwald BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY); 1387d5f5399SMatthias Ringwald if (res != pdTRUE){ 1397d5f5399SMatthias Ringwald log_error("Failed to post fn %p", fn); 1407d5f5399SMatthias Ringwald } 1417d5f5399SMatthias Ringwald btstack_run_loop_freertos_trigger(); 1427d5f5399SMatthias Ringwald } 1437d5f5399SMatthias Ringwald 144*d145c914SMatthias Ringwald #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1) 145*d145c914SMatthias Ringwald void btstack_run_loop_freertos_trigger_from_isr(void){ 146*d145c914SMatthias Ringwald BaseType_t xHigherPriorityTaskWoken; 147*d145c914SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 148*d145c914SMatthias Ringwald xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken); 149*d145c914SMatthias Ringwald if (xHigherPriorityTaskWoken) { 150*d145c914SMatthias Ringwald #ifdef ESP_PLATFORM 151*d145c914SMatthias Ringwald portYIELD_FROM_ISR(); 152*d145c914SMatthias Ringwald #else 153*d145c914SMatthias Ringwald portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); 154*d145c914SMatthias Ringwald #endif 155*d145c914SMatthias Ringwald } 156*d145c914SMatthias Ringwald #else 157*d145c914SMatthias Ringwald xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken); 158*d145c914SMatthias Ringwald #endif 159*d145c914SMatthias Ringwald } 160*d145c914SMatthias Ringwald 1617d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){ 1627d5f5399SMatthias Ringwald function_call_t message; 1637d5f5399SMatthias Ringwald message.fn = fn; 1647d5f5399SMatthias Ringwald message.arg = arg; 1657d5f5399SMatthias Ringwald BaseType_t xHigherPriorityTaskWoken; 1667d5f5399SMatthias Ringwald xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken); 1677d5f5399SMatthias Ringwald btstack_run_loop_freertos_trigger_from_isr(); 1687d5f5399SMatthias Ringwald } 1697d5f5399SMatthias Ringwald #endif 1707d5f5399SMatthias Ringwald 1719dc32eb4SMatthias Ringwald void btstack_run_loop_freertos_trigger_exit(void){ 1729dc32eb4SMatthias Ringwald run_loop_exit_requested = true; 1739dc32eb4SMatthias Ringwald } 1749dc32eb4SMatthias Ringwald 1757d5f5399SMatthias Ringwald /** 1767d5f5399SMatthias Ringwald * Execute run_loop 1777d5f5399SMatthias Ringwald */ 1784a76e901SMatthias Ringwald static void btstack_run_loop_freertos_execute(void) { 1797d5f5399SMatthias Ringwald log_debug("RL: execute"); 1807d5f5399SMatthias Ringwald 1819dc32eb4SMatthias Ringwald run_loop_exit_requested = false; 1829dc32eb4SMatthias Ringwald 183ff3cc4a5SMatthias Ringwald while (true) { 1847d5f5399SMatthias Ringwald 1857d5f5399SMatthias Ringwald // process data sources 186cd5f23a3SMatthias Ringwald btstack_run_loop_base_poll_data_sources(); 1877d5f5399SMatthias Ringwald 1887d5f5399SMatthias Ringwald // process registered function calls on run loop thread 189ff3cc4a5SMatthias Ringwald while (true){ 1907d5f5399SMatthias Ringwald function_call_t message = { NULL, NULL }; 1917d5f5399SMatthias Ringwald BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0); 1927d5f5399SMatthias Ringwald if (res == pdFALSE) break; 1937d5f5399SMatthias Ringwald if (message.fn){ 1947d5f5399SMatthias Ringwald message.fn(message.arg); 1957d5f5399SMatthias Ringwald } 1967d5f5399SMatthias Ringwald } 1977d5f5399SMatthias Ringwald 198cd5f23a3SMatthias Ringwald // process timers 1997d5f5399SMatthias Ringwald uint32_t now = btstack_run_loop_freertos_get_time_ms(); 200cd5f23a3SMatthias Ringwald btstack_run_loop_base_process_timers(now); 2017d5f5399SMatthias Ringwald 2029dc32eb4SMatthias Ringwald // exit triggered by btstack_run_loop_freertos_trigger_exit (from data source, timer, run on main thread) 2039dc32eb4SMatthias Ringwald if (run_loop_exit_requested) break; 2049dc32eb4SMatthias Ringwald 205828fd804SMatthias Ringwald // wait for timeout or event group/task notification 206cd5f23a3SMatthias Ringwald int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now); 207cd5f23a3SMatthias Ringwald 208cd5f23a3SMatthias Ringwald uint32_t timeout_ms = portMAX_DELAY; 209cd5f23a3SMatthias Ringwald if (timeout_next_timer_ms >= 0){ 210cd5f23a3SMatthias Ringwald timeout_ms = (uint32_t) timeout_next_timer_ms; 211cd5f23a3SMatthias Ringwald } 212cd5f23a3SMatthias Ringwald 2137d5f5399SMatthias Ringwald log_debug("RL: wait with timeout %u", (int) timeout_ms); 214828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS 215828fd804SMatthias Ringwald xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms)); 216828fd804SMatthias Ringwald #else 2177d5f5399SMatthias Ringwald xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms)); 218828fd804SMatthias Ringwald #endif 2197d5f5399SMatthias Ringwald } 2207d5f5399SMatthias Ringwald } 2217d5f5399SMatthias Ringwald 2227d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_init(void){ 223cd5f23a3SMatthias Ringwald btstack_run_loop_base_init(); 2247d5f5399SMatthias Ringwald 2250c9cde95SMatthias Ringwald #ifdef USE_STATIC_ALLOC 2260c9cde95SMatthias Ringwald btstack_run_loop_queue = xQueueCreateStatic(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE, btstack_run_loop_queue_storage, &btstack_run_loop_queue_object); 2270c9cde95SMatthias Ringwald #else 2280c9cde95SMatthias Ringwald btstack_run_loop_queue = xQueueCreate(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE); 2290c9cde95SMatthias Ringwald #endif 2307d5f5399SMatthias Ringwald 231828fd804SMatthias Ringwald #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS 2327d5f5399SMatthias Ringwald // event group to wake run loop 2337d5f5399SMatthias Ringwald btstack_run_loop_event_group = xEventGroupCreate(); 234828fd804SMatthias Ringwald #endif 2357d5f5399SMatthias Ringwald 236f51d404eSMatthias Ringwald // task to handle to optimize 'run on main thread' 237a6f770a0SMatthias Ringwald btstack_run_loop_task = xTaskGetCurrentTaskHandle(); 238a6f770a0SMatthias Ringwald 239f51d404eSMatthias Ringwald log_info("run loop init, task %p, queue item size %u", btstack_run_loop_task, (int) sizeof(function_call_t)); 2407d5f5399SMatthias Ringwald } 2417d5f5399SMatthias Ringwald 2427d5f5399SMatthias Ringwald /** 2437d5f5399SMatthias Ringwald * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 2447d5f5399SMatthias Ringwald */ 2457d5f5399SMatthias Ringwald 2467d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos = { 2477d5f5399SMatthias Ringwald &btstack_run_loop_freertos_init, 248cd5f23a3SMatthias Ringwald &btstack_run_loop_base_add_data_source, 249cd5f23a3SMatthias Ringwald &btstack_run_loop_base_remove_data_source, 250cd5f23a3SMatthias Ringwald &btstack_run_loop_base_enable_data_source_callbacks, 251cd5f23a3SMatthias Ringwald &btstack_run_loop_base_disable_data_source_callbacks, 2527d5f5399SMatthias Ringwald &btstack_run_loop_freertos_set_timer, 253cd5f23a3SMatthias Ringwald &btstack_run_loop_base_add_timer, 254cd5f23a3SMatthias Ringwald &btstack_run_loop_base_remove_timer, 2557d5f5399SMatthias Ringwald &btstack_run_loop_freertos_execute, 256cd5f23a3SMatthias Ringwald &btstack_run_loop_base_dump_timer, 2577d5f5399SMatthias Ringwald &btstack_run_loop_freertos_get_time_ms, 2587d5f5399SMatthias Ringwald }; 2593c4cc642SMatthias Ringwald 2603c4cc642SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){ 2613c4cc642SMatthias Ringwald return &btstack_run_loop_freertos; 2623c4cc642SMatthias Ringwald } 263