[nRF52] Any examples on interrupt handling for the nRF52?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

[nRF52] Any examples on interrupt handling for the nRF52?

Post by WhiteHare » Mon Nov 05, 2018 10:58 pm

I did read the generic documentation on interrupt handling (https://docs.micropython.org/en/latest/ ... rules.html), but I'm having trouble imagining how that works with the nRF52. One or more demo code examples would be helpful.

I've made some further improvements to my OTA code, but to get it to go faster it likely needs to be interrupt driven so that more of the work can be pipelined.

User avatar
FoldedToad
Posts: 7
Joined: Sat Feb 23, 2019 11:50 pm

Re: [nRF52] Any examples on interrupt handling for the nRF52?

Post by FoldedToad » Sun Feb 24, 2019 12:00 am

I have worked though the "pin" module, which supports, among other things, irq().
I have attached a simple demo program which illustrates how to catch interrupt events generated from GPIO pins.
This demo captures interrupts from the four switches on the PCA10040 board.
I have only tested this with the Nordic PCA10040 board, but I believe the general method should be applicable to the other Nordic boards as will as custom boards.

I don't see how to attach a file to this post, so I have copied the text below

----------------------------------------------

import machine
import time

#
# This switch-to-pin mapping is for the PCA10040. Change as needed
#
SWITCH_1_PIN = 13
SWITCH_2_PIN = 14
SWITCH_3_PIN = 15
SWITCH_4_PIN = 16

def switch_name(pin):
return {
SWITCH_1_PIN: "SWITCH_1",
SWITCH_2_PIN: "SWITCH_2",
SWITCH_3_PIN: "SWITCH_3",
SWITCH_4_PIN: "SWITCH_4"
}.get(pin, "SWITCH_?")

def switch_state(pin):
return {
1: "RELEASED",
0: "PRESSED"
}.get(pin, "??")

#
# Switch callback function
#
def switch_callback(pin_obj):
pin = pin_obj.pin()
state = pin_obj.value()
print("event: ", switch_name(pin), " ", switch_state(state))

print("start")

#
# Hook interrupts for each switch and set event callback
#
machine.Pin(SWITCH_1_PIN).irq(switch_callback)
machine.Pin(SWITCH_2_PIN).irq(switch_callback)
machine.Pin(SWITCH_3_PIN).irq(switch_callback)
machine.Pin(SWITCH_4_PIN).irq(switch_callback)

#
# Sleep 15 seconds to allow user to press/release switches
#
time.sleep_ms(15000)

print("end")

Post Reply