vTaskSuspend() how to use correct

petermeier wrote on Wednesday, August 19, 2009:

Hi,
i would like to create a task suspended but it seems, that FreeRTOS does not have such a feature?!. So i’ve tried to create a task suspended… But strangely even if i suspended a task, on scheduler start the task runs… (The defines for vTaskSuspend are enabled)

xTaskHandle xTest1Task;

prvSetupHardware();
res = xTaskCreate( prvTest1Task, "Test1Tsk", 410, NULL, 4, &xTest1Task);
vTaskSuspend(xTest1Task);
vTaskStartScheduler();

Strangely the task is being started. But the following works:

void prvTest1Task(void * pvParameters)
{
while(1)
  {
    vTaskSuspend();
    // we idle here until someone resumes us…
    {…}
}

xTaskHandle xTest1Task;
prvSetupHardware();
res = xTaskCreate( prvTest1Task, "Test1Tsk", 410, NULL, 4, &xTest1Task);
vTaskStartScheduler();

And strangely the following will not work:

void prvTest1Task(void * pvParameters)
{
while(1)
  {
    vTaskSuspend();
    // we idle here until someone resumes us…
    {…}
}

xTaskHandle xTest1Task;
prvSetupHardware();
res = xTaskCreate( prvTest1Task, "Test1Tsk", 410, NULL, 4, &xTest1Task);
vTaskSuspend(xTest1Task);
vTaskStartScheduler();

Kind regards,
Peter

edwards3 wrote on Wednesday, August 19, 2009:

In tasks.c find the implementation of vTaskSuspend(). At the bottom of the function you will see the line if( ( void * ) pxTaskToSuspend == NULL ). Change this to

if( (( void * ) pxTaskToSuspend == NULL )&&(xSchedulerRunning != pdFALSE) )

Then in main() create the task then use the task handle as the parameter to vTaskSuspend().

xTaskCreate( prvTest1Task, "Test1Tsk", 410, NULL, 4, &xTest1Task);
vTaskSuspend(xTest1Task);
//other code goes here.
vTaskStartScheduler().

petermeier wrote on Thursday, August 20, 2009:

Hi Edward,
thanks for the idea - because otherwise the the task is being YIELDed before the scheduler has been started. Unfortunately the solution does not work. Do you have any idea? (Iam not that deep into that, yet).
Thanks in advance,
Peter

ben_fnr wrote on Thursday, August 20, 2009:

If just use the following to suspend a task when it first runs, and then resume it when needed from another task.

void TaskA( void *pvParameters )
{
       vTaskSuspend( NULL );

      while(1) {

     }
}

petermeier wrote on Thursday, August 20, 2009:

Hi Ben,
yes you are right! That was also my idea, but i’ve found that way not that beautiful :wink:

Kind regards,
Peter