Author Topic: Trigger the interrupt whenever the pin changes value  (Read 3745 times)

Feynman

  • RFduino Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Trigger the interrupt whenever the pin changes value
« on: August 05, 2014, 12:58:24 PM »
RFduino_pinWakeCallback only takes HIGH and LOW as parameters. Is there a way to trigger the interrupt whenever the pin changes value (like the Arduinos attachInterrupt(0, foo, CHANGE))?

mkay

  • RFduino Team
  • *****
  • Posts: 405
  • Karma: +15/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #1 on: September 08, 2014, 03:54:39 PM »
Hi Feynman, you can try this:

Code: [Select]
void setup() {
  // put your setup code here, to run once:
pinMode(5, INPUT); // using GPIO 5 as input
Serial.begin(9600);
NVIC_EnableIRQ(GPIOTE_IRQn);
NRF_GPIOTE->CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
              | (5 << GPIOTE_CONFIG_PSEL_Pos) // using GPIO 5 as input
              | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;
}


void GPIOTE_IRQHandler(void)
{
  // Event causing the interrupt must be cleared
  if ((NRF_GPIOTE->EVENTS_IN[0] == 1) && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
  {
    NRF_GPIOTE->EVENTS_IN[0] = 0;
  }
  //DO SOMETHING HERE WHEN PIN CHANGES STATES
}


mauricesvay

  • RFduino Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #2 on: October 14, 2014, 02:46:55 PM »
I'm interested to know if it works. I need to use a rotary encoder, so I need to attach a callback to an interrupt when the value changes (HIGH or LOW).

mtnscott

  • RFduino Jr. Member
  • **
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #3 on: March 10, 2015, 07:48:52 AM »
mkay - this code sample hangs my app.  Maybe I am using it incorrectly.

If I wanted to change the pin to GPIO2 do I just change "5" to "2"?

Will this interrupt occur on each state change, I only need to set it up once with your setup code?

Do I just need to clear it after it occurs with this segment?

  // Event causing the interrupt must be cleared
  if ((NRF_GPIOTE->EVENTS_IN[0] == 1) && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
  {
    NRF_GPIOTE->EVENTS_IN[0] = 0;
  }

It appears my code hangs on the resetting of the flags after the interrupt occurs.

UPDATE:  How do I enable / disable this interrupt?

Can I just use the following to setup the interrupt handler -

NRF_GPIOTE->CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
              | (2 << GPIOTE_CONFIG_PSEL_Pos) // using GPIO 2 as input
              | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;

and then enable the IRQ when I am ready to accept interrupts from this pin with -

NVIC_EnableIRQ(GPIOTE_IRQn);

Then to disable so I don't get any more interrupts will this work -

NVIC_DisableIRQ(GPIOTE_IRQn);
« Last Edit: March 10, 2015, 08:16:39 AM by mtnscott »

gaborpaller

  • RFduino Newbie
  • *
  • Posts: 3
  • Karma: +1/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #4 on: December 01, 2015, 05:54:18 AM »
The following code worked for me (closely based on mkay's code):

Setup: (recvpin is the GPIO number):

Code: [Select]
  pinMode(recvpin, INPUT);
  NRF_GPIOTE->CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
              | (recvpin << GPIOTE_CONFIG_PSEL_Pos)
              | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
  NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;
  attachInterrupt(GPIOTE_IRQn, GPIOTE_Interrupt);   

Interrupt handler:

Code: [Select]

void GPIOTE_Interrupt(void){
  if( (NRF_GPIOTE->EVENTS_IN[0] == 1) && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
NRF_GPIOTE->EVENTS_IN[0] = 0;
... actual processing ...
}

Deactivation:

Code: [Select]
  detachInterrupt(GPIOTE_IRQn);
« Last Edit: December 02, 2015, 07:31:53 AM by tolson »

osenjw

  • RFduino Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #5 on: February 07, 2016, 07:38:11 PM »
This code worked for me as well.  But I would like to issue a warning that may cause a program to hang similar to one alluded to...

If you are working from the VisualMicro (VM) plugin for VisualStudio, setting a break point inside of an interrupts looks to be a bad thing.  It hangs the program.

Usually VM will break, but will not put the cursor at the break point.  You need to look at the output window to see that a break point has been hit.  But for a breakpoint in an interrupt, it hangs.

squatsandscience

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Trigger the interrupt whenever the pin changes value
« Reply #6 on: February 16, 2016, 11:41:28 AM »
Have you tried the Nordic QDEC functions? The nRF51 has a built in quad decoder.

 

anything