Skip to main content
Associate
July 31, 2026
Question

STM32G474RET6 USART

  • July 31, 2026
  • 7 replies
  • 97 views

Hello, I am using the stm32g47ret6 microcontroller and I am having troubles with transmitting and receiving data using the USART2 to my PC through the ST-LINK V3-SET. If I understand correctly, I left the v3-set configured as default (did not change the jumper configurations). I used MX to configure the usart2 module and used vcp_rx and vcp_tx for communication, used the pins PA3 and PA2 and also configured them in MX. I checked the baud rates, COM Port in my device manager. I am using tera term 5 to read the data, but I get a blank screen every time I start the program. To debug this, I tried probing the pins using an oscilloscope and I do see some repeated pulses, the baud rate matches and Uppercase 'U’ does give a square wave. I also checked the UART transmit status which shows HAL_OK in live expressions in the CUBEIDE. I am using a custom hardware board with the stm32g474ret6 mcu, it is connected to the st-link via the stdc14 connecter. This is the schematic for it.

 

 Below is the code. 

/* USER CODE BEGIN Header */

/**

******************************************************************************

* @file : main.c

* @brief : Main program body

******************************************************************************

* @attention

*

* Copyright (c) 2026 STMicroelectronics.

* All rights reserved.

*

* This software is licensed under terms that can be found in the LICENSE file

* in the root directory of this software component.

* If no LICENSE file comes with this software, it is provided AS-IS.

*

******************************************************************************

*/

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"



/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */



/* USER CODE END Includes */



/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */



/* USER CODE END PTD */



/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */



/* USER CODE END PD */



/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */



/* USER CODE END PM */



/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;



/* USER CODE BEGIN PV */

uint8_t data =0;

HAL_StatusTypeDef uart_status;

uint8_t message[] = "Hello STM32\r\n";

/* USER CODE END PV */



/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */



/* USER CODE END PFP */



/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */



/* USER CODE END 0 */



/**

* @brief The application entry point.

* @retval int

*/

int main(void)

{



/* USER CODE BEGIN 1 */



/* USER CODE END 1 */



/* MCU Configuration--------------------------------------------------------*/



/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();



/* USER CODE BEGIN Init */



/* USER CODE END Init */



/* Configure the system clock */

SystemClock_Config();



/* USER CODE BEGIN SysInit */



/* USER CODE END SysInit */



/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

// uint8_t message = 123;





/* USER CODE END 2 */



/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */



/* USER CODE BEGIN 3 */



uart_status = HAL_UART_Transmit(

&huart2,

message,

sizeof(message) - 1,

HAL_MAX_DELAY

);

data++;

HAL_Delay(1000);





/* USER CODE END 3 */

}



/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};



/** Configure the main internal regulator output voltage

*/

HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);



/** Initializes the RCC Oscillators according to the specified parameters

* in the RCC_OscInitTypeDef structure.

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}



/** Initializes the CPU, AHB and APB buses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;



if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

{

Error_Handler();

}

}



/**

* @brief USART2 Initialization Function

* @param None

* @retval None

*/

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_WORDLENGTH_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;

huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN USART2_Init 2 */



/* USER CODE END USART2_Init 2 */



}



/**

* @brief GPIO Initialization Function

* @param None

* @retval None

*/

static void MX_GPIO_Init(void)

{

/* USER CODE BEGIN MX_GPIO_Init_1 */



/* USER CODE END MX_GPIO_Init_1 */



/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();



/* USER CODE BEGIN MX_GPIO_Init_2 */



/* USER CODE END MX_GPIO_Init_2 */

}



/* USER CODE BEGIN 4 */



/* USER CODE END 4 */



/**

* @brief This function is executed in case of error occurrence.

* @retval None

*/

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

__disable_irq();

while (1)

{

}

/* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t *file, uint32_t line)

{

/* USER CODE BEGIN 6 */

/* User can add his own implementation to report the file name and line number,

ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

 

7 replies

Andrew Neil
Super User
July 31, 2026

Welcome to the forum

Please see How to write your question to maximize your chances to find a solution for best results.

See also: How to insert source code

 

In particular, what board are you using, and how is it connected to your STLink ?

Show your schematic; Some good, clear photos of your setup could help.

 

Before adding the complications of an external STLink, have you got this working on a Nucleo board (or similar) with built-in STLink, using a standard ST example?

 

 I tried probing the pins using an oscilloscope and I do see some repeated pulses

Are they valid UART frames, with the expected format, at the expected baud rate ?

 

For easier scope triggering, just send a single repeating character - Uppercase ‘U’ is good, as it gives a square wave...

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.
SamBAuthor
Associate
July 31, 2026

I am using a custom hardware board with the stm32g474ret6 mcu, it is connected to the st-link via the stdc14 connecter. This is the schematic for it. I also checked the baud rate and Uppercase 'U’ does give a square wave.

 

Andrew Neil
Super User
July 31, 2026

I am using a custom hardware board

Before going to a custom board, have you got this working on a known-good ST board?

Have you tried a separate, known-good UART-to-USB converter?

Have you tried a loop-back from your TeraTerm, through the STLink, back to TeraTerm?

 

I also checked the baud rate

How, exactly, did you do that?

 

Uppercase 'U’ does give a square wave

With correct framing, baud rate, etc ?

 

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.
Associate II
July 31, 2026

In your question I miss the connection from PA3 and PA2 to your PC?

You will need some UART/USB converter probably, since PA2 and PA3 are 0/3.3V level.

Andrew Neil
Super User
July 31, 2026

@mfgkw OP is using an ST-Link V3/Set

 

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.
Associate II
July 31, 2026

sorry, I missed that