Hi Steven
Steven Clugston wrote:
> 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?
I suggest you post on a semaphore from an alarm handler function. The
regular thread can then wait on the semaphore each time round the loop
rather than calling cyg_thread_delay().
http://ecos.sourceware.org/docs-latest/ref/kernel-semaphores.html
I hope this helps
John Dallaway
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss