<?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>nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/115415/nrf5340-enter-low-power-sleep-while-timer-enabled</link><description>I&amp;#39;m working on optimizing power consumption in my project (using nRF5340DK; as well as Power Profiler Kit 2 which is connected to DK&amp;#39;s VDD measurement point as an ampere meter). After trial and error, I&amp;#39;ve narrowed down the timer (set up similarly to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 21 Oct 2024 08:16:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/115415/nrf5340-enter-low-power-sleep-while-timer-enabled" /><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/507067?ContentTypeID=1</link><pubDate>Mon, 21 Oct 2024 08:16:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7e4692a-e568-4553-b8e5-ec7e19c10b5c</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;Sorry for confusion. No I do not mean that you should only put the&amp;nbsp;&amp;nbsp;k_sleep(K_FOREVER); to an independent thread. What I meant was to put the whole&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;while (true) {
execute_other_code(); // ... BUT DOESN&amp;#39;T EXECUTE UNTIL BEFORE NEXT INTERRUPT

k_sleep(K_FOREVER);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Into an independent thread just in case there isn&amp;#39;t a way to get the main thread id. But you already find the&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://docs.zephyrproject.org/apidoc/latest/group__thread__apis.html#ga7ef1ed0fb9513df8096ede1e52fc76b2"&gt;k_current_get&lt;/a&gt;() so it&amp;#39;s fine.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506994?ContentTypeID=1</link><pubDate>Sat, 19 Oct 2024 02:53:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9330dbeb-f8d9-4b4e-8a73-2a62164f3f66</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;I will consider that suggestion.&lt;/p&gt;
&lt;p&gt;In the meanwhile, I wanted to circle back to your suggestion to &amp;quot;&lt;span&gt;put the sleep forever into an independent thread&lt;/span&gt;&amp;quot;.&amp;nbsp;I tried this but it just kept the main thread running all the time. If I misinterpreted what you meant, I&amp;#39;d appreciate a clarification. A quick example would also be greatly appreciated if possible.&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;k_tid_t main_thread_id;

void wakeup_main(void)
{
    k_wakeup(main_thread_id);
}

void interrupt_handler(void)
{
  restart_timer();  // for example, with RTC, this is counter_set_channel_alarm()
  wakeup_main();
}

int main()
{
  main_thread_id = k_current_get();
  init_timer();

  while (true) {
    execute_other_code();    // ... BUT DOESN&amp;#39;T EXECUTE UNTIL BEFORE NEXT INTERRUPT
  }
}

#define STACKSIZE 1024
static void sleep_thread(void *arg1, void *arg2, void *arg3)
{
    int rc;
    ARG_UNUSED(arg1);
    ARG_UNUSED(arg2);
    ARG_UNUSED(arg3);
    
    k_sleep(K_FOREVER);
}
K_THREAD_DEFINE(sleep_thread_id, STACKSIZE, sleep_thread, NULL, NULL, NULL,
        K_LOWEST_THREAD_PRIO, 0, 100);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506847?ContentTypeID=1</link><pubDate>Fri, 18 Oct 2024 08:44:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9488fcab-2e17-4d67-a8ae-36a25cfb5682</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;I&amp;#39;m happy to help. But I would suggest to keep your task outside of the main loop. I would prefer to have different threads/timer for your tasks instead of putting them into a main loop .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506807?ContentTypeID=1</link><pubDate>Fri, 18 Oct 2024 03:40:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:262c6b63-a7fd-454a-9ce5-833f181f5f51</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;So I made a simpler project doing this, but it behaved as I expected. I looked into it more on my project (which gets a little more complicated: performs SPI reads in interrupt handler, which completes in another interrupt handler, etc.) and ultimately fixed that issue. Did not find the root cause, but generally having main woken up a little earlier (i.e., before the SPI read) seemed to help.&lt;/p&gt;
&lt;p&gt;Thank you for offering your help though!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506636?ContentTypeID=1</link><pubDate>Thu, 17 Oct 2024 08:06:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c0ccdcb-f839-40ef-b642-2995ce851166</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;Could you provide your toggling code so I can test here.&amp;nbsp;&lt;br /&gt;Please explain what you are trying to do ? If you plan to execute some code periodically, can&amp;#39;t you just use the timer and leave the main loop clean?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506595?ContentTypeID=1</link><pubDate>Thu, 17 Oct 2024 03:34:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:66dc1409-9b2f-41f6-8f85-690bb0e3a63e</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;I used &lt;a href="https://docs.zephyrproject.org/apidoc/latest/group__thread__apis.html#ga7ef1ed0fb9513df8096ede1e52fc76b2"&gt;k_current_get()&lt;/a&gt;&amp;nbsp;to get the main thread ID, like so.&amp;nbsp;But it seems to wakeup much later (just before the interrupt handler gets called again). I tried having the interrupts occur every 3ms or every 3 seconds, and it gave the same behavior. I also tried putting the k_current_get() and infinite loop in another thread using K_THREAD_DEFINE() but it also gave the same behavior.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;k_tid_t main_thread_id;

void wakeup_main(void)
{
    k_wakeup(main_thread_id);
}

void interrupt_handler(void)
{
  restart_timer();  // for example, with RTC, this is counter_set_channel_alarm()
  
  wakeup_main();                  // DOES WAKE UP MAIN...
}

int main()
{
  main_thread_id = k_current_get();

  init_timer();

  while (true) {
    execute_other_code();    // ... BUT DOESN&amp;#39;T EXECUTE UNTIL BEFORE NEXT INTERRUPT

    k_sleep(K_FOREVER);
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I confirmed this using a logic analyzer (sampling rate of 25MHz). Red line toggles each time main() runs, and orange line toggles each time interrupt handler is just about to call k_wakeup(). I did confirm that if I do not call k_wakeup(), red line never toggles.&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/main_5F00_wakes_5F00_up_5F00_but_5F00_later.jpg" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506507?ContentTypeID=1</link><pubDate>Wed, 16 Oct 2024 12:06:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:29f26166-bb60-46f7-87b1-45c60622a122</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;Yes you can use &lt;a href="https://docs.zephyrproject.org/apidoc/latest/group__thread__apis.html#ga9275a019c8ff3c7fe49a81f8c078157e"&gt;k_wakeup&lt;/a&gt;() to wake up a sleeping thread. I believe you can wake up the main thread. You may need to find how to get the main thread ID. But an easy workaround is to put the sleep forever into an independent thread.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506389?ContentTypeID=1</link><pubDate>Wed, 16 Oct 2024 03:17:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05344f16-03d2-4d3c-94a5-22834b98d1e2</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;(OP replying) Somewhat related question, but is it possible to cancel the remainder of a k_sleep() call within an interrupt handler?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void interrupt_handler(void)
{
  restart_timer();  // for example, with RTC, this is counter_set_channel_alarm()
  
  cancel_ksleep();                  // DOES SOMETHING LIKE THIS EXIST...
}

int main()
{
  init_timer();

  while (true) {
    execute_other_code();    // ... SO THAT THIS EXECUTES MORE THAN ONCE?

    k_sleep(K_FOREVER);
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I realize this may not be common to do; for example, I know that things happening periodically could just be put in the main loop with k_sleep(SOME_AMOUNT_OF_TIME). But it may be possible that my project would benefit from something like this what with the timers and other complex portions of our code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506379?ContentTypeID=1</link><pubDate>Tue, 15 Oct 2024 20:33:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c72c69c-d31f-43d9-a0f8-b6bcbbd88979</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;Yes, CONFIG_SERIAL=n seemed to do it. The LBS project seems to have its child image project files over in the sample HCI_RPMSG project (my local NCS installation path,&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;zephyr\samples\bluetooth\hci_rpmsg)&lt;/span&gt;. I edited the prj.conf there w/ CONFIG_SERIAL=n, rebuilt the project, flashed the build, and confirmed w/ PPK2 that when idle it&amp;#39;s drawing roughly 3.5uA. I&amp;#39;ve attached the .ppk file here (sorry about that; I got confused between the Power Profiler&amp;#39;s log and the &lt;span style="text-decoration:underline;"&gt;session data&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/ppk2_2D00_session_2D00_data_2D00_nrf5340dk_5F00_hciRpmsgConfigSerialNo.ppk"&gt;devzone.nordicsemi.com/.../ppk2_2D00_session_2D00_data_2D00_nrf5340dk_5F00_hciRpmsgConfigSerialNo.ppk&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Current measurement setup should be correct. When first making this post, I have been using an nRF5340DK, and did already cut SB40. I put PPK2&amp;#39;s&amp;nbsp;GND to DK&amp;#39;s P21 negative pin, VIN to P22 &amp;#39;bottom pin&amp;#39; (close to SW9), and VOUT to P22 &amp;#39;top pin&amp;#39; (close to P23).&lt;/p&gt;
&lt;p&gt;But thanks! I will take a look at the config from the LBS project, and see how I can apply it to my own project. &lt;span style="text-decoration:underline;"&gt;Still need to check out the timer utilizing RTC; will reply again when I have an update&lt;/span&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506243?ContentTypeID=1</link><pubDate>Tue, 15 Oct 2024 07:55:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a9e10fd-f6ab-4528-aa60-fbc19ea95353</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;How did you use the PPK2 ? If you us the nRF Connect for Desktop -&amp;gt; Power profile app, you can click Save/export and save a .ppk2 file.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I assume you are measuring the current consumption on a DK ? Please let us know how did you do that, did you cut SB40.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Also, since you are measuring on the nRF53, please pay extra extension that you should turn off UART log on the network core.&amp;nbsp;&lt;br /&gt;you should set CONFIG_SERIAL=n on the child image project configuration. I think that&amp;#39;s most likely the reason why you see high current consumption with lbs minimal project.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506204?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 21:49:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac9a3cc5-172d-4958-a8d4-33bd27ea19d4</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;I&amp;nbsp;am fairly confident that I&amp;#39;m using prj_minimal.conf. For context, I use VS Code; when adding the build configuration I select board target as &amp;#39;nrf5340dk_nrf5340_cpuapp&amp;#39; and base configuration files as &amp;#39;prj_minimal.conf&amp;#39;.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve attached the PPK2 log; before log started, board was already flashed with firmware and I have not done anything with it i.e., I haven&amp;#39;t connected to it using nRF Connect for Mobile.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/ppk2_5F00_log_5F00_verbose_5F00_2024_2D00_10_2D00_14T21_5F00_13_5F00_10.988Z_2D00_log.txt"&gt;devzone.nordicsemi.com/.../ppk2_5F00_log_5F00_verbose_5F00_2024_2D00_10_2D00_14T21_5F00_13_5F00_10.988Z_2D00_log.txt&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We were originally more worried about the &lt;a href="https://docs.nordicsemi.com/bundle/ps_nrf5340/page/rtc.html#ariaid-title3"&gt;resolution&lt;/a&gt;&amp;nbsp;of 30us, rather than the accuracy. But, after discussing with my team, that may be fine.&lt;/p&gt;
&lt;p&gt;A timer like that would be great. We&amp;#39;ll take a look; thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/506012?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 07:07:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e363ddc-ed96-4ca0-8585-7d6459b1636e</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;Have you made sure you use the prj_minimal.conf when testing with lbs ?&amp;nbsp;&lt;br /&gt;Please send us your PPK2 log. You should see only about 2-4uA sleeping with lbs minimal configuration.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you need a timer for event every 0.5ms I don&amp;#39;t see a problem with RTC. If you have a 32kHz crystal, the accuracy can be 20ppm , meaning you have a tolerance of 0.00001ms. Or if you use RC the accuracy is 500ppm, it will be 0.00025ms.&amp;nbsp;&lt;br /&gt;But instead of&amp;nbsp; using k_sleep you should set up a &lt;a href="https://docs.zephyrproject.org/latest/kernel/services/timing/timers.html"&gt;timer&amp;nbsp;&lt;/a&gt;&amp;nbsp;. It will use the RTC.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/505956?ContentTypeID=1</link><pubDate>Fri, 11 Oct 2024 16:45:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7738fd7a-7678-4c49-9d8f-6f06dc252c53</guid><dc:creator>esmart-engineering</dc:creator><description>&lt;p&gt;We are using the timer to read data w/ SPI as quickly as every 0.5ms, so the accuracy is a high requirement.&lt;/p&gt;
&lt;p&gt;I will try out the k_sleep() in a separate project momentarily. The 0.2mA I measured is from my project, which hasn&amp;#39;t been fully optimized yet.&lt;/p&gt;
&lt;p&gt;I tested that LBS sample (NCS v2.5.0, nrf/samples/bluetooth/peripheral_lbs/) and got about 461uA when sleeping, with roughly 5mA spikes every 100ms.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF5340, enter low-power/sleep while timer enabled</title><link>https://devzone.nordicsemi.com/thread/505923?ContentTypeID=1</link><pubDate>Fri, 11 Oct 2024 14:28:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:333d9887-7140-47ac-a4ff-c87cfb2f3bbe</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;The TIMER requires the 32MHz oscillator to run and that&amp;#39;s the main reason you are seeing higher current draw.&amp;nbsp;&lt;br /&gt;Do you have a reason why you want to keep a TIMER running ?&amp;nbsp;If you simply want to keeping the time and doesn&amp;#39;t have high requirement on the accuracy you should use the RTC instead. It draws much less current. The k_sleep() function in Zephyr also use RTC.&amp;nbsp;&lt;br /&gt;When you call k_sleep() and when you don&amp;#39;t have any other task running you should see about 2-4 uA current consumption (not 0.2mA)&amp;nbsp;&lt;br /&gt;Could you try to test with the periphreal_lbs sample (use prj_minimal.conf) and measure the current consumption with the PPK2 ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>