Restart a Task From that task using a xTimerChangePeriod() with ticks = 0

Hi,

I have a comm’s “watchdog” timer that deletes the comm’s task and then restarts it.

I wish to initiate a restart from within the comm’s task on a known comm’s error using the timer callback. To “call” it from the task I wish to change the timer period to zero viz:

/**************************************************************************
* WIFI TRANSPORT TASK
***************************************************************************
*/
USE_ITCM void wiFiTask(void *argument)
{
    /* Task startup stuff */
   ...
    while(1)
    {
         /* Do comms stuff */
        ...

         if(commsError == true)
         {
             xTimerChangePeriod(commsTimeoutTmr, 0, 5000); /* Set timer period to zero */
	     vTaskDelay(10000);  /* wait for timer to initiate timer callback */
        }
     }
}

/**************************************************************************
* COMMUNICATION TIMER FUNCTION - CALLBACK
***************************************************************************
*/
static void commsTimeoutCallback(TimerHandle_t commsTimoutTimer)
{
    vTaskSuspend(wiFiTskHandle);
    xTimerStop(commsTimeoutTmr, 1000);

    xTimerChangePeriod(commsTimeoutTmr, COMMS_TIMER_WIFI_START_DELAY, 500);
    wiFiPars.WiFiState = WIFI_COMMS_TIMEOUT_OCURRED;

   /* Destroy the wiFi task and then recreate it */
    vTaskDelete(wiFiTskHandle);

    vTaskDelay(100);
    startWiFiSystem(); /* create the wifi comms task */
}

Is this the correct way to do this or is there a better way? Is it OK to set a timer period to zero or does it have to be greater than zero?

Kind regards
Rob

It seems like you will hit this assert if you set the period to zero - FreeRTOS-Kernel/timers.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub.

Why can’t you do something simple like the following:

USE_ITCM void wiFiTask(void *argument)
{
    for( ;; )
    {
        /* Task startup stuff */

        while( commsError == false )
        {
            /* Do comms stuff. */
        }
    }
}

I had forgotten about the assert!

That’s OK I will set the period to 1 tick and with the delay the time will zero out in 1 tick during the vTaskDelay and all will be well. The reason I’m doing it this way is a long story. The modem I am using is poorly documented (an Inventak) and does all sorts of weird things which cause it to drop connections with the only way of restarting it and reconnecting automatically is to do a hardware reset on it and then run it through its config steps, reconnect it with the WiFi network, create a new socket and hopefully the client will reconnect.

As I have the watchdog code which works I wanted to use this from within the task. It’s a bit like using a sledge hammer to crack a nut. I should re-write the whole driver as I understand the modem better now, but I don’t have the time and the code works well most of the time.

Anyway thanks for the tip.

Additional: I changed the timer period from 0 to 1 and the WiFIDriver is fixed.

Thank you for reporting back!