Page 1 of 1

Setting thread execution rate

Posted: Sat Jun 01, 2013 4:05 pm
by ksweeney
Hi all

This is actually a generic ChibiOS question but I thought that I'd put it here so the SPC56x forum didn't look so lonely :D

How can one set the rate at which a thread is executed?

Example; In my code (using µC/OS-II) I have a 200Hz (highest priority), 100Hz (middle priority), and 10Hz (lowest priority) task. These threads need to execute at precisely these frequencies.

How would this be done in ChibiOS?

Kevin

Re: Setting thread execution rate

Posted: Sat Jun 01, 2013 4:19 pm
by Giovanni
Hi,

Assign tasks their priorities then use one of the methods described in this article: http://www.chibios.org/dokuwiki/doku.ph ... :kb:timing

This forum will not be lonely for long so don't worry and use the correct one next time :)

Giovanni

Re: Setting thread execution rate

Posted: Sun Jun 02, 2013 4:24 am
by ksweeney
Thanks for the response.

I'm still working my way through the documentation.

Let me describe how I set thread execution rate now and perhaps you can tell me if something similar is possible in ChibiOS.

Assume that the system tick rate is set at 200Hz.
At the start of every task I acquire a semaphore for that task, Sem200Hz, for example, for the 200Hz task.
In µC/OS-II there is a function that is user modifiable and called at every system tick.

Code: Select all

void OSTimeTickHook (void)
{   
    OSSemPost(Sem200Hz); //release semaphore

    if(!(TickCount % 2))
    {
        OSSemPost(Sem100Hz); //release semaphore
    }
   
    if(!(TickCount % 20))
    {
        OSSemPost(Sem10Hz); //release semaphore
    }
}


Works well in my application. Would this be possible in ChibiOS?

Kevin

Re: Setting thread execution rate

Posted: Sun Jun 02, 2013 7:18 am
by Giovanni
Hi,

Look into chconf.h in the hooks section: SYSTEM_TICK_EVENT_HOOK(). The counter can be read using chTimeNow().

Remember that you are in ISR context so you need to call I-Class functions from there inside locks:

Code: Select all

chSysLockFromIsr();
if (!(chTimeNow()) % 2)
  chSemResetI(&sem1, 0);
...
chSysUnlockFromIsr();


It is advisable to enable the debug options in chconf.h during development, the system state checker (CH_DBG_SYSTEM_STATE_CHECK) will catch call protocol errors.

Giovanni

Re: Setting thread execution rate

Posted: Sun Jun 02, 2013 4:44 pm
by ksweeney
Thanks, looks like just what I was looking for.

I have the ST dev board for the SPC564A and will be trying out the SPC5 Studio example. Once I get that running I'll try the code you suggested and report back here on my results.