2021-06-21 08:42 AM
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!
2021-06-21 10:20 AM
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.
2021-06-21 10:24 AM
STDIO implementations frequently buffer
2021-06-21 10:25 AM
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);
2021-06-25 08:46 AM
yes, HAL transfer function will always works. I guess the printf implementation is more of a runtime library question.