ISR speed query #12430
-
I am working on a piece of code where interrupts are triggered very frequently and its essential for the ISR handler to complete as quickly as possible. Searching through internets, there is some measurements that Pico takes around 60us (ballpark figure) to respond and enter into ISR, and each Micropython "statement" takes around 10us to execute (on average, obviously it would vary by statement... btw what is considered a statement in Python? A line of code?) With the above in mind, completing ISR handler with 10 statements should take around 160us (60+ 10x10) My question is, is it permitted (and does it have any effect) to decorate ISR handler with @micropython.native or viper to speed up the execution? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
When you specify Rather than rely on estimates, you could measure the speed of your ISR with |
Beta Was this translation helpful? Give feedback.
-
In my tests of the RPI Pico, responding to a Pin.irq took ~26µ for a hard interrupt and ~34µ for a scheduled interrupt. The time to run a statement varies a lot. So saying it takes 10µs in average may not be a good estimation. You have to look at the code in question. And code execution speed can be improved a lot. |
Beta Was this translation helpful? Give feedback.
-
Hi Robert, if it is convenient, could you share your test code with us? |
Beta Was this translation helpful? Give feedback.
-
Sure. The code is dead simple. It just sets up the IRQ with the handler and a PWM for creating the trigger pulse. The delay is measured with an oscilloscope. But you can as well use a simple logic analyzer for that purpose.
And this is a fresh screenshot from the oscilloscope. The yellow track is the trigger pulse, the green the response by the IRQ handler. The average delay shown is 16.8 µs. Actually it is less since what you see is already the result of the first statement in the ISR. And you can see that a call to p15(x) takes about 3.3 µs. |
Beta Was this translation helpful? Give feedback.
See docs for e.g. Pin.irq https://docs.micropython.org/en/latest/library/machine.Pin.html#machine.Pin.irq and the linked document about writing interrupt handlers.
Not all ports support both hard=True and hard=False (i.e. "soft"). e.g. ESP32 only supports soft. rp2040 supports both. Hard interrupts run in the CPU's interrupt context and execute immediately (pre-empting running code). Soft interrupts are scheduled for later execution.
Time per line or s…