[ECOS] Periodic execution of code in a thread context

Steven Clugston steven.clugston@newcastle.ac.uk
Wed Sep 23 15:00:00 GMT 2009

Hi all

I need to have a function with some control code in it to execute at regular and consistent intervals.
The way I've done this is just to create a thread and trap the execution path in an infinite loop using while(1){some code;cyg_thread_delay()} or for(;;){some code; cyg_thread_delay()}.

The trouble with this approach is that if the code doesn't take the same path every time, then the overall interval will vary and the drift will be cumulative. This hasn't mattered too much until now, but I need it to not drift for my current application. I guess it could be improved by using cyg_real_time_clock() at the start and end of the code section to find how long it took then modify the delay time to try to keep the time interval at which the code is started the same, but it seems a bit clumsy and might suffer from jitter which would still be cumulative.

The other way I've tried is to use a timer/alarm attached to the system clock, but this executes in a DSR context which causes some limitations and I would prefer to use a normal thread context.

Could I use a cyg_thread_suspend() at the end of each iteration then re-wake the thread from an alarm handler?

Is there anything which I've missed like a scheduler policy or API call which might help?

Do I need to resort to a hardware timer to achieve this goal?

Thanks for any thoughts/insight,


Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list