cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble with printf and sprintf with float in STM32Cube + VS Code (CMake)

umut373
Associate

Hi everyone,

I'm working on an STM32 (Nucleo-F446RE to be spesific) project using the STM32Cube extension for VS Code, not CubeIDE. I'm using CMake for building the project.

I encountered a few issues:

  1. printf doesn't print anything to the serial monitor, so I switched to using HAL_UART_Transmit.

  2. When I try to print a float using sprintf(msg, "value: %f\n", var);, it doesn’t work. The output just skips the float value (e.g., "value: \n").

  3. After some research, I found out I need to add "-u _printf_float" to the linker flags to enable float formatting support with printf and sprintf.
    However, I'm new to CMake and I couldn’t figure out which CMake file I need to modify in the project folder to add this flag.

I'd really appreciate it if someone could guide me:

  • Where exactly should I add the "-u _printf_float" flag?

  • Is there anything else I should do to make printf work properly over UART?

Thanks in advance for your help.

1 REPLY 1
Andrew Neil
Super User

@umut373 wrote:
  1. printf doesn't print anything to the serial monitor


Have you followed the instructions:

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.