xref: /btstack/platform/freertos/btstack_run_loop_freertos.c (revision 4fd33db7036b1487a4b368055df28fd1c2023015)
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 
144d145c914SMatthias Ringwald #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1)
145*4fd33db7SMatthias Ringwald static void btstack_run_loop_freertos_poll_data_sources_from_irq(void){
146d145c914SMatthias Ringwald     BaseType_t xHigherPriorityTaskWoken;
147d145c914SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
148d145c914SMatthias Ringwald     xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken);
149d145c914SMatthias Ringwald     if (xHigherPriorityTaskWoken) {
150d145c914SMatthias Ringwald #ifdef ESP_PLATFORM
151d145c914SMatthias Ringwald         portYIELD_FROM_ISR();
152d145c914SMatthias Ringwald #else
153d145c914SMatthias Ringwald         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
154d145c914SMatthias Ringwald #endif
155d145c914SMatthias Ringwald     }
156d145c914SMatthias Ringwald #else
157d145c914SMatthias Ringwald     xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken);
158d145c914SMatthias Ringwald #endif
159d145c914SMatthias Ringwald }
160*4fd33db7SMatthias Ringwald void btstack_run_loop_freertos_trigger_from_isr(void){
161*4fd33db7SMatthias Ringwald     btstack_run_loop_freertos_trigger_from_isr();
162*4fd33db7SMatthias Ringwald }
1637d5f5399SMatthias Ringwald #endif
1647d5f5399SMatthias Ringwald 
1659dc32eb4SMatthias Ringwald void btstack_run_loop_freertos_trigger_exit(void){
1669dc32eb4SMatthias Ringwald     run_loop_exit_requested = true;
1679dc32eb4SMatthias Ringwald }
1689dc32eb4SMatthias Ringwald 
1697d5f5399SMatthias Ringwald /**
1707d5f5399SMatthias Ringwald  * Execute run_loop
1717d5f5399SMatthias Ringwald  */
1724a76e901SMatthias Ringwald static void btstack_run_loop_freertos_execute(void) {
1737d5f5399SMatthias Ringwald     log_debug("RL: execute");
1747d5f5399SMatthias Ringwald 
1759dc32eb4SMatthias Ringwald     run_loop_exit_requested = false;
1769dc32eb4SMatthias Ringwald 
177ff3cc4a5SMatthias Ringwald     while (true) {
1787d5f5399SMatthias Ringwald 
1797d5f5399SMatthias Ringwald         // process data sources
180cd5f23a3SMatthias Ringwald         btstack_run_loop_base_poll_data_sources();
1817d5f5399SMatthias Ringwald 
1827d5f5399SMatthias Ringwald         // process registered function calls on run loop thread
183ff3cc4a5SMatthias Ringwald         while (true){
1847d5f5399SMatthias Ringwald             function_call_t message = { NULL, NULL };
1857d5f5399SMatthias Ringwald             BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0);
1867d5f5399SMatthias Ringwald             if (res == pdFALSE) break;
1877d5f5399SMatthias Ringwald             if (message.fn){
1887d5f5399SMatthias Ringwald                 message.fn(message.arg);
1897d5f5399SMatthias Ringwald             }
1907d5f5399SMatthias Ringwald         }
1917d5f5399SMatthias Ringwald 
192cd5f23a3SMatthias Ringwald         // process timers
1937d5f5399SMatthias Ringwald         uint32_t now = btstack_run_loop_freertos_get_time_ms();
194cd5f23a3SMatthias Ringwald         btstack_run_loop_base_process_timers(now);
1957d5f5399SMatthias Ringwald 
1969dc32eb4SMatthias Ringwald         // exit triggered by btstack_run_loop_freertos_trigger_exit (from data source, timer, run on main thread)
1979dc32eb4SMatthias Ringwald         if (run_loop_exit_requested) break;
1989dc32eb4SMatthias Ringwald 
199828fd804SMatthias Ringwald         // wait for timeout or event group/task notification
200cd5f23a3SMatthias Ringwald         int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now);
201cd5f23a3SMatthias Ringwald 
202cd5f23a3SMatthias Ringwald         uint32_t timeout_ms = portMAX_DELAY;
203cd5f23a3SMatthias Ringwald         if (timeout_next_timer_ms >= 0){
204cd5f23a3SMatthias Ringwald             timeout_ms = (uint32_t) timeout_next_timer_ms;
205cd5f23a3SMatthias Ringwald         }
206cd5f23a3SMatthias Ringwald 
2077d5f5399SMatthias Ringwald         log_debug("RL: wait with timeout %u", (int) timeout_ms);
208828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
209828fd804SMatthias Ringwald         xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms));
210828fd804SMatthias Ringwald #else
2117d5f5399SMatthias Ringwald         xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms));
212828fd804SMatthias Ringwald #endif
2137d5f5399SMatthias Ringwald     }
2147d5f5399SMatthias Ringwald }
2157d5f5399SMatthias Ringwald 
2167d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_init(void){
217cd5f23a3SMatthias Ringwald     btstack_run_loop_base_init();
2187d5f5399SMatthias Ringwald 
2190c9cde95SMatthias Ringwald #ifdef USE_STATIC_ALLOC
2200c9cde95SMatthias 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);
2210c9cde95SMatthias Ringwald #else
2220c9cde95SMatthias Ringwald     btstack_run_loop_queue = xQueueCreate(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE);
2230c9cde95SMatthias Ringwald #endif
2247d5f5399SMatthias Ringwald 
225828fd804SMatthias Ringwald #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS
2267d5f5399SMatthias Ringwald     // event group to wake run loop
2277d5f5399SMatthias Ringwald     btstack_run_loop_event_group = xEventGroupCreate();
228828fd804SMatthias Ringwald #endif
2297d5f5399SMatthias Ringwald 
230f51d404eSMatthias Ringwald     // task to handle to optimize 'run on main thread'
231a6f770a0SMatthias Ringwald     btstack_run_loop_task = xTaskGetCurrentTaskHandle();
232a6f770a0SMatthias Ringwald 
233f51d404eSMatthias Ringwald     log_info("run loop init, task %p, queue item size %u", btstack_run_loop_task, (int) sizeof(function_call_t));
2347d5f5399SMatthias Ringwald }
2357d5f5399SMatthias Ringwald 
2367d5f5399SMatthias Ringwald /**
2377d5f5399SMatthias Ringwald  * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
2387d5f5399SMatthias Ringwald  */
2397d5f5399SMatthias Ringwald 
2407d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos = {
2417d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_init,
242cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_add_data_source,
243cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_remove_data_source,
244cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_enable_data_source_callbacks,
245cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_disable_data_source_callbacks,
2467d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_set_timer,
247cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_add_timer,
248cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_remove_timer,
2497d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_execute,
250cd5f23a3SMatthias Ringwald     &btstack_run_loop_base_dump_timer,
2517d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_get_time_ms,
252*4fd33db7SMatthias Ringwald #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1)
253*4fd33db7SMatthias Ringwald     &btstack_run_loop_freertos_poll_data_sources_from_irq,
254*4fd33db7SMatthias Ringwald #else
255*4fd33db7SMatthias Ringwald     NULL,
256*4fd33db7SMatthias Ringwald #endif
2577d5f5399SMatthias Ringwald };
2583c4cc642SMatthias Ringwald 
2593c4cc642SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){
2603c4cc642SMatthias Ringwald     return &btstack_run_loop_freertos;
2613c4cc642SMatthias Ringwald }
262