cancel
Showing results for 
Search instead for 
Did you mean: 

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

rwils.1
Associate III

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

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

6 REPLIES 6
Piranha
Chief II

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.

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

vd
Associate III

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

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?

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

I managed to understand the reason for the RX interrupt, it was pretty simple, basic logic, the processor is communicating with a device and the device returns a character when it receives one , so that triggers the interrupt - pretty basic mistake on my part. I got the HAL routines running but they don't seem to be fast enough to receive a string at 115k. So I'll have to wade through setting registers by hand. Thank you very much for the example code Piranha, much appreciated.