Setting task execution rate

ksweeney21 wrote on Saturday, June 01, 2013:

Hi All,

Investigating using FreeRTOS in an upcoming project. I’ve searched the forums and the documentation, as best I’m able being unfamiliar with the project, but can’t find an answer to my most important question.

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

I guess that I should ask, is this even possible?

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

How would this be done in FreeRTOS?

Of course an example would be ideal :), but a pointer to the correct general location in the documentation would be helpful.

Kevin

rtel wrote on Saturday, June 01, 2013:

To create a periodic task with good accuracy you can use vTaskDelayUntil().

There is also the slightly simpler vTaskDelay(), but that delays relative to when the function is called rather than relative to an absolute time.

Depending on what the code is doing,  you may also be able to use a software timer in place of a task.

Block/delay times are specified in ticks.  Provided the tick period (set by configTICK_RATE_HZ in FreeRTOSConfig.h) is less than 1KHz then you can convert milliseconds into ticks by dividing by portTICK_RATE_MS.  So delaying for 200ms can be achieved by calling vTaskDelay( 200 / portTICK_RATE_MS );

With regards to priority then that is set when the task is created, with 0 being the lowest priority (makes sense, it is the lowest number…) and the maximum priority being set by the FreeRTOSConfig.h constant configMAX_PRIORITIES.  Therefore the highest priority is ( configMAX_PRIORITIES - 1 ).  Task priorities can be changed at run time too.

Hope this helps.

Regards.

edwards3 wrote on Saturday, June 01, 2013:

Look at prvQueueSendTask() on this page http://www.freertos.org/Hardware-independent-RTOS-example.html. It runs at 5Hz.

ksweeney21 wrote on Monday, June 03, 2013:

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

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.

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 FreeRTOS ?

Kevin

rtel wrote on Monday, June 03, 2013:

You can do exactly the same in FreeRTOS, but I would really not recommend that structure because you are greatly increasing the execution time of every tick interrupt, especially when you are doing mod divides in the interrupt.  Also, you are manually scheduling tasks, which defeats the object of using a scheduler in the first place.  If you absolutely must do it that way, then the equivalent would be:

In FreeRTOSConfig.h:
#define configUSE_TICK_HOOK 1

In any C file:

void vApplicationTickHook( void )
{
/* Could also use a static count in the hook function instead of getting the tick count. */
portTickType xTickCount = xTaskGetTickCountFromISR();
    [url=http://www.freertos.org/a00124.html]xSemaphoreGiveFromISR[/url]( Sem200Hz, NULL );
    /* Avoid mod divide. */
    if( ( xTickCount & 0x01 ) == 0 )
    {
        xSemaphoreGiveFromISR( Sem100Hz, NULL );
    }
    /* Etc. */
}

Note FreeRTOS uses data hiding (standard engineering good practice) so the tick count cannot be accessed directly.  Also to ensure the fastest and simplest interrupt entry and exit code, and the smallest code size, there is a separate API for use in interrupts.  Therefore, because the tick hook is called from an interrupt context, the “FromISR” versions of the API are being used.

Regards.