Using the same GPIO for interrupt input and for executing functions (setting it as output after entering the interrupt)

Hello. I am trying to set up some code for an interrupt on the NRF52840 DK. I am using the pin_change_int example code as a starting point. In the example code, one GPIO is used as interrupt input, and another GPIO is set as output and is toggled in the interrupt handler function. 

I want to use the same GPIO as input and output. That is, I first want to configure the GPIO to detect an event (i.e set as input), and then after entering the interrupt routine I was to set the same GPIO as output in order to perform actions. Is this possible? How do I do this? I am very new to NRF programming. 

Parents
  • Hi, 

    Yes, you can reconfigure the pin, and switch between being a input and a output pin at run-time. Use the GPIO API for that.

  • Ok, thank you for the reply.

    The following code is from the example. Are you saying I need to do a similar setup as in gpio_init(void) inside of  the interrupt? Maybe using the line "err_code = nrf_drv_gpiote_out_init(PIN_IN, &out_config);"?

    I need a little bit more guidance if it's possible. Thank you!

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        
       nrf_drv_gpiote_out_toggle(PIN_IN);
    
    }
    /**
     * @brief Function for configuring: PIN_IN pin for input, PIN_OUT 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_OUT, &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_IN, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(PIN_IN, true);
    }
    

  • Yes,

    Something like this:

    #define MY_PIN 3
    
    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        nrf_drv_gpiote_in_uninit(MY_PIN);
    
        ret_code_t err_code;
        err_code = nrf_drv_gpiote_out_init(MY_PIN, &out_config);
        APP_ERROR_CHECK(err_code);
    
    }
    /**
     * @brief Function for configuring: PIN_IN pin for input, PIN_OUT 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_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(MY_PIN, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(MY_PIN, true);
    }

Reply
  • Yes,

    Something like this:

    #define MY_PIN 3
    
    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        nrf_drv_gpiote_in_uninit(MY_PIN);
    
        ret_code_t err_code;
        err_code = nrf_drv_gpiote_out_init(MY_PIN, &out_config);
        APP_ERROR_CHECK(err_code);
    
    }
    /**
     * @brief Function for configuring: PIN_IN pin for input, PIN_OUT 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_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(MY_PIN, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(MY_PIN, true);
    }

Children
Related