Quality RTOS & Embedded Software

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


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 ]    [ Privacy ]    [ Sitemap ]    [ ]

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

Latest News

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

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

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

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