2024-09-19 08:01 PM - last edited on 2024-09-20 08:00 AM by SofLit
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".
Solved! Go to Solution.
2024-09-20 08:09 AM - edited 2024-09-20 09:16 AM
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:
So you need to select USART3 instead of USART2
Refer to this printf example. on NUCLEO-F207
Hope it helps.
2024-09-19 09:22 PM
Hello,
You didn’t tell which board you are using?
2024-09-19 09:41 PM
Hello!
I am using a Nucleo board.
2024-09-20 07:42 AM - edited 2024-09-20 07:44 AM
Which Nucleo board?
Please see the posting tips - including how to properly post source code:
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:
2024-09-20 08:09 AM - edited 2024-09-20 09:16 AM
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:
So you need to select USART3 instead of USART2
Refer to this printf example. on NUCLEO-F207
Hope it helps.
2024-09-23 07:15 PM
I was able to solve the problem with the help of your schematic and example.
Thanks.
>You should always check the schematic or the board's user manual before suspecting a FW problem.
→You're right...
Also, see the tips in your posting - how to properly post source code, etc:
I'll be careful about that too.
2024-09-24 02:57 AM - edited 2024-09-24 03:54 AM
@t-oka wrote:>You should always check the schematic or the board's user manual before suspecting a FW problem.
That's probably the key difference between embedded C on small microcontrollers, and C on "big" systems (Linux, Windows, et al