updating vTaskDelay of blocked Task

marshallbrown wrote on Sunday, 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

Marshall

void SIGN_run(uint32_t runtime_msecs){
    if (sign_on_task_handle == NULL){
	xTaskCreate(SIGN_on, 	
                        (signed char *) "SIGN_ON",	
                        configMINIMAL_STACK_SIZE+100, 
                        &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);
	SIGN_stop(NULL);
    }
}


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

}

richard_damon wrote on Sunday, 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.

marshallbrown wrote on Sunday, 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..
		SIGN_stop(NULL);
		vTaskDelete(NULL);
	}
}

Thanks for the ideas.

Regards
Marshall

richard_damon wrote on Sunday, 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 portTICK_PERIOD_MS * runtime_msecs which does NOT convert a variable in ms into ticks, you want runtime_msecs / portTICK_PERIOD_MS. 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 portTICK_PERIOD_MS = 1 (the default for the demos) so it doesn’t matter. FreeRTOS also defines a macro pdMS_TO_TICKS which will also do the conversion.

marshallbrown wrote on Monday, June 15, 2015:

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

I’ll change it

Regards
Marshall