Timer with sub-microsecond accuracy #2146
-
Hi! If I am correct, the timers on the RP2040 have a maximum resolution of 1microsecond (https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#hardware_timer), but for applications (like Mozzi) it could be useful to have Timer with higher resolution. I know that it is possible to have the CPU count but manually checking if a number of cycles have been elapsed will probably incur a big overhead. Has anyone managed to do such thing? Maybe with a PIO? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
It's be relatively simple to do a timer with a PIO. They can count and send an IRQ in a couple instructions, and they can run at SYSCLK(133MHz). However, the interrupt controller and XIP flash means that you most likely can have jitter on the order of 1us++. So the very precise IRQ signal turns into a random variable WRT code timing. Maybe a better idea would be to understand why you want <1us accuracy and see if there's some other way to get it done? |
Beta Was this translation helpful? Give feedback.
SYSTICK is already hooked for the cycle counter (there's only a 24-bit count OTW, the systick lets us keep an epoch and know the true cycles).
32K IRQs/second seems like a bad idea just in general. AIUI IRQ handling here is not very lightweight so you've got a lot of overhead on each one of those calls just to send 1 sample.
We use the DMA engine with timed transfers to send PWM audio output already, why not use that instead of rolling your own? Send in ping and pong buffers and the HW does all the work, only giving an IRQ when one buffer is free for the next one (which is also taken care of by the PWMAudio class).
And, obviously, having SYSCLK be an exact multiple of your desired output …