Page 1 of 1

gptStartOneShot 'fails' with interval of 1

Posted: Mon Mar 27, 2017 9:40 pm
by crthorne
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.

Re: gptStartOneShot 'fails' with interval of 1

Posted: Mon May 29, 2017 10:45 am
by Giovanni
bump

Re: gptStartOneShot 'fails' with interval of 1

Posted: Mon May 29, 2017 8:44 pm
by RoccoMarco
Fixed as Bug 843

Re: gptStartOneShot 'fails' with interval of 1

Posted: Sat Feb 01, 2025 4:59 pm
by alex31
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

Re: gptStartOneShot 'fails' with interval of 1

Posted: Sat Feb 01, 2025 6:28 pm
by Giovanni
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:

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

Re: gptStartOneShot 'fails' with interval of 1

Posted: Sat Feb 01, 2025 6:36 pm
by alex31
Hi Giovanni,

I arrived at the same conclusion, assertion and addon in the documentation should be an honorable solution :-)

A.