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 */ 43 44 #include <stddef.h> // NULL 45 46 #include "btstack_linked_list.h" 47 #include "btstack_debug.h" 48 #include "btstack_run_loop_freertos.h" 49 50 // #include "hal_time_ms.h" 51 uint32_t hal_time_ms(void); 52 53 #include "freertos/FreeRTOS.h" 54 #include "freertos/task.h" 55 #include "freertos/queue.h" 56 #include "freertos/event_groups.h" 57 58 typedef struct function_call { 59 void (*fn)(void * arg); 60 void * arg; 61 } function_call_t; 62 63 static const btstack_run_loop_t btstack_run_loop_freertos; 64 65 static QueueHandle_t btstack_run_loop_queue; 66 static EventGroupHandle_t btstack_run_loop_event_group; 67 68 // bit 0 event group reserved to wakeup run loop 69 #define EVENT_GROUP_FLAG_RUN_LOOP 1 70 71 // the run loop 72 static btstack_linked_list_t timers; 73 static btstack_linked_list_t data_sources; 74 75 static uint32_t btstack_run_loop_freertos_get_time_ms(void){ 76 return hal_time_ms(); 77 } 78 79 // set timer 80 static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ 81 ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; 82 } 83 84 /** 85 * Add timer to run_loop (keep list sorted) 86 */ 87 static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){ 88 btstack_linked_item_t *it; 89 for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ 90 // don't add timer that's already in there 91 if ((btstack_timer_source_t *) it->next == ts){ 92 log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 93 return; 94 } 95 if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) { 96 break; 97 } 98 } 99 ts->item.next = it->next; 100 it->next = (btstack_linked_item_t *) ts; 101 } 102 103 /** 104 * Remove timer from run loop 105 */ 106 static int btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){ 107 return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); 108 } 109 110 static void btstack_run_loop_freertos_dump_timer(void){ 111 #ifdef ENABLE_LOG_INFO 112 btstack_linked_item_t *it; 113 int i = 0; 114 for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ 115 btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 116 log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); 117 } 118 #endif 119 } 120 121 // schedules execution from regular thread 122 void btstack_run_loop_freertos_trigger(void){ 123 xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP); 124 } 125 126 void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){ 127 function_call_t message; 128 message.fn = fn; 129 message.arg = arg; 130 BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY); 131 if (res != pdTRUE){ 132 log_error("Failed to post fn %p", fn); 133 } 134 btstack_run_loop_freertos_trigger(); 135 } 136 137 #if (INCLUDE_xEventGroupSetBitFromISR == 1) 138 void btstack_run_loop_freertos_trigger_from_isr(void){ 139 BaseType_t xHigherPriorityTaskWoken; 140 xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken); 141 } 142 143 void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){ 144 function_call_t message; 145 message.fn = fn; 146 message.arg = arg; 147 BaseType_t xHigherPriorityTaskWoken; 148 xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken); 149 btstack_run_loop_freertos_trigger_from_isr(); 150 } 151 #endif 152 153 /** 154 * Execute run_loop 155 */ 156 static void btstack_run_loop_freertos_task(void *pvParameter){ 157 UNUSED(pvParameter); 158 159 log_debug("RL: execute"); 160 161 while (1) { 162 163 // process data sources 164 btstack_data_source_t *ds; 165 btstack_data_source_t *next; 166 for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ 167 next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself 168 if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ 169 ds->process(ds, DATA_SOURCE_CALLBACK_POLL); 170 } 171 } 172 173 // process registered function calls on run loop thread 174 while (1){ 175 function_call_t message = { NULL, NULL }; 176 BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0); 177 if (res == pdFALSE) break; 178 if (message.fn){ 179 message.fn(message.arg); 180 } 181 } 182 183 // process timers and get et next timeout 184 uint32_t timeout_ms = portMAX_DELAY; 185 log_debug("RL: portMAX_DELAY %u", portMAX_DELAY); 186 while (timers) { 187 btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; 188 uint32_t now = btstack_run_loop_freertos_get_time_ms(); 189 log_debug("RL: now %u, expires %u", now, ts->timeout); 190 if (ts->timeout > now){ 191 timeout_ms = ts->timeout - now; 192 break; 193 } 194 // remove timer before processing it to allow handler to re-register with run loop 195 btstack_run_loop_remove_timer(ts); 196 log_debug("RL: first timer %p", ts->process); 197 ts->process(ts); 198 } 199 200 // wait for timeout or event group 201 log_debug("RL: wait with timeout %u", (int) timeout_ms); 202 xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms)); 203 } 204 } 205 206 static void btstack_run_loop_freertos_execute(void) { 207 // use dedicated task, might not be needed in all cases 208 xTaskCreate(&btstack_run_loop_freertos_task, "btstack_task", 3072, NULL, 5, NULL); 209 // btstack_run_loop_freertos_task(NULL); 210 } 211 212 static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){ 213 btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); 214 } 215 216 static int btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){ 217 return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); 218 } 219 220 static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 221 ds->flags |= callback_types; 222 } 223 224 static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 225 ds->flags &= ~callback_types; 226 } 227 228 static void btstack_run_loop_freertos_init(void){ 229 timers = NULL; 230 231 // queue to receive events: up to 2 calls from transport, up to 3 for app 232 btstack_run_loop_queue = xQueueCreate(20, sizeof(function_call_t)); 233 234 // event group to wake run loop 235 btstack_run_loop_event_group = xEventGroupCreate(); 236 237 log_info("run loop init, queue item size %u", (int) sizeof(function_call_t)); 238 } 239 240 /** 241 * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init 242 */ 243 const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){ 244 return &btstack_run_loop_freertos; 245 } 246 247 static const btstack_run_loop_t btstack_run_loop_freertos = { 248 &btstack_run_loop_freertos_init, 249 &btstack_run_loop_freertos_add_data_source, 250 &btstack_run_loop_freertos_remove_data_source, 251 &btstack_run_loop_freertos_enable_data_source_callbacks, 252 &btstack_run_loop_freertos_disable_data_source_callbacks, 253 &btstack_run_loop_freertos_set_timer, 254 &btstack_run_loop_freertos_add_timer, 255 &btstack_run_loop_freertos_remove_timer, 256 &btstack_run_loop_freertos_execute, 257 &btstack_run_loop_freertos_dump_timer, 258 &btstack_run_loop_freertos_get_time_ms, 259 }; 260