<?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>Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/57861/large-delay-when-using-app-scheduler-in-gpiote-interrupt</link><description>Hello, 
 I have an external chip in SPI slave mode and connected to 52840 through SPI interface. When data is ready it will notify 52840 through its dedicated IRQ pin (connected to one of GPIO of 52840 ), and then 52840 will read the data back through</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 21 Feb 2020 12:56:57 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/57861/large-delay-when-using-app-scheduler-in-gpiote-interrupt" /><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235717?ContentTypeID=1</link><pubDate>Fri, 21 Feb 2020 12:56:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0cb90db5-6d7a-4624-ae1c-a0e439f34185</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="rolandash"]So the question is scheduler event not able to wake up CPU? or it is because the GPIOTE interrupt handler somehow put CPU back to sleep again after completion?[/quote]
&lt;p&gt;The schedule is just a queue that you pop in the main loop, so it does not wake the CPU from sleep. However, that is not necessary either. In this case, you call it from an interrupt/event handler, so the CPU is awake. Therefore, it will run until it executes&amp;nbsp;sd_app_evt_wait(). And similarly, after any events/interrupts are processed, the main/thread will run from the last call to sd_app_evt_wait() (or WFE/WFI if called directly).&lt;/p&gt;
&lt;p&gt;Do you call sd_app_evt_wait(), WFE or WFI from anywhere other than a single place in your main loop (via idle_state_handle and possible&amp;nbsp;nrf_pwr_mgmt_run())? If so, that could explain this behaviour.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235614?ContentTypeID=1</link><pubDate>Fri, 21 Feb 2020 07:04:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0eefc6fb-4f43-4d4d-abd1-bb772c1f5401</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Another related problem about GPIOTE confused me&amp;nbsp;during the test is, can scheduler event wake CPU up or not?&lt;/p&gt;
&lt;p&gt;To isolate the delay issue, I built a new app from scratch, with the same GPIOTE handling model as original app, but without irrelevant timers and other components.&lt;/p&gt;
&lt;p&gt;as shown above in my test application, the main&amp;nbsp;loop is like:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
    // Enter main loop.
    for (;;)
    {
        //handle usbd event sperately;
        //app_usbd_event_queue_process();
        
        app_sched_execute();
        if (NRF_LOG_PROCESS() == false)
        {
			idle_state_handle();
        }
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;this is the regular recommended routine. It is expected when GPIOTE interrupt happening and the event is issued, the CPU wakeup from the sleep mode and process the event.&lt;/p&gt;
&lt;p&gt;However, I found that after my GPIOTE interrupt handler put the event into queue, the CPU seems go back again into sleep, instead of processing the event. So the program just stuck there after GPIOTE interrupt happened for one time.&lt;/p&gt;
&lt;p&gt;The event will only get processed if I commented out the&amp;nbsp;&lt;/p&gt;
&lt;p&gt;//idle_state_handle();&lt;/p&gt;
&lt;p&gt;line, but now this&amp;nbsp;means CPU never go to sleep.&lt;/p&gt;
&lt;p&gt;So the question is scheduler event not able to wake up CPU? or it is because the GPIOTE interrupt handler somehow put CPU back to sleep again after completion?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235611?ContentTypeID=1</link><pubDate>Fri, 21 Feb 2020 06:49:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:25ab78c2-ce81-4238-89e7-f52f39929e83</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;thanks, I will look into it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235555?ContentTypeID=1</link><pubDate>Thu, 20 Feb 2020 16:33:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b971acbd-a44f-4675-b8e2-a5e06fe6f73f</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Maybe not directly relevant, but have a look at this&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/39188/nrf_log_frontend_dequeue-must-be-atomically-protected-against-re-entry-from-interrupt-context"&gt;nrf_log_frontend_dequeue-must-be-atomically-protected-against-re-entry-from-interrupt-context&lt;/a&gt; issue, where I just posted some code following the suggestions by the reporter&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235472?ContentTypeID=1</link><pubDate>Thu, 20 Feb 2020 13:40:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:380f4b21-dbcc-4aeb-ae19-3ee74c3f7e85</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;As you suggested, I took some screenshots from a oscilloscope, toggling a GPIO pin.&lt;/p&gt;
&lt;p&gt;First one is taken when nrf_log is on (I use deferred log)&lt;/p&gt;
&lt;p&gt;&lt;img alt="Fig. 1" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/41582201751_5F00_.pic_5F00_hd-copy.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;Second one is taken when nrf_log is totally disabled.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img class="align-left" style="float:left;" alt="Fig. 3" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/71582204844_5F00_.pic_5F00_hd-copy.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As you can see when log is on, the delay is between 500us ~ 2.5ms,&lt;/p&gt;
&lt;p&gt;and when log is off, the delay is ~ 4us.&lt;/p&gt;
&lt;p&gt;I have to say that this result surprised me, as I did not expect logging can make such big difference, even in deferred mode. Is it normal? or did I miss some configuration to make it correct?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235201?ContentTypeID=1</link><pubDate>Wed, 19 Feb 2020 13:43:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f31bb76a-83cd-4bad-b9b8-0aec97540183</guid><dc:creator>Einar Thorsrud</dc:creator><description>[quote user="rolandash"]Any other thing I can try?[/quote]
&lt;p&gt;I don&amp;#39;t see why you get such a large number this way, but anyway, the LFCLK is very slow compared to the CPU clock, so you will not get an accurate measurement this way. Can you try simply toggling a GPIO in&amp;nbsp;gpiote_irq_handler() and&amp;nbsp;data_event_dispatch() and measure the time using a logic analyzer or an oscilloscope? That would give you a definitive answer about the time, removing any doubt about how it is measured.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235199?ContentTypeID=1</link><pubDate>Wed, 19 Feb 2020 13:38:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22922150-7234-4092-b7ae-df0837ecf705</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I use following method, in GPIOTE IRQ handler, record the current ticks into data event,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void gpiote_irq_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    
    if (pin == EXT_DATA_IRQ_PIN) {
     

        ex_data_event_t evt;
        //fill in data evt with current ticks;
        evt.cur_ticks = app_timer_cnt_get();
        ....
        app_sched_event_put(&amp;amp;evt, sizeof(evt), data_event_dispatch);

    }
    
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and then in scheduler event handler, calculate the delay,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void data_event_dispatch(void *p_event_data, uint16_t event_size) {
    ex_data_event_t* p_evt = (ex_data_event_t*)p_event_data;
    ...
    case EXT_DATA_OPS_TYPE_IRQ:
        {
            //calc delay;
            uint32_t cur_ticks = app_timer_cnt_get();
            uint32_t elapsed = cur_ticks - p_evt-&amp;gt;cur_ticks;
            float ms = elapsed / 32.768;
            NRFX_LOG_DEBUG(&amp;quot;\t call IRQ handler....after &amp;quot;NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(ms));    
        }
        break;
    ...

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The printed result varies between 1~2ms.&lt;/p&gt;
&lt;p&gt;Today I did more tests, basically disabled any irrelevant&amp;nbsp;things, like timers, ble activities, other GPIOTE channels, but the result just remains the same :-(&lt;/p&gt;
&lt;p&gt;Any other thing I can try?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/235194?ContentTypeID=1</link><pubDate>Wed, 19 Feb 2020 13:02:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0a4a723b-c4ce-4c0a-b8b3-dbca45b57fde</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I do not see how it should be related. How do you measure the time from you call&amp;nbsp;app_sched_event_put() and&amp;nbsp;app_sched_event_put() runs?&amp;nbsp;Could it be a problem with your time measurement, rather than that it actually takes that long time?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/234745?ContentTypeID=1</link><pubDate>Mon, 17 Feb 2020 17:15:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b6207cb6-03ae-4fa0-9df8-7b681dc246ab</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;One thing I forgot to mention is, I have an external PA(RF2411) connected to 52840, so it is also using GPIOTE channel to switch between PA/LNA.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;52840 is also advertising while the code is running. Do you think it is possible that the PA/LNA GPIOTE task could be the reason of this delay?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/234695?ContentTypeID=1</link><pubDate>Mon, 17 Feb 2020 14:52:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ebf931d7-2219-468a-a3a7-d414d1aa01dc</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;thx for the swift&amp;nbsp;reply @Einar Thorsrud. this also seems the same strange to me.&lt;/p&gt;
&lt;p&gt;I&amp;nbsp;could not think of what the processor is really doing during this 2ms. &lt;span&gt; I added log at the end of gpiote_irq_handler and the first beginning of&amp;nbsp;&lt;/span&gt;&lt;span&gt;app_sched_execute, t&lt;/span&gt;his delay appears exactly between every GPIOTE interrupt ends and the beginning of app_sched_execute. And this delay does not happen whenever app_shed_event_put is not called from GPIOTE interrupt, for example, a timer interrupt context.&lt;/p&gt;
&lt;p&gt;I did have few other timers defined, but the intervals are all above 30ms, so seems not possible this inteference can happen in this accuracy.&lt;/p&gt;
&lt;p&gt;I tried to tap into the scheduler queue to see if any other events blocked there. From the output I can see when this delay happens there is only 1 event in queue, i.e., the GPIOTE interrupt generated event itself. So the queue is really quite empty.&lt;/p&gt;
&lt;p&gt;I also tried to config different GPIOTE/SPIM priority, but the result is just the same.&lt;/p&gt;
&lt;p&gt;I tend to guess this delay is somewhat related with GPIOTE module specificly. Do you have any suggestions on how to debug further that I can find out what CPU is really doing in this 2ms delay?&lt;/p&gt;
&lt;p&gt;My setup:&lt;/p&gt;
&lt;p&gt;SDK v15.2 with softdevice v610 presented.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Large delay when using app scheduler in GPIOTE interrupt</title><link>https://devzone.nordicsemi.com/thread/234667?ContentTypeID=1</link><pubDate>Mon, 17 Feb 2020 13:54:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07f73afa-f14f-47cb-86c2-ead72ed95a8c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The app scheduler is just a queue that you pop ad process in the main loop. Your main loop does not do much else, and the queue is processed before logs (assuming you use deferred logging), so I do not see an explanation there. Could it be caused by other interrupts that are processed at the same time, and that takes some time? (they happen at the same time for some reason)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>