On Chibios 16.1.5, HAL for STM32. If I call gptStatOneShot with an interval value of 1 I 'never' get the timeout event.
Using GPT5 on STM32F4xx.
Tracking down the calls, I get to this function in os/hal/ports/STM32/LLD/TIMv1/gpt_lld.c:
void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval) {
gptp->tim->ARR = (uint32_t)(interval - 1); /* Time constant. */
gptp->tim->EGR = STM32_TIM_EGR_UG; /* Update event. */
gptp->tim->CNT = 0; /* Reset counter. */
/* NOTE: After generating the UG event it takes several clock cycles before
SR bit 0 goes to 1. This is because the clearing of CNT has been inserted
before the clearing of SR, to give it some time.*/
gptp->tim->SR = 0; /* Clear pending IRQs. */
if (NULL != gptp->config->callback)
gptp->tim->DIER |= STM32_TIM_DIER_UIE; /* Update Event IRQ enabled.*/
gptp->tim->CR1 = STM32_TIM_CR1_URS | STM32_TIM_CR1_CEN;
}
Setting the APR with (interval - 1) will set it to 0 and the down counter will have to go through an entire 32-bit count before it rolls to zero and triggers the event. This of course is a very long time, so effectively the timeout never happens. I do not think the (interval-1) should be used, I think it should just be (interval) and perhaps there should be a check for an interval value of 0.
gptStartOneShot 'fails' with interval of 1 Topic is solved
- Giovanni
- Site Admin
- Posts: 14563
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1111 times
- Been thanked: 937 times
- Contact:
- RoccoMarco
- Posts: 655
- Joined: Wed Apr 24, 2013 4:11 pm
- Location: Munich (Germany)
- Has thanked: 83 times
- Been thanked: 67 times
- Contact:
- alex31
- Posts: 412
- Joined: Fri May 25, 2012 10:23 am
- Location: toulouse, france
- Has thanked: 47 times
- Been thanked: 80 times
- Contact:
Re: gptStartOneShot 'fails' with interval of 1
Hello,
It seems this bug has come back from the past.
In latest stable for oneshot and continuous : called with interval == 1, callback is never called.
Alexandre
It seems this bug has come back from the past.
In latest stable for oneshot and continuous : called with interval == 1, callback is never called.
Alexandre
Last edited by alex31 on Sat Feb 01, 2025 6:32 pm, edited 1 time in total.
- Giovanni
- Site Admin
- Posts: 14563
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1111 times
- Been thanked: 937 times
- Contact:
Re: gptStartOneShot 'fails' with interval of 1
Hi,
I think cannot be fixed, it is the behavior of the timer, using an interval of 1 makes the counter to stay at 0 and never increase, the interrupt is not generated.
This is the line:
Removing the "-1" would make the count wrong, 1 would count 2 cycles, 2 would count 3 cycles and so on. Best I can think of is to add an assertion and make the limitation clear.
Suggestions are welcome.
Giovanni
I think cannot be fixed, it is the behavior of the timer, using an interval of 1 makes the counter to stay at 0 and never increase, the interrupt is not generated.
This is the line:
Code: Select all
gptp->tim->ARR = (uint32_t)(interval - 1U); /* Time constant. */
Removing the "-1" would make the count wrong, 1 would count 2 cycles, 2 would count 3 cycles and so on. Best I can think of is to add an assertion and make the limitation clear.
Suggestions are welcome.
Giovanni
- alex31
- Posts: 412
- Joined: Fri May 25, 2012 10:23 am
- Location: toulouse, france
- Has thanked: 47 times
- Been thanked: 80 times
- Contact:
Re: gptStartOneShot 'fails' with interval of 1
Hi Giovanni,
I arrived at the same conclusion, assertion and addon in the documentation should be an honorable solution
A.
I arrived at the same conclusion, assertion and addon in the documentation should be an honorable solution

A.
Who is online
Users browsing this forum: No registered users and 4 guests