Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Setting task execution rate

Posted by Kevin Sweeney on June 1, 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.


RE: Setting task execution rate

Posted by Richard on June 1, 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.


RE: Setting task execution rate

Posted by MEdwards on June 1, 2013
Look at prvQueueSendTask() on this page http://www.freertos.org/Hardware-independent-RTOS-example.html. It runs at 5Hz.

RE: Setting task execution rate

Posted by Kevin Sweeney on June 3, 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 ?


RE: Setting task execution rate

Posted by Richard on June 3, 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();

xSemaphoreGiveFromISR( 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.0 is available for immediate download. MIT licensed, and including RISC-V and ARMv8-M (Cortex-M33) demos.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers





STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner