How to use printf in the stm32duino?

I include the <stdio.h> <stdlib.h> even overwrite the fputc function, but there is no output, but I use Serai.printf it can print on the screen.

Could you help me solve the problem?





meaning calling printf() with no self-defined _write or fputc should try to initialize the UART at pins PIN_SERIAL_TX at a default of 9600 baud (or the baud defined by build_flags = -DDEBUG_UART_BAUDRATE =...) and print the message there.

I want to define by myself, usually, When I use stm32 in the cubeide, I redefine the fputc and I can use the printf, but with your help, I still do not know how to use it , I init the Serial.begin(9600) and prinf(“Hello World”) run in the loop, it still no effect?

How can I do?


Works also

Yeah actually 0 need to call into uart_debug_init

Probably your pin settings / macros are wrong.

I do it as your settings, It still do not work,

and I comment it and try to set it in the variant_generic.h file, it still not work

May be it was caused by I use the FreeRTOS? And I find some ridiculous place, I use printf in a other .c file it can work, I define three functions in that file, but only one function can work? I can not figure out what happend?

Can you reproduce this for your genericSTM32F407VET6 in a minimal project 1:1 without anything else?

Thank you for your help, I do it as your suggestion, and I show the result.
Hardware: nucleo-l476rg
First, I do it as follow, it works well,

But if I add Serial.println() as the picture shows, it works wrong, and printf only work well in the loop()

Serial.println conflicts with the printf logic. The Serial uses a buffer and starts transmitting one character, then in its interrupt sends the next character until it’s all done. The _write implementation used by printf() uses HAL_UART_Transmit to try and transmit all characters but gets then overruled by the probably interrupt logic.

What happens when you add a Serial.flush(); after every Serial.print..and a fflush(stdout); (may need #include <stdio.h>) after every printf()? No conflict should occur then and all should be printed.

Add as your advise, it succeed, but after the freeRTOS kernel start, even I do not use the Serial.flush and fflush(), the printf can work well sometimes, how to explain it.

In addition, I use a log library to print my system’s log, when I port the hal layer’s code like [logWrite()],

  1. Serial.write or 2.printf, which is better?

Thank you for your help, everytime you are so patiently and knowledgeable!

Another FreeRTOS task is causing a race condition? Use a mutex when wanting to print something from 2 threads simultaneously.

For printing pure strings or simple concatinated string, Serial.println(), then next in line would be Serial.printf(),

And after that, printf. The Serial object has a good implementation of printing characters without blocking the CPU too much, the _write implementation not so much.