cancel
Showing results for 
Search instead for 
Did you mean: 

Help understanding why my UART printf actually works - linker query

John123
Associate II

NUCLEO-C031C6 board.

My printf over the UART works fine - would anyone be able to explain the 'bold' comment below.

How does such a simple bit of code overload the whole printf function.

What is 'small printf' ?

Regards, John.

 

#if defined(__GNUC__)

/* With GCC, small printf (option LD Linker->Libraries->Small printf

set to 'Yes') calls __io_putchar() */

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#endif

.

.

PUTCHAR_PROTOTYPE

{

/* Place your implementation of fputc here */

/* e.g. write a character to the USART2 and Loop until the end of transmission */

HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

 

return ch;

}

1 ACCEPTED SOLUTION

Accepted Solutions
STea
ST Employee

Hello @John123 ,

the printf implementation in the standard library is using low-level output function like __write which uses the __io_putchar() function to put characters in the std output so by surcharging this function to call the uart_transmit() function we will get printf printing over UART with the Virtual Com Port (VCP).

so, by redefining the __io_putchar() function declared as weak in the syscalls.c into your main you simply are redirecting the function that output characters in the standard output to output to UART.

BR

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
STea
ST Employee

Hello @John123 ,

the printf implementation in the standard library is using low-level output function like __write which uses the __io_putchar() function to put characters in the std output so by surcharging this function to call the uart_transmit() function we will get printf printing over UART with the Virtual Com Port (VCP).

so, by redefining the __io_putchar() function declared as weak in the syscalls.c into your main you simply are redirecting the function that output characters in the standard output to output to UART.

BR

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
John123
Associate II

Many thanks