Skip to content

FreeRTOS restore original behaviour and allow override startup hooks #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ portenta_c33.build.fpu=-mfpu=fpv5-sp-d16
portenta_c33.build.float-abi=-mfloat-abi=hard

portenta_c33.build.board=PORTENTA_C33
portenta_c33.build.defines=-DF_CPU=200000000 -DPROVIDE_FREERTOS_HOOK
portenta_c33.build.defines=-DF_CPU=200000000
portenta_c33.vid.0=0x2341
portenta_c33.pid.0=0x0068
portenta_c33.vid.1=0x2341
Expand Down
4 changes: 2 additions & 2 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void unsecure_registers() {

extern "C" void Stacktrace_Handler(void);
extern "C" __attribute__((weak)) void start_freertos_on_header_inclusion() {}
extern "C" __attribute__((weak)) void early_start_freertos_on_header_inclusion() {}

void arduino_main(void)
{
Expand Down Expand Up @@ -112,10 +113,9 @@ void arduino_main(void)
Serial.begin(115200);
#endif
startAgt();
early_start_freertos_on_header_inclusion();
setup();
#ifdef PROVIDE_FREERTOS_HOOK
start_freertos_on_header_inclusion();
#endif
while (1)
{
loop();
Expand Down
21 changes: 21 additions & 0 deletions libraries/Arduino_FreeRTOS/src/Arduino_FreeRTOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ extern "C" {
#include "lib/FreeRTOS-Kernel-v10.5.1/semphr.h"
#include "lib/FreeRTOS-Kernel-v10.5.1/task.h"
#include "lib/FreeRTOS-Kernel-v10.5.1/timers.h"
#include <stdbool.h>


// If you need to automatically start FREERTOS, declare either EARLY_AUTOSTART_FREERTOS or
// AUTOSTART_FREERTOS in your library or sketch code (.ino or .cpp file)
//
// EARLY_AUTOSTART_FREERTOS -> if you need the scheduler to be already running in setup()
// AUTOSTART_FREERTOS -> if you only declare the threads in setup() and use them in loop()

void _start_freertos_on_header_inclusion_impl(bool early_start);
void early_start_freertos_on_header_inclusion();
void start_freertos_on_header_inclusion();
#define EARLY_AUTOSTART_FREERTOS \
void early_start_freertos_on_header_inclusion() { \
_start_freertos_on_header_inclusion_impl(true); \
}
#define AUTOSTART_FREERTOS \
void start_freertos_on_header_inclusion() { \
_start_freertos_on_header_inclusion_impl(false); \
}


#ifdef __cplusplus
}
Expand Down
37 changes: 22 additions & 15 deletions libraries/Arduino_FreeRTOS/src/portable/FSP/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "FreeRTOSConfig.h"
#include "../../lib/FreeRTOS-Kernel-v10.5.1/FreeRTOS.h"
#include "../../lib/FreeRTOS-Kernel-v10.5.1/task.h"
#include "portmacro.h"

#if BSP_TZ_NONSECURE_BUILD
#include "tz_context.h"
Expand Down Expand Up @@ -225,28 +226,34 @@ static void prvTaskExitError(void);

#endif

#ifdef PROVIDE_FREERTOS_HOOK
void loop_thread_func(void* arg) {
extern void setup(void);
extern void loop(void);

static void sketch_thread_func(void* arg) {
bool early_start = (bool)arg;
if (early_start) {
setup();
}
while (1)
{
loop();
}
}

static TaskHandle_t loop_task;
void start_freertos_on_header_inclusion() {
xTaskCreate(
(TaskFunction_t)loop_thread_func,
"Loop Thread",
4096 / 4, /* usStackDepth in words */
NULL, /* pvParameters */
4, /* uxPriority */
&loop_task /* pxCreatedTask */
);

vTaskStartScheduler();
void _start_freertos_on_header_inclusion_impl(bool early_start) {
static TaskHandle_t sketch_task;
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
xTaskCreate(
(TaskFunction_t)sketch_thread_func,
"Sketch Thread",
4096 / 4, /* usStackDepth in words */
(void*)early_start, /* pvParameters */
4, /* uxPriority */
&sketch_task /* pxCreatedTask */
);
vTaskStartScheduler();
}
}
#endif

/* Arduino specific overrides */
void delay(uint32_t ms) {
Expand Down
Loading