As per request in IRC, we might want an interface for GPIO interrupts.
To implement GPIO interrupts, a number of things are needed:
- A new API-call in
epicardium.hto enable/disable interrupts for a specific GPIO + set whether it triggers on rising or on falling (Read the API Guide for more info). Take a look at the existing GPIO API (Docs & Code) and make it compatible with that.
- Additionally, a new interrupt needs to be defined in
epicardium.h:202. Here we have to make a decision: One catch-all interrupt or separate interrupts for each GPIO pin? Read the Interrupts documentation for a bit more insight into how they work. I know, this part of the docs is a bit lacking ...
- The implementation of this API-call should setup the interrupt similar to this code for example.
- The interrupt handler defined for the previous point can't directly trigger the API interrupt. Instead, it needs to enqueue a job into FreeRTOS using
- This job can then call
api_interrupt_trigger()to trigger an interrupt in MicroPython.
- On the MicroPython side, we have a very minimal (sadly undocumented as of now!) module to install handlers for interrupts. For all new interrupt IDs defined in
epicardium.h, an entry needs to be added in
- You also need to add a wrapper for you new API call in
pycardium/modules/gpio.cand document it in
After doing all that, the following code should be able to catch an interrupt (With the small caveat that MicroPython needs to be busy-spinning for the handler to be executed for now ... See !324 (merged)):
import interrupt def handler(x): print("Got interrupt!") # Call the new API-call to enable the GPIO interrupt here ... interrupt.set_callback(interrupt.GPIO_INTERRUPT, handler) interrupt.enable_callback(interrupt.GPIO_INTERRUPT) while True: pass
Hope this is enough to get someone started on implementing GPIO interrupts. If you have any questions, feel free to ping me here or in IRC!