Use RESET pin as GPIO - nRF52840

Hi all,

I am developing an application for a custom board using the nRF52840. I need to use the reset pin (P0.18) as a regular GPIO, configured as an input with an associated interrupt. However, I am unable to set this pin as a GPIO. The steps I have followed are based on this thread.

My setup:

- First, I try to run the application on nRF52840dk

- I am using the Visual Studio nRF Connect extension, with version v2.3.0 for both toolchain and SDK to build the application.

- I flash the board using the following commands on a powershell terminal

nrfjprog -e
west flash --softreset

Code configuration:

- I added CONFIG_GPIO_AS_PINRESET=n on prj.conf file

- Then, I added the following code before configuring the pin to turn on the disconnect flag on the PSELRESET registers

#ifdef CONFIG_GPIO_AS_PINRESET
    if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Disconnected << UICR_PSELRESET_CONNECT_Pos)) || 
          ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Disconnected << 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 | (1 << 31);
      while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
      NRF_UICR->PSELRESET[1] = 18 | (1 << 31);
      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){}
      // UICR changes require a reset to be effective
      NVIC_SystemReset();  
    }
#endif

Results:

After setting these configurations, I get CONFIG_GPIO_AS_PINRESET is not set after building the application, and I get a HARD FAULT debug event when the pin configuration is set.

How can I solve this problem?

Candela

Parents
  • Hi,

    There are two aspects here. Firstly, CONFIG_GPIO_AS_PINRESET is deprecated, and you need to configure this in the device tree with  gpio-as-nreset. So if you have your custom board files, don't add it. If you are using DK board files (which as it), make an app.overlay file or similar with this:

    &uicr {
            /delete-property/ gpio-as-nreset;
        };
    

    With this, the firmware itself will not configure the pin as reset, and you can use it as a GPIO.

    The next is programming. By default, pin reset will be enabled when programming with west. You can avoid this with the "--softreset" option to west flash. This is also used by nRF Connect for VS Code, and to make vs code use soft reset you can add a softreset config as explained here. Note that you need to provide a path to a specific application, and this config only applies to that application (to use on multiple applications you need to specify it separately for each of the apps you need to use). (I have asked the team to make this a generic configuration and they are looking into that.)

  • Hi,

    I've been trying to modify the UICR node on an overlay file, but upon reviewing the nRF52840dk original device tree file, I could not locate the "gpio-as-nreset" property, so I get an error when I try to remove it. I am using SDK v2.3.0 version, could this be the problem?

    I managed to softreset the device with west flash too.

    Thanks a lot,

    Candela

  • Hi Candela,

    Ah, sorry. On 2.3.0 CONFIG_GPIO_AS_PINRESET is what you should use, so you can ignore my comment about the devicetree as that is only relevant for 2.5.0 and later.

    Do you get it to work with VS Code as well as west flash now? For reference, this is what I tested with that worked for me:

            "nrf-connect.applicationOptions": {
                "/home/eith/ncs/v2.5.0/zephyr/samples/basic/blinky": {
                  "flash": {
                    "softreset": true,
                    "skipBuild": false
                  }
                }
              }

Reply
  • Hi Candela,

    Ah, sorry. On 2.3.0 CONFIG_GPIO_AS_PINRESET is what you should use, so you can ignore my comment about the devicetree as that is only relevant for 2.5.0 and later.

    Do you get it to work with VS Code as well as west flash now? For reference, this is what I tested with that worked for me:

            "nrf-connect.applicationOptions": {
                "/home/eith/ncs/v2.5.0/zephyr/samples/basic/blinky": {
                  "flash": {
                    "softreset": true,
                    "skipBuild": false
                  }
                }
              }

Children
  • Hi Einar,

    I am currently using v2.7.0 with NRF52840 DK.  I want to use p0.18 as a chip select pin for SPI.  Per your answer above, I've modified the device tree overlay to match your UICR redefinition.

    Upon booting, I still get this assertion:

    ASSERTION FAIL [(cfg->port_pin_mask & (gpio_port_pins_t)(1UL << (pin))) != 0U] @ WEST_TOPDIR/zephyr/include/zephyr/drivers/gpio.h:1005
    Unsupported pin

    Here are my overlay definitions:

    &pinctrl {
    spi2_default: spi2_default {
            group1 {
                psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                    <NRF_PSEL(SPIM_MOSI, 0, 23)>,
                    <NRF_PSEL(SPIM_MISO, 1, 00)>;
            };
        };
    };
    spi2: &spi2 {
        compatible = "nordic,nrf-spi";
        status = "okay";
        pinctrl-0 = <&spi2_default>;
        pinctrl-1 = <&spi2_sleep>;
        pinctrl-names = "default", "sleep";
        cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
    };

    &uicr {
        /delete-property/ gpio-as-nreset;
    };
    I've also tried without the uicr redefinition and also tried CONFIG_GPIO_AS_PINRESET in the proj.conf but it shows deprecrated.
  • Hi,

    The correct way to use the reset pin as normal GPIO in recent SDK releases is to do as you have done, making sure gpio-as-nreset is not set (either by not setting it in the board files or deleting the propery in an overlay file).

    Have you checked with a debugger which pin number you get this assert for?

  • Hi,

    The only way to make it works for me is to launch the command at first use of my custom board:

    nrfjprog --eraseuicr


    Before using Flash command (with softreset) from VS Code action.

  • Hi,

    Yes, if the board was previously programmed in a way that enabled pin reset, you will have to earse the UICR, as that is persistent. That can be done like you do, or more generall with "nrfjprog --recover" (which will also work if AP protect is enabled). 

  • My nrf52840dk_nrf52840.dts file had the following:

    &gpio0 {
        status = "okay";
        gpio-reserved-ranges = <0 2>, <6 1>, <8 3>, <17 7>;
        gpio-line-names = "XL1", "XL2", "AREF", "A0", "A1", "RTS", "TXD",
            "CTS", "RXD", "NFC1", "NFC2", "BUTTON1", "BUTTON2", "LED1",
            "LED2", "LED3", "LED4", "QSPI CS", "RESET", "QSPI CLK",
            "QSPI DIO0", "QSPI DIO1", "QSPI DIO2", "QSPI DIO3","BUTTON3",
            "BUTTON4", "SDA", "SCL", "A2", "A3", "A4", "A5";
    };
    I verified UICR was cleared and it kept asserting.  The gpio-reserved-ranges was what caused the assertion.  Commenting that line out allowed me to move forward.  Putting this here in case someone else runs into the same issue.
    Thanks!
Related