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


updating vTaskDelay of blocked Task

Posted by marshallbrown on June 14, 2015

Hi There, I'm just starting out with FreeRTOS, it is quite a steep learning curve coming from statemachines....

Anyway, an easy question

I have a task that I have started, and I would like to update the vTaskDelay on the blocked task, in my case it will allow the user to adjust the runtime of the blocked function, how would I do this.

Also any comments on my implementation of the tasks would be appreciated. (I only want to create the task once so am checking for the existence of a handle to prevent the recreation of it)

Thanks and Regards


void SIGN_run(uint32_t runtime_msecs){
    if (sign_on_task_handle == NULL){
                        (signed char *) "SIGN_ON",	
                        &runtime_msecs, (tskIDLE_PRIORITY + 1UL), 
                        (TaskHandle_t *) &sign_on_task_handle);

void SIGN_on(void *arg){
uint32_t runtime_msecs = *((uint32_t *)arg);

while (1){
	vTaskDelay(portTICK_PERIOD_MS * runtime_msecs);

void SIGN_stop(void *arg){
if (sign_on_task_handle!=NULL){	
	sign_on_task_handle = NULL;


updating vTaskDelay of blocked Task

Posted by richard_damon on June 14, 2015

To my knowledge, there is no way to change the delay in a vTaskDelay that is currently in progress.

One option might be to use a Timer, which can be adjusted. You need to be somewhat limited in what you do in the Timer function (since all timers share the same task), but it could kick off a task to do what you need.

The second option which might work is rather than do a vTaskDelay, wait for a semaphore with a timeout equal to the delay you want. If you want to change the time, you can set some mailbox variables for the task and give the semaphore, which will restart the task, and it can check if timed out or was given, and on given reset the time to what is needed that is left on the delay and re-take the semaphore.

updating vTaskDelay of blocked Task

Posted by marshallbrown on June 14, 2015

Thanks Richard,

I implemented it as per your suggestion number 2. I wasn't sure about the mailbox options and how to do this so I have a local variable that is shared between the two functions, a bit ugly but the functions are simple.

while (1){
	//block until sem_SignOnInProgress either updates our timeout period or we timeout. 
	if( xSemaphoreTake( sem_SignOnInProgress, ( TickType_t ) portTICK_PERIOD_MS * runtime_msecs ) == pdTRUE ){
		runtime_msecs = some_new_value; //this will loop around now and try to retake the semaphore it just obtained, updating the runtime with the new value.
	else {//we reached our alloted timeout without the semaphore being updated.
		sign_on_task_handle = NULL;	//make our handle invalid so the STOP function won't delete us, we'll do it ourselves as it doesn't seem to clear down 
									//very well when the child deletes the parent..

Thanks for the ideas.

Regards Marshall

updating vTaskDelay of blocked Task

Posted by richard_damon on June 14, 2015

A "mailbox" is just some method of transferring data between tasks. A global variable is fine for that.

One thing I noticed, your code has the term portTICKPERIODMS * runtimemsecs which does NOT convert a variable in ms into ticks, you want runtimemsecs / portTICKPERIODMS. For example, if you want to wait 50ms with a tick period of 10ms you want 50/10 = 5 not 50*10 = 500 (which would be 5 seconds). My guess is that you have portTICKPERIODMS = 1 (the default for the demos) so it doesn't matter. FreeRTOS also defines a macro pdMSTOTICKS which will also do the conversion.

updating vTaskDelay of blocked Task

Posted by marshallbrown on June 15, 2015

Thanks for pointing out the portTICKPERIODMS, you are correct. that would be a really dumb mistake.

I'll change it

Regards Marshall

[ 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