Skip to main content
Anwar.786
Associate
February 24, 2023
Solved

wrong UART interrupt is getting fired

I am using UART to transmit and receive data. for transmitting I am using polling method function HAL_UART_Transmit(&hart1, buf, 8,1000) and for receiving i am using HAL_UART_Receive_IT(&hart, Rx_buf,8);

receiving is working fine but after transmitting any data the receive callback is getting fired and after that the receiving interrupts are not getting fired even though I am transmitting data from another device.

This topic has been closed for replies.
Guenael Cadier
Guenael CadierBest answer
ST Employee
February 28, 2023

So it looks like there's a connection between your Tx and Rx pins on sender side.

If you look at RX pin signal with a logic analyzer, what do you see when you are sending your 8 bytes ?

Either bytes are really echoed by distant part, or it is a hw issue on your sender device.

What happens if you disconnect anything from the sender device ? Is your Rx callback still executed ?

17 replies

ST Employee
February 24, 2023

Hello @Anwar.786​ and welcome to ST Community,

You may not have enabled the UART receive interrupt in your initialization code. Make sure you have enabled the receive interrupt before attempting to send and receive data.

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.
Anwar.786
Anwar.786Author
Associate
February 24, 2023

Hi @Sarra.S​ I already enabled the USART interrupts in STM32CUBE MX, do I also have to force enable the receive interrupt individually?

Karl Yamashita
Principal
February 24, 2023

You should always post your relevant code so we can see what you're doing. But from what I'm reading you're saying that you get at least one receive interrupt but after that it doesn't interrupt again.

Sounds like you are not calling HAL_UART_Receive_IT again. Everytime HAL_UART_RxCpltCallback is called, the Receive interrupt is disabled. You have to re-enable it again each time.

See the code that I've posted here to get the understanding of enabling interrupts again and also checking for HAL status to be sure HAL_UART_Receive_IT returns HAL_OK.

https://community.st.com/s/question/0D53W000022Hi9ySAC/stm32-hal-uart-receive-interrupt-stops-receiving-at-random-times

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Anwar.786
Anwar.786Author
Associate
February 27, 2023
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_USART1_UART_Init();
 /* USER CODE BEGIN 2 */
 HAL_UART_Receive_IT(&huart1, RX_Buf, 8);
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
	 if(i==5 || i==10){
		 Uart_status=HAL_UART_Transmit(&huart1, TX_Buf, 8, 1000);
	 }
 
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	Uart_status=HAL_UART_Receive_IT(&huart1, RX_Buf, 8);
	i++;
}

The only problem i am facing is once the transmission is completed the Rx callback is getting executed which is not supposed to happen.

can you explain why it is happing?

Guenael Cadier
ST Employee
February 27, 2023

Hello @Anwar.786​ 

What is the content of RX_Buf buffer at the moment when your Rx Complete callback is executed ? (if callback is executed, I guess that at least 8 datas have been detected on rx line)

Could it correspond to some echo of the transmitted data ?

Regards

Anwar.786
Anwar.786Author
Associate
February 28, 2023

After transmission is completed, it is Rx callback is executed and the content of the RX_Buf is same as the TX_Buf.

Yes, it corresponds to echo of the data transmitted.

Anwar.786
Anwar.786Author
Associate
February 27, 2023

@Karl Yamashita please look into above reply, I forgot to tag you there.

Karl Yamashita
Principal
February 27, 2023

  • What are you doing with RX_Buf data?
  • What data is in TX_Buf?
  • What device are you sending/receiving from?
  • What is the purpose of the variable "i"?
  • Is that all the code in HAL_UART_RxCpltCallback? You have Uart_status but not doing anything with the status and the same for HAL_UART_Transmit
  • Is it on the first transmit that you get an RX interrupt?
  • You didn't show how the variables are initialized including "i".
If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Anwar.786
Anwar.786Author
Associate
February 28, 2023

uint8_t RX_Buf[8];

uint8_t i;

uint8_t TX_Buf[8]={1,2,3,4,5,6,7,8};

uint8_t Uart_status;

these are the variables I am using.

Answers to your questions

  • RX_Buf is for storing the data once the reception is completed.
  • TX_Buf is having a size 8 and the data is 1,2,3,4,5,6,7,8.
  • I am using STM32H743BIT6 and Dock light software.
  • Yes, havent implemented the code for Uart_status.
  • During all the transmissions.
  • I mentioned those in above.

S.Ma
Principal
February 28, 2023

Sanity check : Did you select the right part number in the compiler settings ?

Anwar.786
Anwar.786Author
Associate
February 28, 2023

I am using STM32H743BIT6 and i selected STM32H743BI

Karl Yamashita
Principal
February 28, 2023

So it comes down to how the hardware is connected. Is this a custom board with a STM32H743BIT6? I assume you are using a USB<>Serial adapter to connect to Docklight and if so what is the part # or link to the adapter? A block diagram of your connections would help a lot.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source