<?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>NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76783/nrfx-rtc-driver---how-to-stop-compare-interrupt-handler-disabling-interrupts</link><description>I&amp;#39;m using the nrfx_rtc drivers on RTC2 to generate a regular interrupt once a second, using compare interrupts. 
 My problem is that the nrfx_rtc interrupt handler disables compare events and interrupts, so the interrupt only happens once. I want it to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 07 Oct 2022 07:20:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76783/nrfx-rtc-driver---how-to-stop-compare-interrupt-handler-disabling-interrupts" /><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/389754?ContentTypeID=1</link><pubDate>Fri, 07 Oct 2022 07:20:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88d61c26-dbab-4168-99dc-f3751ed0912a</guid><dc:creator>benmack</dc:creator><description>&lt;p&gt;Thanks, much appreciated&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/389716?ContentTypeID=1</link><pubDate>Thu, 06 Oct 2022 20:21:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86c9b460-61b1-408c-ba7f-b64a4421a300</guid><dc:creator>Toastus</dc:creator><description>&lt;p&gt;I know this thread is old, but currently nrfx interrupts are working very well if you use the following procedure. It is not necessary to manually reenable. I have tried it across multiple NRF52 and NRF53 hardware with consistent results, including Nordic and 3rd-party boards. Under nrf connect v2.0.0, interrupts do not get disabled. Perhaps something strange is going on with your handler. Here is everything...it works, it&amp;#39;s tested, and it&amp;#39;s quite up to date.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;void timer_event_handler(nrf_timer_event_t event_type, void *p_context)&lt;/div&gt;
{
&lt;div&gt;&amp;nbsp;&amp;nbsp; switch (event_type) {&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case NRF_TIMER_EVENT_COMPARE0:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;/*&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* Be aware that this event handler callback function, not just the ISR, runs in an
&lt;div&gt;&amp;nbsp;* interrupt context, so some architectures may behave differently due to varying&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* implementations. Found this out when an led function on thingy52 wouldn&amp;#39;t work&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* because a series of improvements had turned a gpio toggle into i2c operations&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* on the thingy52&amp;#39;s sx_1509b, which would block, so I had to defer those operations&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* to mainline processing.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;*/&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Do stuff, turn on light, etc. */&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Uncomment next line if you implemented ONE-SHOT below, but want to resume timer anyway&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // nrfx_timer_resume(&amp;amp;timer);&lt;br /&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default:&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;nrfx_err_t err;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;err = nrfx_timer_init(&amp;amp;timer, &amp;amp;config, &amp;amp;timer_event_handler);&lt;/div&gt;
&lt;div&gt;if (err != NRFX_SUCCESS) {&lt;/div&gt;
&lt;div&gt;printk(&amp;quot;nrfx_timer_init failed! err = %d\n&amp;quot;, err);&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
/*
&lt;div&gt;&amp;nbsp;* The following macro is *NECESSARY* to get an operational timer.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* If you do not enable this ISR, the timer will not appear to be working.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* The IRQ_PRIO should match roughly what you set up in the config&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* struct of the init call above (default lowest is a good starting point).&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* Note that the irq name matches exactly the function defined in nrfx_timer.c&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* It seems like that part could have been canned in another macro&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* (maybe it is and I just overlooked it). Unless I set interrupts to true, my&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* function never got triggered, and unless I ran this connect, the default&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* callback never got connected--the system would just crash as soon as&lt;/div&gt;
&lt;div&gt;&amp;nbsp;* my trigger value hit.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;*/&lt;/div&gt;
&lt;div&gt;IRQ_CONNECT(TIMER0_IRQn, IRQ_PRIO_LOWEST,&lt;/div&gt;
&lt;div&gt;nrfx_timer_0_irq_handler, NULL, 0);&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;/* STOP (with clear) in the MASK below gives a ONE-SHOT timer */&lt;/div&gt;
&lt;div&gt;/* CLEAR (alone) in the MASK gives a REPEATING timer */&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;nrfx_timer_extended_compare(&amp;amp;timer,&lt;/div&gt;
&lt;div&gt;NRF_TIMER_CC_CHANNEL0,&lt;/div&gt;
&lt;div&gt;3500U, // Time in ticks, use one of the functions if you wish to pass microseconds or milliseconds&lt;/div&gt;
&lt;div&gt;// NRF_TIMER_SHORT_COMPARE0_STOP_MASK |&amp;nbsp; // Uncommenting this makes a ONE-SHOT timer&lt;/div&gt;
&lt;div&gt;NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,&lt;/div&gt;
&lt;div&gt;true);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
nrfx_timer_clear(&amp;amp;timer);
&lt;div&gt;nrfx_timer_enable(&amp;amp;timer);&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;............&lt;/p&gt;
&lt;p&gt;Hope this helps someone.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317908?ContentTypeID=1</link><pubDate>Wed, 30 Jun 2021 09:16:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fbe4aab6-bc25-4134-9ab1-18f134e3bf1d</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I understand the concern , but I believe the overhead of re-enabling the interrupt from the driver callback should be negligible considering the CPU is running at 64 MHz. That said, I don&amp;#39;t foresee any problems with patching the driver like you did either, it should be fine. I will update the thread once I hear back from the team.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317754?ContentTypeID=1</link><pubDate>Tue, 29 Jun 2021 14:12:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c8b86908-af6c-4425-91c2-7e7f1a7a5081</guid><dc:creator>benmack</dc:creator><description>&lt;p&gt;Hi Vidar, thanks for that&lt;/p&gt;
&lt;p&gt;I did try that, but it is a heavy bit of code to call in every interrupt&lt;/p&gt;
&lt;p&gt;Call me old-school but I think I&amp;#39;ll stick with my direct register writes for now, and hope they tweak the drivers in the future.&lt;/p&gt;
&lt;p&gt;Thanks again&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317660?ContentTypeID=1</link><pubDate>Tue, 29 Jun 2021 10:22:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9dede465-8862-455f-ba8b-0e09f8218020</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;I understand, it can be nice to work closer to the HW sometimes &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt; The benefit of the timer library is that it makes it easy to set up multiple periodic and one-off timer interrupts with just a single RTC instance. However, in this case, when you only need one periodic interrupt source, it should be easy enough to just work directly with the driver.&lt;/p&gt;
&lt;p&gt;I haven&amp;#39;t heard back from the driver team yet, but I think another approach you can use it to re-enable the interrupt from your application callback. This way you don&amp;#39;t have to patch the driver code.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** @brief: Function for handling the RTC0 interrupts.
 * Triggered on TICK and COMPARE0 match.
 */
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    uint32_t err_code; 

    if (int_type == NRF_DRV_RTC_INT_COMPARE0)
    {
        nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
        
        nrf_drv_rtc_counter_clear(&amp;amp;rtc);
        err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317524?ContentTypeID=1</link><pubDate>Mon, 28 Jun 2021 14:53:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f2b9653d-035e-44b2-bcdd-ef4ef1423c57</guid><dc:creator>benmack</dc:creator><description>&lt;p&gt;Thanks Vidar, much appreciated :-)&lt;/p&gt;
&lt;p&gt;I did look at the app_timer stuff, but to be honest I struggled with the extra layer of abstraction. I&amp;#39;m an old school low-level programmer, much happier dealing direct with hardware registers...&lt;/p&gt;
&lt;p&gt;Cheers, Ben&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317517?ContentTypeID=1</link><pubDate>Mon, 28 Jun 2021 14:25:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e5d9aa0-4dca-4cf5-a6e7-1d609409cf10</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;I was a bit suprised to see it implemented like this too. Typically you have to explitely disable interrupts when using the nRFX drivers. I will report this internally and ask if there is a particular reason for disabling the interrupt from the IRQ handler.&lt;/p&gt;
[quote user="benmack"]I must admit, this seems a really common application (generating a repeating 1 second interrupt), and I was a bit surprised that the nrfx driver behaves this way[/quote]
&lt;p&gt;&amp;nbsp;It&amp;#39;s very common, but we generally recommend using the app timer library on top the RTC driver for this (&lt;span&gt;&lt;a title="Timer library" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_timer.html?cp=8_1_3_53"&gt;Timer library&lt;/a&gt;&lt;/span&gt;). Is that something you have considered for your application? &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRFX RTC Driver - How to stop compare interrupt handler disabling interrupts</title><link>https://devzone.nordicsemi.com/thread/317436?ContentTypeID=1</link><pubDate>Mon, 28 Jun 2021 10:49:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f2ee6fd-badb-4a43-a299-1b71e99a37b9</guid><dc:creator>benmack</dc:creator><description>&lt;p&gt;Just to clarify, I am using a compare interrupt, and clearing the counter in the interrupt handler, so it should repeat. However the nrfx_rtc.c irq_handler() calls nrf_rtc_event_disable() and nrf_rtc_int_disable(), so I have to re-enable them in the interrupt handler&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my init code&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;   nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
   config.prescaler = 4095;  // gives 8Hz (32kHz/(prescaler + 1)) - note 12 bit so this is max prescaler value
   err_code = nrfx_rtc_init(&amp;amp;rtc_inst, &amp;amp;config, rtc_handler);
   APP_ERROR_CHECK(err_code);
   err_code = nrfx_rtc_cc_set(&amp;amp;rtc_inst, 0, 8, true);    // Compare counter 
   APP_ERROR_CHECK(err_code);
   nrfx_rtc_enable(&amp;amp;rtc_inst);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and my interrupt handler (with cludge)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void rtc_handler(nrfx_rtc_int_type_t int_type)
{
   if (int_type == NRFX_RTC_INT_COMPARE0)
   {
      nrfx_rtc_counter_clear(&amp;amp;rtc_inst);        // clear counter
      NRF_RTC2-&amp;gt;EVTENSET = (1UL &amp;lt;&amp;lt; 16);         // cludge to re-enable event and interrupt
      NRF_RTC2-&amp;gt;INTENSET = (1UL &amp;lt;&amp;lt; 16);
      // 1 second tick timers....
      if (ben_timer)
         ben_timer--;
   }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;My question is how I do this properly, either by persuading the nrfx driver to not disable interrupts, or by re-enabling them each time using nrfx calls rather than my cludge&lt;/p&gt;
&lt;p&gt;I must admit, this seems a really common application (generating a repeating 1 second interrupt), and I was a bit surprised that the nrfx driver behaves this way&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>