-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathbtstack_run_loop_freertos.c
More file actions
322 lines (278 loc) · 11.4 KB
/
btstack_run_loop_freertos.c
File metadata and controls
322 lines (278 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (C) 2017 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_run_loop_freertos.c
*
* Run loop on dedicated thread on FreeRTOS
* The run loop is triggered from other task/ISRs either via Event Groups
* or Task Notifications if HAVE_FREERTOS_TASK_NOTIFICATIONS is defined
*/
#define BTSTACK_FILE__ "btstack_run_loop_freertos.c"
#include <stddef.h> // NULL
#include "btstack_run_loop_freertos.h"
#include "btstack_linked_list.h"
#include "btstack_debug.h"
#include "btstack_util.h"
#include <platform/embedded/hal_time_ms.h>
// some SDKs, e.g. esp-idf, place FreeRTOS headers into an 'freertos' folder to avoid name collisions (e.g. list.h, queue.h, ..)
// wih this flag, the headers are properly found
#ifdef HAVE_FREERTOS_INCLUDE_PREFIX
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#else
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "event_groups.h"
#endif
typedef struct function_call {
void (*fn)(void * arg);
void * arg;
} function_call_t;
// pick allocation style, prefer static
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
#define USE_STATIC_ALLOC
#elif( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
// ok, nothing to do
#else
#error "Either configSUPPORT_STATIC_ALLOCATION or configSUPPORT_DYNAMIC_ALLOCATION in FreeRTOSConfig.h must be 1"
#endif
// queue to receive events: up to 2 calls from transport, rest for app
#define RUN_LOOP_QUEUE_LENGTH 20
#define RUN_LOOP_QUEUE_ITEM_SIZE sizeof(function_call_t)
#ifdef USE_STATIC_ALLOC
static StaticQueue_t btstack_run_loop_queue_object;
static uint8_t btstack_run_loop_queue_storage[ RUN_LOOP_QUEUE_LENGTH * RUN_LOOP_QUEUE_ITEM_SIZE ];
#endif
static QueueHandle_t btstack_run_loop_queue;
static TaskHandle_t btstack_run_loop_task;
#ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS
static EventGroupHandle_t btstack_run_loop_event_group;
#endif
// bit 0 event group reserved to wakeup run loop
#define EVENT_GROUP_FLAG_RUN_LOOP 1
// the run loop
static btstack_linked_list_t timers;
static btstack_linked_list_t data_sources;
static uint32_t btstack_run_loop_freertos_get_time_ms(void){
return hal_time_ms();
}
// set timer
static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1;
}
/**
* Add timer to run_loop (keep list sorted)
*/
static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
// don't add timer that's already in there
btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
if (next == ts){
log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
return;
}
// exit if new timeout before list timeout
int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
if (delta < 0) break;
}
ts->item.next = it->next;
it->next = (btstack_linked_item_t *) ts;
}
/**
* Remove timer from run loop
*/
static bool btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){
return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
}
static void btstack_run_loop_freertos_dump_timer(void){
#ifdef ENABLE_LOG_INFO
btstack_linked_item_t *it;
int i = 0;
for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
}
#endif
}
// schedules execution from regular thread
void btstack_run_loop_freertos_trigger(void){
#ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
xTaskNotify(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits);
#else
xEventGroupSetBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP);
#endif
}
void btstack_run_loop_freertos_execute_code_on_main_thread(void (*fn)(void *arg), void * arg){
// directly call function if already on btstack task
if (xTaskGetCurrentTaskHandle() == btstack_run_loop_task){
(*fn)(arg);
return;
}
function_call_t message;
message.fn = fn;
message.arg = arg;
BaseType_t res = xQueueSendToBack(btstack_run_loop_queue, &message, 0); // portMAX_DELAY);
if (res != pdTRUE){
log_error("Failed to post fn %p", fn);
}
btstack_run_loop_freertos_trigger();
}
#if defined(HAVE_FREERTOS_TASK_NOTIFICATIONS) || (INCLUDE_xEventGroupSetBitFromISR == 1)
void btstack_run_loop_freertos_trigger_from_isr(void){
BaseType_t xHigherPriorityTaskWoken;
#ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
xTaskNotifyFromISR(btstack_run_loop_task, EVENT_GROUP_FLAG_RUN_LOOP, eSetBits, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
#ifdef ESP_PLATFORM
portYIELD_FROM_ISR();
#else
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
#endif
}
#else
xEventGroupSetBitsFromISR(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, &xHigherPriorityTaskWoken);
#endif
}
void btstack_run_loop_freertos_execute_code_on_main_thread_from_isr(void (*fn)(void *arg), void * arg){
function_call_t message;
message.fn = fn;
message.arg = arg;
BaseType_t xHigherPriorityTaskWoken;
xQueueSendToBackFromISR(btstack_run_loop_queue, &message, &xHigherPriorityTaskWoken);
btstack_run_loop_freertos_trigger_from_isr();
}
#endif
/**
* Execute run_loop
*/
static void btstack_run_loop_freertos_execute(void) {
log_debug("RL: execute");
while (true) {
// process data sources
btstack_data_source_t *ds;
btstack_data_source_t *next;
for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){
next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
}
}
// process registered function calls on run loop thread
while (true){
function_call_t message = { NULL, NULL };
BaseType_t res = xQueueReceive( btstack_run_loop_queue, &message, 0);
if (res == pdFALSE) break;
if (message.fn){
message.fn(message.arg);
}
}
// process timers and get next timeout
uint32_t timeout_ms = portMAX_DELAY;
log_debug("RL: portMAX_DELAY %u", portMAX_DELAY);
while (timers) {
btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
uint32_t now = btstack_run_loop_freertos_get_time_ms();
int32_t delta_ms = btstack_time_delta(ts->timeout, now);
log_debug("RL: now %u, expires %u -> delta %d", now, ts->timeout, delta_ms);
if (delta_ms > 0){
timeout_ms = delta_ms;
break;
}
// remove timer before processing it to allow handler to re-register with run loop
btstack_run_loop_freertos_remove_timer(ts);
log_debug("RL: first timer %p", ts->process);
ts->process(ts);
}
// wait for timeout or event group/task notification
log_debug("RL: wait with timeout %u", (int) timeout_ms);
#ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS
xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms));
#else
xEventGroupWaitBits(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, 1, 0, pdMS_TO_TICKS(timeout_ms));
#endif
}
}
static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){
btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
}
static bool btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){
return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
}
static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
ds->flags |= callback_types;
}
static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
ds->flags &= ~callback_types;
}
static void btstack_run_loop_freertos_init(void){
timers = NULL;
#ifdef USE_STATIC_ALLOC
btstack_run_loop_queue = xQueueCreateStatic(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE, btstack_run_loop_queue_storage, &btstack_run_loop_queue_object);
#else
btstack_run_loop_queue = xQueueCreate(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE);
#endif
#ifndef HAVE_FREERTOS_TASK_NOTIFICATIONS
// event group to wake run loop
btstack_run_loop_event_group = xEventGroupCreate();
#endif
// task to handle to optimize 'run on main thread'
btstack_run_loop_task = xTaskGetCurrentTaskHandle();
log_info("run loop init, task %p, queue item size %u", btstack_run_loop_task, (int) sizeof(function_call_t));
}
/**
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
*/
static const btstack_run_loop_t btstack_run_loop_freertos = {
&btstack_run_loop_freertos_init,
&btstack_run_loop_freertos_add_data_source,
&btstack_run_loop_freertos_remove_data_source,
&btstack_run_loop_freertos_enable_data_source_callbacks,
&btstack_run_loop_freertos_disable_data_source_callbacks,
&btstack_run_loop_freertos_set_timer,
&btstack_run_loop_freertos_add_timer,
&btstack_run_loop_freertos_remove_timer,
&btstack_run_loop_freertos_execute,
&btstack_run_loop_freertos_dump_timer,
&btstack_run_loop_freertos_get_time_ms,
};
const btstack_run_loop_t * btstack_run_loop_freertos_get_instance(void){
return &btstack_run_loop_freertos;
}