Skip to main content
SMusc.1
Associate III
May 29, 2025
Solved

STM32 - USART1 & USART2 (USART2 non working)

  • May 29, 2025
  • 3 replies
  • 1382 views

 

 

Hello everyone, I am having a problem with a simple program. My board is a STM32-U575ZITxQ.
Using only the USART1 interface, if I try to print the string: "USART1_READY" on my laptop's serial port, I have no problem. However, as soon as I also enable the USART2, I cannot print anything. Whether with only the USART2 enabled, or with both USART1 and USART2 enabled, if I try to print the string with USART2, I cannot. I checked all the settings of the USART1 and USART2 and they are the same (in GPIO, in Parameter setting, etc.).

/* USER CODE BEGIN PV */
char USART1 = "USART1_READY:\r\n";
char USART2= "USART2_READY:\r\n";
/* USER CODE END PV */

/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim4);//ready for trigger at 1 seconds
/* USER CODE END 2 */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 if(htim->Instance == TIM4)
 {
 //HAL_UART_Transmit(&huart1, &USART1, sizeof(USART1),HAL_MAX_DELAY);
 HAL_UART_Transmit(&huart2, &USART2, sizeof(USART2),HAL_MAX_DELAY);
 }
}

where am I going wrong?

Best answer by TDK

Is this the NUCLEO-U575ZI-Q board?
USART1 is hooked up to the VCP port. USART2 is not. That's probably why one works and one does not.

3 replies

urbito
Senior II
May 29, 2025

Can you also share the INIT functions for both, usart1 and usart2.

Also, what you mean with usart1 and usart2 having same GPIO?

SMusc.1
SMusc.1Author
Associate III
May 29, 2025

Hi, here the main function where is wrote the INIT function(i removed all timer and complication), non are enabled only the USART1 and USART2 and all other defaoult settings. I add only a delay in while loop and on the USART2 i am not able to print on serial of my laptop. If I use USART1 I can print the messagge corrctly. Only by using USART2 i can't print and he doesnt work. I attached picture where i checked the setting for both GPIO of USART1 and USART 2, I put the same.

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();

 /* Configure the System Power */
 SystemPower_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_ADC1_Init();
 MX_ICACHE_Init();
 MX_UCPD1_Init();
 MX_USB_OTG_FS_PCD_Init();
 MX_MEMORYMAP_Init();
 MX_USART1_UART_Init();
 MX_USART2_UART_Init();
 /* USER CODE BEGIN 2 */
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
	 HAL_Delay(1000);
	 HAL_UART_Transmit(&huart2, &MESSAGE_FROM_US2, sizeof(MESSAGE_FROM_US2),HAL_MAX_DELAY);
 }
 /* USER CODE END 3 */
}

 

Andrew Neil
Super User
May 29, 2025

@SMusc.1 wrote:

My board is a STM32-U575ZITxQ.


That's just a chip part number - it doesn't tell what board you're using.

How are you connecting both UARTs to your laptop?

Before adding the complication of timers & interrupts, have you got this working just using blocking delays in a simple loop?

How to write your question to maximize your chances to find a solution

 

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.
SMusc.1
SMusc.1Author
Associate III
May 29, 2025

Hi Andrew, for first question: one at a time. (of course not both at same time).

 

I tried this way:
1) with both UART1 and UART2 enabled, both initialized, but using only the line of code that prints a message via USART1. Then I repeated it using USART2. It only works with USART1.
2) with only USART1 enabled and initialized, I can print a message on my laptop's serial port.
3) with only USART2 enabled and initialized, I can't print a message on my laptop's serial port. In this case the communication is frozen as in the attached image.

Andrew Neil
Super User
May 29, 2025

You still haven't said how you are actually connecting the two UARTs to your PC.

What is the physical connection?

As @TDK  said, the VCP (Virtual COM Port) of the ST-Link in only connected to USART1 - so you won't get anything from any other UART via that route.

To see output from UART2 (or UART3, or any other UART), you will have to provide some other connection from UART2 to your PC.

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.
TDK
TDKBest answer
Super User
May 29, 2025

Is this the NUCLEO-U575ZI-Q board?
USART1 is hooked up to the VCP port. USART2 is not. That's probably why one works and one does not.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SMusc.1
SMusc.1Author
Associate III
May 29, 2025

Yes, Is that my board. I tried also by using USART3 and not working as well. VCP is the com serial port of laptop right? How can I change this com port association in relation to the USART I am using?

TDK
Super User
May 29, 2025

The USART1 pins on the board are hooked up to the st-link chip, see the user manual and the schematic.

If you want to change to USART2 or something else, you will need to physically modify the board by breaking those connections and creating new connections with the pins for the other peripheral.

You should probably just stick with using USART1.

"If you feel a post has answered your question, please click ""Accept as Solution""."