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


double/float problem with *printf

Posted by Maziar on April 22, 2011
Dear FreeRTOS people,

Ive been messing around with an annoying problem in FreeRTOS and after few hours of testing, google searching i still cant resolve my problem. I am currently developing on a LM3S6965 Ethernet Eval Board.

In my project its necessary to use float and double but i cant seem to get it right.

INT8U buffer[50];
float value = 123.45;
sprintf(buffer, "%3.2f", value);

When following code has been executed the buffer still remains 0x00 in all fields. Although normal integers does work with sprintf.

The interesting part of my config looks like following:

#define configUSE_PREEMPTION1
#define configUSE_IDLE_HOOK0
#define configUSE_TICK_HOOK0
#define configCPU_CLOCK_HZ( ( unsigned portLONG ) 50000000 )
#define configTICK_RATE_HZ( ( portTickType ) 100 )
#define configMINIMAL_STACK_SIZE( ( unsigned portSHORT ) 40 )
#define configTOTAL_HEAP_SIZE( ( size_t ) ( 2400 ) )
#define configMAX_TASK_NAME_LEN( 12 )
#define configUSE_TRACE_FACILITY0
#define configUSE_16_BIT_TICKS0
#define configIDLE_SHOULD_YIELD0
#define configUSE_CO_ROUTINES 0
#define configUSE_MUTEXES1

And my portmacro.h

/* Architecture specifics. */
#define portSTACK_GROWTH( -1 )
#define portTICK_RATE_MS( ( portTickType ) 1000 / configTICK_RATE_HZ )
#define portBYTE_ALIGNMENT8

Basically it does not function in any way by using float or double.

Is it a hardware restriction or have a simply overseen something?

Thanks in advance.

RE: double/float problem with *printf

Posted by Richard on April 22, 2011
Many of the standard FreeRTOS demos include a tiny printf()/sprintf() implementation that is contained in a file called printf-stdarg.c. Are you including this file in your build, because it does not include floating point support. If you are including it, simply remove it to fall back to the default compiler provided library version, and then it should work (but will also take a lot more stack space to execute).


RE: double/float problem with *printf

Posted by Maziar on April 22, 2011
Thanks for the quick reply richardbarry.

I am using a template from Code Red and as far as i can see in the directory theres no such file as printf-stdarg.c

RE: double/float problem with *printf

Posted by Maziar on April 23, 2011
Unfortunately the problem was not solved, but i found another solution.

RE: double/float problem with *printf

Posted by jinx on May 20, 2011
Do you mind sharing your solution. I was having the exact same issue and I saw this post. I'm curious as to how you solved it.

RE: double/float problem with *printf

Posted by Franz on April 13, 2012
Dear freeRTOS Team!

I am facing the exact same problem with my STM32F4. I am using the same settings like flashyarm. I found that this problem only occures when using the port.c for the Cortex M4F with FPU support activated. when using the M3 port ( no FPU support) it is working well. However internally the floating point calculations seem to be right, because when i cast them to int and then call printf() the values are right. Is there any solution to this?

@flashyarm could you please be so kind to share your solution with us?


RE: double/float problem with *printf

Posted by Richard on April 13, 2012
Some of these problems were caused by stack alignment issues with 8 byte data types. The Cortex-M4F code is new, so could potentially have the same problem, but there are safeguards in the code now that should prevent it. Put a break point on entry to a task (before the variable declarations), and look at the stack pointer value when the break point is hit - is it eight byte aligned?


RE: double/float problem with *printf

Posted by Franz on April 13, 2012
Thanks richardbarry for your quick reply!

You're right it is not byte-aligned - The stack pointer value modulo 8 gives 4 rest.
How can I fix this?


RE: double/float problem with *printf

Posted by Richard on April 13, 2012
Hmm. Which FreeRTOS version are you using, and are you using the official FreeRTOS CM4F code? If it is official code you should see something like this in the IAR CM4F port.c file:

portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
/* Simulate the stack frame as it would be created by a context switch
interrupt. */

/* Offset added to account for the way the MCU uses the stack on entry/exit
of interrupts, and to ensure alignment. */
pxTopOfStack -= 2;

That line of code should have taken care of your problem, but may need more or less subtracting from it. I can look at this when I have some CM4F hardware with me.


RE: double/float problem with *printf

Posted by Franz on April 13, 2012
Hi richardbarry!
thanks for your answer!

I am using the latest freertos V7.1.0 but I am using the RVDS port.c for KEIL. Changing the value of pxTopOfStack does not take any effect. Any Ideas?

Thanks for your effort!

RE: double/float problem with *printf

Posted by Richard on April 13, 2012
Please try the following.

In FreeRTOS/Source/Portable/RVDS/ARM_CM3/portmarco.h, add the following line:

#define portALIGNMENT_ASSERT_pxCurrentTCB ( void )

then, in FreeRTOS/Source/Portable/RVDS/ARM_CM3/port.c, change the first line in pxPortInitialiseStack() from

pxTopOfStack -= 2;


pxTopOfStack -= 1;

and let me know if that fixes the problem of printing floating point numbers.

Thanks and regards.

RE: double/float problem with *printf

Posted by Franz on April 14, 2012
You're great!

This fixes my Problem, now it's working just fine!

Thank you!

[ 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