While using PWM for measuring low frequencies is enough, there's no reason not to use the PIO counter if there are unused state machines and has the advantage of having a better resolution for a given gate time.
That being said, inspired by this thread, I decided to check out the DMA support and combine it with my own PWMCounter module to see how it'll work for frequency measurements and I'm quite happy with the results. Here's a gist of my proof-of-concept: https://gist.github.com/phoreglad/a287a ... c85b449194. It uses two chained DMA channels, one to reset the counter and the second to read the counter value. Gate time is a combination of paced DMA reads using TIMER0 and the read count. It uses IRQ to get the data at the end of the gate period and calculate the frequency. This way the PWM and DMA can continuously read the frequency with minimal load on the cores, allowing the main program to do whatever it needs to, while always having access to the latest frequency measurement. I haven't tested it yet, but it should also be slightly more accurate than using a loop to check if gate time expired, as is currently done in the example I provided in the repo. It works pretty well, though I noticed soft resetting the Pico while this program is running causes errors and a hard reset is required before it's in a usable state again.
Overall, I like this solution and will definitely consider using something similar whenever I get to rewriting my PIO counter.
That being said, inspired by this thread, I decided to check out the DMA support and combine it with my own PWMCounter module to see how it'll work for frequency measurements and I'm quite happy with the results. Here's a gist of my proof-of-concept: https://gist.github.com/phoreglad/a287a ... c85b449194. It uses two chained DMA channels, one to reset the counter and the second to read the counter value. Gate time is a combination of paced DMA reads using TIMER0 and the read count. It uses IRQ to get the data at the end of the gate period and calculate the frequency. This way the PWM and DMA can continuously read the frequency with minimal load on the cores, allowing the main program to do whatever it needs to, while always having access to the latest frequency measurement. I haven't tested it yet, but it should also be slightly more accurate than using a loop to check if gate time expired, as is currently done in the example I provided in the repo. It works pretty well, though I noticed soft resetting the Pico while this program is running causes errors and a hard reset is required before it's in a usable state again.
Overall, I like this solution and will definitely consider using something similar whenever I get to rewriting my PIO counter.
Statistics: Posted by horuable — Thu Feb 29, 2024 1:32 pm