cancel
Showing results for 
Search instead for 
Did you mean: 

How to use printf() ? stm32f4, stm32cubeide 1.1.0, ubuntu18.04, cutecom, uart1, ch341usb.

Wwei.1007
Associate II

In windows7, my project (iar) can send string to serial ports with printf, but in ubuntu 18.04 (stm32cubeide), HAL_UART_Transmit can send char, but it seems that printf do nothing.

Thanks for your help.

5 REPLIES 5

With GNU/GCC you will need to make sure the pluming is present and connected.

ST typically has a _write() function in syscalls or newlib that calls __io_putchar(), and that has to output characters to your chosen UART, via HAL_UART_Transmit() or whatever.

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

thank you for your answer.

I read the example at STM32Cube_FW_F4_V1.24.1/Projects/STM32F410xx-Nucleo/Examples/UART/UART_Printf, and i use the code in my main file before i ask the question. By the way , i use c++, not c:

#ifdef __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)

#else

 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE

{

 /* Place your implementation of fputc here */

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

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

 return ch;

}

in ubuntu 18.04, if i test HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF), so i can see a test char with cutecom

but if i test printf("test"), not happened with cutecom, no output, no error

What should I do...

Wwei.1007
Associate II

I move ​__io_putchar from main file to syscalls.c, so now __io_putchar can be called after _write() was called.

I see the "test" with cutecom now...

But I don't know if I should do it(move ​__io_putchar to syscalls.c file ). It seems that something is not so good

Put a breakpoint in your PUTCHAR_PROTOTYPE and see if it is hit.

-- pa

​__io_putchar in syscalls.c usually is declared as weak, so no need to modify syscalls.c.

It can stay in main.c. Use a breakpoint to verify it is called.

-- pa