Quality RTOS & Embedded Software

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


gcc optimisation freertos and all that

Posted by kurator on January 15, 2009
I got hit by a very strange GCC/FreeRTOS problem.
In a task I check the return value of a function like this:

int ret;

if( xQueueReceive(QueueMain, &Event, portMAX_DELAY) == pdTRUE )
ret = menu_descend();
if( ret != 0 )

Using this code the system hangs after returning from menu_descend()!
The program gets stuck in xQueueReceive() in an endless loop of xTaskResumeAll().
There is a while loop:
while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xPendingReadyList ) ) ) != NULL )

As there is an portENTER_CRITICAL() in the beginning of that routine there
interrupts are disabled, the os tick is no longer called.

But, when I declare ret as volatile
volatile int ret;
the program runs as expected!

Looking at the assembler code shows:
Without volatile:

cmpr0, #0

With volatile:

strr0, [sp, #84]
ldrr3, [sp, #84]
cmpr3, #0

So it looks like that storing the return value on the stack
and using r3 for comparison against zero saves my program
from getting stuck.

From here I only can guess.
Maybe gcc tricks itself by the assumption that there lies a return value
of the function on stack, but optimisation uses r0 for handover directly instead,
and from there on the stack pointer is one position to low?

Or has it to do with register treatment of freertos, especially register r0?

I use freertos version 3.2 ( because thats what the project started with
and I'm not allowed to upgrade ;-)
It's a port for the STR7 ARM7 from ST.
gcc is arm-elf-gcc 4.2.2
optimization level is 2

All that really frightens me!
I must use optimization, at least for size, as the code got to big.

What goes wrong here?

RE: gcc optimisation freertos and all that

Posted by Dave on January 15, 2009
Wow, that is an old version.

Could you post more of the function? The snippet you posted has brackets missing, I'm guessing this is just a typo on your part.

Getting stuck in loops like that is normally a sign of memory corruption occurring somewhere. The most likely cause of which is a stack overflow. Unfortunately the version you are using does not have the stack traps in it.

RE: gcc optimisation freertos and all that

Posted by kurator on January 15, 2009
the missing bracket is just a typo
I sampled the code together, there is much more between the queuereceive and the switch
I forgot that spaces and tabs got eaten up here
the stack looks good to me
everything works fine when I change to volatile, so....

RE: gcc optimisation freertos and all that

Posted by kurator on January 19, 2009

no ideas around?
I know its an old FreeRTOS version but maybe
someone stumbled over something similar?

RE: gcc optimisation freertos and all that

Posted by Rainer Bittendorf on January 13, 2012
Hi kurator,

I have a problem similar to yours. I am using gcc 4.3.3 and FreeRTOS 6.0.4 on Atmel AT91SAM7S. Everythins seems to work fine but from time to time the return value of 'cComReceiveChar ()' is lost and 'vReadLine ()' receives '\0' instead. The functions are located in different modules. The problem is dependent on optimization level: With -O3 and -O2 it occurs, with -O1 or without optimization it doesn't. I checked stack sizes and found that there's space left on every stack I use. With better optimizations I found that variables are placed in registers - so memory location overwrites are improbable. The return value is returned in r0 (as in your code).

portCHAR cComReceiveChar (portBASE_TYPE xComIndex)
portCHAR cReceived;

switch (xComIndex)
case 0: xQueueReceive (xCom0Receive, & cReceived, portMAX_DELAY);
case 1: xQueueReceive (xCom1Receive, & cReceived, portMAX_DELAY);
default: cReceived = '\0';
return (cReceived);
} /* cComReceiveChar () */

static void vReadLine (portCHAR *pcLine, portBASE_TYPE xMaxChars, portBASE_TYPE xComIndex, portCHAR cTerm)
portBASE_TYPE xIndex = 0;
portCHAR cCom;

cCom = cComReceiveChar (xComIndex);
pcLine [xIndex++] = cCom;
} while ((pcLine [xIndex - 1] != cTerm) && (xIndex < xMaxChars));


} /* vReadLine () */

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

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.


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


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