cancel
Showing results for 
Search instead for 
Did you mean: 

Single shot timer for peripheral timeout check

regjoe
Senior

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

4 REPLIES 4
TDK
Super User

Use HAL_UARTEx_ReceiveToIdle_DMA to receive data and get notified when an IDLE condition happens.

STM32CubeF4/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/readme.txt at 5c556f15a2b074ff2d60461be34bfb6766f4df8c · STMicroelectronics/STM32CubeF4

If you feel a post has answered your question, please click "Accept as Solution".

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.

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. 

I was told that if a devices starts to smoke, put the smoke back in. I guess I never got all the smoke because the device never worked afterwards.
Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.

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.

If you feel a post has answered your question, please click "Accept as Solution".