creating task inside other task in freertos

7.6k Views Asked by At

I am an RTOS newbie and I am creating a simple real time system for automotive

I am wondering if it possible to create a task inside another task. I tried to do this by the following method but it doesn't work.

 void vTask1 { *pvParameters){
unsigned portBASE_TYPE taskPriority;
taskPriority=uxTaskPriorityGet( NULL );
char x;
while (1){
 x= 5 ;
if (x==5)
xTaskCreate( vTask2 , "task2", 1000, "task2 is running", taskPriority+5 , NULL );
}

when I debug that code it hangs at xTaskCreate without executing the new task and I searched the manual and the internet for something about this but I didn't find any.

would anyone tell me is that possible to do in RTOS or I am doing it in a wrong way?

2

There are 2 best solutions below

0
On BEST ANSWER

Tasks can be created before the scheduler has been started (from main), or after the scheduler has been started (from another task). The xTaskCreate() API documentation is here: http://www.freertos.org/a00125.html . You will also find a set of demo tasks that demonstrate creating and deleting tasks from another task in the main FreeRTOS .zip file download. Look in the FreeRTOS/Demo/Common/Minimal/death.c file (death for suicidal tasks as they delete themselves after creation).

If xTaskCreate() returns NULL then you will probably have run out of heap space. See http://www.freertos.org/a00111.html. I think most of the hundreds or pre-configured examples that come in the zip file download have comments to this effect.

2
On

Check the return value of xTaskCreate api.

one more thing the second task which you are creating is vtask2 which is having lower priority than vtask1 the one who is creating . And vtask1 is running in while(1) scheduler will not schedule vtask2. you can delay or suspend the vtask1 after creating vtask2.

then vtask2 may execute.