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


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