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
  • No errors?  Should I assume that the Nordic chip is faulty?

    Don

    DG Devices

  • How did you found that your interrupt handler is not called? Did you set a breakpoint?

    The hardware seems to be configured ok. What I can see - last_ecount and count are defined inside an interrupt handler and will be zeroed every time the handler is called, they're probably will be optimized out by the compiler as well. You need to define last_ecount as global variable outside your handler.

  • You are right about making last_ecount global, but I haven't gotten that far yet. I still need to interrupt.

    I verify the interrupt by putting 

    digitalWrite(LED, HIGH);  (Arduino method)

    Inside the ISR.  It never lights up when I connect pin2 to 3.3V or ground.  I also tried code to light the LED directly with pin2, which works, so I know my GPIO number is correct (2) and that the LED is functioning properly.

    Another interesting thing is that I can't get it to interrupt even if I explicitly set 

    NRF_GPIOTE->EVENTS_IN[0] = 1;

    in the loop code.

    So you can see my dilemma.  Is there some other enable for interrupts besides INTENSET or NVIC_EnableIRQ(GPIOTE_IRQn)?

    Don

  • Maybe you could put the whole project here? I don't see anything wrong in your configuration.

    Another interesting thing is that I can't get it to interrupt even if I explicitly set  NRF_GPIOTE->EVENTS_IN[0] = 1; in the loop code.

    This won't work, you can only clear EVENTS register by software, not set.

  • I cut the code down to the bare minimum and ran it.  This is literally the entire program now:

    #include <nrf.h>
    #include "nrf_timer.h"
    #include "nrf_gpio.h"
    
    volatile uint32_t last_ecount = 0;                //last count values
    const int epin = 2;                               //board pin D2, signal from E-wave
    int LED = LED_BUILTIN;                            //built-in orange LED for debug
    static void gpio_initialize(void);
    
    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){                     //interrupts
      uint32_t count = 0;
      int avcount = 0;
      
      digitalWrite(LED, HIGH);
       
      if(NRF_GPIOTE->EVENTS_IN[0] == 1){              //filter interrupt for E-wave
        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
      }
    }

    When I connect pin2 (epin = 2) to ground or to 3.3V I get nothing on the LED. 

    However, when I move the LED output driver from the ISR to the loop() and control it directly with epin like this:

    #include <nrf.h>
    #include "nrf_timer.h"
    #include "nrf_gpio.h"
    
    volatile uint32_t last_ecount = 0;                //last count values
    const int epin = 2;                               //board pin D2, signal from E-wave
    int LED = LED_BUILTIN;                            //built-in orange LED for debug
    static void gpio_initialize(void);
    
    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() {
      int pin2 = 0;
      pin2 = digitalRead(epin);
      if(pin2)
        digitalWrite(LED, HIGH);
        else
          digitalWrite(LED, LOW);
    }
       
    void GPIOTE_IRQHandler(void){                     //string interrupts
      uint32_t count = 0;
      int avcount = 0;
       
      if(NRF_GPIOTE->EVENTS_IN[0] == 1){              //filter interrupt for E-string
        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 vibration (CC0)
        last_ecount = count;                          //reset for next count
      }
    }

    I get a lit LED, which I can turn off by connecting pin2 to ground.  No other input pin does this but pin2.

    I don't understand.

    Don

Reply
  • I cut the code down to the bare minimum and ran it.  This is literally the entire program now:

    #include <nrf.h>
    #include "nrf_timer.h"
    #include "nrf_gpio.h"
    
    volatile uint32_t last_ecount = 0;                //last count values
    const int epin = 2;                               //board pin D2, signal from E-wave
    int LED = LED_BUILTIN;                            //built-in orange LED for debug
    static void gpio_initialize(void);
    
    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){                     //interrupts
      uint32_t count = 0;
      int avcount = 0;
      
      digitalWrite(LED, HIGH);
       
      if(NRF_GPIOTE->EVENTS_IN[0] == 1){              //filter interrupt for E-wave
        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
      }
    }

    When I connect pin2 (epin = 2) to ground or to 3.3V I get nothing on the LED. 

    However, when I move the LED output driver from the ISR to the loop() and control it directly with epin like this:

    #include <nrf.h>
    #include "nrf_timer.h"
    #include "nrf_gpio.h"
    
    volatile uint32_t last_ecount = 0;                //last count values
    const int epin = 2;                               //board pin D2, signal from E-wave
    int LED = LED_BUILTIN;                            //built-in orange LED for debug
    static void gpio_initialize(void);
    
    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() {
      int pin2 = 0;
      pin2 = digitalRead(epin);
      if(pin2)
        digitalWrite(LED, HIGH);
        else
          digitalWrite(LED, LOW);
    }
       
    void GPIOTE_IRQHandler(void){                     //string interrupts
      uint32_t count = 0;
      int avcount = 0;
       
      if(NRF_GPIOTE->EVENTS_IN[0] == 1){              //filter interrupt for E-string
        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 vibration (CC0)
        last_ecount = count;                          //reset for next count
      }
    }

    I get a lit LED, which I can turn off by connecting pin2 to ground.  No other input pin does this but pin2.

    I don't understand.

    Don

Children
Related