<?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>Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/119908/wake-up-main-thread-on-any-interrupt</link><description>Hi, 
 Is there a way to make the main thread to execute after any interrupt? 
 For now, I have something like this (ncs 2.7.0): 
 in main thread : 
 
 In each IRQ handlers (gpio, timers...) 
 
 I think that this is error prone, as I can&amp;#39;t be sure to fill</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 21 Mar 2025 15:55:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/119908/wake-up-main-thread-on-any-interrupt" /><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/528446?ContentTypeID=1</link><pubDate>Fri, 21 Mar 2025 15:55:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0cbcf19-ad7d-4b20-afc6-ccd7a4f2f80b</guid><dc:creator>Ben_Squar3d</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Solution was found, I was using IRQ_DIRECT_CONNECT on RTC, and IRQ_CONNECT on gpio, that is why it was working with gpio and not RTC IRQ. IRQ_DIRECT_CONNECT makes scheduling decision to be optionnal.&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/528248?ContentTypeID=1</link><pubDate>Thu, 20 Mar 2025 14:08:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1864f6e-1de5-4650-b8ac-9b9bddeb807e</guid><dc:creator>Ben_Squar3d</dc:creator><description>&lt;p&gt;Good call,&lt;/p&gt;
&lt;p&gt;It seems I am using softdevice as it is the default value (could you confirm this is the default value for nrf5340?). I have 2 questions :&lt;/p&gt;
&lt;p&gt;1) How can I switch to zephyr BLE stack just to try?&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;CONFIG_BT_LL_SW_SPLIT&lt;/span&gt;&lt;span&gt;=y doesn&amp;#39;t work :&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1742478874875v1.png" /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;CONFIG_BT_CTRL throw an error regarding the RNG feature...&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;[EDIT] I was putting this in the wrong prj.conf file, I moved it to the cpunet prj file and it is OK, but nothing changes, still the same issue&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;2) Until now, we were performing a k_sleep(10ms) in the main, and the ble was working fine while the handler of RTC0 was already present :&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1742479548184v2.png" /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;mHWInterrupt is a quite complex function that handle a list of software timers and execute the callback of the expired timers. BLE and our timer module is working fine. How is it possible?&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Thank you for your help&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;span&gt;[EDIT] So for it to be event more confusing, it seems we are using Zephyr BLE controller API such as&amp;nbsp;bt_le_adv_start and&amp;nbsp;&lt;/span&gt;&lt;/span&gt;bt_enable. This would explain why our BLE and our software timers are working&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So &lt;em&gt;I would say&amp;nbsp;&lt;/em&gt;we use the Zephyr BLE API. This is unclear.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Here is the zephyr.dts files (I provided the cpuapp and the cpunet dts)&amp;nbsp; :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/0830.zephyr.dts"&gt;devzone.nordicsemi.com/.../0830.zephyr.dts&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/zephyr-_2800_4_2900_.dts"&gt;devzone.nordicsemi.com/.../zephyr-_2800_4_2900_.dts&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Regards&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/528220?ContentTypeID=1</link><pubDate>Thu, 20 Mar 2025 12:55:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd79f88a-34b2-42ed-9194-a76a8ef415a2</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="Ben_Squar3d"]So it seems that k_wakeup is doing what is expected within the gpio handler, but not the rtc0 handler.[/quote]
&lt;p&gt;Missed to see that you are using RTC0 handler, If you are using BLE with softdevice controller (for difference check &lt;a href="https://stackoverflow.com/questions/75505136/comparison-zephyr-vs-softdevice"&gt;this external post&lt;/a&gt;)&amp;nbsp; then RTC0 is a reserved peripheral If you are not using BLE then the RTC0 is available for application to use and if that is the case for you that you are not using BLE and still this doesn&amp;#39;t work, then we need to see in your&amp;nbsp;zephyr.dts to see if it is used somewhere else.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/528195?ContentTypeID=1</link><pubDate>Thu, 20 Mar 2025 11:33:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e689c8c-4b90-4583-b37c-37b651c3d178</guid><dc:creator>Ben_Squar3d</dc:creator><description>&lt;p&gt;I also found that if I press a button, the main thread is executed right away :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[183536]  t/nrf5340/drv_timer:83 RTC0_IRQHandler
[184736]  t/nrf5340/drv_timer:83 RTC0_IRQHandler
[185793]  0/drv_internal_gpio:259 Interrupt received from PIN: 42
[185794]  DIR/app/XSWIFI/main:470 m_t count 21
[185794]  f5340/drv_ble_stack:32 bt
[185795]  f5340/drv_ble_stack:32 bt
[185805]  t/nrf5340/drv_timer:83 RTC0_IRQHandler
[185936]  t/nrf5340/drv_timer:83 RTC0_IRQHandler
[185941]  0/drv_internal_gpio:259 Interrupt received from PIN: 42
[185942]  DIR/app/XSWIFI/main:470 m_t count 22&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So it seems that k_wakeup is doing what is expected within the gpio handler, but not the rtc0 handler.&lt;/p&gt;
&lt;p&gt;Here is a screenshot of a debug session on k_wakeup :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1742469965905v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;When RT0 handler is called, it breaks at line 1213 and&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;span&gt;thread&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;base&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;&lt;span&gt;thread_state is 128, which is&amp;nbsp;&lt;/span&gt;&lt;/span&gt;_THREAD_QUEUED. So the function returns. But the thread should be suspended (see the callstack)&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Every 10 sec, it does not break here, and breaks at line 1223, which effectively wakes up the main thread.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;When I click a button, it breaks at line 1223 everytime. I don&amp;#39;t think it is normal&lt;/div&gt;
&lt;div&gt;Any help is appreciated&lt;/div&gt;
&lt;div&gt;Regards&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/527963?ContentTypeID=1</link><pubDate>Wed, 19 Mar 2025 08:47:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:23892cd7-70a8-4d5d-8e86-14b4282d2417</guid><dc:creator>Ben_Squar3d</dc:creator><description>&lt;p&gt;Hi, thank you for your answer, I will try it, but it doesn&amp;#39;t adress the question of an existing general irq_handler. It would be great to have this to be sure to not miss any event (the same way WFE() would not miss any event either).&lt;/p&gt;
&lt;p&gt;[EDIT]&amp;nbsp; either I use k_sleep/k_wakeup, semaphores or k_thread_suspend/k_thread_resume, it takes 10s for the main thread to execute after the wakeup. here is what I do&lt;/p&gt;
&lt;p&gt;-the main thread run a custom scheduler that pop event from a fifo&lt;/p&gt;
&lt;p&gt;-the main thread goes to sleep or same take or suspend&lt;/p&gt;
&lt;p&gt;-the different irq do a k_wakeup / give the semaphore / k_thread_resume&lt;/p&gt;
&lt;p&gt;-the main thread is supposed to resume right away, but takes 10s.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;here is the logs with a timestamp like this [time_ms] :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[090135]  DIR/app/main:462 m_t
[090135]  f5340/drv_ble_stack:33 bt
[090136]  f5340/drv_ble_stack:33 bt
[090136]  f5340/drv_ble_stack:33 bt
[090137]  f5340/drv_ble_stack:33 bt
[090137]  f5340/drv_ble_stack:33 bt
[090138]  f5340/drv_ble_stack:33 bt
[090139]  f5340/drv_ble_stack:33 bt
[090139]  f5340/drv_ble_stack:33 bt
[090661]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[091861]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[093061]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[094261]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[095461]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[096662]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[097862]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[099062]  t/nrf5340/drv_timer:79 RTC0_IRQHandler
[100140]  DIR/app/main:462 m_t
[100140]  f5340/drv_ble_stack:33 bt
[100141]  f5340/drv_ble_stack:33 bt
[100142]  f5340/drv_ble_stack:33 bt
[100142]  f5340/drv_ble_stack:33 bt
[100143]  f5340/drv_ble_stack:33 bt
[100143]  f5340/drv_ble_stack:33 bt
[100144]  f5340/drv_ble_stack:33 bt
[100144]  f5340/drv_ble_stack:33 bt&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;m_t&amp;quot; is when main thread is executed&lt;/p&gt;
&lt;p&gt;&amp;quot;bt&amp;quot; is a BLE action that the main thread execute when needed&lt;/p&gt;
&lt;p&gt;Every 1.2s, the RTC trigger an irq as exepected, and is supposed to wake up the main thread to execute a BLE action.&lt;/p&gt;
&lt;p&gt;But we see that the main thread does not&amp;nbsp; wake up before 10s, and then pop all BLE actions.&lt;/p&gt;
&lt;p&gt;Why does the main thread not waking up right away?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you for your help&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wake up main thread on any interrupt</title><link>https://devzone.nordicsemi.com/thread/527941?ContentTypeID=1</link><pubDate>Wed, 19 Mar 2025 06:36:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17efa501-0f5a-419e-8c66-bdc36cfb8679</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;You should be able to use thread suspend and resume as mentioned in this &lt;a href="https://docs.zephyrproject.org/latest/kernel/services/threads/index.html"&gt;Zephyr &lt;/a&gt;documentation to achieve what you want.&lt;/p&gt;
&lt;p&gt;The template code can be something like below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static struct k_thread *main_thread;

void main(void)
{
    main_thread = k_current_get();  // Get the main thread handle

    for (;;) {
        k_thread_suspend(main_thread);  // Suspend the thread
        ...  // whatever logic you want to continue here if any
    }
}

And in the irq handler you can do below
void irq_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    printk(&amp;quot;Interrupt Triggered!\n&amp;quot;);

    // Resume the main thread
    k_thread_resume(main_thread);
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>