<?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>How to generate a GPIOTE interrupt on the press of a button?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89305/how-to-generate-a-gpiote-interrupt-on-the-press-of-a-button</link><description>Hi, I am currently working on nRF52840-DK board and trying to implement simple programs. I want to toggle a LED(P0.13) on the press of a button(P0.11) but I dont want to use the SDK example where driver functions such as nrf_gpio_cfg_input are used but</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 27 Jun 2022 21:16:49 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89305/how-to-generate-a-gpiote-interrupt-on-the-press-of-a-button" /><item><title>RE: How to generate a GPIOTE interrupt on the press of a button?</title><link>https://devzone.nordicsemi.com/thread/374416?ContentTypeID=1</link><pubDate>Mon, 27 Jun 2022 21:16:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:211437a1-43f2-4f8d-8141-b42114b40848</guid><dc:creator>shakingwindow</dc:creator><description>&lt;p&gt;Thanks a lot. The issue is resolved.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to generate a GPIOTE interrupt on the press of a button?</title><link>https://devzone.nordicsemi.com/thread/374282?ContentTypeID=1</link><pubDate>Mon, 27 Jun 2022 08:10:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9c102b97-1539-4f9f-99fd-c8db35879f54</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;If the LED dims/blinks, it sounds like the input to the pin is floating, causing the task to be triggered seemingly at random.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the test program that I use, which is essentially your original one with setting a pull-up on the button:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;quot;nrf52840.h&amp;quot;
#include &amp;quot;nrf52840_bitfields.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;

#define LED 13
#define BUTTON 11

void GPIOTE_IRQHandler(void) {
  if (NRF_GPIOTE-&amp;gt;EVENTS_IN[0] != 0) {
    NRF_GPIOTE-&amp;gt;EVENTS_IN[0] = 0;

    // toggle LED
    NRF_GPIOTE-&amp;gt;TASKS_OUT[1] = 1;
  }
}

int blinky() {
  /* Start LFCLK in low power mode driven by LFXO */
  NRF_CLOCK-&amp;gt;LFRCMODE = CLOCK_LFRCMODE_MODE_ULP;
  NRF_CLOCK-&amp;gt;LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal;
  NRF_CLOCK-&amp;gt;TASKS_LFCLKSTART = 1;

  // config button as an event
  NRF_GPIOTE-&amp;gt;CONFIG[0] = (GPIOTE_CONFIG_MODE_Event &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos) | (BUTTON &amp;lt;&amp;lt; 8) |
                          (GPIOTE_CONFIG_POLARITY_HiToLo &amp;lt;&amp;lt; 16);
  // config led as a task
  NRF_GPIOTE-&amp;gt;CONFIG[1] = (GPIOTE_CONFIG_MODE_Task &amp;lt;&amp;lt; 0) | (LED &amp;lt;&amp;lt; 8) |
                          (GPIOTE_CONFIG_POLARITY_Toggle &amp;lt;&amp;lt; 16) |
                          (GPIOTE_CONFIG_OUTINIT_High &amp;lt;&amp;lt; 20);

  // enable interrupt
  NRF_GPIOTE-&amp;gt;INTENSET = GPIOTE_INTENSET_IN0_Set;

  NVIC_EnableIRQ(GPIOTE_IRQn);

  return 0;
}

int main(void) {

  blinky();
  nrf_gpio_cfg_input(BUTTON, NRF_GPIO_PIN_PULLUP);
 
  while (1) {
    nrf_delay_ms(1000);
  }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that I set the task in the ISR each time, as the task is &amp;quot;toggle&amp;quot; - ie. no need to toggle the task register as well.&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: How to generate a GPIOTE interrupt on the press of a button?</title><link>https://devzone.nordicsemi.com/thread/374218?ContentTypeID=1</link><pubDate>Fri, 24 Jun 2022 21:01:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e3893fa-fb65-450f-8c5f-21aa8d5a591f</guid><dc:creator>shakingwindow</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks for the reply.I made a few changes to the above program. I am able to generate interrupt on pressing button.As you can see in the below program, I have used the gpio function&amp;nbsp;nrf_gpio_cfg_input along with GPIOTE config register. Is it considered OK?&lt;/p&gt;
&lt;p&gt;Another thing is, I have configured LED to toggle, but the LED does not go completely off but just dims a little bit and then switches on, when i press the button again. What am i doing wrong here?&lt;/p&gt;
&lt;p&gt;.&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;quot;nrf52840.h&amp;quot;
#include &amp;quot;nrf52840_bitfields.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;printf.h&amp;quot;
#include &amp;quot;uart.h&amp;quot;

#include &amp;lt;nrf.h&amp;gt;
#define PIN_LED 13
#define PIN_BUTTON 11

int main(void)
{
  // Start LFCLK (32kHz) crystal oscillator. If you don&amp;#39;t have crystal on your board, choose RCOSC instead.
  NRF_CLOCK-&amp;gt;LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal &amp;lt;&amp;lt; CLOCK_LFCLKSRC_SRC_Pos;
  NRF_CLOCK-&amp;gt;TASKS_LFCLKSTART = 1;
  while (NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED == 0);
  NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED = 0;

  // config button as an event
    NRF_GPIOTE-&amp;gt;CONFIG[0] = (GPIOTE_CONFIG_MODE_Event &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos) | (PIN_BUTTON &amp;lt;&amp;lt; GPIOTE_CONFIG_PSEL_Pos) |
				(GPIOTE_CONFIG_POLARITY_HiToLo &amp;lt;&amp;lt; GPIOTE_CONFIG_POLARITY_Pos) ;
   
   // pull up for the button
   nrf_gpio_cfg_input(PIN_BUTTON,NRF_GPIO_PIN_PULLUP);

    // config led as a task
	NRF_GPIOTE-&amp;gt;CONFIG[1] = (GPIOTE_CONFIG_MODE_Task &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos) | (PIN_LED &amp;lt;&amp;lt; GPIOTE_CONFIG_PSEL_Pos) |
				(GPIOTE_CONFIG_POLARITY_Toggle &amp;lt;&amp;lt; GPIOTE_CONFIG_POLARITY_Pos) | (GPIOTE_CONFIG_OUTINIT_Low &amp;lt;&amp;lt; GPIOTE_CONFIG_OUTINIT_Pos);
	
	// enable interrupt 
	NRF_GPIOTE-&amp;gt;INTENSET = GPIOTE_INTENSET_IN0_Msk;
    
    NVIC_EnableIRQ(GPIOTE_IRQn);


	while (1)
  {
    __WFE();

  }
}

void GPIOTE_IRQHandler(void)
{
    volatile uint32_t dummy;
    if(NRF_GPIOTE-&amp;gt;EVENTS_IN[0] != 0)
    {
        NRF_GPIOTE-&amp;gt;EVENTS_IN[0] = 0;
       
	   NRF_GPIOTE-&amp;gt;TASKS_OUT[1] = !(NRF_GPIOTE-&amp;gt;TASKS_OUT[1]);
    // Read back event register so ensure we have cleared it before exiting IRQ handler.
       dummy = NRF_GPIOTE-&amp;gt;EVENTS_IN[0];
       dummy;
    }

}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to generate a GPIOTE interrupt on the press of a button?</title><link>https://devzone.nordicsemi.com/thread/374128?ContentTypeID=1</link><pubDate>Fri, 24 Jun 2022 11:08:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:446bbe5e-8a3f-4120-afae-c48b15d7cfda</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;A generic comment: I&amp;#39;d recommend that you use macros for shifting, like &amp;quot;GPIOTE_CONFIG_MODE_Pos&amp;quot; etc, and possibly the hal helper headers (nrf_gpio.h) so you do not need to think about which position you need to shift to etc.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;You&amp;#39;ll need to set a pull on your input pin, in the NRF_GPIO-&amp;gt;PIN_CNF[BUTTON] register:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/gpio.html?cp=4_0_0_5_8_1_9#register.PIN_CNF-0-31"&gt;https://infocenter.nordicsemi.com/topic/ps_nrf52840/gpio.html?cp=4_0_0_5_8_1_9#register.PIN_CNF-0-31&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you try this and report back?&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>