nrf_reset_resetreas_get(NRF_RESET) determine which GPIO pin caused the wake?

I can read the button and see if it is being held.  I would like to know which GPIO pin caused the wake without having to hold the button down to signify the button that woke the device.

I don't see a way to do that.  Is that possible?

    reas = nrf_reset_resetreas_get(NRF_RESET);
    nrf_reset_resetreas_clear(NRF_RESET, reas);
    if (reas & NRF_RESET_RESETREAS_NFC_MASK) {
        LOG_DBG("Wake up by NFC field detect\n");
    } else if (reas & NRF_RESET_RESETREAS_RESETPIN_MASK) {
        LOG_DBG("Reset by pin-reset\n");
    } else if (reas & NRF_RESET_RESETREAS_SREQ_MASK) {
        LOG_DBG("Reset by soft-reset\n");
    } else if (reas) {
        LOG_DBG("Reset by a different source (0x%08X)\n", reas);
        int val=0;
#ifdef CONFIG_BOARD_NRF5340DK_NRF5340_CPUAPP
            val = gpio_pin_get_dt(&button2);
            if (val > 0) {
                LOG_DBG("button2 %d was held.",val);
            } else {
                LOG_DBG("button2 was NOT held.\n");
            }
            val = gpio_pin_get_dt(&button3);
            if (val > 0) {
                LOG_DBG("button3 %d was held.",val);
                }
            } else {
                LOG_DBG("button3 was NOT held.\n");
            }
#endif
Parents
  • Hello,

    You may try to read out the GPIO LATCH register on startup by doing something like this: 

    #include <hal/nrf_gpio.h>
    ...
        uint32_t latch_mask[GPIO_COUNT];
        nrf_gpio_latches_read_and_clear(0, GPIO_COUNT, latch_mask);
    
        for (int i=0; i < GPIO_COUNT; i++) {
            if (latch_mask[i] != 0) {
                printk("Latch for pin P%d.%d is set\n", i, NRF_CTZ(latch_mask[i]));
            }
        }

    You may also need to call nrf_gpio_latches_read_and_clear() before entering system OFF to ensure none of the latches are set.

    This approach assumes that the LATCH registers can't be set by other pins during boot. For instance, while the bootloader is running.

    Best regards,

    Vidar

  • thanks for the code.  just so happens that this project sets mcuboot's CONFIG_GPIO=n.  So I would hope that I would not be affected and have to revisit the mcuboot; maybe I need to disable it all together.

    for some reason GPIO_COUNT is 2.

    always reports (I added an else and replaced "Latch" with no):
    [00:00:00.669,158] <inf> main: Latches GPIO count: 2
    [00:00:00.674,804] <inf> main: no for pin P0.32 is set
    [00:00:00.680,725] <inf> main: no for pin P1.32 is set

Reply
  • thanks for the code.  just so happens that this project sets mcuboot's CONFIG_GPIO=n.  So I would hope that I would not be affected and have to revisit the mcuboot; maybe I need to disable it all together.

    for some reason GPIO_COUNT is 2.

    always reports (I added an else and replaced "Latch" with no):
    [00:00:00.669,158] <inf> main: Latches GPIO count: 2
    [00:00:00.674,804] <inf> main: no for pin P0.32 is set
    [00:00:00.680,725] <inf> main: no for pin P1.32 is set

Children
  • The GPIO_COUNT is actually reflecting the number of GPIO ports on the device. I was checking the LATCH register for both ports, but this is not necessary if your wake-up pins are all on the same port. You can also optimize the implementation to only test relevant bits in the LATCH register.

    tldr said:
    always reports (I added an else and replaced "Latch" with no):

    Please make sure you are not passing '0' to the NRF_CTZ macro. 

  • I'm confused; aren't there are more than 2 ports?

    I'd like to be able to read when the accel or a switch is pressed.

    NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw1), gpios) or 
    NRF_GPIO_PIN_MAP(0,19)

    I would have expected at least 19 from GPIO_COUNT.  We're only getting 0 for the latch coming back from the 2 ports. 
Related