cancel
Showing results for 
Search instead for 
Did you mean: 

"System Workbench�?(SW4STM32) printf redirection not work

L C
Associate II

Dear All

I would like to redirect printf output to UART.

It seem very simple in this field and many example in internet.

I try that with "System Workbench"(SW4STM32).

The IDE doesn't show anything wrong.

adding below to "uart.c" 

#ifdef __GNUC__
  /* With GCC/RAISONANCE, 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__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
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;
}

calling printf() in "main.c"

printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");

The "printf()" seem don't work in my chip.

But "HAL_UART_Transmit" is function in main.c. That imply the UART port work fine.

Then try adding something GPIO action(output toggle) inside the "PUTCHAR_PROTOTYPE" routine. If printf() running, "PUTCHAR_PROTOTYPE"(fputc() or __io_putchar()) should be call. The GPIO action hasn't execute, mean "PUTCHAR_PROTOTYPE" hasn't be call.

Is there any where necessary to modify for such redirection except "uart.c"?

My code is generated by STM32CubeMX and hasn't "syscall.c"

Is it a root cause of my problem?

Or System Workbench is not support printf() redirection?

Please share your comment.

BR

1 REPLY 1
David SIORPAES
ST Employee

Hello @L C​ ,

you are right you'd need to add syscall.c (example attached) or directly implement

int _write(int file, char *ptr, int len);