<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/84298/nrf51822-and-zephyr-gpio-interrupt-on-multiple-gpios</link><description>Hello, guys! 
 I am using nRF51822 SoC with Zephyr. There is a need to configure multiple GPIOs as inputs, with the possibility to fire an interrupt on rising/falling/both edges. The following piece of code is used to configure one single GPIO: Everything</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 07 Feb 2022 07:09:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/84298/nrf51822-and-zephyr-gpio-interrupt-on-multiple-gpios" /><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/351452?ContentTypeID=1</link><pubDate>Mon, 07 Feb 2022 07:09:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7e16d898-8b76-457c-98b4-95f42904f7ad</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;It is working. We can now move forward.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for your assistance, Hakon!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Bojan.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/351138?ContentTypeID=1</link><pubDate>Thu, 03 Feb 2022 14:04:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5f9d5334-4aba-4699-81b8-5591ab42db94</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Did you try the example that I tested to see if this also gives a similar error?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/351128?ContentTypeID=1</link><pubDate>Thu, 03 Feb 2022 13:48:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6d8243b8-6425-47f5-b2fc-0c61f06ef3e4</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;It did not help, Hakon.&lt;/p&gt;
&lt;p&gt;I now have 4 different &lt;span style="background-color:#00ffff;"&gt;callback&lt;/span&gt; variables but am still unable to properly configure more than two GPIOs.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/351125?ContentTypeID=1</link><pubDate>Thu, 03 Feb 2022 13:42:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45b86707-e725-46c8-8cd7-568a6646d8ea</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Bojan,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Try to give each pin-interrupt each own &amp;quot;callback&amp;quot; variable. It looks like you are overwriting &amp;quot;callback&amp;quot; with Touch2IrqCallback now.&lt;/p&gt;
&lt;p&gt;Same goes both interrupt pins on the da271gpio device, where accCallback is used on both pins.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/351001?ContentTypeID=1</link><pubDate>Thu, 03 Feb 2022 07:58:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ebf769d-9d40-4249-8586-ba828b492b1c</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;Hello, Hakon. &lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Here is how I tried to initialize GPIOs used to catch external interrupts:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/* Touch Pads */
#if DT_NODE_HAS_STATUS(DT_NODELABEL(touchpadsgpio), okay)
	tp1GpioDevice = device_get_binding(DT_LABEL(DT_PHANDLE(DT_NODELABEL(touchpadsgpio), tp1_gpio)));
	tp1Pin = DT_PROP(DT_NODELABEL(touchpadsgpio), tp1_pin);            
	gpio_pin_configure(tp1GpioDevice, tp1Pin, GPIO_INPUT | GPIO_PULL_UP);
	gpio_pin_interrupt_configure(tp1GpioDevice, tp1Pin, GPIO_INT_EDGE_BOTH);
	gpio_init_callback(&amp;amp;callback, Touch1IrqCallback, BIT(tp1Pin));    
	gpio_add_callback(tp1GpioDevice, &amp;amp;callback);

	tp2GpioDevice = device_get_binding(DT_LABEL(DT_PHANDLE(DT_NODELABEL(touchpadsgpio), tp2_gpio)));
	tp2Pin = DT_PROP(DT_NODELABEL(touchpadsgpio), tp2_pin);
	gpio_pin_configure(tp2GpioDevice, tp2Pin, GPIO_INPUT | GPIO_PULL_UP);
	gpio_pin_interrupt_configure(tp2GpioDevice, tp2Pin, GPIO_INT_EDGE_BOTH);
	gpio_init_callback(&amp;amp;callback, Touch2IrqCallback, BIT(tp2Pin));    
	gpio_add_callback(tp2GpioDevice, &amp;amp;callback);
#else 
#error &amp;quot;Can&amp;#39;t find TouchPads GPIO node!&amp;quot;
#endif 

/* Accelerometer */
#if DT_NODE_HAS_STATUS(DT_NODELABEL(da217gpio), okay)	
	int1GpioDevice = device_get_binding(DT_LABEL(DT_PHANDLE(DT_NODELABEL(da217gpio), int1_gpio)));            
	int1Pin = DT_PROP(DT_NODELABEL(da217gpio), int1_pin);
	gpio_pin_configure(int1GpioDevice, int1Pin, GPIO_INPUT | GPIO_PULL_DOWN);
	gpio_pin_interrupt_configure(int1GpioDevice, int1Pin, GPIO_INT_EDGE_RISING);	                        
	gpio_init_callback(&amp;amp;accCallback, AccInt1IrqCallback, BIT(int1Pin));    
	ret = gpio_add_callback(int1GpioDevice, &amp;amp;accCallback);

	int2GpioDevice = device_get_binding(DT_LABEL(DT_PHANDLE(DT_NODELABEL(da217gpio), int2_gpio)));
	int2Pin = DT_PROP(DT_NODELABEL(da217gpio), int2_pin);                      
	gpio_pin_configure(int2GpioDevice, int2Pin, GPIO_INPUT | GPIO_PULL_DOWN);
	gpio_pin_interrupt_configure(int2GpioDevice, int2Pin, GPIO_INT_EDGE_RISING);
	gpio_init_callback(&amp;amp;accCallback, AccInt2IrqCallback, BIT(int2Pin));    
	gpio_add_callback(int2GpioDevice, &amp;amp;accCallback);
#else 
#error &amp;quot;Can&amp;#39;t find DA217 GPIO node!&amp;quot;
#endif &lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#00ffff;"&gt;&lt;em&gt;touchpadgpio&lt;/em&gt;&lt;/span&gt; and &lt;span style="background-color:#00ffff;"&gt;&lt;em&gt;da217gpio&lt;/em&gt;&lt;/span&gt; are defined in .overlay file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    da217gpio: da217gpio {
        status = &amp;quot;okay&amp;quot;;
        compatible = &amp;quot;da217-gpio&amp;quot;;
        int1-gpio = &amp;lt;&amp;amp;gpio0&amp;gt;;
        int1-pin = &amp;lt;15&amp;gt;;
        int2-gpio = &amp;lt;&amp;amp;gpio0&amp;gt;;
        int2-pin = &amp;lt;16&amp;gt;;
    };        

    touchpadsgpio: touchpadsgpio {
        status = &amp;quot;okay&amp;quot;;
        compatible = &amp;quot;touchpads-gpio&amp;quot;;
        tp1-gpio = &amp;lt;&amp;amp;gpio0&amp;gt;;
        tp1-pin = &amp;lt;5&amp;gt;;
        tp2-gpio = &amp;lt;&amp;amp;gpio0&amp;gt;;
        tp2-pin = &amp;lt;2&amp;gt;;
    };&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Do you have any idea why with that approach I can properly initialize only 2 GPIOs and the 3rd one is returning&amp;nbsp;&lt;strong&gt;-19&lt;/strong&gt; at &lt;span style="background-color:rgba(0, 255, 255, 1);"&gt;&lt;em&gt;gpio_pin_interrupt_configure()&lt;/em&gt;&lt;/span&gt; point?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Bojan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/350937?ContentTypeID=1</link><pubDate>Wed, 02 Feb 2022 15:56:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d52d991d-3ab6-4be6-b230-120bcb19b97d</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;Thanks, Hakon. Let me dive deeper and figure out what I am missing.&lt;/p&gt;
&lt;p&gt;Thanks for the great support!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/350934?ContentTypeID=1</link><pubDate>Wed, 02 Feb 2022 15:51:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eabc3a62-96b0-443f-a99b-64a839c950da</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Bojan,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I just quickly added more inputs to the zephyr/samples/basic/button sample in ncs v1.8.0 (zephyr v2.7.0):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2016 Open-RnD Sp. z o.o.
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;
#include &amp;lt;sys/util.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;inttypes.h&amp;gt;

#define SLEEP_TIME_MS	1

/*
 * Get button configuration from the devicetree sw0 alias. This is mandatory.
 */
#define SW0_NODE	DT_ALIAS(sw0)
#if !DT_NODE_HAS_STATUS(SW0_NODE, okay)
#error &amp;quot;Unsupported board: sw0 devicetree alias is not defined&amp;quot;
#endif
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios,
							      {0});
static struct gpio_callback button0_cb_data;
static struct gpio_callback button1_cb_data;
static struct gpio_callback button2_cb_data;
static struct gpio_callback button3_cb_data;

#define PIN1 18
#define PIN2 19
#define PIN3 20

/*
 * The led0 devicetree alias is optional. If present, we&amp;#39;ll use it
 * to turn on the LED whenever the button is pressed.
 */
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios,
						     {0});

void button_pressed(const struct device *dev, struct gpio_callback *cb,
		    uint32_t pins)
{
	uint32_t bit_to_pin = 0;
	for (int i = 0; i &amp;lt; 32; i++) {
		if ((pins &amp;gt;&amp;gt; i) == 1) {
			bit_to_pin = i;
			break;
		}
	}

	printk(&amp;quot;Button %d pressed at %&amp;quot; PRIu32 &amp;quot;\n&amp;quot;, bit_to_pin, k_cycle_get_32());
	static bool val;
	val = !val;
	gpio_pin_set_dt(&amp;amp;led, val);	
}

void main(void)
{
	int ret;

	if (!device_is_ready(button.port)) {
		printk(&amp;quot;Error: button device %s is not ready\n&amp;quot;,
		       button.port-&amp;gt;name);
		return;
	}

	ret = gpio_pin_configure_dt(&amp;amp;button, GPIO_INPUT);
	if (ret != 0) {
		printk(&amp;quot;Error %d: failed to configure %s pin %d\n&amp;quot;,
		       ret, button.port-&amp;gt;name, button.pin);
		return;
	}

	ret = gpio_pin_interrupt_configure_dt(&amp;amp;button,
					      GPIO_INT_EDGE_TO_ACTIVE);
	if (ret != 0) {
		printk(&amp;quot;Error %d: failed to configure interrupt on %s pin %d\n&amp;quot;,
			ret, button.port-&amp;gt;name, button.pin);
		return;
	}

	ret = gpio_pin_configure(button.port, PIN1, GPIO_INPUT | GPIO_PULL_UP);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	ret = gpio_pin_configure(button.port, PIN2, GPIO_INPUT | GPIO_PULL_UP);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	ret = gpio_pin_configure(button.port, PIN3, GPIO_INPUT | GPIO_PULL_UP);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	ret = gpio_pin_interrupt_configure(button.port, PIN1, GPIO_INT_EDGE_BOTH);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	ret = gpio_pin_interrupt_configure(button.port, PIN2, GPIO_INT_EDGE_BOTH);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	ret = gpio_pin_interrupt_configure(button.port, PIN3, GPIO_INT_EDGE_BOTH);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	gpio_init_callback(&amp;amp;button1_cb_data, button_pressed, BIT(PIN1));
	ret = gpio_add_callback(button.port, &amp;amp;button1_cb_data);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	gpio_init_callback(&amp;amp;button2_cb_data, button_pressed, BIT(PIN2));	
	ret = gpio_add_callback(button.port, &amp;amp;button2_cb_data);
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	gpio_init_callback(&amp;amp;button3_cb_data, button_pressed, BIT(PIN3));	
	ret = gpio_add_callback(button.port, &amp;amp;button3_cb_data);		
	printk(&amp;quot;ret %d\n&amp;quot;, ret);
	

	gpio_init_callback(&amp;amp;button0_cb_data, button_pressed, BIT(button.pin));
	gpio_add_callback(button.port, &amp;amp;button0_cb_data);
	printk(&amp;quot;Set up button at %s pin %d\n&amp;quot;, button.port-&amp;gt;name, button.pin);

	if (led.port &amp;amp;&amp;amp; !device_is_ready(led.port)) {
		printk(&amp;quot;Error %d: LED device %s is not ready; ignoring it\n&amp;quot;,
		       ret, led.port-&amp;gt;name);
		led.port = NULL;
	}
	if (led.port) {
		ret = gpio_pin_configure_dt(&amp;amp;led, GPIO_OUTPUT);
		if (ret != 0) {
			printk(&amp;quot;Error %d: failed to configure LED device %s pin %d\n&amp;quot;,
			       ret, led.port-&amp;gt;name, led.pin);
			led.port = NULL;
		} else {
			printk(&amp;quot;Set up LED at %s pin %d\n&amp;quot;, led.port-&amp;gt;name, led.pin);
		}
	}

	printk(&amp;quot;Press the button\n&amp;quot;);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And it works as expected on my end (on my nrf51dk):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;*** Booting Zephyr OS build v2.7.0-ncs1  ***
...
Set up button at GPIO_0 pin 17
Set up LED at GPIO_0 pin 21
Press the button
Button 17 pressed at 234164
Button 18 pressed at 253241
Button 18 pressed at 256734
Button 18 pressed at 256835
Button 19 pressed at 272220
Button 19 pressed at 276119
Button 20 pressed at 288819
Button 20 pressed at 292737
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/350928?ContentTypeID=1</link><pubDate>Wed, 02 Feb 2022 15:27:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10f7a8bb-673c-4616-86b0-59f0890c7251</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;Hello, Hakon. &lt;/p&gt;
&lt;p&gt;Thanks for the clarifications. I don&amp;#39;t use GPIOTE channels in other parts of the code.&lt;/p&gt;
&lt;p&gt;Perhaps the error occurs because I have 2xGPIOs sensitive to BOTH edges?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Bojan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 and Zephyr: GPIO interrupt on multiple GPIOs</title><link>https://devzone.nordicsemi.com/thread/350925?ContentTypeID=1</link><pubDate>Wed, 02 Feb 2022 15:09:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bc6bbc98-1411-4cb6-8603-a78b4a58a3cf</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I highly suspect that it&amp;#39;s this call that returns ENODEV:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.7.0-ncs1/drivers/gpio/gpio_nrfx.c#L60"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v2.7.0-ncs1/drivers/gpio/gpio_nrfx.c#L60&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The nRF51 devices has 4 GPIOTE channels available, are you using any of them in other parts of your application?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>