adafruit / Adafruit_nRF52_Arduino

Adafruit code for the Nordic nRF52 BLE SoC on Arduino
Other
601 stars 488 forks source link

No interrupt from pin D2 #728

Closed ajs123 closed 2 years ago

ajs123 commented 2 years ago

Operating System

MacOS

IDE version

Arduino CLI 0.18.3

Board

Feather nrf52840 Express

BSP version

Release 1.3.0

Sketch

// Testing GPIO interrupts

include

include

define PIN_A 5

define PIN_B 2

bool intA = false; bool intB = false;

void intAHandler() { intA = true; }

void intBHandler() { intB = true; }

void setup() { Serial.begin(115200); while (!Serial) delay(10); pinMode(PIN_A, INPUT_PULLUP); pinMode(PIN_B, INPUT_PULLUP); attachInterrupt(PIN_A, intAHandler, CHANGE); attachInterrupt(PIN_B, intBHandler, CHANGE); }

void loop() { if (intA) { Serial.println("Interrupt A"); intA = false; } if (intB) { Serial.println("Interrupt B"); intB = false; } }

What happened ?

Grounding pin 5 gives "Interrupt A" on the serial port, as expected. Grounding pin 2 gives no response.

How to reproduce ?

Load the sketch and ground either of the pins.

Debug Log

BSP Library : 1.3.0 Bootloader : s140 6.1.1 Serial No : 2152F8836D2C9B52

Screenshots

No response

ajs123 commented 2 years ago

I may have tracked this down to the pin setup code in cores/nRF5/nordic/nrfx/mdk/system_nrf52840.c, line 160. If CONFIG_NFCT_PINS_AS_GPIOS is not defined, then D2 remains reserved for NFC.

The overview at adafruit.com states

The D2 pin is uses the same pad as one-half of the NFC antenna pins. By default, the nRF52840 Feather ships with these pins configured for GPIO mode, which is done by writing a value to the UICR flash config memory. If you wish to use NFC, you will need to erase the UICR memory which requires erasing the entire chip, and you will need a Segger J-Link to reflash the bootloader and firmware.

Is it possible that the CONFIG_NFCT_PINS_AS_GPIOS got dropped from the bootloader config?

It seems that it should work to run the code block from system_nrf52840.c, which should do a one-time update to the UICR...

void NFCAsGPIO() {
    if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
        ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        NRF_UICR->PSELRESET[0] = 18;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        NRF_UICR->PSELRESET[1] = 18;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        NVIC_SystemReset();
    }
}

I'm thinking that I shouldn't try that before seeing what you think!

hathach commented 2 years ago

D2 is indeed used for NFC probably by default, you could run this sketch once to convert it to normal GPIO mode

https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/libraries/Bluefruit52Lib/examples/Hardware/nfc_to_gpio/nfc_to_gpio.ino

ajs123 commented 2 years ago

Thanks! And sorry that I missed seeing the example sketch. I use Arduino-CLI via VSCode and the examples are tucked away a bit deeper in the UI. It seems like either the product page or the compilation options for the softdevice should change :-).

Anyway, thanks for confirming and pointing to the existing sketch.

hathach commented 2 years ago

No problems at all, can you confirm D2 work fine after convert from nfc to gpio so that we could close this issue

ajs123 commented 2 years ago

Yes - I can confirm. I verified that I now get interrupts on D2.

hathach commented 2 years ago

thanks for confirming