Hooking up an output derived from the GPT shows 1.333Hz instead of 2Hz. Multiplying the frequency internal by 1.5 speeds up the clock to 2Hz.
I tried in both tick and tickless mode, works the same.
I am using the system tick time to measure an input, and the calculations are correct for what I would expect (using a known external frequency generator). That just means that the math relative to the system clock is correct with regards to sampling time. (i.e., the tick frequency and when I measure something with it, stay consistent).
That leads me to believe that somehow, when the Hz value of the GPT2CFG I am creating is off, the math that converts that into the proper system tick times is wrong.
When you start a continuous GPT, how is the time out calculated from the hz value you pass in?
Here is a portion of my code:
Code: Select all
gpt2cfg.frequency = (768*CH_CFG_ST_FREQUENCY)/(currentAverage);
uint32_t pscCheck = ((GPTD2.clock / GPTD2.config->frequency) - 1);
if( pscCheck >= 2<<15 ) {
preScaleDivider=2;
pscCheck = pscCheck >> 1;
gpt2cfg.frequency = gpt2cfg.frequency << 1;
while(pscCheck >= 2<<15) {
preScaleDivider = preScaleDivider << 1;
pscCheck = pscCheck >> 1;
gpt2cfg.frequency = gpt2cfg.frequency << 1;
}
preScaleCnt=0;
} else {
preScaleDivider = 1;
}
chSysLockFromISR();
uint16_t psc = (uint16_t)pscCheck;
/* Timer configuration.*/
GPTD2.tim->CR1 = 0; /* Initially stopped. */
GPTD2.tim->CR2 = STM32_TIM_CR2_CCDS; /* DMA on UE (if any). */
GPTD2.tim->PSC = psc; /* Prescaler value. */
GPTD2.tim->DIER = GPTD2.config->dier & /* DMA-related DIER bits. */
STM32_TIM_DIER_IRQ_MASK;
GPTD2.tim->SR = 0; /* Clear pending IRQs. */
gptStartContinuousI(&GPTD2, 2);
chSysUnlockFromISR();
currentAverage is measured in system tick time, so if I had as many ticks as the current frequency, than the frequency I request would be 768Hz. (in other words, if my tick time is 1khz, and currentAverage is 1000, then I'm requesting 768Hz).
Also note, the timer is stopped before all this is done, then restarted. I used the prescalar mechanism you see before I switched from a much older version of the code base. All this has worked for a few years no problem. I'm just unclear what could be creating this timing issue.
Thanks!
Rick