cancel
Showing results for 
Search instead for 
Did you mean: 

CubeIDE sprintf does not work with %f

KWuer
Associate II

When I create a new project and put in "-u _printf_float"  as linker option and select "Use float with printf from newlib-nano (-u _printf_float) then sprintf does not convert float values.

but when I import a TrueStudio project where sprintf works with floats, then it works also in CubeIDE. What did I miss?

Compiler Settings:

-mcpu=cortex-m4 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DSTM32F446xx -DDEBUG -c -I../Inc -I../Drivers/CMSIS/Include -I../Drivers/STM32F4xx_HAL_Driver/Inc -I../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb

Linker settings:

-mcpu=cortex-m4 -T"D:\HiDrive\STM32\workspace_1.0.0\printf\STM32F446RETX_FLASH.ld" --specs=nosys.specs -Wl,-Map="${ProjName}.map" -Wl,--gc-sections -static -u _printf_float --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -Wl,--start-group -lc -lm -Wl,--end-group

11 REPLIES 11
KWuer
Associate II

It looks like that I found the problem. The linkersript is wrong. The value _estack = 0x2001ffff; should be point one byte after the stack _estack = 0x20020000;

Then snprintf works again.

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

>>The value _estack = 0x2001ffff;

I can't believe that continues to permeate

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Just wasted several hours until I found that stackoverflow answer... yay!

GStra.1
Associate II

I'm experiencing the same problems using

STm32cubeide Version: 1.2.1 with Nucleo-F446RE.

My printf works with strings and integers but crashes

my microcontrollor when i try to print floats.

You have said that the mistake is in linker file definition

_estack.

In my link file i read:

/* Highest address of the user mode stack */

_estack = ORIGIN(RAM) + LENGTH(RAM); /* 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

{

 RAM  (xrw)  : ORIGIN = 0x20000000, LENGTH = 128K

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

}

What do you think?

G.

Your _estack is OK. Look for soft/hard floating point compiler configuration and this:

https://community.st.com/s/question/0D50X0000BB1eL7SQJ/bug-cubemx-freertos-projects-corrupt-memory

GStra.1
Associate II

Below my compiler configuration:

-ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb

Is it not enough?

Ml.1
Associate

Hello,

I am using STM32F429VE microcontroller with stm32cube ide and I have the same problem, but I can not get fix it yet. I tried both:

*change _estack definition in linker file.

*add "-u _printf_float" as linker option

I attach both images. What is wrong?

Thanks!

RaphyLoulou
Associate II

Hello,

I read many topics dealing with the issue concerning float and sprintf, which I have on my CubeIDE project with STM32G474 microcontroller.

_estack definition in the linker file is good: _estack = ORIGIN(RAM) + LENGTH(RAM);

and project option are correct as mentionned previously.

The workaround that correct this issue is simply to remove the _sbrk function definition that can be found within sysmem.c file. It seems to get in conflict with memory management scheme used by FreeRTOS, even if I don't have the exact explanation.

Good luck!

Here is an explanation for the heap issues.