Creating and handling a setup task

Hey all,

I want to preface this by saying I’m very new to FreeRTOS (or any RTOS in general), and I’m looking for a way to run the setup function as a task. I want to be able to run it once and then delete the task, and I wanted to know if there are any accepted solutions.

Currently, I create the task within the setup function and then delete the task handle at the very end of the setup task, but this causes some other tasks to break.

If anyone has any questions, please let me know.

Thanks in advance!

Have you considered using the Daemon Task Startup Hook instead? If you set configUSE_TIMERS and configUSE_DAEMON_TASK_STARTUP_HOOK to 1 in FreeRTOSConfig.h, and provide a function called vApplicationDaemonTaskStartupHook(), then that function will get called automatically when the scheduler starts. If the timer/daemon task is the highest priority task in the system, that is if configTIMER_TASK_PRIORITY is set higher than any other task priority, then the function will get called before any other task runs.

I’m a little confused on how exactly this would work. Would this be the proper implementation? Would this run once on start up and then not be ran again? Can I also use this for my global variables inits and peripheral instantiations (like my LSM instance)?

#include <nrf52840.h>
#include <LSM6DS3.h>
#include <SPI.h>
#include <Time.h>
#include <Wire.h>

LSM6DS3 myIMU; // Instance


void setup() {
    // Setup code
    vTaskStartScheduler();
    void vApplicationDaemonTaskStartupHook( void );   
}

void loop() {
    // empty
}

void vApplicationDaemonTaskStartupHook( void ) {

    Serial.begin(9600);
}

Here is my FreeRTOSConfig.h without any modifications, I didn’t see 'configUSE_DAEMON_TASK_STARTUP_HOOK ':

See FreeRTOS - RTOS hook (callback) functions for task stack overflows, tick interrupts, idle task, daemon task startup, and malloc failure (pvPortMalloc() returning NULL) for the supported hooks.
A hook is not explicitely called, but like a callback function called by someone else.
The only thing to do is to provide an implementation of vApplicationDaemonTaskStartupHook (which is called once by the timer task after starting the scheduler).

This is the Arduino port, right? Why should the setup function be a task? Wouln’t you also need to explicitly invoke the scheduler then?

I’m working in Arduino IDE with the Seeed Studio XIAO nRF52840 sense board. There have been timing issues where some portions are code are skipped within the setup in the past.

That does not make sense. The setup function is pretty much the only place in the code where there is predictable timing, so introducing any kind of multitasking there will do eherything else but improve tming.

Your fallacy here may be that serial output on the Arduino is tucked behind each loop invocation.