Measuring clock signal frequency

We need to precisely measure frequency of a clock signal on pin 32 (PA0).

We’ve been trying to use ETR mode of TIM2. However, it seems like it’s currently not implemented in the TimerBase class and doing this with HAL alongside libDaisy seems to be not working for us.

However, we managed to get this to work without libDaisy in STM32CubeIDE. Part of the problem is that in libDaisy the interrupt handlers are already defined in tim.cpp and getting them to work requires initializing one of the timers with your Init method, which has no support for the ETR mode.
As a workaround, we added our code to one of those handlers and used HAL to initialize TIM2 into ETR mode the same way as we did in the working CubeIDE project, but it still does not work for us with libDaisy in VSCode.

Help with this issue would be hugely appreciated, since it would help us develop a really cool digital theremin. Do you have any plans to add support for ETR in TimerBase? Perhaps there’s some other solution of measuring this signal? The signal is around 500 to 520 kHz and we need 0.1Hz measurement resolution.

Welcome @aethersis!
Intrigued to hear more about this theremin project :eyes:

As for ETR, I asked the team and we unfortunately don’t have any plans implementing it (at least anytime soon) for libDaisy. We’re sorry for the inconvenience.

If you do decide to open a PR, we would be more than happy to review and merge as soon as we get the chance.

That’s understandable. Thank you for your reply.
If possible, I’d love if someone from the devs could take a look at this code

I literally disable any includes related to Daisy and yet the behavior is completely unexpected. The TIM8 counter interrupt gets internally trigerred from some clock source (presumably 32MHz) that I did not specify and as the result, the LED blinks at 487.8Hz frequency (measured with an oscilloscope).

The expected behavior would be that TIM8 is configured in ETR mode and PA0 is the ETR pin. If I’d input 500kHz on that ETR pin, the LED should blink at 500,000/65535 period.

I tested the same code in a clean STM32CubeIDE project and it works as expected.
I test it in VSCode and it does the weird stuff I described above.

The way I have the project configured is I used the script from DaisyExamples to create a new project. Do you have some custom script or something that configures the timers in the demo project setup? I can’t make the slightest sense out of this.

If you help me clarify what could the potential reason for this be, I will gladly proceed to modify tim.cpp file to add support for ETR in your library, but first I really need to understand this weird behavior in the minimal example I provided (i.e. where & how are clocks configured).