Board Crashed on Upload... GPIOTE_IRQHandler_v()

Hi there,

I'm experimenting with GPIOTE inputs. I have one Nano 33 BLE wired and sending signals to another Nano 33 BLE.

I'm successfully able to acknowledge inputs via Serial.print() by adding... if (NRF_GPIOTE->EVENTS_IN[0] != 0) within loop(). Therefore I know that the input signals are being processed ok.

Where I'm stuck is that whenever NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos is uncommented (i.e., I want to have control by using the GPIOTE handler), the board is in a state of being crashed immediately after uploading, requiring pressing the bootloader button.

Incidentally, I've had several test programs working perfectly which use: Timer Handlers, GPIOTE, PPI. GPIOTE handlers from examples seem similar in style to timer handlers, and so I'm wondering what I could have done wrong. Here's my short test program... (Even with all my checks commented, the board is still crashed on upload)

#include <nrf.h>

#define PIN_GPIO (2) // ~D10 = P1.02 ... Nano 33 BLE
#define PORT (1)

void setup()
{			
	Serial.begin(115200);
	
	// Configure PIN_GPIO as input
	// ---------------------------		
	NRF_GPIO->DIRSET = GPIO_DIRSET_PIN2_Input << GPIO_DIRSET_PIN2_Pos; // Not actually required based on "Input" being the default
	
	// Configure GPIOTE
	// ----------------
	NRF_GPIOTE->CONFIG[0] =
	
	(GPIOTE_CONFIG_MODE_Event       << GPIOTE_CONFIG_MODE_Pos) |
	(GPIOTE_CONFIG_POLARITY_LoToHi	<< GPIOTE_CONFIG_POLARITY_Pos) | // (Commenting this line stops the board crashing. But: GPIOTE_IRQHandler_v() still doesn't run) 
	(PORT                           << GPIOTE_CONFIG_PORT_Pos) |     // (Although it's actually pointless commenting the above line because then no IN[n] events will be generated)
	(PIN_GPIO                       << GPIOTE_CONFIG_PSEL_Pos);		
	
	// NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos (same as INTENSET = 1)
	NRF_GPIOTE->INTENSET = 1; // Set = 1 & [0] = Bit number 0 (Commenting this line stops the board crashing, which causes "EVENTS_IN Has Fired" to successfully be displayed periodically)		
	NVIC_EnableIRQ(GPIOTE_IRQn); 
}

volatile bool handlerTest = false;

extern "C" void GPIOTE_IRQHandler_v()
{			
	 /* if (NRF_GPIOTE->EVENTS_IN[0] != 0)
	{
		NRF_GPIOTE->EVENTS_IN[0] = 0;
        handlerTest = true;					
	} */
}

void loop()
{		
    /* if (NRF_GPIOTE->EVENTS_IN[0] != 0)
	{
		NRF_GPIOTE->EVENTS_IN[0] = 0;
		Serial.println("EVENTS_IN Has Fired");				
	} */

    /* if (handlerTest)
    {
        handlerTest = false;
        Serial.println("Handler Has Fired");
    } */
}
1 Like

I also am experiencing issues with crashing GPIOTE on Arduino Nano 33 BLE; however I did find a bug with your current implementation.

NRF_GPIO is deprecated, and instead you should use NRF_P0 and NRF_P1 for your GPIO interfaces. That way you can access both pins for Ports 0 and 1 respectively.

I'll work some more and should have something soon regarding that crashing. I have been able to understand the issue occurs when the NRF_GPIOTE INTENSET register is set. If you come up with a working setup, please share if possible.

Below is my current debugging program. After the configure_gpiote_for_events function is called, the board freezes.

// Rising Edge Interrupt for GPIO (D2-9) Events
extern "C" void GPIOTE_IRQHandler_v() {
  pinMode(LEDG, OUTPUT);
  if (NRF_GPIOTE->EVENTS_IN[0] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[0] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[1] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[1] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[2] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[2] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[3] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[3] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[4] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[4] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[5] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[5] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[6] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[6] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_IN[7] == 1)
  {
    NRF_GPIOTE->EVENTS_IN[7] = 0;
    
    // Code Here
  }
  else if (NRF_GPIOTE->EVENTS_PORT == 1)
  {
    NRF_GPIOTE->EVENTS_PORT = 0;
  }
}

#define GPIO_P0 0UL
#define GPIO_P1 1UL

// P0
#define D7_PIN_NUM 23UL
#define D8_PIN_NUM 21UL
#define D9_PIN_NUM 27UL

// P1
#define D2_PIN_NUM 11UL
#define D3_PIN_NUM 12UL
#define D4_PIN_NUM 15UL
#define D5_PIN_NUM 13UL
#define D6_PIN_NUM 14UL


constexpr uint32_t GPIOTE_PINS[] = {D2, 
                                    D3, 
                                    D4, 
                                    D5, 
                                    D6, 
                                    D7, 
                                    D8, 
                                    D9};

constexpr uint32_t NUM_GPIOTE_PINS = sizeof(GPIOTE_PINS) / sizeof(uint32_t);

enum NRF_GPIOTE_CONFIG_POS
{
  GPIOTE_CONFIG_MODE_POS     = 0UL,
  GPIOTE_CONFIG_PSEL_POS     = 8UL,
  GPIOTE_CONFIG_PORT_POS     = 13UL,
  GPIOTE_CONFIG_POLARITY_POS = 16UL,
  GPIOTE_CONFIG_OUTINIT_POS  = 20UL
};

/**
 * Configure the Digital Pins on the Nano 33 for GPIOTE Signalling
 */
void configure_digital_pins_for_gpiote()
{

  Serial.println("\nNRF_GPIO Configuration:");
  Serial.println(NRF_P0->DIR, BIN);
  Serial.println(" ");

  //configure_digital_pins();
  for (uint32_t i = 0 ; i < NUM_GPIOTE_PINS ; ++i)
  {
    pinMode(GPIOTE_PINS[i], INPUT);
  }
  
  Serial.println("\nNRF_GPIO Configuration (After INPUT Initialization):");
  Serial.println(NRF_P0->DIR, BIN);
  Serial.println(" ");
  
  NRF_GPIOTE->CONFIG[0] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D7_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P0                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  NRF_GPIOTE->CONFIG[1] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D8_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P0                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);
  
  NRF_GPIOTE->CONFIG[2] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D9_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P0                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);
                          
  NRF_GPIOTE->CONFIG[3] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D2_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P1                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  NRF_GPIOTE->CONFIG[4] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D3_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P1                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  NRF_GPIOTE->CONFIG[5] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D4_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P1                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  NRF_GPIOTE->CONFIG[6] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D5_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P1                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  NRF_GPIOTE->CONFIG[7] = (1UL                        << GPIOTE_CONFIG_MODE_POS)     |
                          (D6_PIN_NUM                 << GPIOTE_CONFIG_PSEL_POS)     |
                          (GPIO_P1                    << GPIOTE_CONFIG_PORT_POS)     |
                          (NRF_GPIOTE_POLARITY_LOTOHI << GPIOTE_CONFIG_POLARITY_POS);

  Serial.println("NRF_GPIOTE Configuration:");
  Serial.println(NRF_GPIOTE->CONFIG[0], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[1], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[2], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[3], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[4], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[5], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[6], BIN);
  Serial.println(NRF_GPIOTE->CONFIG[7], BIN);
}

void configure_gpiote_for_events()
{

  configure_digital_pins_for_gpiote();

  // Enable GPIOTE Interrupts
  NRF_GPIOTE->INTENSET = 0x01 |
                         0x02 |
                         0x04 |
                         0x08 |
                         0x10 |
                         0x20 |
                         0x40 |
                         0x80;


  NVIC_SetPriority( GPIOTE_IRQn, 1UL );
  NVIC_EnableIRQ( GPIOTE_IRQn );

  uint32_t i = 0;
  while(1)
  {
    // Wait here
    Serial.print(++i);
  }
}

Hi ejbasile, and thank you for sharing,

I will eventually post an update here of any progress I make. I'm currently using the nRF5 SDK directly, downloaded from Nordic. I also now have one of their official development kits (nRF52840-DK).

Up until now I've pretty much only been accessing the registers directly, in contrast to the option of calling the various functions of the SDK.

Please can you let me know how you discovered that "NRF_GPIO is depreciated"? I've been referring to Nordic's info centre for details about the registers, in particular for GPIO the following page... GPIO — General purpose input/output

As an example, I've successfully used NFR_GPIO within a program that produces pulse position modulation (PPM) for servo control... Pulse Position Modulation Examples

Like I say, I do fully intend to post an update here once I know more... Cheers! for now.

1 Like

No problem,

I have been doing the same due to Arduino's lack of integration with the NRF52840 SDK.

If you look below. you can see the following from the link you provided above.

And thanks for the update!

1 Like

Thank you for showing me the table. It's easy to overlook. I'll try my Arduino code this evening with the relevant changes. It'll be interesting to see if it fixes it. Edit: ah sorry... of course our INTENSET issue is a separate matter... good info though regarding deprecated.

I guess my PPM program works because deprecated can take time to become a big problem, depending on how up to date everything is.

Interesting though because the Nordic guys never mentioned about it to me... NRF_GPIOTE->INTENSET = 1... Crashes...

Thanks again :+1:

1 Like

No problem Gary, I also opened an issue with Arduino mbed OS's Github as there does seem to be an issue with GPIOTE on mbed OS. It is located below.

1 Like

I got side-tracked discovering how to get Nordic's Open Bootloader working on the Nano 33 BLE, although I do know that Arduino's version of bossac is an option as well.

The best solution I've found is to use Nordic's drivers as per the following Nordic "pin_change_int" example:

/**
 * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
 * @defgroup pin_change_int_example_main main.c
 * @{
 * @ingroup pin_change_int_example
 * @brief Pin Change Interrupt Example Application main file.
 *
 * This file contains the source code for a sample application using interrupts triggered by GPIO pins.
 *
 */

#include <stdbool.h>
#include "nrf.h"
#include "nrf_drv_gpiote.h"
#include "app_error.h"
#include "boards.h"

#ifdef BSP_BUTTON_0
    #define PIN_IN 43 // BSP_BUTTON_0
#endif
#ifndef PIN_IN
    #error "Please indicate input pin"
#endif

#ifdef BSP_LED_0
    #define PIN_OUT 41 // BSP_LED_0
#endif
#ifndef PIN_OUT
    #error "Please indicate output pin"
#endif

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    nrf_drv_gpiote_out_toggle(PIN_OUT);
}
/**
 * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output,
 * and configures GPIOTE to give an interrupt on pin change.
 */
static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);

    err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    gpio_init();

    while (true)
    {
        // Do nothing.
    }
}

/** @} */

I've tested this example exactly as it is, apart from that I've changed the input pin and LED to be as I require for my receiving Nano 33 BLE. I'm using Segger Embedded Studio to program (via my nRF52840-DK) my Nano boards via their debug interface.

I did try continuing with setting the registers directly as much as possible, but I've found that certain things don't work (at least not when starting via the SES pre-built example projects/settings that I've tried).

For example, the following doesn't configure and set the input pin to logic high...

// Configure PIN_GPIO as input
// ---------------------------
NRF_GPIO->PIN_CNF[PIN_GPIO] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
                            | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
					        | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
						    | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
						    | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);

But the following (which I copied from nrf_gpio_cfg(...) within nrf_gpio.h, substituted the relevant values directly into the nrf_gpio_cgf(...) function, and then added it to main()) does set the pin...

NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&BUTTON_GPIO);

   reg->PIN_CNF[BUTTON_GPIO] =
       ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
       ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
       ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
       ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
       ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);

But then the following doesn't work (i.e., always returns 0 or 1, but I can't remember which) nrf_gpio_pin_read(BUTTON_GPIO).

So, by using: nrf_gpio_cfg_input(BUTTON_GPIO, NRF_GPIO_PIN_PULLUP) instead, it does then indeed work (Segger Embedded Studio is absolutely awesome for hoping through function calls across different files by mouse-right-click options).

Incidentally, from searching via SES, I noticed that the previously mentioned: nrf_gpio_cfg(...) is called from: nrf_gpio_cfg_input(...)'s definition.

Incidentally, from the above "pin_change_int" example...

err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler)

"nrf_drv_gpiote_in_init(...)" leads to: "nrfx_gpiote_in_init(...)" which uses: nrf_gpio_cfg_input(...)

However, although I was now able to send PPM from one board to another in demo/slow mode, making the receiving board LED blink exactly synchronised: GPIOTE_IRQHandler() wasn't getting called (but at least the receiving board doesn't crash). Incidentally, ultimately I might not need to use the GPIOTE handler, although it'll probably be best to, and in any case it's useful to know about for the future.

Then I was led to discovering the following as quoted below...

"Why don't you use the driver. You cannot implement GPIOTE_IRQHandler if you include nrf_drv_gpiote, and I do not recommend to modify the GPIOTE_IRQHandler in nrf_drv_gpiote.".

And: #include "nrf_gpio.h", has: #include <nrfx.h>, which has: nrf_drv_gpiote.h, which is within: ...integration/nrfx/legacy

Anyway, I'm tempted to ask questions about all this on DevZone, but maybe not. Probably just using their SDK's functions is best, and they have indeed worked perfectly for me so far.

Cheers! :+1:

1 Like

Thanks Gary for your detailed update! I was able to integrate some of these insights into my framework and was able to get <3us syncing between MCUs.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.