vtaskDelayuntill

chaabanemalki wrote on Thursday, August 14, 2014:

Hello,
I have this simple 2 tasks

void vTestTask (void *pvParameter)
{
    portTickType xLastExecutionTime;
    xLastExecutionTime = xTaskGetTickCount();
    char x = 5;
   
    for(;;)
    {
        xSemaphoreTake(SwitchTaskSemaphore,portMAX_DELAY);

        vTaskDelayUntil(&xLastExecutionTime, 300/portTICK_RATE_MS);
        x++;
        x+=x;
        vTaskDelayUntil(&xLastExecutionTime, T4s);
        x++;
        x+=x;
        xSemaphoreGive(SwitchTaskSemaphore1);
    }
}

void vOneSecTask(void *pvParameter)
{
    portTickType xLastExecutionTime;
    xLastExecutionTime = xTaskGetTickCount();
    char x;
    xSemaphoreTake(SwitchTaskSemaphore,0);
    xSemaphoreTake(SwitchTaskSemaphore1,0);
    for(;;)
    {
        vTaskDelayUntil(&xLastExecutionTime, DelayTime);
        xSemaphoreGive(SwitchTaskSemaphore);
        xSemaphoreTake(SwitchTaskSemaphore1,portMAX_DELAY);
        x++;
    }
}

I’m using FreeRTOS+Trace to visualize what happends in the code
when DelayTime to zero. vTestTask works just fine and I can see in FreeRTOS+Trace that the response time for vTestTask is around 4s 300ms

If I change DelayTime to 1 seconde the response time for vTestTask becomes ~3s 300ms
If I change DelayTime to 2 seconde the response time for vTestTask becomes ~2s 300ms

I’m not sure what I’m missing here, Any ideas ??

rtel wrote on Thursday, August 14, 2014:

It is not clear to me what your code is supposed to be doing, or what it is not doing that you expect it to.

Please state how you expect the code to behave, how the behaviour deviates from your expectation - along with the priority at which the two tasks are created, the function used to create the semaphore, the value of T4s, and what x is doing.

Regards.

chaabanemalki wrote on Thursday, August 14, 2014:

Okey, I forget to add all the details

I have simplified the code to do tests with basic things an show it here too.

The code is suppose to reset a device : pull down a device’s pin during 300ms, pull it up and then wait for T4s = 4 secondes ( #define T4s 4000/portTICK_RATE_MS).
To do that, vOneSecTask wish has priority of 2, gives SwitchTaskSemaphore and vTestTask (priority 1) should take it and pull down a pin, wait for 300ms and then pull the pin back, and wait for 4 secondes, in the end vTestTask gives back SwitchTaskSemaphore1 to vOneSecTask indicating that the reset sequence has ended.
x is just something i put to add debug breakpoints.
and I use vSemaphoreCreateBinary to create the 2 semaphores.

When running this without any delay in vOneSecTask (DelayTime = 0).The response time of vTestTask (End Time- Start Time) is exactly as expected 4seconde+300ms+ few microsecondes

When running this with delay in vOneSecTask (DelayTime = 1000/portTICK_RATE_MS).The response time of vTestTask (End Time- Start Time) is 3seconde+300ms+ few microsecondes. which is not what expected.

hope this cleared things

Regards

davedoors wrote on Thursday, August 14, 2014:

I’m still confused. If you use vTaskDelay() in vTestTask() then it will wait 300ms between receiving the semaphore and x++, which I think is what you want. But you are using vTaskDelayUntil() so it will wait 300ms from the last call to vTaskDelayUntil(), not from when it receives the semaphore. The longer vOneSecTask() delays before giving the semaphore the shorter this time will be, which is what you are reporting. Does it do what you expect if you change vTaskDelayUntil() to vTaskDelay()?

chaabanemalki wrote on Thursday, August 14, 2014:

Okey, I miss understand how vTaskDelayUntil() work.

Now everything works just fine.
Thank you very much, it’s always helpful to come here