2022-04-25 09:59 AM
On an STM32F746 I'm using TIM3 in encoder mode to count pulses from a motor's quadrature encoder. I'm using TIM2 to generate interrupts every 10 msec were I read the TIM3 CNT register to calculate the motor's RPM. There's some jitter here because of the interrupt latency getting into the TIM2 ISR and reading TIM3's CNT register.
What I'd like to be able to do is have the TIM2 period match event trigger an input capture of TIM3's CNT register. This would eliminate the interrupt latency and reduce jitter when reading TIM3->CNT. Is this possible? I can't see a way to implement this from my reading of the reference manual. The reference manual even alludes to this being possible when it says "Depending on the time between two events, the counter can also be read at regular times. You can do this by latching the counter value into a third input capture register if available (then the capture signal must be periodic and can be generated by another timer)."
Can anyone point me to a reference describing how to do this (assuming it's possible)?
2022-04-25 10:46 AM
Another option would be to tie one of the lines to an EXTI trigger and use that as the time to read. Probably disable the interrupt within the handler and enable it every 10ms (or whatever) to keep CPU usage reasonable.
2022-04-25 11:23 AM
> You can do this by latching the counter value into a third input capture register if available (then the capture signal must be periodic and can be generated by another timer)."
As the internal inter-timer connections have been "exhausted" due to the encoder mode employing the slave-mode controller, you can do this by connecting a TIM2_CHx pin to TIM3_CHx pin externally.
Then you can also simply read out TIM3_CNT using DMA triggered by TIM2. DMA is not without latency and jitter, but it's usually an order of magnitude better than interrupts.
You can also work on improving the TIM2 interrupt latency, e.g. by increasing its priority, ditching unnecessary fluff like Cube/HAL, increasing compiler optimization level.
JW