Skip to main content
yakabmarci
Associate III
May 7, 2019
Solved

how to use float in printf?

  • May 7, 2019
  • 7 replies
  • 20658 views

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)

This topic has been closed for replies.
Best answer by Ethan HUANG

​Besides enabling -u _printf_float (either by manually typing in MCU GCC Linker/Miscellaneous or checking option in MCU Settings), please modify _estack in linker script for example from 0x2001ffff to 0x20020000 as described in the thread:

https://community.st.com/s/question/0D50X0000AldaPzSQI/cubeide-sprintf-does-not-work-with-f

7 replies

Ozone
Principal
May 8, 2019

> bad(new project):

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

This most probably means you selected a library version without float support in this case.

I'm no CubeIDE user, but such options are usually set while creating the project. Check the IDE documentation/help on how to fix it.

yakabmarci
Associate III
May 8, 2019

Thanks,

There is not need to repeat what i said, i know that this is the problem, the wrong printf is included even tho I specified that I want the float one with the linker flag -u _printf_float. The -u _printf_float is what is supposed to fix this, and it does for the imported project, but not for the newly generated one

Ozone
Principal
May 8, 2019

> There is not need to repeat what i said, ...

It is. as reference to the point I'm referring to.

> The -u _printf_float is what is supposed to fix this, and it does for the imported project, but not for the newly generated one

Perhaps because it referenced another library ? You could compare the sw4stm32 version and the CubeIDE version, to find out.

Check the CubeIDE documentation, as said.

Knut_Knusper
Associate II
May 8, 2019
yakabmarci
Associate III
May 8, 2019

This is so annoying I spent so much time on this.

Now i checked that the right printf is included but it only prints 0.0

The testcode is very simple, just create a standard template project for any stm32 (i tested this stm32f746ng), add code below somewhere

add option "Use float with printf from newlib-nano (-u _printf_float)" in the Tool Settings\MCU Settings

check the buffer via debugger.

any ideas on this?

volatile float a = 3.14f;

char buff[100];

while(1){

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

a+= 0.001234;

}

stm32ide project -> it only prints a=0.0

sw4stm32 project-> works correctly

binok
Associate
May 18, 2020

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

Knut_Knusper
Associate II
May 8, 2019

Did you try the Runtime Library "Standard C" and the option Use float with printf from newlib-nano (-u _printf_float) together?

Because this Setting works at my projekt.

yakabmarci
Associate III
May 8, 2019

Thanks for the feedback,

I tried newlib (nano.spec) and standard lib, i have the same wrong result in both cases.

But -u _printf_float makes sense only if newlib-nano is used, is it not?

The difference is that the sw4stm32 is a c only project the stm32cubeide is a c++ one (tho only c code is used).

the first one is linked with arm-none-eabi-gcc, the other with arm-none-eabi-g++

I think the problem is in this area somehow, both have the same options, the c++ one has additional options (-lstdc++ -lsupc++)

Edit:

Note: the stm32 project wizard always creates a c++ project, even if i select c project

Ethan HUANG
ST Employee
May 8, 2019

​Besides enabling -u _printf_float (either by manually typing in MCU GCC Linker/Miscellaneous or checking option in MCU Settings), please modify _estack in linker script for example from 0x2001ffff to 0x20020000 as described in the thread:

https://community.st.com/s/question/0D50X0000AldaPzSQI/cubeide-sprintf-does-not-work-with-f

yakabmarci
Associate III
May 8, 2019

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

Knut_Knusper
Associate II
May 8, 2019

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

yakabmarci
Associate III
May 8, 2019

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

Day.Max
Visitor II
June 15, 2019

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.