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

vTaskDelay() not giving consistent times

Posted by Matt Budd on June 6, 2008
I have a problem with vTaskDelay. What I want to do is perform an action on a regular task once every tick, and then every say 100 ticks (1 second in my config) have a periodic task wake up and report on the status of the regular task. Here is my code:

//--------------------------------------------------------------
portTASK_FUNCTION_PROTO(regularTask, ppvParameters);
portTASK_FUNCTION_PROTO(periodicTask, ppvParameters);

static volatile unsigned int m_iCounter = 0;

void CreateTasks(void)
{
xTaskCreate(regularTask, "reg", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(periodicTask, "per", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
}

portTASK_FUNCTION(regularTask, ppvParameters)
{
while (1)
{
m_iCounter++;
vTaskDelay(1);
}
}

portTASK_FUNCTION(periodicTask, ppvParameters)
{
while (1)
{
vTaskDelay(1000 / portTICK_RATE_MS);
CSLog("counter = %d, tickcount = %d", m_iCounter, xTaskGetTickCount()); //This just logs to the debug window
}
}
//--------------------------------------------------------------

So the periodic task is a higher priority, but it doesn't seem to fire off every 1 second...it takes about 3 or 4 second to fire off. If I take out the "vTaskDelay(1)" in the regular task, it will fire off at the correct speed, but then m_iCounter gets updated more than once per tick. How do I yield the regular task to the next tick...taskYield() seems have the same issue.
Any ideas?

RE: vTaskDelay() not giving consistent times

Posted by Dave on June 6, 2008
If you just call taskYIELD() then you are not delaying so FreeRTOS will just choose the same task to run again.

If you call vTaskDelay( 1 ) then you are on the limit of the resolution and the period you delay for will depend on where in the current time slice the function is called. This is also very inefficient.

Can you replace regularTask with a tick hook? http://www.freertos.org/a00016.html

RE: vTaskDelay() not giving consistent times

Posted by Matt Budd on June 6, 2008
Thanks for your help. Some further comments:

1. So then I would expect that if I called taskYIELD() that is would be the same as having no statement in there at all (since this is the only task (besides the idle task) that is in the ready state), so like you say it would give execution right back to the "regular" task. However, I don't see that....if I use taskYIELD() it still takes about 4 seconds for my periodic guy to fire and the regular task still fires once per tick.

2. So lets say I simplify the "regular task" like this:
while (1)
{
//Do nothing except sleep
vTaskDelay(1);
}
Why do I still see the problem? Why would a task that executes during every tick at lower priority affect the overall time duration of the tick. The periodic task is still waiting 100 ticks, its just that those ticks are taking 4 times as long to happen. And nothing else is going on in the system besides the idle task and the regular task that does nothing except sleep. Does the overhead of switching to a task every tick really lengthen the tick period that much?

3. I can't use a tick hook, because this problem/code is not really what I am trying to do. It is just a sample that demonstrates the same problem I am having in my much more complicated code. I stripped it down to this ficticious example to make it really simple for people to analyze.

RE: vTaskDelay() not giving consistent times

Posted by Matt Budd on June 6, 2008
A updated code listing for the simplified version:

//--------------------------------------------------------------
portTASK_FUNCTION_PROTO(regularTask, ppvParameters);
portTASK_FUNCTION_PROTO(periodicTask, ppvParameters);

void CreateTasks(void)
{
xTaskCreate(regularTask, "reg", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(periodicTask, "per", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
}

portTASK_FUNCTION(regularTask, ppvParameters)
{
while (1)
{
vTaskDelay(1);
}
}

portTASK_FUNCTION(periodicTask, ppvParameters)
{
while (1)
{
vTaskDelay(1000 / portTICK_RATE_MS);

//ERR: I don't get here every second...I get here about every 4 seconds. Why?
CSLog("tickcount = %d", xTaskGetTickCount());
}
}
//--------------------------------------------------------------


[ 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