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




Loading

xTaskGetTickCount

Posted by incrediball on July 9, 2010
I repeatedly run into the problem of trying to use xTaskGetTickCount within an interrupt (actually I'm trying to use some other code that sets up a timer) but this crashes the system because of the critical section. Rather than writing an interrupt safe version that does not use a critical section, would this be a possible solution?

Instead of:

portTickType xTaskGetTickCount( void )
{
portTickType xTicks;

/* Critical section required if running on a 16 bit processor. */
taskENTER_CRITICAL();
{
xTicks = xTickCount;
}
taskEXIT_CRITICAL();

return xTicks;
}

do:

portTickType xTaskGetTickCount( void )
{
portTickType xTicks;

do {
xTicks = xTickCount;
} while (xTicks != xTickCount);

return xTicks;
}

The loop will repeat if xTickCount is updated while it is being read. Since xTickCount is declared as volatile, the loop condition should not be optimised away by the compiler.

RE: xTaskGetTickCount

Posted by incrediball on July 13, 2010
I'm surprised that no one else seems to have run into the problem with not being able to use xTaskGetTickCount in interrupts. For example what if you want to put a timestamp on some received data? Or what if you want to record the last time a particular interrupt was raised? At present it is not possible.

I imagine that fixing this issue would stop one or two "why does my project keep crashing" posts in this forum because it is not immediately obvious that this function cannot be used in an interrupt.

RE: xTaskGetTickCount

Posted by Richard Damon on July 14, 2010
There are two simple mods to get around the problem, one is to add a function xTaskGetTickCountFromISR function that just return xTickCount, the second is remove the static qualifier on the declaration of xTickCount and just access the variable in the ISR.

RE: xTaskGetTickCount

Posted by Dave on July 14, 2010
Calling vTaskSetTimeOutState() will also allow you access to the tick count, but indirectly.

RE: xTaskGetTickCount

Posted by incrediball on July 14, 2010
richard_damon: sure, it's easy to get to the information if one patches the FreeRTOS source. However I prefer not to do this sort of thing for a number of reasons, e.g. it makes upgrading more difficult. It would be better if the original FreeRTOS source would provide a function that works in both cases (no patches necessary and no "interrupt version" that must be used in interrupts otherwise a crash results). I believe my version of the function would be one way to achieve this and using a loop to check if an update occurred while reading might even be more efficient than the critical section. It is also a method not seldomly used in embedded systems for checking if updates occur while reading peripherals (e.g. when reading a 16 bit running timer on an 8 bit processor).

davedoors: interesting tip but this does not appear to be part of the FreeRTOS API so might not be available in this form in future versions.


[ 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