Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

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.

Kevin

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.

Regards.

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 ?

Kevin

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.

Regards.


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




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists