2024-11-11 02:11 PM
On a STM32G030 MCU, i have Timer-1 (Channel 2) configured in "Input Capture Mode" and is running at 4 Mhz (64 prescaled by 16), So effectively the TIM1 counter increments at every 0.25uS. This is used to measure "On time" of a LED strobe-Light on a GPIO line(which can be as wide as 20 microsec multiple times in a span of 16 millisec window slot).
The approach : Set timer-1 trigger for rising edge,on rising edge interrupt start the timer-counter and reset trigger for falling edge, and on falling edge, stop the counter and read the counter difference and set back to rising edge again. div the difference by 4 gives you the pulse ON time in micro sec.
The measurement happens correctly for most of the time .. but often(every 20 sec or so) i noticed that the timer counter adds a value of 1000 incorrectly. I arrived at this number by repeated captures on this error and its always been 1000 more. 1000 implies its 250 microsec more. The strobe pulses are very regular in the system and i confrmed via scope. I break on error and hooked this condition to scope and look back the aquired train of pulses and confirmed that system was sending a constant pulse train of 20uS with a ~1.2ms gap between them.
I have disabled all other MCU activities but this timer1, hoping there is a culprit within... but still this happens.
this being exactly 1000 counts or 250uS makes me think this is a MCU issue. if it was any other rogue thread or watch dog.. or something else stuck doing some house keeping, it would be a random number and counts should have been different. composed below table, where edge column can be Rising(R) or Falling(F) or X=error
Edge | Timer Counter | Diff | Diff (uS) | Strobe Count | |
[0] | 'F' | 990 | 80 | 20 | 2 |
[1] | 'R' | 911 | |||
[2] | 'F' | 991 | 80 | 20 | 3 |
[3] | 'R' | 910 | |||
[4] | 'F' | 990 | 80 | 20 | 4 |
[5] | 'R' | 909 | |||
[6] | 'F' | 989 | 80 | 20 | 5 |
[7] | 'R' | 909 | |||
[8] | 'F' | 1991 | 1082 | 270.5 | 6 |
[9] | 'X' |
Any thoughts? if you disagree, please share your thoughts on this.
@st Micro : If you have any Errata on this, please point out and save me some time
Thanks in advance
Nag
2024-11-11 04:27 PM
Can you provide a minimal project that reproduces the behavior.
2024-11-12 06:05 AM
I can try making a project for Nucleo board (STM32G031K8) which is not the same chip we have it on our custom board(STM32G030K8T6). but also you might need a function generator that makes a 20uS pulse every 1200uS and input that line to one of the Nucleo pin(PB3) and observe if it can fault as often as i say.
2024-11-12 06:42 AM
> On a STM32G030 MCU, i have Timer-1 (Channel 2) configured in "Input Capture Mode"
And when evaluating the pulse, do you read the TIM1_CCR2 register, or TIM1_CNT?
I see "FreeRTOS" in the tags, that's essentially a timer interrupt. Make sure it has a lower interrupt priority than your interrupt. Make your interrupt the highest priority of all other interrupts you have.
JW