cancel
Showing results for 
Search instead for 
Did you mean: 

"printf" implementation question\n\r

zrs5532
Associate II

Hi,

I implemented printf on my DISCO-F407VG board according to this tutorial: http://www.emcu.eu/how-to-implement-printf-for-send-message-via-usb-on-stm32-nucleo-boards/

however, I found that if I do not include "\n\r" at the end of a print, I do not receive any character on my terminal. Neither does only "\n" or "\r". But "\r\n" or "\n\r" both work.

for example, I start a bare metal project from stm32cube IDE for my board, and have

printf("this is a testing script for printf: %d\r\n", cycle);

in my while loop, and included "#include <stdio.h>"

when it works, I can see the led flashing on my uart to usb dongle, when it does not work, I do not see it flashing. I am using Tera Term on my PC.

I actually do not quiet understand how the printf function is implemented on stm32. I can only see printf is prototyped in stdio.h file as:

int printf (const char *__restrict, ...)

        _ATTRIBUTE ((__format__ (__printf__, 1, 2)));

so if anybody could direct me to somewhere or give some explain would be most appreciated! thank you so much!

4 REPLIES 4
TDK
Guru

The terminal program is probably caching characters until it received CRLF. Print \r\n only every 3rd call and you'll probably see the output all at once.

It's also possible printf/stdout is the one buffering. How printf is implemented will depend on your compiler and what library you're using.

https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin

If you feel a post has answered your question, please click "Accept as Solution".

STDIO implementations frequently buffer

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

fflush() can often be used, and querying for input can also force output in many cases.

Consider if sprintf(), or vsprintf() type implementation, with a means of pushing the data directly to the output would be a better solution.

ie HAL_UART_Transmit(hUart, string, sprintf(string,"%10d", x), 1000);

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

yes, HAL transfer function will always works. I guess the printf implementation is more of a runtime library question.