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.

Hi,

I have the same doubt, but i want do this with a function that can be called at any time by user, when the user need to do some hardware installation config or change the config at any time.

For example:
In a ihm the user press a virtual button in lcd called config( like the gear symbol on android ).

I would like to create a task that does not block the entire system because at least the ihm task needs to be running.

Some suggestion is appreciated.

Thank’s.

I would just do the setup configuration code in the hmi task that saw the virtual button press. Why do you need another task? You may want to send some form of notiification to other tasks that you are switching to “config” mode and those tasks wait for instructions that the new config is ready.