Quality RTOS & Embedded Software

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


Using vTaskSuspend/vTaskResume

Posted by Nobody/Anonymous on July 5, 2005

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??


RE: Using vTaskSuspend/vTaskResume

Posted by Richard on July 5, 2005
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.


RE: Using vTaskSuspend/vTaskResume

Posted by Nobody/Anonymous on July 5, 2005
That's absolutely fine, I understood! It solved my problem! Thanks


[ 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