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

xTimerCreate() and configASSERT( )

Posted by Friedrich Lobenstock on February 3, 2013
Hello!

In timers.c in function xTimerCreate() the term "xTimerPeriodInTicks > 0"
always evaluates to ZERO/FALSE:


xTimerHandle xTimerCreate(...)
{
xTIMER *pxNewTimer;

/* Allocate the timer structure. */
if( xTimerPeriodInTicks == ( portTickType ) 0U )
{
pxNewTimer = NULL;
configASSERT( ( xTimerPeriodInTicks > 0 ) );
}


Therefore one will always see a compiler warning similar to:
["..\FreeRTOS_Source\timers.c" 236/3] condition is always true


What is the sense of this assert when timers.h documents:


If the timer cannot be created (because [...], or the
timer period was set to 0
) then 0 is returned.


In my view this assert should be removed as this code does not do
what is documented. I suggest:


xTimerHandle xTimerCreate(...)
{
xTIMER *pxNewTimer = NULL;

/* Allocate the timer structure. */
if( xTimerPeriodInTicks > ( portTickType ) 0U )
{


Regards
Friedl

RE: xTimerCreate() and configASSERT( )

Posted by Richard on February 3, 2013
“Therefore one will always see a compiler warning similar to:”


I'm not sure I have ever seen that warning, which is curious, although software timers are a new(ish) features and most of the demos don't include them because they are too old. Still, I have used timers with IAR, Keil, GCC, etc...

The assertion is valid, but will be moved to outside of the 'if' statement to remove the possibility of a warning.

“If the timer cannot be created (because [...], or the
timer period was set to 0) then 0 is returned.


“In my view this assert should be removed as this code does not do
what is documented”


Well, the code returns NULL, which is 0 cast to a pointer, so it does behave as documented, although the documentation is somewhat imprecise. I have change the documentation to instead say "then NULL is returned".

Regards.

RE: xTimerCreate() and configASSERT( )

Posted by Friedrich Lobenstock on February 3, 2013

Still, I have used timers with IAR, Keil, GCC, etc...


This is a TASKING compiler and it sees that "xTimerPeriodInTicks > 0" is always false and therefore the assert always true inside "xTimerPeriodInTicks == ( portTickType ) 0U".


The assertion is valid, but will be moved to outside of the 'if' statement to remove the possibility of a warning.


You can't have a

configASSERT( ( xTimerPeriodInTicks > 0 ) )

there as well or the function will NEVER return 0 nor NULL in case of xTimerPeriodInTicks == 0.

RE: xTimerCreate() and configASSERT( )

Posted by Richard on February 4, 2013
I can see that the original position of the assert can only be reached when ( xTimerPeriodInTicks == 0 ), and therefore the assert will always be evaluated to false, because if ( xTimerPeriodInTicks == 0 ) it cannot simultaneously be > 0. The assert is useful to keep, but should not be placed in a position where it could cause a compiler warning to be generated, so I have moved it outside of the if() statement....but you say:


“You can't have a

configASSERT( ( xTimerPeriodInTicks > 0 ) )

there as well or the function will NEVER return 0 nor NULL in case of xTimerPeriodInTicks == 0.”



The assert is a debugging statement only, it can be defined however the user wants and in production code (where it will presumably not be defined) its position will not change the logic of a function as it won't generate any code. Setting the timer period to 0 is an error. The C code protects against that eventuality by trapping the error and exiting the function returning NULL. The assert() gives people an opportunity to catch that happening while they are developing their application. I think I disagree with your statement that the function will never return NULL - it will if xTimerPeriodInTicks == 0 if configASSERT is not defined, or configASSERT() is defined to something that does not prevent the application from continuing executing. The assert is not a logic statement that effects the flow of the code, just provided for the convenience of users while they are creating their applications.

Regards.


[ 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