Using vTaskSuspend/vTaskResume

Hello! I want to use vTaskSuspend to suspend a task from sampling values. When the task is not suspended I use vTaskDelayUntil in order to get the task to execute every 1000 ticks. BUT, when the task has been suspended for some time and then resumed this delay does not apply for some reason. The task executes without any delay until it has "caught up" with the time somehow??!! Why is this and how do I fix it?? Thanks! /MB

Using vTaskSuspend/vTaskResume

Taking the example: void vTaskFunction( void * pvParameters ) { portTickType xLastWakeTime; const portTickType xFrequency = 10; ____// Initialise the xLastWakeTime variable with the ____current time. ____xLastWakeTime = xTaskGetTickCount(); ____for( ;; ) ____{ ________// Wait for the next cycle. ________vTaskDelayUntil( &xLastWakeTime, xFrequency ); ________// Perform action here. ____} } xLastWakeTime holds the time in ticks the task was last unblocked from the vTaskDelayUntil() call.  When vTaskDelayUntil() is called again the block time is added to xLastWakeTime to create the time at which the task should again be unblocked. For example, if xLastWakeTime is 1000, the delay period is 100 and 50 ticks have passed since the task last unblocked then:     xLastWakeTime is set to 1100, the current time is 1050 and the task will block for 50 ticks (until the actual time reaches the wanted unblock time). Now if the time that has passed between calles is greater than the block time then the value of xLastWakeTime will be less than the current time and the task will not block. For example, if xLastWakeTime is 1000, and the delay period is 100, but 200 ticks have passed since the task was last unblocked then:     xLastWakeTime will be set to 1100 but the current time will be 1200.  If the task wants to unblock at time 1100 but the time is already 1200 then the unblock time has passed and the task never blocks. This is what will happen if you suspend the task – and is the correct behaviour.  The tick count continues when the task is suspended so the wanted wake time will be less than the current time and the task will not block. To get around this you will need to reinitialise xLastWakeTime when the task is unblocked using: xLastWakeTime = xTaskGetTickCount(); This should only be done when the task unblocks, not each cycle of the loop. This line will refresh the xLastWakeTime with the current time, so the generated block time is relative to the current time, not the time the task last unblocked. Maybe I have made this sound too complex (?) it is not really. Regards.

Using vTaskSuspend/vTaskResume

That’s absolutely fine, I understood! It solved my problem! Thanks //MB