Skip to main content
Visitor II
January 28, 2025
Question

STM32F407: UART transmit stopped when USB is receiving

  • January 28, 2025
  • 2 replies
  • 658 views

I simplified the code belows ;

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USB_DEVICE_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

HAL_Delay(1000);

// CDC_Transmit_FS(buff , sizeof(buff));

// if (user_inp[0]=='x'){

// CDC_Transmit_FS(buff , sizeof(buff));}

//

// else{

// CDC_Transmit_FS(user_inp,2);}

//readbuff[0] = user_inp[0];

//readbuff[1] = user_inp[1];

// if (user_inp[0]=='x'){

// HAL_UART_Transmit(&huart2, readbuff,sizeof(readbuff),0xff);}

//

//

// else{ HAL_UART_Transmit(&huart2, buff,sizeof(buff),0xff);}

HAL_UART_Transmit(&huart2, buff,sizeof(buff),0xff);

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

 

the only code is uart_trans,i open two windows for uart(ttl to usb) and usb com.after i reset ,the uart trans data per second.when i send data to the usb,the uart trans stops suddenly.is there something interrupt uart?

    This topic has been closed for replies.

    2 replies

    Super User
    January 28, 2025

    Chip can only do one thing at a time. What does your USB data reception code look like? USB will interrupt whatever is happening and execute that code. HAL_UART_Transmit is blocking, so it will be pause until receive code is complete.

    Technical Moderator
    January 30, 2025

    Hi @Vaguesunrain 

    You need to enable UART interrupt in NVIC settings using CubeMX. After generating your code, you need to use HAL_UART_Transmit_IT instead of HAL_UART_Transmit to ensure non-blocking transmission of UART