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

How to set two interrupts for one pin?

On nRF52 interrupts for GPIOTE, I need to perform one event when a button is pushed, and perform another different event when the button is released.

I've checked these links already, buy they are quit old.

devzone.nordicsemi.com/.../

devzone.nordicsemi.com/.../

devzone.nordicsemi.com/.../

The question is, do I need to set up two events or toggle? Any other idea?

    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);	

I wonder if I need separate handlers for two functionality?

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){

		nrf_drv_gpiote_out_toggle(PIN_OUT);

		// IF BUTTON IS PUSHED
		if (action == NRF_GPIOTE_POLARITY_HITOLO){
			// DO SOMETHING
		}

		// IF BUTTON IS RELEASED 
		if (action == NRF_GPIOTE_POLARITY_LOTOHI){
			// DO SOMETHING	
		}	





		

		
  • Hi,

    The driver does not allow you to configure one pin for multiple actions, this will only result in an error.

    If you set PIN_IN to sense TOGGLE, you will get one event when the button is pressed and another event when the button is released. You can use a variable to keep track of the state:

    uint8_t button_pressed = 0;
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        if(button_pressed == 0)
        {
            NRF_LOG_INFO("Button pressed!\r\n");
            button_pressed = 1;
        }
        else
        {
            NRF_LOG_INFO("Button released!\r\n");
            button_pressed = 0;
        }
    }
    

    Best regards,

    Jørgen

Related