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

nrf9160 GPIOTE with Interrupt

Hi

i am using nrf9160 based custom board with zephyr, and trying to build GPIOTE with interrupt for a button, and led finally connect with PPI

i have been trying for Blinky using GPIOTE like this:

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>


#include <nrfx_gpiote.h>

#define LED_PIN NRF_GPIO_PIN_MAP(0,1)
#define BT_PIN NRF_GPIO_PIN_MAP(0,27)

/* The devicetree node identifier for the "led0" alias. */
#define LED1_NODE DT_ALIAS(led1)

#if DT_NODE_HAS_STATUS(LED1_NODE, okay)
#define LED1	DT_GPIO_LABEL(LED1_NODE, gpios)
#define PIN	DT_GPIO_PIN(LED1_NODE, gpios)
#define FLAGS	DT_GPIO_FLAGS(LED1_NODE, gpios)
#endif

void gpio_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
  nrfx_gpiote_out_toggle(LED_PIN);
  k_sleep(K_MSEC(100));
//nothing
  
}

void gpio_inits(void)
{

nrfx_err_t err;

	///* Connect GPIOTE_0 IRQ to nrfx_gpiote_irq_handler */
	//IRQ_CONNECT(DT_IRQN(DT_NODELABEL(gpiote)),
	//	    DT_IRQ(DT_NODELABEL(gpiote), priority),
	//	    nrfx_isr, nrfx_gpiote_irq_handler, 0);

        //nrfx_gpiote_irq_handler();

  if (!nrfx_gpiote_is_init()) {
    err = nrfx_gpiote_init(0);
  }
  nrfx_gpiote_out_config_t const out_config = {
        .init_state = NRF_GPIOTE_INITIAL_VALUE_HIGH,
        .action     = NRF_GPIOTE_POLARITY_TOGGLE,
        .task_pin   = true,
  };  
  err = nrfx_gpiote_out_init(PIN, &out_config);
  nrfx_gpiote_out_task_enable(PIN);

//------------------------------------------------------------------------------------------------
  nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
  in_config.pull = NRF_GPIO_PIN_PULLUP;

  
  err = nrfx_gpiote_in_init(BT_PIN, &in_config, gpio_handler);
  
  if (err != NRFX_SUCCESS)
          printk("DRDY_init error: %08x \n", err); 
nrfx_gpiote_in_event_enable(BT_PIN, true);


}

void main(void)
{
gpio_inits();
while (1)
{
  nrf_gpio_pin_toggle(PIN);
 // k_sleep(K_MSEC(1000));
}


}

prj.conf file:

CONFIG_GPIO=y
CONFIG_GPIO_NRFX=y
CONFIG_GPIO_NRF_P0=y
CONFIG_NRFX_GPIOTE=y

after uploading, the code is not working, but when i change the initial status of LED pin in config, its changing the initial status, but still output is not working in while loop.

Please help me to understand my mistake, 

please provide me a example project which is having GPIOTE with interrupt (if possible)

Best Regards

Rajender

Parents Reply Children
  • Hi kenneth,

    Thank you very much,

    The only change i done to your code is IRQ_CONNECT(GPIOTE1_IRQn, 7, nrfx_isr, nrfx_gpiote_irq_handler, 0); (of course i changed CONFIG_GPIO=n also)

    but i didnt understand one thing, when i use GPIOTE0_IRQn in above function the board is restarting, im using port 0 for the gpio but still why its happening like that?

    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    
    
    #include <nrfx_gpiote.h>
    
    #define LED_1 DT_GPIO_PIN(DT_ALIAS(led1), gpios)
    #define LED_2 DT_GPIO_PIN(DT_ALIAS(led2), gpios)
    #define BTN   DT_GPIO_PIN(DT_ALIAS(factoryrestore), gpios)
    
    
    void button_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
    nrf_gpio_pin_toggle(LED_1);
      
    }
    
    void gpio_inits(void)
    {
    
      nrfx_err_t err;
    
      IRQ_CONNECT(GPIOTE1_IRQn, 7, nrfx_isr, nrfx_gpiote_irq_handler, 0);
    
      if (!nrfx_gpiote_is_init()) {
        err = nrfx_gpiote_init(0);
      }
    	nrf_gpio_cfg_output(LED_1);
            nrf_gpio_cfg_output(LED_2);
       //------------------------------------------------------------------------------------------------
    	nrfx_gpiote_in_config_t const in_config = {
    		.sense = NRF_GPIOTE_POLARITY_HITOLO,
    		.pull = NRF_GPIO_PIN_PULLUP,
    		.is_watcher = false,
    		.hi_accuracy = true,
    		.skip_gpio_setup = false,
    	};
    
    	err = nrfx_gpiote_in_init(BTN, &in_config, button_handler);
    	if (err != NRFX_SUCCESS) {
    		printk("nrfx_gpiote_in_init error: %08x", err);
    		return;
    	}
    
    	nrfx_gpiote_in_event_enable(BTN, true);
    
    }
    
    void main(void)
    {
    gpio_inits();
    nrf_gpio_pin_set(LED_1);
    while (1)
    {
      nrf_gpio_pin_toggle(LED_2);
      k_sleep(K_MSEC(1000)); 
    }
    
    
    }

    Best Regards

    Rajender

  • Can you add some debugging by:

    CONFIG_ASSERT=y
    CONFIG_ASSERT_VERBOSE=y
    CONFIG_ASSERT_NO_COND_INFO=n
    CONFIG_ASSERT_NO_MSG_INFO=n
    CONFIG_RESET_ON_FATAL_ERROR=n
Related