Interrupt handling example for nRF52833 with nRF Connect SDK 2.3.0

Dear Support Team,

I just started evaluating a nRF52833 DK board with the nRF Connect SDK 2.3.0. At this point I would like to try interrupt handling from a GPIO pin but unfortunately I can't find any working example that I could use.

Ultimately I would like achive that the nRF52833 wakes up from the lowest energy mode via pin interrupt. How could I achive this?

Thank you very much in advance and all the best,

Viktor

Parents
  • Hello again Viktor and thanks for your patience,

    I just started evaluating a nRF52833 DK board with the nRF Connect SDK 2.3.0

    Great! Then in case this is your first glimpse into nRF Connect SDK and nRF's, welcome! 

    I would recommend you to start off by looking into our DevAcademy. I see that Lesson 2 on NCS fundamentals actually happens explain the subject of pin interrupts, I think you'll find your questions answered there.

    Regards,

    Elfving

Reply
  • Hello again Viktor and thanks for your patience,

    I just started evaluating a nRF52833 DK board with the nRF Connect SDK 2.3.0

    Great! Then in case this is your first glimpse into nRF Connect SDK and nRF's, welcome! 

    I would recommend you to start off by looking into our DevAcademy. I see that Lesson 2 on NCS fundamentals actually happens explain the subject of pin interrupts, I think you'll find your questions answered there.

    Regards,

    Elfving

Children
  • Hi Elfving,

    Thank you very much for the answer! I managed to make the interrupt handling work.

    This is the code for configuring an interrupt on pin4 on both rising and falling edge:

    Device tree:

    custom_gpios {
    	compatible = "gpio-keys";
    	acc_irq_pin1: acc_irq_pin_1 {
    	   gpios = <&gpio0 4 (GPIO_ACTIVE_HIGH)>;
    	   label = "Acc irq pin";
    	};
    
     };

    Main.c

    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    
    
    #define IRQ_ACC_NODE	DT_ALIAS(accirq0)
    static const struct gpio_dt_spec irq_acc_pin = GPIO_DT_SPEC_GET_OR(IRQ_ACC_NODE, gpios, {0});
    static struct gpio_callback irq_acc_callback;
    
    void IRQ_ACC_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
    {
    	if(gpio_pin_get_dt(&irq_acc_pin) == 0)
    	{
    		printk("Falling edge interrupt\n");
    	}
    	else
    	{
    		printk("Rising edge interrupt\n");
    	}
    }
    
    
    void main(void)
    {
    	if (!device_is_ready(irq_acc_pin.port)) 
    	{
    		printk("Error: gpio device %s is not ready\n", irq_acc_pin.port->name);
    		return;
    	}
    
    	if (gpio_pin_configure_dt(&irq_acc_pin, GPIO_INPUT) != 0) 
    	{
    		printk("Error: failed to configure %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	if (gpio_pin_interrupt_configure_dt(&irq_acc_pin, GPIO_INT_EDGE_BOTH) != 0) 
    	{
    		printk("Error: failed to configure interrupt on %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	gpio_init_callback(&irq_acc_callback, IRQ_ACC_handler, BIT(irq_acc_pin.pin));
    	gpio_add_callback(irq_acc_pin.port, &irq_acc_callback);
    
    }

  • Hi Elfving,

    Just one more question related to this topic:

    How to configure a pin (or the sleep mode itself) to wake up from System off mode via pin interrupt?

    All the best,

    Viktor

  • Hi Elfving,

    Thank you very much, it seems to work but not exactly how I was anticipating it.

    Sensing is configure like this:

    nrf_gpio_cfg_sense_input(irq_acc_pin.pin, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);

    But the MCU wakes up also on high-to-low transition of the pin. So, if the pint was high while it entered OFF mode, it wakes up when the pin returns to low.

    I would like to achieve a state where the MCU only returns to ON mode when low-to-high transition is happening.

  • Hello again Viktor and thank you for your patience,

    Unfortunately its only level triggered in system off, not edge triggered. You would have to make sure that the pin is low before going into system off.

    I would also recommend you to take a look at our system off sample to see this in use, maybe particularly these lines.

    Regards,

    Elfving

Related