Skip to main content
rwils.1
Associate III
July 20, 2022
Solved

UART issues-I am trying to discern the difference between a transmit and a receive interrupt flag. I am using the following code if(__HAL_UART_GET_IT_SOURCE(&huart4, UART_IT_RXNE)) { function} It triggers on both receive and transmit interrupts

  • July 20, 2022
  • 6 replies
  • 1228 views

Shouldn't just the RXNE flag trigger with this code? or does the outgoing transmit message also generate an RXNE flag?

Thanks!

This topic has been closed for replies.
Best answer by Piranha

A good example of how "helpful" the HAL is...

There is a decent USART example:

https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx

6 replies

Piranha
Principal III
July 20, 2022

With HAL you are supposed to use it's callbacks.

A hardware loop or a half-duplex mode with receiving not turned off while transmitting will result in receiving everything that is transmitted.

rwils.1
rwils.1Author
Associate III
July 20, 2022

Thanks for replying, no hardware loop and full duplex, so I don't believe that's the issue.

vd_it
Associate II
July 21, 2022

Are you using the cubemx generated HAL, API or building your own? - I'd recommend not using the cubemx generated HAL, their ISR's are massively bloated and takes up too much time. Anyway, regarding your issue, if you're trying to check if there's a byte pending to be read in the RX register then you should be checking the RXNE bit in the ISR register something like below.

if (READ_BIT(uart_ptr->Instance->ISR, USART_ISR_RXNE)) {
 // read it, so that RXNE is cleared by the hardware
 char byte = uart_ptr->Instance->RDR;
 }

And you're using the wrong macro. The __HAL_UART_GET_IT_SOURCE macro doesn't give you the source of an interrupt trigger but instead lets you check if the interrupts for TX or RX are enabled or disabled in the control registers.

rwils.1
rwils.1Author
Associate III
July 21, 2022

Thank you very much- that makes a lot of sense, I didn't realise that the macro was the wrong one. I was trying to use a mixture of HAL and my own interrupt, I want to trigger an interrupt whenever a byte is received and once detected use the HAL routine to handle it. You are right, the HAL serial interrupt is bloated, is there better example code for UART handling?