This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Can't Get an Interrupt to Execute

Gentlemen:

I'm new to Nordic processors, but I have implemented projects with Silabs Gecko and ATTiny processors in the past.  I'm working on a project for a consumer product with an Arduino Nano 33 BLE that uses the Nordic RNF52840 processor (I picked this board because of its tiny footprint and unique peripherals).  Arduino, as yet, still hasn't offered their own software tools for timers and interrupts, so I'm largely using Nordic code.

I'm having a terrible time getting a simple hardware interrupt to work.  It's as if the interrupts are not properly enabled or something.  Here is my basic code.  I am using simple hex to set registers to be more understandable than the long variable names.  I'm a mechanical engineer by trade, so please excuse my poor coding technique.

#include <nrf.h>
#include "nrf_timer.h"
#include "nrf_gpio.h"

const int epin = 2;
int LED = LED_BUILTIN;
uint32_t last_ecount = 0;

void setup() {

    nrf_gpio_cfg_output(LED);                           //set built-in LED pin as output
    nrf_gpio_cfg_input(epin, NRF_GPIO_PIN_PULLUP);      //set pin 2 as pullup input

    NVIC_EnableIRQ(GPIOTE_IRQn);                        //enable interrupts
    NRF_GPIOTE->CONFIG[0] = 0x00010201;                 //low-to-high, pin2, event mode
    NRF_GPIOTE->INTENSET = 0x00000001;                  //enable IN[0]
    
    NRF_TIMER1->TASKS_STOP = 1;
    NRF_TIMER1->MODE = 0;
    NRF_TIMER1->BITMODE = 3;                            //timer1 32-bit mode
    NRF_TIMER1->PRESCALER = 0;                          //use full system clock for timer1: 16 MHz
    NRF_TIMER1->TASKS_CLEAR = 1;
    NRF_TIMER1->CC[0] = 0;                              //zero timer1
    NRF_TIMER1->TASKS_START = 1;                        //start timer1
...

}

void loop(){...}

void GPIOTE_IRQHandler(void){
    uint32_t count = 0;
    
    if(NRF_GPIOTE->EVENTS_IN[0] == 1){                  //filter interrupt for wave E
        NRF_GPIOTE->EVENTS_IN[0] = 0;                   //clear interrupt flag
        NRF_TIMER1->TASKS_CAPTURE[0] = 1;               //capture timer1 value in CC0
        count = NRF_TIMER1->CC[0] - last_ecount;        //time elaspsed for one wave (CC0)
        last_ecount = count;                            //reset for next count
    }
}

Pin2 gets a square wave and the object is to measure the period of the wave (which is then Bluetoothed to another processor). I have troubleshooted the input and know that I'm getting a signal on GPIO2 (I can light an LED using that input pin number).  But when I run the code I get no interrupt at all, ever.  I don't even get an interrupt flag NRF_GPIOTE->EVENTS_IN[0].

I feel like I've tried everything and I'm at wits end.  I'm hoping that there is something simple and stupid I'm doing/not doing. 

Does anyone see my error?  Thanks for your help.

Don

DG Devices

Parents Reply Children
  • I still don't understand. If I run this code:

    int epin = 2;
    void setup() {
      nrf_gpio_cfg_output(LED_BUILTIN);                       //set built-in LED pin as output
      nrf_gpio_cfg_input(epin, NRF_GPIO_PIN_PULLUP);  //set pin 2 as pullup input
    }
    
    void loop() {
     int pin2 = 0;
      pin2 = digitalRead(epin);
      if(pin2)
        digitalWrite(LED_BUILTIN, HIGH);
        else
          digitalWrite(LED_BUILTIN, LOW);
    }

    The LED will be on.  If I connect pin D2 to ground, the LED turns off, if I disconnect it, it turns back on.  No other GPIO pin will affect the LED but D2.  Doesn't that mean that D2 is mapped to GPIO 2?  I don't understand.

    If D2 is mapped to P1.11, then what GPIO number do I use?  The GPIO number is an integer, not a P number.

    If I use the Arduino method I get the same result:

    int epin = 2;
    void setup() {
      pinMode(LED_BUILTIN, OUTPUT);                       //set built-in LED pin as output
      pinMode(epin, INPUT);                                  //set pin 2 as pullup input
    }
    
    void loop() {
     int pin2 = 0;
      pin2 = digitalRead(epin);
      if(pin2)
        digitalWrite(LED_BUILTIN, HIGH);
        else
          digitalWrite(LED_BUILTIN, LOW);
    }

    I don't understand what you're telling me.  The wiring schematic you refer to shows D2 connected to GPIO39, but GPIO's only are supposed to go to 31. How can this be?

    All I need is to have six input pins that I can use for external interrupts.  If someone can tell me what pins to use and what GPIO numbers to use, that will get me by.

    Don

    DG Devices

  • Post the code for pinmode, digitalRead and digitalWrite, maybe there is some mapping there. The Nordic functions such as nrf_gpio_cfg_output deal with nRF52840 pin numbers P0.02 is pin 2 in that notation. GPIO39 is Nordic nRF52840 pin P1.11

  • But If D2 works when directly connected to the LED, then shouldn't it work when used to trigger an external interrupt? A pin is a pin, isn't it?  If the mapping were different, it would cause serious problems.  In my code above (two posts ago) I used D2 (GPIO2) as an interrupt and it doesn't work.

    I don't think there even is a "GPIO39".  I tried this:

    int epin = 39;
    void setup() {
      pinMode(LED_BUILTIN, OUTPUT);                       //set built-in LED pin as output
      pinMode(epin, INPUT);                                  //set pin 39 as pullup input
    }
    
    void loop() {
     int pin39 = 0;
      pin39 = digitalRead(epin);
      if(pin39)
        digitalWrite(LED_BUILTIN, HIGH);
        else
          digitalWrite(LED_BUILTIN, LOW);
    }

    And nothing happens with any pin.  GPIO numbers are only supposed to be 0 to 31.

    Regardless of what is mapped to what, how can D2 operate the LED directly, but not generate an interrupt on the same GPIO?

    Don

    DG Devices

  • There are actually 48 i/o pins, not 32; nRF52840 has 2 i/o ports P0 and P1, nRF52832 has only 1 (P0).

    /**
     * @brief Function for extracting port and the relative pin number from the absolute pin number.
     *
     * @param[in,out] p_pin Pointer to the absolute pin number overriden by the pin number that is relative to the port.
     *
     * @return Pointer to port register set.
     */
    __STATIC_INLINE NRF_GPIO_Type * nrf_gpio_pin_port_decode(uint32_t * p_pin)
    {
        NRFX_ASSERT(*p_pin < NUMBER_OF_PINS);
    #if (GPIO_COUNT == 1)
        return NRF_P0;
    #else
        if (*p_pin < P0_PIN_NUM)
        {
            return NRF_P0;
        }
        else
        {
            *p_pin = *p_pin & (P0_PIN_NUM - 1);
            return NRF_P1;
        }
    #endif
    }

    I'm not sure I'm helping here, I have no idea what those non-Nordic definitions are doing in relation to mapping pins, such as pinmode() for example, if no code is posted. Maybe others are familiar with them ..

  •  is right. Look at digitalWrite() and digitalRead() in wiring_digital.c, you can see that GPIO pin number passes through remapping:

    ulPin = g_ADigitalPinMap[ulPin];

    Pin mapping is defined in variants subdirectory, for BleNANO it will be in variant.cpp, where GPIO 2 corresponds to P0.10 in Nordic numeration. I don't know exactly which variant corresponds to your board, but pin numbering in Arduino IDE and Nordic SDK files are definitely different.

Related