<?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>GPIO interrupt custom callback data</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/91360/gpio-interrupt-custom-callback-data</link><description>Hi, 
 
 I just started looking around with the NRF52840 development kit and tried to implement simple GPIO interrupt based on the button sample . 
 
 I settled multiple GPIO pins configured in interrupt mode and wanted to use only one callback function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 29 Aug 2022 11:56:24 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/91360/gpio-interrupt-custom-callback-data" /><item><title>RE: GPIO interrupt custom callback data</title><link>https://devzone.nordicsemi.com/thread/383666?ContentTypeID=1</link><pubDate>Mon, 29 Aug 2022 11:56:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:122ff821-f4be-4ffb-abf8-d6f6349d143f</guid><dc:creator>&amp;#216;ivind</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Yes, for a common callback you will have to use the pin mask to identify the pin. How you solve this is up to you.&lt;/p&gt;
&lt;p&gt;A left shift: &lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;pins &amp;amp; (1 &amp;lt;&amp;lt; pin_num)&lt;/pre&gt;&amp;nbsp;&lt;br /&gt;or BIT():&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;pins &amp;amp; BIT(pin_num)&lt;/pre&gt;&amp;nbsp;&lt;br /&gt;are simple solutions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: GPIO interrupt custom callback data</title><link>https://devzone.nordicsemi.com/thread/383474?ContentTypeID=1</link><pubDate>Fri, 26 Aug 2022 12:36:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47bf59b8-5e9d-4435-8557-69225a3d855a</guid><dc:creator>AnomalySmith</dc:creator><description>&lt;p&gt;For now, I&amp;#39;ve done something like this as a workaround to identify which pin triggered the common callback :/&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/zephyr.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;
#include &amp;lt;math.h&amp;gt;


#define SW0_NODE DT_ALIAS(sw0)
#define SW1_NODE DT_ALIAS(sw1)

#if !DT_NODE_HAS_STATUS(SW0_NODE, okay)
#error &amp;quot;Unsupported board: sw0 devicetree alias is not defined&amp;quot;
#endif
#if !DT_NODE_HAS_STATUS(SW1_NODE, okay)
#error &amp;quot;Unsupported board: sw1 devicetree alias is not defined&amp;quot;
#endif

static const struct gpio_dt_spec swUp = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
static const struct gpio_dt_spec swDown = GPIO_DT_SPEC_GET_OR(SW1_NODE, gpios, {0});

static struct gpio_callback swUp_cb_data;
static struct gpio_callback swDown_cb_data;

/* Interrupt routine: must be kept whithout blocking calls */
void btn_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {

  if(pins == (uint32_t)pow(2, (double)swUp.pin)) { // It is SW0_NODE
    // Doing stuff
  } else if(pins == (uint32_t)pow(2, (double)swDown.pin)) { // It is SW1_NODE
    // Doing other stuff
  }
  // ...
}

int set_pin_int_cb(const struct gpio_dt_spec itr_pin, gpio_callback_handler_t cb, struct gpio_callback *itr_pin_cb_data) {
  // ...
  
  gpio_init_callback(itr_pin_cb_data, cb, BIT(itr_pin.pin));
  gpio_add_callback(itr_pin.port, itr_pin_cb_data);
}

void main(void) {
  
  set_pin_int_cb(swUp, &amp;amp;btn_pressed, &amp;amp;swUp_cb_data); // Initialising GPIO interrupt here as in basic/button sample
  set_pin_int_cb(swDown, &amp;amp;btn_pressed, &amp;amp;swDown_cb_data);
  
  while(1) { ... }

  return;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>