Page 1 of 1

.irq().flags() - what is this?

Posted: Tue Mar 08, 2022 1:03 pm
by beetle
Im learning about micropython on the rpi pico. In the Pi Pico Python SDK doc it shows a nice example of setting interrupts.

from machine import Pin
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
p2.irq(lambda pin: print("IRQ with flags:", pin.irq().flags()), Pin.IRQ_FALLING)

And it works, when I press a button attached to a pin (I used pin 22) it reports :

IRQ with flags: 4

Ok, but what does flags 4 mean. I've looked at the micropython docs, and googled and I cant find anything about .irq().flags() at all. I'm probably missing the obvious but can anyone explain what flags 4 means and perhaps point me in the direction of some documents on this.

Thanks

Re: .irq().flags() - what is this?

Posted: Tue Mar 08, 2022 7:52 pm
by beetle
A little progress - I found this:
irq.flags() get the triggers that caused the current irq. Only returns useful values when called inside the irq handler. The flags are cleared automatically when leaving the handler, therefore, this method always returns 0 when called outside.
in a document:
https://github.com/micropython/micropyt ... e-API#irqs (nothing found the the main micropython docs though)

So the irq.flags() seems to be related to the trigger. So I then tried RISING instead of FALLING

p2.irq(lambda pin: print("IRQ with flags:", pin.irq().flags()), Pin.IRQ_RISING)

and now the output is:
IRQ with flags: 8 (whereas FALLING was 4)

But still I have not found any documentation on these flag numbers :roll: They must be useful for something ;)

Re: .irq().flags() - what is this?

Posted: Sat Mar 19, 2022 4:25 am
by taPIQoLEHUMA
See the RP2040 datasheet (mine is from 2021-11-04, version 150df05-clean).
Section 2.19 GPIO, and section 2.19.6 List of Registers, around page 272.
Look at registers INTR0 through INTR3.
They hold the individual flags for GPIO interrupts: each pin hi, lo, rising, falling.
It looks like they don't return the whole register, just the 4 bits for the pin that generated the interrupt.

A snippet:

Code: Select all

bit name            type 
  ...                         
 5 GPIO1_LEVEL_HIGH
 4 GPIO1_LEVEL_LOW
 3 GPIO0_EDGE_HIGH   wc
 2 GPIO0_EDGE_LOW    wc
 1 GPIO0_LEVEL_HIGH  ro
 0 GPIO0_LEVEL_LOW   ro
GPIOx_EDGE_HIGH will give you 8, GPIOx_EDGE_LOW is 4, GPIOx_LEVEL_HIGH is 2, GPIOx_LEVEL_LOW is 1.

(I hope this made some sense.)