Skip to main content
Senior III
March 25, 2026
Solved

UART printing gibbersh to TeraTerm

  • March 25, 2026
  • 5 replies
  • 810 views

NUCLEO F767ZI, CubeIDE 2.1.1, CubeMX 6.17.0

I'm trying to set up UART with printf()

#include <stdio.h>

UART_HandleTypeDef huart3;
static void MX_USART3_UART_Init(void);

int __io_putchar(int ch)
{
 HAL_UART_Transmit(&huart3, (uint8_t *)ch, 1, 10);
 return ch;
}

....
// in main
// in while
prinf("UART test\r\n");

// MX generated USART3 init
static void MX_USART3_UART_Init(void)
{

 /* USER CODE BEGIN USART3_Init 0 */

 /* USER CODE END USART3_Init 0 */

 /* USER CODE BEGIN USART3_Init 1 */

 /* USER CODE END USART3_Init 1 */
 huart3.Instance = USART3;
 huart3.Init.BaudRate = 115200;
 huart3.Init.WordLength = UART_WORDLENGTH_8B;
 huart3.Init.StopBits = UART_STOPBITS_1;
 huart3.Init.Parity = UART_PARITY_NONE;
 huart3.Init.Mode = UART_MODE_TX_RX;
 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
 huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART3_Init 2 */

 /* USER CODE END USART3_Init 2 */

}

 

Clock tree from MX

MX_CLK_Tree.jpg

 

Output in Tera Term window

TT_window.png

 

TT is set up exactly the same as the UART init in the code.

Anyone know whats going on here?

Best answer by NicRoberts

My original code,

HAL_UART_Transmit(&huart3, (uint8_t *)ch, 1, 10);

 

What it should be,

HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);

 

Notice the & in front of the ch

 

5 replies

mƎALLEm
Technical Moderator
March 25, 2026

Hello,

You have set a wrong HSE clock value to 25MHz:

screenshot.png

You need to set it to 8MHz as the ST-LINK-V2 MCO output on that Nucleo board is 8MHz

"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."
Senior III
March 25, 2026

Still gibberish

Revised_clk_tree.png

mƎALLEm
Technical Moderator
March 25, 2026

Did you regenerate the code, compile it with that config and upload the application to the MCU?

"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."
Andrew Neil
Super User
March 25, 2026

Gibberish on a terminal is almost always due to wrong baud rate.

As @mƎALLEm said, if you enter the wrong value for the HSE clock frequency, then your baud rate will be wrong - as will any other frequencies derived from HSE.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
Technical Moderator
March 25, 2026

And what if you select HSI as clock source instead of HSE?:

screenshot.png

If it's working, that means definetly you have an issue with HSE, a missing solder bridge on STLINK MCO output path or something similar. If not, you have an issue with TeraTerm UART configuration.

"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."
Senior III
March 25, 2026

No not working with HSI either
Revised_clk_tree_again.png

 

Settings in TT are the same as in the USART3 init. (I've also tried RealTerm & get the same gibberish there too)

USART3_Init.png

mƎALLEm
Technical Moderator
March 25, 2026

Configure the data lenght in Tera term to 7 bit instead of 8 bit.

This is the example from CubeF7 https://github.com/STMicroelectronics/STM32CubeF7/blob/master/Projects/STM32F767ZI-Nucleo/Examples/UART/UART_Printf/

that suggests in its readme file that you need to set the datalenght to 7 bit:

screenshot.png

This is what was said in a comment the example code: Word Length = 8 Bits (7 data bit + 1 parity bit)

 /* UART configured as follows:
 - Word Length = 8 Bits (7 data bit + 1 parity bit) : 
	 BE CAREFUL : Program 7 data bits + 1 parity bit in PC HyperTerminal
 - Stop Bit = One Stop bit
 - Parity = ODD parity
 - BaudRate = 9600 baud
 - Hardware flow control disabled (RTS and CTS signals) */
 UartHandle.Instance = USARTx;

 UartHandle.Init.BaudRate = 9600;
 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
 UartHandle.Init.StopBits = UART_STOPBITS_1;
 UartHandle.Init.Parity = UART_PARITY_ODD;
 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 UartHandle.Init.Mode = UART_MODE_TX_RX;
 UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
 if (HAL_UART_Init(&UartHandle) != HAL_OK)
 {
 /* Initialization Error */
 Error_Handler();
 }

 This is apart of the wrong HSE frequency value you set in CubeMx: 25MHz instead of 8MHz (the issue I raised in a previous post).

Hope that helps

"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."
Senior III
March 26, 2026

@mƎALLEm 
OK found it after comparison with the reference code linked above. user error (when isn't it) it turns out I wasn't retargeting the printf function correctly. A missing ampersand in the data passed to the UART_transmit function,

 

int __io_putchar(int ch)
{
 HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);
 return ch;
}

 

mƎALLEm
Technical Moderator
March 26, 2026

@NicRoberts wrote:
int __io_putchar(int ch)
{
 HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);
 return ch;
}

 


Sorry I didn't understand your feedback. What is the difference between the retargeting printf you have shared now and what you have shared in your original post? I don't see any difference!: 

int __io_putchar(int ch)
{
 HAL_UART_Transmit(&huart3, (uint8_t *)ch, 1, 10);
 return ch;
}

 

"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."
Andrew Neil
Super User
March 26, 2026

@mƎALLEm wrote:

What is the difference

Before After:

HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);
 ^-- Here!

 

After Before:

HAL_UART_Transmit(&huart3, (uint8_t *)ch, 1, 10);
 ^-- Here!

 

@NicRoberts so the problem was that you were actually sending gibberish to the terminal!

 

This is exactly why I always recommend to get direct comms working first - before adding the extra complications of printf redirection!


Edited to correct 'Before' & 'After'

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
CTapp.1
Senior III
March 27, 2026

One of those mistakes that we've probably all made that take forever to spot as you read what you thought you had typed when looking at the code ;)

Something that static analysis can help with on a good day.

All posts are made in a personal capacityMISRA C++ ChairMISRA C WG MemberDirector The MISRA Consortium Limited (TMCL)
Andrew Neil
Super User
March 27, 2026

Unfortunately, the mistake was masked by the (uint8_t*) cast - without that, you'd have got a compiler warning...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
CTapp.1
Senior III
March 27, 2026

@Andrew Neil wrote:

Unfortunately, the mistake was masked by the (uint8_t*) cast - without that, you'd have got a compiler warning...


GCC does generate a warning for it:

 Screenshot 2026-03-27 at 11.37.04.png

All posts are made in a personal capacityMISRA C++ ChairMISRA C WG MemberDirector The MISRA Consortium Limited (TMCL)