cancel
Showing results for 
Search instead for 
Did you mean: 

how to use float in printf?

yakabmarci
Senior

Float in printf doesn't seem to work in new projects created in stm32cubeide

If i create a project in sw4stm32 it works, there, it also works if i import the sw4stm32 project, it does not work if i create a new project in cubeide

In all cases the option -u _printf_float is used, but in case of new cubeide project, this setting is somehow ignored and the wrong prinf is included, visible in mapfile

good (imported project):

../arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard\libc_nano.a(lib_a-nano-vfprintf_float.o)

bad(new project):

../arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard\libc_nano.a(lib_a-printf.o)

14 REPLIES 14

Thanks, this fixed it, so the problem was stack alignment? just to make it clear...

Knut_Knusper
Associate III

That explains why it works in my project. I use a modified linker script.

yakabmarci
Senior

So here is the conclusion to the topic.

It is likely that others will run into this until is fixed.

The liker file generated by stm32cubeide stm32 wizard seems to be the issue.

The stack pointer is only 4bytes aligned.

Some operations, like working with doubles require 8 byte alignment.

Printing floats via %f, uses doubles internally.

So, to fix this you have to adjust the liker file so stack is 8 bytes aligned.

see _estack  in *.ld file

More details about the issue here:

https://stackoverflow.com/questions/28746062/snprintf-prints-garbage-floats-with-newlib-nano

Thank you guys for the support

You're right on that! I just ran into this with a STM32F401RE using STM32CubeIDE 1.0.1. It's not fixed yet! :(

In my case, I had in the linker scripts:

/* Highest address of the user mode stack */

_estack = 0x20017fff; /* end of "RAM" Ram type memory */

_Min_Heap_Size = 0x200 ; /* required amount of heap */

_Min_Stack_Size = 0x400 ; /* required amount of stack */

/* Memories definition */

MEMORY

{

  FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K

  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 96K

}

Changing the _estack setting to 0x20018000 solved my sprintf float not working.

sprintf(buff,"a=%.6f,a);