Setup() with delay() functionality, before creating Task

vr6073 wrote on Sunday, October 26, 2014:

Hello,
I Am using freeRTOS with STM32 F2 microprocessor.

I need to execute some operations for configuring my hardware, before creating my Tasks. In this Setup operations, I need to use any kind of delay() function.

But At the beginning of the program, vTaskDelay() is not still available. What is the way for using delay functionality before create Tasks and starts scheduler, by calling vTaskStartScheduler() ?

Or stated another way:

Do I have to create a Setup() function as a Task, and then the other Tasks have to wait to start till the setup() finalizes? Is there an specific freeRTOS function to synchronize them that way?

Thanks,

heinbali01 wrote on Sunday, October 26, 2014:

Hello,

If you want to have a blocking delay without using RTOS, you’ll have to setup a timer that triggers an interrupt.

What I would do:

  • Setup the clocks and a timer for RTOS
  • Start your first task and from there:
  • Call your Setup() and use vTaskDelay()
  • Start-up the other tasks
    void vMainTask (void *pvParameters)
    {
        /* While running Setup(), there are
        no other tasks yet */
        Setup( );

        xTaskCreateStack (vMacbTASK, ...)
        xTaskCreateStack (vUsbTASK, ...)
        xTaskCreateStack (vLoggingTASK, ...)

        for( ; ; )
        {
            /* Do the normal work */
        }
    }

Regards,
Hein

vr6073 wrote on Thursday, October 30, 2014:

Thank you, Hein,

I am testing another way to do it:

In order to be able to use vTaskDelay() in my Setup() function , I am creating all task at the same time, but Setup with higher priority. That way I am sure that it starts the first:

xTaskCreate(mySetup, "mySetup", STACK_SIZE, NULL, tskIDLE_PRIORITY+2, NULL);

xTaskCreate(Task1, "Task1", STACK_SIZE, NULL, tskIDLE_PRIORITY+1, &xTasksHandle1);

xTaskCreate(AxisControl, "Task2", STACK_SIZE, NULL, tskIDLE_PRIORITY+1, &xTasksHandle2);

In the body of Setup, I suspend all others Tasks. At the end of Setup, I resume all other tasks and delete Setup Task.

void mySetup(void * pvParameters) {

vTaskSuspend(xTasksHandle1);	   
vTaskSuspend(xTasksHandle2);	

for (;;) {
.........................    
......................... 
doing things, and using 
vTaskDelay() delays
.........................     
......................... 
// Resume the suspended tasks.
vTaskResume(xTasksHandle1);     
vTaskResume(xTasksHandle2);
	
// Delete Setup task (ourselves).
vTaskDelete(NULL);
}

}

It is working right now...

markwrichardson wrote on Thursday, October 30, 2014:

When I encountered a similar issue in my design, I chose to implement as Hein described. I felt this was better for the reader and eliminated the chance of a race condition. The way you describe requires internal knowledge of the OS, which high-level code should not rely on. For example, if a maintainer moved TaskCreate( mySetup ) after the other TaskCreates, then the outcome wouldn’t be clear even with knowledge of the priorities.

Anyone else have preferences they use ?

Mark

vr6073 wrote on Thursday, October 30, 2014:

I’ve been studying the Hein’s code again, and it’s really good. I had misunderstood.