2025-07-17 10:31 AM
Hello,
I'm here on H75x. I'm looking for portable way to do a timeout check via timer.
Here, I want a timeout IRQ if UART receive DMA hangs up.
First I setup a OnePulse timer in Cube. It generates the following code:
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE) != HAL_OK)
{
Error_Handler();
}
The second init function does almost the same as the first but activates the OneShot mode which stops the counter immediately if IRQ is fired.
I was looking for a suitable HAL function to setup the timeout value but failed. Instead I use
__HAL_TIM_SetCounter(htim, 1); // set start value
__HAL_TIM_SetAutoreload(htim, ulDelay); // set end value
__HAL_TIM_ENABLE(htim);
The last macro is required because after the RX DMA is done in time, the timer has to be stopped to prevent from false timeout IRQs.
Is this the way to go? Is there a HAL-compatible solution which I didn't get?
Thanks
2025-07-17 12:06 PM
Use HAL_UARTEx_ReceiveToIdle_DMA to receive data and get notified when an IDLE condition happens.
2025-07-17 12:55 PM
Hi @TDK,
I already do, but I'm unsure if this function solves all sources of trouble. What happens if the sender does not respond at all / not in time? Are there any chances that the DMA blocks forever? I'll have to check multiple different error situations. This timer timeout shall handle all error situations which are probably not handled by ReceiveToIdleDMA, I hope.
Nevertheless, I want also other things do with the timer, e.g. wait a certain time and then start an action when the timer IRQ is fired e.g. insert a short delay between switching the RS484 driver IC to transmit mode before starting the transmit DMA.
2025-07-17 1:14 PM
Look at this video on using a timer callback. https://www.youtube.com/watch?v=o0qhmXR5LD0
You set the timer callback to start a timer. If you receive some data within a time frame, you can disable the timer. If no data within a time frame, you can have the timer callback call a function.
2025-07-17 1:20 PM
If the sender doesn't respond, the DMA doesn't do anything since no data shows up and the line remains idle. It would not block since there is nothing for it to do. You would not be notified at all in this case and would need some other timer mechanism.