cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-H7A3ZI-Q USART2 not communicating with serial console

KRISH_
Associate III

Originally a comment on this tutorial  - moved to main forum for better visibility & discussion ...


I followed the first part of the tutorial (enabled USART2) and included the following code snippets for my STM32 NUCLEO-H7A3ZI-Q board.

 
/* USER CODE BEGIN PV */
uint8_t buffer[1];
/* USER CODE END PV */
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive(&huart2, buffer, 1, 0xFFFF);
  HAL_UART_Transmit(&huart2, buffer, 1, 0xFFFF);
  /* USER CODE END 2 */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
	HAL_Delay(100);

    /* USER CODE END WHILE */

 But I wasn't able to write any characters or anything on my serial console. Kindly give some suggestions.

1 ACCEPTED SOLUTION

Accepted Solutions

@KRISH_ wrote:

After enabling Local echo in Putty I can see the characters I enter in  Putty.


That gives reassurance that you are actually hitting the keys but, as the name suggests, it is purely local to putty.

 


@KRISH_ wrote:

I haven't soldered any pins onto this board.


Is it a new board? Has anyone else used it before you?

 


@KRISH_ wrote:

I can see a 3.68V PWM signal on the PB10.


PWM?

Do you mean a UART signal?

The VCP is connected to pins PD8 (UART TX output) and PD9 (UART RX input):

AndrewNeil_0-1750927152698.png

You won't get anything through the VCP unless you use those pins!

 

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.

View solution in original post

16 REPLIES 16
Andrew Neil
Super User

Please show the full code.

 

  HAL_UART_Receive(&huart2, buffer, 1, 0xFFFF);
  HAL_UART_Transmit(&huart2, buffer, 1, 0xFFFF);

You should check the return values from the calls; eg, what if the Receive just times out without receiving anything?

 


@KRISH_ wrote:

 But I wasn't able to write any characters or anything on my serial console. .


What serial console are you using?

How is it connected to USART2 of your STM32 ?

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.

@Andrew Neil wrote:

How is it connected to USART2 of your STM32 ?


@KRISH_  If you're expecting to use the Virtual COM Port (VCP) in the ST-Link, that is connected to USART3 - not USART2:

AndrewNeil_1-1750845262372.png

 

https://www.st.com/resource/en/user_manual/um2408-stm32h7-nucleo144-boards-mb1363-stmicroelectronics.pdf#page=29

via: https://www.st.com/en/evaluation-tools/nucleo-h7a3zi-q.html#documentation

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.
KRISH_
Associate III

Merged - please keep the discussion in one place.


I checked the User manual, it said USART3 .But still I wasn't able to enter anything in the serial console.1.png2.png

KRISH_
Associate III

You're right. I tried with 

 

USART3, and I am still unable to enter anything in the serial console.

/* USER CODE BEGIN PV */
uint8_t buffer[1];
/* USER CODE END PV */

  

  /* USER CODE BEGIN 2 */
  HAL_UART_Receive(&huart3, buffer, 1, 0xFFFF);
  HAL_UART_Transmit(&huart3, buffer, 1, 0xFFFF);
  /* USER CODE END 2 */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
	HAL_Delay(100);

    /* USER CODE END WHILE */

The LED1 blinks 

So you are trying to go via the ST-Link's VCP, then?

Were you misled by the tutorial into using UART2?

 


@KRISH_ wrote:

I am still unable to enter anything in the serial console. 


How are you testing that?

Are you sure that your board hasn't been reconfigured?

Are you using the correct pins?

Are you sure your console is connected to the correct COM port?

Can you just send from the STM32 to the console?

Again, please show the complete code.

 

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.

Using Putty (serial connection), i am testing whether I can enter characters to the terminal. I believe the characters I enter should be visible to me. It is not just about HAL_UART_Receive, I can't see the transmitted data either with sprintf().

The pins shown in the user manual are PD8 and PD9, but by default, the assigned pins are PB10 and PB11.In the NVIC settings I have enabled USART3 as global interrupt.

I have chosen the correct COM port, which is COM20 (I double checked using the device manager). I do not know whether the data I am sending via USART is reaching the MCU. But the printf() statements do get displayed on the serial monitor. Screenshot 2025-06-25 194826.png

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 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 huart3;

/* USER CODE BEGIN PV */
uint8_t buffer[1];
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_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 */

  /* MPU Configuration--------------------------------------------------------*/
  MPU_Config();

  /* 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_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive(&huart3, buffer, 1, 0xFFFF);
  HAL_UART_Transmit(&huart3, buffer, 1, 0xFFFF);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
	HAL_Delay(100);

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
//	HAL_UART_Transmit(&huart2, tx_data, sizeof(tx_data), 10);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /*AXI clock gating */
  RCC->CKGAENR = 0xFFFFFFFF;

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** 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_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = 64;
  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_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART3 Initialization Function
  *  None
  * @retval None
  */
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.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */

  /* USER CODE END USART3_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  *  None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* USER CODE BEGIN MX_GPIO_Init_1 */

  /* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pin : PB0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN MX_GPIO_Init_2 */

  /* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//  /* Prevent unused argument(s) compilation warning */
//  UNUSED(huart);
//
//  /* NOTE : This function should not be modified, when the callback is needed,
//            the HAL_UART_RxCpltCallback can be implemented in the user file.
//   */
//  HAL_UART_Transmit(&huart2,rx_data,6,1);
//}
/* USER CODE END 4 */

 /* MPU Configuration */

void MPU_Config(void)
{
  MPU_Region_InitTypeDef MPU_InitStruct = {0};

  /* Disables the MPU */
  HAL_MPU_Disable();

  /** Initializes and configures the Region and the memory to be protected
  */
  MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  MPU_InitStruct.BaseAddress = 0x0;
  MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  MPU_InitStruct.SubRegionDisable = 0x87;
  MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;

  HAL_MPU_ConfigRegion(&MPU_InitStruct);
  /* Enables the MPU */
  HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);

}

/**
  * @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.
  *   file: pointer to the source file name
  *   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 */

 

Did you check the solder bridges?

 


@KRISH_ wrote:

Using Putty (serial connection), i am testing whether I can enter characters to the terminal. I believe the characters I enter should be visible to me.


Only if you have local echo enabled.

 


@KRISH_ wrote:

I can't see the transmitted data either with sprintf().


To start with, just use a fixed text string.

 


@KRISH_ wrote:

The pins shown in the user manual are PD8 and PD9


Those are the pins connected to the VCP - so those are the pins you must use.

 


@KRISH_ wrote:

I do not know whether the data I am sending via USART is reaching the MCU. 


Use an oscilloscope or similar to check.

Or a separate UART-to-USB adapter.

 


@KRISH_ wrote:

the printf() statements do get displayed on the serial monitor. 


What do you mean by, "the serial monitor" here - PuTTY? The CubeIDE monitor?

You can't have both PuTTY and the CubeIDE monitor connected at the same time.

 

PS:

Local echo in PuTTY: https://documentation.help/PuTTY/config-localecho.html

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.

I haven't soldered any pins onto this board.I am currently using the Zio connector of this nucleo (144) H7A3ZI-Q board.

After enabling Local echo in Putty I can see the characters I enter in  Putty.I can see a 3.68V PWM signal on the PB10. But I couldn't see the msg ="hello world" getting printed on the Putty after running the following code:

#include <string.h>

#define TIMEOUT 100

/* USER CODE BEGIN 1 */
  char msg[]="hello world/n";
  /* USER CODE END 1 */
 while (1)
  {
	HAL_UART_Transmit(&huart3, (const uint8_t)msg, strlen(msg), TIMEOUT);

The putty is blank


@KRISH_ wrote:

After enabling Local echo in Putty I can see the characters I enter in  Putty.


That gives reassurance that you are actually hitting the keys but, as the name suggests, it is purely local to putty.

 


@KRISH_ wrote:

I haven't soldered any pins onto this board.


Is it a new board? Has anyone else used it before you?

 


@KRISH_ wrote:

I can see a 3.68V PWM signal on the PB10.


PWM?

Do you mean a UART signal?

The VCP is connected to pins PD8 (UART TX output) and PD9 (UART RX input):

AndrewNeil_0-1750927152698.png

You won't get anything through the VCP unless you use those pins!

 

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.