Skip to main content
Visitor II
December 5, 2023
Solved

FreeRTOS xTaskGetTickCount() erroneously skips ahead

  • December 5, 2023
  • 2 replies
  • 2445 views
xTaskGetTickCount() erroneously skips ahead after every 65536 ticks. So when calculating timing using values returned by xTaskGetTickCount(), there are problems!
 
vTaskDelay() has no problems. The problem is with xTaskGetTickCount()...
 
For example:
Code:
 

 

TickType_t tickCount1, tickCount2;
while (1) {
	tickCount1 = xTaskGetTickCount();
	vTaskDelay(25);
	tickCount2 = xTaskGetTickCount();
	printf("[%ld] %lu ms have passed!\r\n",
			tickCount2, (tickCount2-tickCount1));
}

 

Output:

 

[64887] 25 ms have passed!
[64912] 25 ms have passed!
[64937] 25 ms have passed!
[64962] 25 ms have passed!
[66087] 1125 ms have passed!
[66112] 25 ms have passed!
[66137] 25 ms have passed!
[66162] 25 ms have passed!
[66187] 25 ms have passed!

 

 
Workaround is to use vTaskDelay() instead:
 

 

TickType_t tickCount = 0;
while (1) {
	vTaskDelay(25);
	tickCount += 25;
	printf("[%ld] %lu ms have passed!\r\n",
		xTaskGetTickCount(), tickCount);
}

 

 
PLATFORM:
Microprocessor: STM32L496xx (ARM Cortex-M4)
GNU Tools for STM32 (11.3.rel1)
FreeRTOS API CMSIS v2
FreeRTOS version 10.3.1
CMSIS-RTOS version 2.00
    This topic has been closed for replies.
    Best answer by asvatik

    There is no problem with FreeRTOS. I let myself become fooled.

    If I move my test-code to a separate task with a high enough priority, I am unable to reproduce the problem. 

    2 replies

    Visitor II
    December 5, 2023
    asvatikAuthorAnswer
    Visitor II
    December 6, 2023

    There is no problem with FreeRTOS. I let myself become fooled.

    If I move my test-code to a separate task with a high enough priority, I am unable to reproduce the problem.