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

nrf_drv_gpiote_in_event_enable

Hello,

I'm using multiple GPIO pins as inputs. Should I use  nrf_drv_gpiote_in_event_enable() for each pin? Also how to configure the input_event_handler() to handle the events from different inputs?

Regards,

Sunil

  • Hi

    Here is a modified version of the Pin Change Interrupt Example in SDK 15. It shows how to use two input pins to control two output pins. 

    /** @file
     * @defgroup pin_change_int_example_main main.c
     * @{
     * @ingroup pin_change_int_example
     * @brief Pin Change Interrupt Example Application main file.
     *
     * This file contains the source code for a sample application using interrupts triggered by GPIO pins.
     *
     */
    
    #include <stdbool.h>
    #include "nrf.h"
    #include "nrf_drv_gpiote.h"
    #include "app_error.h"
    #include "boards.h"
    
    #define PIN_IN1	    BSP_BUTTON_0
    #define PIN_IN2	    BSP_BUTTON_1
    
    
    #define PIN_OUT1	    BSP_LED_0
    #define PIN_OUT2    BSP_LED_1
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        if(pin == PIN_IN1)
    	nrf_drv_gpiote_out_toggle(PIN_OUT1);
        else if(pin == PIN_IN2)
    	nrf_drv_gpiote_out_toggle(PIN_OUT2);
    }
    /**
     * @brief Function for configuring: PIN_IN1 pin for input, PIN_OUT1 pin for output,
     * and configures GPIOTE to give an interrupt on pin change.
     */
    static void gpio_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    
        err_code = nrf_drv_gpiote_out_init(PIN_OUT1, &out_config);
        APP_ERROR_CHECK(err_code);
    
    
        err_code = nrf_drv_gpiote_out_init(PIN_OUT2, &out_config);
        APP_ERROR_CHECK(err_code);
        
        
    
        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_IN1, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
        
        
        err_code = nrf_drv_gpiote_in_init(PIN_IN2, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(PIN_IN1, true);
        nrf_drv_gpiote_in_event_enable(PIN_IN2, true);
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        gpio_init();
    
        while (true)
        {
            // Do nothing.
        }
    }
    
    
    /** @} */
    

  • Hi Martin,

    Thanks for answering my question.

    Regards,

    Sunil

Related