xref: /btstack/platform/freertos/btstack_run_loop_freertos.c (revision 0c2f495327059726d32fda30be2d13e92c936f09)
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 
46*0c2f4953SMatthias Ringwald #define __BTSTACK_FILE__ "btstack_run_loop_freertos.c"
47*0c2f4953SMatthias Ringwald 
487d5f5399SMatthias Ringwald #include <stddef.h> // NULL
497d5f5399SMatthias Ringwald 
507d5f5399SMatthias Ringwald #include "btstack_linked_list.h"
517d5f5399SMatthias Ringwald #include "btstack_debug.h"
527d5f5399SMatthias Ringwald #include "btstack_run_loop_freertos.h"
537d5f5399SMatthias Ringwald 
547d5f5399SMatthias Ringwald // #include "hal_time_ms.h"
557d5f5399SMatthias Ringwald uint32_t hal_time_ms(void);
567d5f5399SMatthias Ringwald 
577d5f5399SMatthias Ringwald #include "freertos/FreeRTOS.h"
587d5f5399SMatthias Ringwald #include "freertos/task.h"
597d5f5399SMatthias Ringwald #include "freertos/queue.h"
607d5f5399SMatthias Ringwald #include "freertos/event_groups.h"
617d5f5399SMatthias Ringwald 
627d5f5399SMatthias Ringwald typedef struct function_call {
637d5f5399SMatthias Ringwald     void (*fn)(void * arg);
647d5f5399SMatthias Ringwald     void * arg;
657d5f5399SMatthias Ringwald } function_call_t;
667d5f5399SMatthias Ringwald 
677d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos;
687d5f5399SMatthias Ringwald 
697d5f5399SMatthias Ringwald static QueueHandle_t        btstack_run_loop_queue;
70828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
71828fd804SMatthias Ringwald static TaskHandle_t    		btstack_run_loop_task;
72828fd804SMatthias Ringwald #else
737d5f5399SMatthias Ringwald static EventGroupHandle_t   btstack_run_loop_event_group;
74828fd804SMatthias Ringwald #endif
757d5f5399SMatthias Ringwald 
767d5f5399SMatthias Ringwald // bit 0 event group reserved to wakeup run loop
777d5f5399SMatthias Ringwald #define EVENT_GROUP_FLAG_RUN_LOOP 1
787d5f5399SMatthias Ringwald 
797d5f5399SMatthias Ringwald // the run loop
807d5f5399SMatthias Ringwald static btstack_linked_list_t timers;
817d5f5399SMatthias Ringwald static btstack_linked_list_t data_sources;
827d5f5399SMatthias Ringwald 
837d5f5399SMatthias Ringwald static uint32_t btstack_run_loop_freertos_get_time_ms(void){
847d5f5399SMatthias Ringwald     return hal_time_ms();
857d5f5399SMatthias Ringwald }
867d5f5399SMatthias Ringwald 
877d5f5399SMatthias Ringwald // set timer
887d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
897d5f5399SMatthias Ringwald     ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1;
907d5f5399SMatthias Ringwald }
917d5f5399SMatthias Ringwald 
927d5f5399SMatthias Ringwald /**
937d5f5399SMatthias Ringwald  * Add timer to run_loop (keep list sorted)
947d5f5399SMatthias Ringwald  */
957d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){
967d5f5399SMatthias Ringwald     btstack_linked_item_t *it;
977d5f5399SMatthias Ringwald     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
987d5f5399SMatthias Ringwald         // don't add timer that's already in there
997d5f5399SMatthias Ringwald         if ((btstack_timer_source_t *) it->next == ts){
1007d5f5399SMatthias Ringwald             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
1017d5f5399SMatthias Ringwald             return;
1027d5f5399SMatthias Ringwald         }
1037d5f5399SMatthias Ringwald         if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) {
1047d5f5399SMatthias Ringwald             break;
1057d5f5399SMatthias Ringwald         }
1067d5f5399SMatthias Ringwald     }
1077d5f5399SMatthias Ringwald     ts->item.next = it->next;
1087d5f5399SMatthias Ringwald     it->next = (btstack_linked_item_t *) ts;
1097d5f5399SMatthias Ringwald }
1107d5f5399SMatthias Ringwald 
1117d5f5399SMatthias Ringwald /**
1127d5f5399SMatthias Ringwald  * Remove timer from run loop
1137d5f5399SMatthias Ringwald  */
1147d5f5399SMatthias Ringwald static int btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){
1157d5f5399SMatthias Ringwald     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
1167d5f5399SMatthias Ringwald }
1177d5f5399SMatthias Ringwald 
1187d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_dump_timer(void){
1197d5f5399SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1207d5f5399SMatthias Ringwald     btstack_linked_item_t *it;
1217d5f5399SMatthias Ringwald     int i = 0;
1227d5f5399SMatthias Ringwald     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
1237d5f5399SMatthias Ringwald         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
1247d5f5399SMatthias Ringwald         log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
1257d5f5399SMatthias Ringwald     }
1267d5f5399SMatthias Ringwald #endif
1277d5f5399SMatthias Ringwald }
1287d5f5399SMatthias Ringwald 
1297d5f5399SMatthias Ringwald // schedules execution from regular thread
1307d5f5399SMatthias Ringwald void btstack_run_loop_freertos_trigger(void){
131828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
132828fd804SMatthias Ringwald     xTaskNotify(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits);
133828fd804SMatthias Ringwald #else
1347d5f5399SMatthias Ringwald     xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP);
135828fd804SMatthias Ringwald #endif
1367d5f5399SMatthias Ringwald }
1377d5f5399SMatthias Ringwald 
1387d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){
1397d5f5399SMatthias Ringwald     function_call_t message;
1407d5f5399SMatthias Ringwald     message.fn  = fn;
1417d5f5399SMatthias Ringwald     message.arg = arg;
1427d5f5399SMatthias Ringwald     BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY);
1437d5f5399SMatthias Ringwald     if (res != pdTRUE){
1447d5f5399SMatthias Ringwald         log_error("Failed to post fn %p", fn);
1457d5f5399SMatthias Ringwald     }
1467d5f5399SMatthias Ringwald     btstack_run_loop_freertos_trigger();
1477d5f5399SMatthias Ringwald }
1487d5f5399SMatthias Ringwald 
149828fd804SMatthias Ringwald #if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1)
1507d5f5399SMatthias Ringwald void btstack_run_loop_freertos_trigger_from_isr(void){
1513b1180c9SMatthias Ringwald     BaseType_t xHigherPriorityTaskWoken;
152828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
153828fd804SMatthias Ringwald     xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken);
154828fd804SMatthias Ringwald     if (xHigherPriorityTaskWoken) {
155828fd804SMatthias Ringwald         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
156828fd804SMatthias Ringwald     }
157828fd804SMatthias Ringwald #else
1583b1180c9SMatthias Ringwald     xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken);
159828fd804SMatthias Ringwald #endif
1607d5f5399SMatthias Ringwald }
1617d5f5399SMatthias Ringwald 
1627d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){
1637d5f5399SMatthias Ringwald     function_call_t message;
1647d5f5399SMatthias Ringwald     message.fn  = fn;
1657d5f5399SMatthias Ringwald     message.arg = arg;
1667d5f5399SMatthias Ringwald     BaseType_t xHigherPriorityTaskWoken;
1677d5f5399SMatthias Ringwald     xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken);
1687d5f5399SMatthias Ringwald     btstack_run_loop_freertos_trigger_from_isr();
1697d5f5399SMatthias Ringwald }
1707d5f5399SMatthias Ringwald #endif
1717d5f5399SMatthias Ringwald 
1727d5f5399SMatthias Ringwald /**
1737d5f5399SMatthias Ringwald  * Execute run_loop
1747d5f5399SMatthias Ringwald  */
1754a76e901SMatthias Ringwald static void btstack_run_loop_freertos_execute(void) {
1767d5f5399SMatthias Ringwald     log_debug("RL: execute");
1777d5f5399SMatthias Ringwald 
178828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
179828fd804SMatthias Ringwald     btstack_run_loop_task = xTaskGetCurrentTaskHandle();
180828fd804SMatthias Ringwald #endif
181828fd804SMatthias Ringwald 
1827d5f5399SMatthias Ringwald     while (1) {
1837d5f5399SMatthias Ringwald 
1847d5f5399SMatthias Ringwald         // process data sources
1857d5f5399SMatthias Ringwald         btstack_data_source_t *ds;
1867d5f5399SMatthias Ringwald         btstack_data_source_t *next;
1877d5f5399SMatthias Ringwald         for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){
1887d5f5399SMatthias Ringwald             next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
1897d5f5399SMatthias Ringwald             if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
1907d5f5399SMatthias Ringwald                 ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
1917d5f5399SMatthias Ringwald             }
1927d5f5399SMatthias Ringwald         }
1937d5f5399SMatthias Ringwald 
1947d5f5399SMatthias Ringwald         // process registered function calls on run loop thread
1957d5f5399SMatthias Ringwald         while (1){
1967d5f5399SMatthias Ringwald             function_call_t message = { NULL, NULL };
1977d5f5399SMatthias Ringwald             BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0);
1987d5f5399SMatthias Ringwald             if (res == pdFALSE) break;
1997d5f5399SMatthias Ringwald             if (message.fn){
2007d5f5399SMatthias Ringwald                 message.fn(message.arg);
2017d5f5399SMatthias Ringwald             }
2027d5f5399SMatthias Ringwald         }
2037d5f5399SMatthias Ringwald 
2047d5f5399SMatthias Ringwald         // process timers and get et next timeout
2057d5f5399SMatthias Ringwald         uint32_t timeout_ms = portMAX_DELAY;
2067d5f5399SMatthias Ringwald         log_debug("RL: portMAX_DELAY %u", portMAX_DELAY);
2077d5f5399SMatthias Ringwald         while (timers) {
2087d5f5399SMatthias Ringwald             btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
2097d5f5399SMatthias Ringwald             uint32_t now = btstack_run_loop_freertos_get_time_ms();
2107d5f5399SMatthias Ringwald             log_debug("RL: now %u, expires %u", now, ts->timeout);
2117d5f5399SMatthias Ringwald             if (ts->timeout > now){
2127d5f5399SMatthias Ringwald                 timeout_ms = ts->timeout - now;
2137d5f5399SMatthias Ringwald                 break;
2147d5f5399SMatthias Ringwald             }
2157d5f5399SMatthias Ringwald             // remove timer before processing it to allow handler to re-register with run loop
216b7832c7fSMatthias Ringwald             btstack_run_loop_freertos_remove_timer(ts);
2177d5f5399SMatthias Ringwald             log_debug("RL: first timer %p", ts->process);
2187d5f5399SMatthias Ringwald             ts->process(ts);
2197d5f5399SMatthias Ringwald         }
2207d5f5399SMatthias Ringwald 
221828fd804SMatthias Ringwald         // wait for timeout or event group/task notification
2227d5f5399SMatthias Ringwald         log_debug("RL: wait with timeout %u", (int) timeout_ms);
223828fd804SMatthias Ringwald #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
224828fd804SMatthias Ringwald         xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms));
225828fd804SMatthias Ringwald #else
2267d5f5399SMatthias Ringwald         xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms));
227828fd804SMatthias Ringwald #endif
2287d5f5399SMatthias Ringwald     }
2297d5f5399SMatthias Ringwald }
2307d5f5399SMatthias Ringwald 
2317d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){
2327d5f5399SMatthias Ringwald     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
2337d5f5399SMatthias Ringwald }
2347d5f5399SMatthias Ringwald 
2357d5f5399SMatthias Ringwald static int btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){
2367d5f5399SMatthias Ringwald     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
2377d5f5399SMatthias Ringwald }
2387d5f5399SMatthias Ringwald 
2397d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
2407d5f5399SMatthias Ringwald     ds->flags |= callback_types;
2417d5f5399SMatthias Ringwald }
2427d5f5399SMatthias Ringwald 
2437d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
2447d5f5399SMatthias Ringwald     ds->flags &= ~callback_types;
2457d5f5399SMatthias Ringwald }
2467d5f5399SMatthias Ringwald 
2477d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_init(void){
2487d5f5399SMatthias Ringwald     timers = NULL;
2497d5f5399SMatthias Ringwald 
2507d5f5399SMatthias Ringwald     // queue to receive events: up to 2 calls from transport, up to 3 for app
2517d5f5399SMatthias Ringwald     btstack_run_loop_queue = xQueueCreate(20, sizeof(function_call_t));
2527d5f5399SMatthias Ringwald 
253828fd804SMatthias Ringwald #ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS
2547d5f5399SMatthias Ringwald     // event group to wake run loop
2557d5f5399SMatthias Ringwald     btstack_run_loop_event_group = xEventGroupCreate();
256828fd804SMatthias Ringwald #endif
2577d5f5399SMatthias Ringwald 
2587d5f5399SMatthias Ringwald     log_info("run loop init, queue item size %u", (int) sizeof(function_call_t));
2597d5f5399SMatthias Ringwald }
2607d5f5399SMatthias Ringwald 
2617d5f5399SMatthias Ringwald /**
2627d5f5399SMatthias Ringwald  * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
2637d5f5399SMatthias Ringwald  */
2647d5f5399SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){
2657d5f5399SMatthias Ringwald     return &btstack_run_loop_freertos;
2667d5f5399SMatthias Ringwald }
2677d5f5399SMatthias Ringwald 
2687d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos = {
2697d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_init,
2707d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_add_data_source,
2717d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_remove_data_source,
2727d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_enable_data_source_callbacks,
2737d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_disable_data_source_callbacks,
2747d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_set_timer,
2757d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_add_timer,
2767d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_remove_timer,
2777d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_execute,
2787d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_dump_timer,
2797d5f5399SMatthias Ringwald     &btstack_run_loop_freertos_get_time_ms,
2807d5f5399SMatthias Ringwald };
281