-1

I have got two task (task1 and task2). And I need restart second task, when I receive message from CAN. Any idea how?

Ok. I solved the problem.

volatile uint8_t restart = 0;
extern void task1(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 5 / portTICK_RATE_MS;

    int16_t stop = 0;
    int16_t fast= 0;

    for (;;)
    {
        if (xQueueReceive(can_message, &stop, 100) == pdPASS)
           restart = 1;
        vTaskDelay(xDelayTime);
    }
}

extern void task2(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 15 / portTICK_RATE_MS;

    for (;;)
    {
    /*inits of task, vatiable set,...*/
        while(restart != 1)
        {
        /*function loop*/
        restart = 0;
        }
        vTaskDelay(xDelayTime);
    }
}
Jirka
  • 63
  • 1
  • 15
  • Have you ever heard something about concurrency? BTW you also, I think, need to put a delay into `while(restart != 1)` loop to relax the scheduler.And answers must be posted as answers, not editing your question. – LPs Jun 24 '16 at 14:39

1 Answers1

0

First question would be why you want to do that. Do you really want to delete a task and recreate it - or just branch to the start of the task again? Deleting the task and re-creating it is inefficient and almost never really required.

Assuming you do want to delete a task and recreate it then if one task wants to re-start a different task then it just needs to delete the task and create it again.

If on the other hand you want one task to delete and recreate itself then one way of achieving that would be to use xTimerPendFunctionCall().

Implement a function that creates the task you want to restart as so:

void vPendableFunction( void * pvParameter1, uint32_t ulParameter2 )
{
    [xTaskCreate][2]( whatever );
}

then when a task wants to recreate itself if first calls xTimerPendFunctionCall() with the above function as the appropriate xTimerPendFunctionCall() parameter, then deletes itself. The function that was 'pended' will execute and create the task again.

Richard
  • 3,081
  • 11
  • 9