Strategy to implement a periodic task.

simointe wrote on Monday, March 06, 2017:

Setup: FreeRTOS version is V9.0.0 (comes from here, download link at file menu), port TMS570.

My current implementation uses wait on notification and timeout value on this notification to be able to react on different events along have a periodic execution in case of no event.
BaseType_t xResult = xTaskNotifyWait( pdFALSE, // Don’t clear bits on entry.
ULONG_MAX, // Clear all bits on exit.
&ulNotifiedValue, // Stores the notified value.
nbTick );
The problem is that this design don’t take into accompt the time to execute the thread itself. For instance, if it takes 200us to run my periodic thread of 1Khz, the resulting frequency will be around 800Hz instead of 1Khz.
A solution could be to use auto-reload timer to notify the task to run, which is in fact to use default timer task to notify my thread.
Is there another solution to make the thread more stand alone than using timer ?

rtel wrote on Monday, March 06, 2017:

Am I right in interpreting this to mean you want to perform periodic
processing, and handle events in between each processing cycle?

There are two things that might work:

  1. Use vTaskDelayUntil()
    to get a fixed period, then use xTaskAbortDelay()
    to unblock the task when an event needs processing.

  2. Continue with a scheme similar to the one you are already using, and
    use vTaskSetTimeOutState()
    and xTaskCheckForTimeOut()
    to adjust the time the task blocks to wait for the event so that the
    task always unblocks when it is time to perform the periodic processing

simointe wrote on Tuesday, March 07, 2017:

That’s right I want to perform periodic processing, and handle events in between each processing cycle. But that’s not my problem here. My problem is that I “restart” my waiting (on timeout) AFTER I’ve done the execution of my task, so this execution time is added to the overall period.
Is there some API functions to soustract this execution time to the next timeout value ?

edwards3 wrote on Wednesday, March 08, 2017:

thats what vTaskDelayUntil() does. When it unblocks is relative to the last time it unblocked so the time between calls doesnt matter.

simointe wrote on Thursday, March 09, 2017:

Ok, I see.
It’s look like there is not the same built-in feature within Notification API. I guess I should to it by hand.

richarddamon wrote on Thursday, March 09, 2017:

Yes, the timeout parameters for the various API doesn’t have the same ‘until’ feature as DelayUntil.

If you want a timeout for a given period from the point you last got an event, you would need to read the tick couter yourself and adjust. For me, most of the time the timeout period isn’t that crutial, but is mostly to detect that something has gone wrong or away, so the variation isn’t that important, if it is, you will need to correct for it.