Setting thread execution rate

ChibiOS public support forum for topics related to the STMicroelectronics SPC56x family of automotive micro-controllers.
ksweeney
Posts: 6
Joined: Sat Jun 01, 2013 3:41 pm

Setting thread execution rate

Postby ksweeney » Sat Jun 01, 2013 4:05 pm

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

User avatar
Giovanni
Site Admin
Posts: 11153
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 391 times
Been thanked: 322 times
Contact:

Re: Setting thread execution rate

Postby Giovanni » Sat Jun 01, 2013 4:19 pm

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

ksweeney
Posts: 6
Joined: Sat Jun 01, 2013 3:41 pm

Re: Setting thread execution rate

Postby ksweeney » Sun Jun 02, 2013 4:24 am

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

User avatar
Giovanni
Site Admin
Posts: 11153
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 391 times
Been thanked: 322 times
Contact:

Re: Setting thread execution rate

Postby Giovanni » Sun Jun 02, 2013 7:18 am

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

ksweeney
Posts: 6
Joined: Sat Jun 01, 2013 3:41 pm

Re: Setting thread execution rate

Postby ksweeney » Sun Jun 02, 2013 4:44 pm

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.


Return to “SPC56x Support”

Who is online

Users browsing this forum: No registered users and 1 guest