vTaskSetTimeOutState and Task Execution

groger57 wrote on Tuesday, October 24, 2017:


I am presently using a task that waits for a binary semaphore “give” from the ISR. This works good. Now, I want to change that to a task that executes differently based on the existence of a response, or timeout, whicever occurs first.

1 - A command is transmitted
2 - A response (1 byte) should be received in 2000ms. If not, set the sequence as “incomplete”

I know that I will be using vTaskSetTimeOutState. My question is this. How does the task that uses vTaskSetTimeOutState know that it should start executing and subsequently using up the 2000ms timeout? Does the task get created like any other task, then suspended, and resumed just after transmitting the command?

Thanks for your help, always appreciated!

FYI: The current sempahore task:

static void prvCOMprocessTask( void pvParameters )
configASSERT( xCOMsemaphore );
Take semaphore to get it to correct state. */
xSemaphoreTake( xCOMsemaphore, mainDONT_BLOCK );

for( ;; )
xSemaphoreTake( xCOMsemaphore, portMAX_DELAY );
    /* do some stuff, I got a message.... */


richard_damon wrote on Tuesday, October 24, 2017:

It sounds like what you want to do is rather than wait for portMAX_DELAY, you wait for your timeout period, and check the return value of xSemaphoreTake(). If it is success, they you got the message, if it is a timeout error, you didn’t in the time period.

groger57 wrote on Wednesday, October 25, 2017:

Ok, thanks! It makes sense…I will try it.
Just one other question - is there any advantage/disadvantage between this, and using vTaskSetTimeOutState ?

richard_damon wrote on Wednesday, October 25, 2017:

I would use vTaskSetTimeOutState if you need to abort a wait for some other reason than a timeout, but still want a ‘failure’. Maybe something like the task is blocked on a queue waiting for a reply, but you get some sort of NAK or error instead, No need at that point to wait for the timeout, you can just tell the task no answer is comming.