cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407: UART transmit stopped when USB is receiving

Vaguesunrain
Associate

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?

2 REPLIES 2
TDK
Guru

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.

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

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

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.