cancel
Showing results for 
Search instead for 
Did you mean: 

wrong UART interrupt is getting fired

Anwar.786
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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 ?

View solution in original post

17 REPLIES 17
Sarra.S
ST Employee

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
Associate II

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
Lead III

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

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Anwar.786
Associate II
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?

Anwar.786
Associate II

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

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

Karl Yamashita
Lead III

  • 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".
Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
S.Ma
Principal

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

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.