Skip to main content
Senior
July 17, 2025
Question

Single shot timer for peripheral timeout check

  • July 17, 2025
  • 3 replies
  • 933 views

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.

regjoe_0-1752772838108.png

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

3 replies

TDK
Super User
July 17, 2025
"If you feel a post has answered your question, please click ""Accept as Solution""."
regjoeAuthor
Senior
July 17, 2025

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.

Karl Yamashita
Principal
July 17, 2025

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. 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Karl Yamashita
Principal
July 17, 2025

@regjoe 

You can look at this project I just added to Github. https://github.com/karlyamashita/Nucleo-G071RB_UART_Timeout/wiki

 

I set a timeout for 5 seconds when you send a message "are you there?". It will start printing the count down value until it receives a message "Hello".

If it doesn't by the time it counts down to 0, it will print a "Failed" message.

If it does receive "Hello" in time, it'll stop the count down timer and print  a "Good" message.

 

But you can substitute the Good message with some other routine of your choice.

 

 

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
regjoeAuthor
Senior
July 21, 2025

According to this post here Callback function for HAL_TIM_OnePulse_Start_IT? it seems that there is no HAL function implemented to re-configure a timeout value after timer initialization. All you can do is disable/re-enable the one shot timer after initialization.