Skip to content

Commit 71e2fd2

Browse files
facchinmpennam
authored andcommitted
FreeRTOS: allow override startup hooks default implementation
The default behaviour is do nothing: scheduler has to be started manually by the user. Use AUTOSTART_FREERTOS if you declare tasks in your setup() and use them in loop(). Use EARLY_AUTOSTART_FREERTOS if you need the scheduler to be already running in setup(). Libraries that need to use threads/semaphores during setup() (for example, to implement the usual myObj.begin(); <- this starts a thread while (!myObj) { <- this becomes true when the thread is unlocked delay(); } will need to declare EARLY_START_FREERTOS_HOOK in one of their cpp files
1 parent 00692e6 commit 71e2fd2

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

cores/arduino/main.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ void unsecure_registers() {
6464
#define str(s) #s
6565

6666
extern "C" void Stacktrace_Handler(void);
67+
extern "C" __attribute__((weak)) void start_freertos_on_header_inclusion() {}
68+
extern "C" __attribute__((weak)) void early_start_freertos_on_header_inclusion() {}
6769

6870
void arduino_main(void)
6971
{
@@ -111,7 +113,9 @@ void arduino_main(void)
111113
Serial.begin(115200);
112114
#endif
113115
startAgt();
116+
early_start_freertos_on_header_inclusion();
114117
setup();
118+
start_freertos_on_header_inclusion();
115119
while (1)
116120
{
117121
loop();

libraries/Arduino_FreeRTOS/src/Arduino_FreeRTOS.h

+21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ extern "C" {
2727
#include "lib/FreeRTOS-Kernel-v10.5.1/semphr.h"
2828
#include "lib/FreeRTOS-Kernel-v10.5.1/task.h"
2929
#include "lib/FreeRTOS-Kernel-v10.5.1/timers.h"
30+
#include <stdbool.h>
31+
32+
33+
// If you need to automatically start FREERTOS, declare either EARLY_AUTOSTART_FREERTOS or
34+
// AUTOSTART_FREERTOS in your library or sketch code (.ino or .cpp file)
35+
//
36+
// EARLY_AUTOSTART_FREERTOS -> if you need the scheduler to be already running in setup()
37+
// AUTOSTART_FREERTOS -> if you only declare the threads in setup() and use them in loop()
38+
39+
void _start_freertos_on_header_inclusion_impl(bool early_start);
40+
void early_start_freertos_on_header_inclusion();
41+
void start_freertos_on_header_inclusion();
42+
#define EARLY_AUTOSTART_FREERTOS \
43+
void early_start_freertos_on_header_inclusion() { \
44+
_start_freertos_on_header_inclusion_impl(true); \
45+
}
46+
#define AUTOSTART_FREERTOS \
47+
void start_freertos_on_header_inclusion() { \
48+
_start_freertos_on_header_inclusion_impl(false); \
49+
}
50+
3051

3152
#ifdef __cplusplus
3253
}

libraries/Arduino_FreeRTOS/src/portable/FSP/port.c

+28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "FreeRTOSConfig.h"
3333
#include "../../lib/FreeRTOS-Kernel-v10.5.1/FreeRTOS.h"
3434
#include "../../lib/FreeRTOS-Kernel-v10.5.1/task.h"
35+
#include "portmacro.h"
3536

3637
#if BSP_TZ_NONSECURE_BUILD
3738
#include "tz_context.h"
@@ -225,6 +226,33 @@ static void prvTaskExitError(void);
225226

226227
#endif
227228

229+
230+
static void sketch_thread_func(void* arg) {
231+
bool early_start = (bool)arg;
232+
if (early_start) {
233+
setup();
234+
}
235+
while (1)
236+
{
237+
loop();
238+
}
239+
}
240+
241+
void _start_freertos_on_header_inclusion_impl(bool early_start) {
242+
static TaskHandle_t sketch_task;
243+
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
244+
xTaskCreate(
245+
(TaskFunction_t)sketch_thread_func,
246+
"Sketch Thread",
247+
4096 / 4, /* usStackDepth in words */
248+
(void*)early_start, /* pvParameters */
249+
4, /* uxPriority */
250+
&sketch_task /* pxCreatedTask */
251+
);
252+
vTaskStartScheduler();
253+
}
254+
}
255+
228256
/* Arduino specific overrides */
229257
void delay(uint32_t ms) {
230258
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {

0 commit comments

Comments
 (0)