Skip to main content
Graduate II
September 20, 2024
Solved

I ran printf over UART but the results were not displayed in Tera Term

  • September 20, 2024
  • 1 reply
  • 2123 views

 

Hello everyone

I'm using stm32f207ztg6 and want to do printf using UART, but it doesn't show up in Tera Term.
How can I solve this problem?

■ Program used

 

// *-*-*↓main()-*-*-*-*-*-*-*-*-*-*-*-*
setbuf(stdout, NULL);
while (1)
{
/* USER CODE END WHILE */
printf("Hello World\r\n");
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

int _write(int file, char *ptr, int len)
{
HAL_UART_Transmit(&huart2,(uint8_t *)ptr,len,10);
return len;
}

static void MX_USART2_UART_Init(void)
{

/* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_W ORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */

/* USER CODE END USART2_Init 2 */

}

 

■STLink: STLink Virtual COM Port

■Situation
When HAL_UART_Transmit is executed, HAL_OK is returned, but nothing is displayed in "Tera Term".

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello @t-oka ,

    First, before starting the printf usage over UART, you need to validate the UART transmission by for example just sending a couple of characters using HAL_UART_Transmit().

    Second, you need always to check the schematics or board's user manual before suspecting an issue in the FW.

    According to the schematics of MB1137 the UART instance connected to STLink-VCP on NUCLEO-F207ZG (MB1137) is USART3 and not USART2:

    SofLit_0-1726844839253.png

    So you need to select USART3 instead of USART2

    Refer to this printf example. on NUCLEO-F207

    Hope it helps.

    1 reply

    Technical Moderator
    September 20, 2024

    Hello,

    You didn’t tell which board you are using?

    t-okaAuthor
    Graduate II
    September 20, 2024

    Hello!
    I am using a Nucleo board.

    Super User
    September 20, 2024

    Which Nucleo board?

    Please see the posting tips - including how to properly post source code:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     

    Before adding the complications of printf, have you got this working by just doing the HAL_UART_Transmit direct?

    If that doesn't work then, clearly, printf is never going to work!

     

    Also note that stdout is line-buffered - ie, the output won't appear until you send a newline...

     

    PS:

    https://community.st.com/t5/stm32cubeide-mcus/retarget-printf-on-stm32g070rbxx-cortex-m0/m-p/721066/highlight/true#M30729