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-21 10:31 AM
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.
2025-07-21 12:46 PM
The TimerCallback is my implementation. The project I posted for you was ported from a FPGA project. We have to do a power up sequence with 10+ microcontrollers. We needed a timeout in case any of the other devices didn't reply so we could do a shut down sequence.
I'm using the SysTick as the timer source. The TimerCallback can be configured for a 1 shot.
Besides using the SysTick, you can create another timer along side it. For instance, a 1us interrupt, if the clock is fast enough and the microcontroller can handle the overhead.
You can see this video of the many uses the TimerCallback has https://www.youtube.com/watch?v=o0qhmXR5LD0
2025-07-22 10:27 PM
@Karl Yamashita wrote:I'm using the SysTick as the timer source. The TimerCallback can be configured for a 1 shot.
Besides using the SysTick, you can create another timer along side it. For instance, a 1us interrupt, if the clock is fast enough and the microcontroller can handle the overhead.
I don't want to use SysTick because it is already occupied by RTOS.
I need 10us ticks for timing control of 5 UARTs running at 115kBaud.
I want to free the CPU from communication tasks as much as possible because it is busy doing other stuff.
UART communication is running continously and requests to UARTs are asynchronous.
For this I configured 5x 16bit one-shot timers for independent timeout generation.
This seems to do the job but I'm unsure if I am using the timers correct here and if there is a better way to use the one-shot mechanism.
2025-07-23 1:47 AM
I never said you had to use the SysTick. I did say you could create another timer. Seems like a waste to use 5 timers where 1 timer would work.
5 UART at 115200 isn't much, the H7 can handle that especially since you're DMA. I'd be more concern on why the other devices didn't send data to the point that you need some kind of timeout?
You haven't clearly explained what the 10us ticks are going to do? Are you timing out in increments of 10us or exactly at 10us? What do you do after you timeout?
2025-07-23 10:06 AM - edited 2025-07-23 10:22 AM
@Karl Yamashita wrote:I never said you had to use the SysTick. I did say you could create another timer. Seems like a waste to use 5 timers where 1 timer would work.
Yes you did. I just wanted to say that I need an additional one for the sub-ms timer, so I just need 4 additional timers.
I have a dozen of unused timers so why not off-load the CPU from high frequency IRQs? Hopefully I can reduce the CPU clock in order to save current.
@Karl Yamashita wrote:I'd be more concern on why the other devices didn't send data to the point that you need some kind of timeout?
The devices vary in response time between some us to some ms and data length 1-256bytes which cannot be predicted. Does this answer your question?
@Karl Yamashita wrote:You haven't clearly explained what the 10us ticks are going to do? Are you timing out in increments of 10us or exactly at 10us? What do you do after you timeout?
First I have to control the timing of the RS485 communication. The rx/tx lines of each UART go thru a RS485 driver. The direction is controlled via GPIO which is unfortunately not driven by UART HW. The protocol requires 10-100us delay before and after switching the driver direction depending on baudrate. Therefore I need the sub-ms timer granularity. The same timer can be used for the receive timeout check. I have a variable timeout depending on the device type, e.g. 50ms timeout for the first byte and 300ms for up to 256byte data. This is for very slow devices at 9600baud. Because of this double timeout check I can go on and poll the next device after 50ms if a device is not up or busy and timed out.
If a timeout occured I abort the DMA-to-IDLE, re-init the USART and continue polling the next device.
2025-07-23 12:31 PM
@regjoe wrote:
@Karl Yamashita wrote:I never said you had to use the SysTick. I did say you could create another timer. Seems like a waste to use 5 timers where 1 timer would work.
Yes you did. I just wanted to say that I need an additional one for the sub-ms timer, so I just need 4 additional timers.
I have a dozen of unused timers so why not off-load the CPU from high frequency IRQs? Hopefully I can reduce the CPU clock in order to save current.
Really? You even quoted me. You asked why you didn't see any timers used in my IOC. I just said I'm using the SysTick as my source, but I also said you could create another timer. Where did you interpret that you "had" to use SysTick from that?
You've asked if you're using the timers correct here and if there is a better way to use the one-shot mechanism. So why use 5 timers with 5 different interrupts when 1 timer/interrupt will work?
Just create 5 timer instances that keep track of the 5 UARTS timeout but still work with that 1 timer.
2025-07-23 9:44 PM
@Karl Yamashita wrote:Really? You even quoted me. You asked why you didn't see any timers used in my IOC. I just said I'm using the SysTick as my source, but I also said you could create another timer. Where did you interpret that you "had" to use SysTick from that?
Sorry, I used the wrong wording, please excuse me.
@Karl Yamashita wrote:
So why use 5 timers with 5 different interrupts when 1 timer/interrupt will work?Just create 5 timer instances that keep track of the 5 UARTS timeout but still work with that 1 timer.
This should be possible.