cancel
Showing results for 
Search instead for 
Did you mean: 

Retargeting printf on STM32F746G

Renaud Dk
Associate II

Hi,

I'm using STM32CubeIDE and I'm trying to retarget printf to the USART1 of the STM32F746G.

USART1 of the MCU is connected to the ST-Link Virtual com. I have tested the connection the USART1 directly using the function "HAL_UART_Transmit(&huart1, "A", 1, 0xFFFF)" this does work meaning that the hardware is correctly setup. So far I have implemented __io_putchar() function in my main.cpp like the following :

int __io_putchar(int ch)

{

 /* 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;

}

I took the code from the one of the example in the STM32Cube_FW_F7_V1.15.0 but for an other board since there is no UART_printf example for the STM32F746G.

Any of what could be wrong with my implementation ?

3 REPLIES 3
Olivier GALLIEN
ST Employee

Hi @Renaud Dk​ 

Does your project embed syscall.c ?

Else customized _write function has to be defined also.

Please refer to "AN4989 STM32 microcontroller debug toolbox" Chapter 7.

https://www.st.com/content/ccc/resource/technical/document/application_note/group0/3d/a5/0e/30/76/51/45/58/DM00354244/files/DM00354244.pdf/jcr:content/translations/en.DM00354244.pdf

Olivier

Olivier GALLIEN
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.
Renaud Dk
Associate II

Hi @Community member​ 

Yes I have syscall.c integrated in my project (under [PROJECT_ROOT]/Core/Src). I read the app note and this why I chose to implement the __io_putchar() instead of _write(). One word about my project configuration, my main.cpp is in C++ because I'm using touchgfx framework with CMSIS-Freertos.

Renaud

Pavel A.
Evangelist III

> Any of what could be wrong with my implementation ?

Try two small changes before digging deeper:

1: in your __io_putchar:

extern "C"
int __io_putchar(int ch)
{
    if (ch == '\n')
        HAL_UART_Transmit(&huart1, (uint8_t *)"\r", 1, 0xFFFF);
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
    return ch;
}

2: in UART1 advanced settings (in CubeMX) - make sure to disable overrun detection.

-- pa