Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


Tickless mode time drift

Posted by lantczak on September 5, 2016


I'm using tickless mode. I noticed that there is quite big time drift between freertos tick and real time value. I expect some tiny drift but my measurement results are 2s differencial in 15 min perion. I'm using timers to trigger some actions in the system and this value of drift is too big.

My system: cortex m4 (atmel sam4e16e) + freertos 9.0.0 + configUSETICKLESSIDLE = 1 (original implementation) configEXPECTEDIDLETIMEBEFORESLEEP = 5

What about following code in port.c line. 526:

~~~ /* Stop the SysTick momentarily. The time the SysTick is stopped for is accounted for as best it can be, but using the tickless mode will inevitably result in some tiny drift of the time maintained by the kernel with respect to calendar time. */ portNVICSYSTICKCTRLREG &= ~portNVICSYSTICKENABLEBIT;

	/* Calculate the reload value required to wait xExpectedIdleTime
	tick periods.  -1 is used because this code will execute part way
	through one of the tick periods. */
	ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
	if( ulReloadValue > ulStoppedTimerCompensation )
		ulReloadValue -= ulStoppedTimerCompensation;

	/* Enter a critical section but don't use the taskENTER_CRITICAL()
	method as that will mask interrupts that should exit sleep mode. */
	__asm volatile( "cpsid i" );
	__asm volatile( "dsb" );
	__asm volatile( "isb" );


The ulReloadValue is calculated before critical section. If IRQ will occur just after calculation and before critical section then ulReloadValue will be inacurate.

Tickless mode time drift

Posted by lantczak on September 5, 2016

I moved critical section just before calculation and no improvement. I found define portMISSEDCOUNTSFACTOR as I understand it can use it to tune sleep time

Tickless mode time drift

Posted by heinbali01 on September 7, 2016

Good you found the portMISSED_COUNTS_FACTOR!

There is indeed some drift in the clock-tick count when using tickless idle. A 2s difference in 15 minutes is a lot. How precise did you get it?

What you could do is find another source to measure time. Use a 32-bit Timer-Counter and give it a very low tick-rate: choose a prescaler so that it overflows e.g. every 30 seconds or less often (frequent interrupts would increase the power consumption of your MCU).

The measured time will then consist of two numbers: an interrupt count plus the actual value of the TC. Reading these two values requires some care, as an interrupt might occur while fetching them.

You can either read them from with a critical section, or use the following method that allows for interrupts:

~~~~ extern unsigned long ulInterruptCount;

unsigned long ulSlowCount, ulCounts[ 2 ];
for( ;; )

	ulCounts[ 0 ] = tc_read_cv( TC0, ulTCChannel );
	ulSlowCount = ulInterruptCount;
	ulCounts[ 1 ] = tc_read_cv( TC0, ulTCChannel );

	if( ulCounts[ 1 ] >= ulCounts[ 0 ] )
		/* TC0_Handler() has not occurred in between. */



Tickless mode time drift

Posted by lantczak on September 9, 2016

I found that my system is switching to/from tickless each 50ms. It is due to one of the tasks that has to be redesing to no interrupt so often. I suspect that high frequency of on/off (each on/off is adding some drift) actions is reason of 2s differencial.

After increasing portMISSEDCOUNTSFACTOR I don't have anymore problems with time drift. But what is supprising I had to increase it from 45 to 600. My CPU & SystTick is working on external 11MHz. Not sure why I need 600 processor cycles. The code when systick is off (reason of time drift) needs someting about 128 cycles on my CPU.

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists