cancel
Showing results for 
Search instead for 
Did you mean: 

UART printing gibbersh to TeraTerm

NicRoberts
Senior II

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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

 

View solution in original post

21 REPLIES 21
mƎALLEm
ST Employee

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.
Andrew Neil
Super User

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.

Still gibberish

Revised_clk_tree.png

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.

Yes

mƎALLEm
ST Employee

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.

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

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.

Sorry but this doesn't solve the issue either.

Even reconfiguring the UART with an odd partity bit & changing the corresponding settings in TT as suggested, I'm still getting gibberish.