To prevent spurious GPIO wake-up from DeepSleep in the XIAO nRF52840

The nRF52840 can wake up from DeepSleep using GPIO as a signal source, but the following settings alone often cause the device to wake up without a signal.

  nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
  NRF_POWER->SYSTEMOFF = 1;

The prevention method is described in “6.10.2 PORT event” of the datasheet, and following this method will avoid unintended wakeups. See also 6.10.4.5 EVENTS_PORT, 6.10.4.6 INTENSET, 6.10.4.7 INTENCLR.

  NRF_GPIOTE->INTENCLR |= 0x80000000;   // disable interrupt
  nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);  	// configure gpio
  NRF_GPIOTE->EVENTS_PORT = 0;          // Event not generated
  NRF_GPIOTE->INTENSET |= 0x80000000;   // enable interrupt

  NRF_POWER->SYSTEMOFF = 1;			    // enter to DeepSleep

I hope this will be helpful to those interested.

For reference, here is the sketch I used for the experiment.

//----------------------------------------------------------------------------------------------
// BSP : Seeed nRF52 Borads 1.1.8
// Board : Seeed nRF52 Borads / Seeed XIAO nRF52840 Sense 
//----------------------------------------------------------------------------------------------
// See datasheet "6.10.2 Port event"
// In order to prevent spurious interrupts from the PORT event while configuring the sources, the user
// shall first disable interrupts on the PORT event (through INTENCLR.PORT), then configure the sources
// (PIN_CNF[n].SENSE), clear any potential event that could have occurred during configuration
// (write '0' to EVENTS_PORT), and finally enable interrupts (through INTENSET.PORT).

// 2025/01/21

#include <Adafruit_SPIFlash.h>    // 5.0.0, NOTE:need to deleted /Documents/Arduino/libraries/SdFat
#include <flash_devices.h>

// for flashTransport definition
Adafruit_FlashTransport_QSPI flashTransport;
Adafruit_SPIFlash flash(&flashTransport);
static const SPIFlash_Device_t my_flash_devices[] = {P25Q16H,};
const int flashDevices = 1;

#define PIN_WAKEUP (28)  // D0:(2), D1:(3), D2:(28), D3:(29), D4:(4), D5(5), D6(43), D7(44), D8(45), D9(46), D10(47)

void setup()
{
  // Enable DC-DC converter Mode
  NRF_POWER->DCDCEN = 1;            // Enable DC/DC converter for REG1 stage
  
  // on board Flash enter to Deep Power-Down Mode
  flash.begin(my_flash_devices, flashDevices);
  flashTransport.begin();
  flashTransport.runCommand(0xB9);  // enter deep power-down mode
  delayMicroseconds(5);             // tDP=3uS
  flashTransport.end();

  // LED on for 1 sec.
  digitalWrite(LED_GREEN, LOW);
  delay(1000);
  digitalWrite(LED_GREEN, HIGH);

// https://forum.seeedstudio.com/t/xiao-ble-long-boot-up-time/280116/13
//  NRF_POWER->GPREGRET = 0x6D; // Option : Set GPREGRET0 to 0x6D (DFU SKIP)

  NRF_GPIOTE->INTENCLR |= 0x80000000;   // disable interrupt for event PORT
//  nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);  // configure gpio for wakeup event
  nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);  // configure gpio for wakeup event
  NRF_GPIOTE->EVENTS_PORT = 0;          // Event not generated
  NRF_GPIOTE->INTENSET |= 0x80000000;   // enable interrupt for event PORT

  // enter to Deep Sleep Mode
  NRF_POWER->SYSTEMOFF = 1;
}

void loop()
{
}