1*7d5f5399SMatthias Ringwald /* 2*7d5f5399SMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH 3*7d5f5399SMatthias Ringwald * 4*7d5f5399SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*7d5f5399SMatthias Ringwald * modification, are permitted provided that the following conditions 6*7d5f5399SMatthias Ringwald * are met: 7*7d5f5399SMatthias Ringwald * 8*7d5f5399SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*7d5f5399SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*7d5f5399SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*7d5f5399SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*7d5f5399SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*7d5f5399SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*7d5f5399SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*7d5f5399SMatthias Ringwald * from this software without specific prior written permission. 16*7d5f5399SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*7d5f5399SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*7d5f5399SMatthias Ringwald * monetary gain. 19*7d5f5399SMatthias Ringwald * 20*7d5f5399SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*7d5f5399SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*7d5f5399SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*7d5f5399SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*7d5f5399SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*7d5f5399SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*7d5f5399SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*7d5f5399SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*7d5f5399SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*7d5f5399SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*7d5f5399SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*7d5f5399SMatthias Ringwald * SUCH DAMAGE. 32*7d5f5399SMatthias Ringwald * 33*7d5f5399SMatthias Ringwald * Please inquire about commercial licensing options at 34*7d5f5399SMatthias Ringwald * [email protected] 35*7d5f5399SMatthias Ringwald * 36*7d5f5399SMatthias Ringwald */ 37*7d5f5399SMatthias Ringwald 38*7d5f5399SMatthias Ringwald /* 39*7d5f5399SMatthias Ringwald * btstack_run_loop_freertos.c 40*7d5f5399SMatthias Ringwald * 41*7d5f5399SMatthias Ringwald * Run loop on dedicated thread on FreeRTOS 42*7d5f5399SMatthias Ringwald */ 43*7d5f5399SMatthias Ringwald 44*7d5f5399SMatthias Ringwald #include <stddef.h> // NULL 45*7d5f5399SMatthias Ringwald 46*7d5f5399SMatthias Ringwald #include "btstack_linked_list.h" 47*7d5f5399SMatthias Ringwald #include "btstack_debug.h" 48*7d5f5399SMatthias Ringwald #include "btstack_run_loop_freertos.h" 49*7d5f5399SMatthias Ringwald 50*7d5f5399SMatthias Ringwald // #include "hal_time_ms.h" 51*7d5f5399SMatthias Ringwald uint32_t hal_time_ms(void); 52*7d5f5399SMatthias Ringwald 53*7d5f5399SMatthias Ringwald #include "freertos/FreeRTOS.h" 54*7d5f5399SMatthias Ringwald #include "freertos/task.h" 55*7d5f5399SMatthias Ringwald #include "freertos/queue.h" 56*7d5f5399SMatthias Ringwald #include "freertos/event_groups.h" 57*7d5f5399SMatthias Ringwald 58*7d5f5399SMatthias Ringwald typedef struct function_call { 59*7d5f5399SMatthias Ringwald void (*fn)(void * arg); 60*7d5f5399SMatthias Ringwald void * arg; 61*7d5f5399SMatthias Ringwald } function_call_t; 62*7d5f5399SMatthias Ringwald 63*7d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos; 64*7d5f5399SMatthias Ringwald 65*7d5f5399SMatthias Ringwald static QueueHandle_t btstack_run_loop_queue; 66*7d5f5399SMatthias Ringwald static EventGroupHandle_t btstack_run_loop_event_group; 67*7d5f5399SMatthias Ringwald 68*7d5f5399SMatthias Ringwald // bit 0 event group reserved to wakeup run loop 69*7d5f5399SMatthias Ringwald #define EVENT_GROUP_FLAG_RUN_LOOP 1 70*7d5f5399SMatthias Ringwald 71*7d5f5399SMatthias Ringwald // the run loop 72*7d5f5399SMatthias Ringwald static btstack_linked_list_t timers; 73*7d5f5399SMatthias Ringwald static btstack_linked_list_t data_sources; 74*7d5f5399SMatthias Ringwald 75*7d5f5399SMatthias Ringwald static uint32_t btstack_run_loop_freertos_get_time_ms(void){ 76*7d5f5399SMatthias Ringwald return hal_time_ms(); 77*7d5f5399SMatthias Ringwald } 78*7d5f5399SMatthias Ringwald 79*7d5f5399SMatthias Ringwald // set timer 80*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 81*7d5f5399SMatthias Ringwald ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; 82*7d5f5399SMatthias Ringwald } 83*7d5f5399SMatthias Ringwald 84*7d5f5399SMatthias Ringwald /** 85*7d5f5399SMatthias Ringwald * Add timer to run_loop (keep list sorted) 86*7d5f5399SMatthias Ringwald */ 87*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){ 88*7d5f5399SMatthias Ringwald btstack_linked_item_t *it; 89*7d5f5399SMatthias Ringwald for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ 90*7d5f5399SMatthias Ringwald // don't add timer that's already in there 91*7d5f5399SMatthias Ringwald if ((btstack_timer_source_t *) it->next == ts){ 92*7d5f5399SMatthias Ringwald log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 93*7d5f5399SMatthias Ringwald return; 94*7d5f5399SMatthias Ringwald } 95*7d5f5399SMatthias Ringwald if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) { 96*7d5f5399SMatthias Ringwald break; 97*7d5f5399SMatthias Ringwald } 98*7d5f5399SMatthias Ringwald } 99*7d5f5399SMatthias Ringwald ts->item.next = it->next; 100*7d5f5399SMatthias Ringwald it->next = (btstack_linked_item_t *) ts; 101*7d5f5399SMatthias Ringwald } 102*7d5f5399SMatthias Ringwald 103*7d5f5399SMatthias Ringwald /** 104*7d5f5399SMatthias Ringwald * Remove timer from run loop 105*7d5f5399SMatthias Ringwald */ 106*7d5f5399SMatthias Ringwald static int btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){ 107*7d5f5399SMatthias Ringwald return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); 108*7d5f5399SMatthias Ringwald } 109*7d5f5399SMatthias Ringwald 110*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_dump_timer(void){ 111*7d5f5399SMatthias Ringwald #ifdef ENABLE_LOG_INFO 112*7d5f5399SMatthias Ringwald btstack_linked_item_t *it; 113*7d5f5399SMatthias Ringwald int i = 0; 114*7d5f5399SMatthias Ringwald for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ 115*7d5f5399SMatthias Ringwald btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 116*7d5f5399SMatthias Ringwald log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); 117*7d5f5399SMatthias Ringwald } 118*7d5f5399SMatthias Ringwald #endif 119*7d5f5399SMatthias Ringwald } 120*7d5f5399SMatthias Ringwald 121*7d5f5399SMatthias Ringwald // schedules execution from regular thread 122*7d5f5399SMatthias Ringwald void btstack_run_loop_freertos_trigger(void){ 123*7d5f5399SMatthias Ringwald xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 124*7d5f5399SMatthias Ringwald } 125*7d5f5399SMatthias Ringwald 126*7d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){ 127*7d5f5399SMatthias Ringwald function_call_t message; 128*7d5f5399SMatthias Ringwald message.fn = fn; 129*7d5f5399SMatthias Ringwald message.arg = arg; 130*7d5f5399SMatthias Ringwald BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY); 131*7d5f5399SMatthias Ringwald if (res != pdTRUE){ 132*7d5f5399SMatthias Ringwald log_error("Failed to post fn %p", fn); 133*7d5f5399SMatthias Ringwald } 134*7d5f5399SMatthias Ringwald btstack_run_loop_freertos_trigger(); 135*7d5f5399SMatthias Ringwald } 136*7d5f5399SMatthias Ringwald 137*7d5f5399SMatthias Ringwald #if (INCLUDE_xEventGroupSetBitFromISR == 1) 138*7d5f5399SMatthias Ringwald void btstack_run_loop_freertos_trigger_from_isr(void){ 139*7d5f5399SMatthias Ringwald xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 140*7d5f5399SMatthias Ringwald } 141*7d5f5399SMatthias Ringwald 142*7d5f5399SMatthias Ringwald void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){ 143*7d5f5399SMatthias Ringwald function_call_t message; 144*7d5f5399SMatthias Ringwald message.fn = fn; 145*7d5f5399SMatthias Ringwald message.arg = arg; 146*7d5f5399SMatthias Ringwald BaseType_t xHigherPriorityTaskWoken; 147*7d5f5399SMatthias Ringwald xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken); 148*7d5f5399SMatthias Ringwald btstack_run_loop_freertos_trigger_from_isr(); 149*7d5f5399SMatthias Ringwald } 150*7d5f5399SMatthias Ringwald #endif 151*7d5f5399SMatthias Ringwald 152*7d5f5399SMatthias Ringwald /** 153*7d5f5399SMatthias Ringwald * Execute run_loop 154*7d5f5399SMatthias Ringwald */ 155*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_task(void *pvParameter){ 156*7d5f5399SMatthias Ringwald UNUSED(pvParameter); 157*7d5f5399SMatthias Ringwald 158*7d5f5399SMatthias Ringwald log_debug("RL: execute"); 159*7d5f5399SMatthias Ringwald 160*7d5f5399SMatthias Ringwald while (1) { 161*7d5f5399SMatthias Ringwald 162*7d5f5399SMatthias Ringwald // process data sources 163*7d5f5399SMatthias Ringwald btstack_data_source_t *ds; 164*7d5f5399SMatthias Ringwald btstack_data_source_t *next; 165*7d5f5399SMatthias Ringwald for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ 166*7d5f5399SMatthias Ringwald next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself 167*7d5f5399SMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ 168*7d5f5399SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_POLL); 169*7d5f5399SMatthias Ringwald } 170*7d5f5399SMatthias Ringwald } 171*7d5f5399SMatthias Ringwald 172*7d5f5399SMatthias Ringwald // process registered function calls on run loop thread 173*7d5f5399SMatthias Ringwald while (1){ 174*7d5f5399SMatthias Ringwald function_call_t message = { NULL, NULL }; 175*7d5f5399SMatthias Ringwald BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0); 176*7d5f5399SMatthias Ringwald if (res == pdFALSE) break; 177*7d5f5399SMatthias Ringwald if (message.fn){ 178*7d5f5399SMatthias Ringwald message.fn(message.arg); 179*7d5f5399SMatthias Ringwald } 180*7d5f5399SMatthias Ringwald } 181*7d5f5399SMatthias Ringwald 182*7d5f5399SMatthias Ringwald // process timers and get et next timeout 183*7d5f5399SMatthias Ringwald uint32_t timeout_ms = portMAX_DELAY; 184*7d5f5399SMatthias Ringwald log_debug("RL: portMAX_DELAY %u", portMAX_DELAY); 185*7d5f5399SMatthias Ringwald while (timers) { 186*7d5f5399SMatthias Ringwald btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; 187*7d5f5399SMatthias Ringwald uint32_t now = btstack_run_loop_freertos_get_time_ms(); 188*7d5f5399SMatthias Ringwald log_debug("RL: now %u, expires %u", now, ts->timeout); 189*7d5f5399SMatthias Ringwald if (ts->timeout > now){ 190*7d5f5399SMatthias Ringwald timeout_ms = ts->timeout - now; 191*7d5f5399SMatthias Ringwald break; 192*7d5f5399SMatthias Ringwald } 193*7d5f5399SMatthias Ringwald // remove timer before processing it to allow handler to re-register with run loop 194*7d5f5399SMatthias Ringwald btstack_run_loop_remove_timer(ts); 195*7d5f5399SMatthias Ringwald log_debug("RL: first timer %p", ts->process); 196*7d5f5399SMatthias Ringwald ts->process(ts); 197*7d5f5399SMatthias Ringwald } 198*7d5f5399SMatthias Ringwald 199*7d5f5399SMatthias Ringwald // wait for timeout or event group 200*7d5f5399SMatthias Ringwald log_debug("RL: wait with timeout %u", (int) timeout_ms); 201*7d5f5399SMatthias Ringwald xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms)); 202*7d5f5399SMatthias Ringwald } 203*7d5f5399SMatthias Ringwald } 204*7d5f5399SMatthias Ringwald 205*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_execute(void) { 206*7d5f5399SMatthias Ringwald // use dedicated task, might not be needed in all cases 207*7d5f5399SMatthias Ringwald xTaskCreate(&btstack_run_loop_freertos_task, "btstack_task", 3072, NULL, 5, NULL); 208*7d5f5399SMatthias Ringwald // btstack_run_loop_freertos_task(NULL); 209*7d5f5399SMatthias Ringwald } 210*7d5f5399SMatthias Ringwald 211*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){ 212*7d5f5399SMatthias Ringwald btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); 213*7d5f5399SMatthias Ringwald } 214*7d5f5399SMatthias Ringwald 215*7d5f5399SMatthias Ringwald static int btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){ 216*7d5f5399SMatthias Ringwald return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); 217*7d5f5399SMatthias Ringwald } 218*7d5f5399SMatthias Ringwald 219*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 220*7d5f5399SMatthias Ringwald ds->flags |= callback_types; 221*7d5f5399SMatthias Ringwald } 222*7d5f5399SMatthias Ringwald 223*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 224*7d5f5399SMatthias Ringwald ds->flags &= ~callback_types; 225*7d5f5399SMatthias Ringwald } 226*7d5f5399SMatthias Ringwald 227*7d5f5399SMatthias Ringwald static void btstack_run_loop_freertos_init(void){ 228*7d5f5399SMatthias Ringwald timers = NULL; 229*7d5f5399SMatthias Ringwald 230*7d5f5399SMatthias Ringwald // queue to receive events: up to 2 calls from transport, up to 3 for app 231*7d5f5399SMatthias Ringwald btstack_run_loop_queue = xQueueCreate(20, sizeof(function_call_t)); 232*7d5f5399SMatthias Ringwald 233*7d5f5399SMatthias Ringwald // event group to wake run loop 234*7d5f5399SMatthias Ringwald btstack_run_loop_event_group = xEventGroupCreate(); 235*7d5f5399SMatthias Ringwald 236*7d5f5399SMatthias Ringwald log_info("run loop init, queue item size %u", (int) sizeof(function_call_t)); 237*7d5f5399SMatthias Ringwald } 238*7d5f5399SMatthias Ringwald 239*7d5f5399SMatthias Ringwald /** 240*7d5f5399SMatthias Ringwald * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 241*7d5f5399SMatthias Ringwald */ 242*7d5f5399SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){ 243*7d5f5399SMatthias Ringwald return &btstack_run_loop_freertos; 244*7d5f5399SMatthias Ringwald } 245*7d5f5399SMatthias Ringwald 246*7d5f5399SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_freertos = { 247*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_init, 248*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_add_data_source, 249*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_remove_data_source, 250*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_enable_data_source_callbacks, 251*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_disable_data_source_callbacks, 252*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_set_timer, 253*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_add_timer, 254*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_remove_timer, 255*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_execute, 256*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_dump_timer, 257*7d5f5399SMatthias Ringwald &btstack_run_loop_freertos_get_time_ms, 258*7d5f5399SMatthias Ringwald }; 259