Quality RTOS & Embedded Software

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


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 ]    [ 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.


Careers

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

Renesas

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