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

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
beetle
Posts: 51
Joined: Sat Oct 16, 2021 11:35 am

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

Post by beetle » Tue Mar 08, 2022 1:03 pm

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

beetle
Posts: 51
Joined: Sat Oct 16, 2021 11:35 am

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

Post by beetle » Tue Mar 08, 2022 7:52 pm

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 ;)

taPIQoLEHUMA
Posts: 15
Joined: Thu Mar 04, 2021 2:59 am

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

Post by taPIQoLEHUMA » Sat Mar 19, 2022 4:25 am

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.)

Post Reply