How to start with sleepy code in nRF connect Desktop SDK

Hi Team,

How to start with the sleepy code in nRF connect desktop SDK, can anyone provide me a refernce which has clear insights?

Thanks,

Pranathi

  • Hi,

    They key point regardign sleep is that you can forget about the sleep, as that is handled automatically. Whenever you do not do anything, the kernel automatically ensures that the device sleeps in sytem ON low power mode (waiting for events/interrupts).

    Note that the timers you describe are not low power, and they are normally never used while in sleep. Instead, an RTC instance is used for keeping track of time, and that is handled for you by the Zephyr kernel, which proivde many APIs for that (see Kernel Timing).

    For isntance, you can make threads thta sleep and wake up every x amount of time, or threads that sleep because of soemthing else, or you can use dalayed items in a workqueue in order to schedule work to be done at a specific time in the future.

    We are finishing up a more advanced course on this that we plan to release shortly, which I recomend you take a look at (this is indended as a natural continuation of nRF Connect SDK Fundamentals course, which I suggest you look at when it is available (you should also look at the Fundamentals course if you have not).

  • Hi, 

    Can I use k_sleep(K_FOREVER) in my code (in main) to make my system go in constant sleep and then use an RTC as a wakeup source to wake the core of my system?

    I am getting around 5mA of current consumption without any sleep exhibited and I am aiming to reduce the current consumption for a better battery life.

  • Pranathi CH said:
    Can I use k_sleep(K_FOREVER) in my code (in main) to make my system go in constant sleep and then use an RTC as a wakeup source to wake the core of my system?

    No, that is not how it works. If you don't want the main thread (or any other thread for that matter) anymore, just let it return. The idle task always runs.

    Pranathi CH said:
    I am getting around 5mA of current consumption without any sleep exhibited and I am aiming to reduce the current consumption for a better battery life.

    Can you share what you are doing? If for instance you have an eternal loop with no sleep so that the CPU is on 100% that could give you numbers of that magnitude.

  • I am using a thread that is defined as

    K_THREAD_DEFINE(control, 1024, control_thread, NULL, NULL, NULL, -1, 0, 1000);
    And at the end of the entry "control_thread" function I am using k_sleep(K_MSEC(50));
    int main(void)
    {
        #My code#
        
        return 0;
    }
    
    void control_thread(void)
    {
        while(true)
        {
            #My logic#
            k_sleep(K_MSEC(50));
        }
    }
    
    K_THREAD_DEFINE(control, 1024, control_thread, NULL, NULL, NULL, -1, 0, 1000);
  • I see. Nothing obviously stands out here. If you comment out your logic and keep only the sleep, what current consumption do you measure then? If that is also high, you need to look somewhere else. Either in other parts of the application, or it oculd also be worth looking at your hardware and how you measure the current consumption.

Related