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

detect toggle button with PORT event

Hi,

I am trying to use the PORT event to trigger interrupts when a button is toggled, i.e. both on LoToHi and HiToLo.

To my understanding PORT events are generated by the DETECT signal which is sent if a pin has sense enabled. It seems sense can only be set to disabled, high or low, there is no toggle option. Is it possible to use the PORT event to generate interrupts on button toggle? If so, how?

Below is my test code:

// Test application that toggles a LED on button click    
extern "C" void GPIOTE_IRQHandler(void) {
        // Any pin interrupt will trigger the PORT event
        if ((NRF_GPIOTE->EVENTS_PORT != 0) && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_PORT_Msk)) {
          // Reset PORT event
          NRF_GPIOTE->EVENTS_PORT = 0;
          // Toggle LED
          pinToggle(LED_RED);
        }
    }

int main(void) {
  // LED Init
  setupLEDPins();

  // Set up interrupt
  pinInput(BUTTON_1, Pull::Up);
  // Here I want to find a way to generate the detect signal on toggle
  setPinSense(BUTTON_1, Sense::LOW); 

  // Enable interrupt
  NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk;
  NVIC_ClearPendingIRQ(GPIOTE_IRQn);
  NVIC_SetPriority(GPIOTE_IRQn, 3);
  NVIC_EnableIRQ(GPIOTE_IRQn);

  while (true) {
    pinToggle(LED_GREEN);
    delayMs(100);
  }
}

Grateful for some help, thanks!

Parents
  • Hi gammarui,

    The input sensing for GPIO pins can only be configured to sense a change from low to high or high to low. There is not a way to track the previous state and detect if the pin change from a previous state to the other alternate state. You could instead implement this yourself by changing the configured sense each time you set an interrupt.

    For example, to check if an active low button was pressed and then released:

    1. Configure sense low for input pin
    2. In interrupt, reconfigure input pin for sense high
    3. After high is received, reconfigure for sense low to capture next button press

    Note that you'll want some debouncing for the above example (either SW or HW debouncing).

    Also note that you can configure the GPIOTE to perform an output toggle but since you are looking for an input toggle, this doesn't help much.

    Cheers,

    Eric

  • That worked! Feels like a bit of a hack but as long as it works :)

    Thanks a lot for your help!

Reply Children
No Data
Related