xTicksToWait - accuracy

yevpator75 wrote on Sunday, January 13, 2019:


I just want to ensure I understand properly the meaning of the xTicksToWait parameter that exist in many of the API functions.

Assume I specify xTicksToWait to be 1, e.g. xSemaphoreTake (p_mutexHandle, 1). That is to say I want a timeout after 1 tick (1ms in my case).
Can I say that the accuracy of this timing is 1 tick , or in other words, when I configure the timeout to be 1 tick, it can actually be any value in [0…1] ms range? So, if I want to prevent an immediate timeout, I need to cofigure the timeout at least 2 ticks instead of 1?


richarddamon wrote on Sunday, January 13, 2019:

Yes, the parameter is how many tick interrupts to wait, so if it is almost time for the next tick interrupt, the time period could be 1 tick short. The other thing to watch out for is with say a 10ms tick, the default pdMS_TO_TICKS will convert 15 ms to 1 tick, but if you really want an ‘at least 15 ms’ delay, you want to round any fraction up, and then add 1.

The one big exception would be when using the ‘Delay Until’ function, which bases the time on the previous timeout point, you don’t need to plus 1, as your time referance point is exactly on a tick interrupt so no short period issue. This doesn’t mean that a vTaskDelayUntil with a timeout of 1 will wait a full tick from the call, but a full tick from the last call waking up.

The other thing to remember is the delay can almost always be longer (unless you are the sole highest priority task) as a higher priority task running may prevent you from getting CPU time, even if you are woken up.

yevpator75 wrote on Sunday, January 13, 2019:

Thanks Richard, this answers my question.

Maybe I will just ask one more thing realted to this question. I am right now trying to understand where in the code the tick is managed , is there a task on the FreeRTOS level that handles the ticks ? Where can I find the tick management in the source ? Is that xTickCount that holds the current tick value?

richard_damon wrote on Sunday, January 13, 2019:

Most of the handling of the ticks are done in the Tick Interrupt, with a routine called something like TickIncrement that is called every tick, increments it and sees if any task needs to be woken, and if so initiates the scheduler.

There is also a task, the Sevice Task, that handles things like Timers, which are call backs that can be setup to be called at defined rates or elapsed time.