cancel
Showing results for 
Search instead for 
Did you mean: 

UART RX Interrupt timeout

SGian.1
Senior

Hi there!

I start to use a UART (RS485) with stm32g031f6 , and i use

HAL_UART_Transmit_IT

HAL_UART_Receive_IT

and both works. So now i want implemet an timeout on RX line. I saw the app note 3109 and i have a couple of question:

  • if i use timer input capture thst wanna mean i "lose"" one pin of micro because i need the connect on rx line right ?
  • I can use usart interrupt

My goal is use the second one the problem is i have an interrupt when i tx data and when i rx a data.

I read the data sheet and i know there is a ISR register and the bit RXNE:is set when the data is ready is coorect check if is set to one ? and is coorect do in this way USART2-> ISR & 0x20 ? and do in the interrupt UART ? or i need the check in the while loop ? and afther i receive a time out how i can reset the pointer use from the software for save the data in the array ?

Forgive me for this long question but i'm really soo confuse.

Thanks a lot

Sergio

8 REPLIES 8
KnarfB
Principal III

Hi Sergio,

yes, checking USART2-> ISR & 0x20 in the interrupt handler is correct.

But, the app note dates back to a time when neither HAL nor G0 series existed.

If you're already using HAL, you can simply use the provides callback funtions instead of tampering with the raw UART interrupt. I.e you simply implement two functions

void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart);
void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart);

and HAL will crack the register bits for you. Look also at the

HAL_UARTEx_ReceiveToIdle_DMA

HAL function which might be helpful for you.

HAL is not everybody's darling and has its limitations or even shows bugs in complex scenarios. There is a detailed explanation and sample "HAL free" code in https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx .

hth

KnarfB

SGian.1
Senior

Thanks for your fast replay.

I want create a timeout because i have one master and 18 slave and if i don't receive all data i need the call again the slave from master and after a while write message on display. I think the callback function can help me to do that. I know the HAL function are no the best one and one day or another i need the start to write my own library. The last question some one have idea how clear the pointer or mabye i can just setup the rx to receive one data and create my own circula buffer.

Thanks again

Sergio

SGian.1
Senior

I think the callback function can help me to do that. Sorry i want wrote I think the callback function can't help me to do that

S.Ma
Principal

If the timeout can be in msec, clear a ram variable within the interrupt, and increase it say in systick interrupt. Once this coumter reaches a defined threshold, do something.

Of course you can make countdowns too. Interrupt priority well tuned.

> I want create a timeout because i have one master and 18 slave and if i don't receive all data i need the call again the slave

Then it is very simple. Just call HAL_UART_Transmit to send, then immediately HAL_UART_Receive() to get the reply. The last arg of HAL_UART_Receive is the timeout that you need. No any circular buffers.

Later replace the HAL with something better, as you already plan.

SGian.1
Senior

Thanks a lot

Sergio

And no any callback functions. Just keep it simple.

Yes is true , i just want say can't help me with timeout , because if i don't receive all data i don't receive the interrupt