system power standby best practice

Hi all.

What is the best practice to set the system in standby? I have a PWM and when is active the system can be in a low power consumption until a button is pressed. But for that, using zephyr, what can I do to reduce consumption? is the k_msleep() a good practice for that?

Also, for example a code when I set a led to turn ON when the system is fully charged, is a good practice to also keep a k_msleep() to do nothing waiting for the state to be off to power_off the system?

Best regards

Parents
  • Hi, in Zephyr the "standby/idle state" is handled automatically. This means that whenever a thread is finished it will go back to the idle thread, as long as no other threads are being queued. You can even return the main() function, and it will stay in idle.

    k_msleep() is used if you want to pause the thread for a give time. It will wait in the idle thread until the k_msleep() timer runs out, and continue the thread. So if you for example have a for-loop in the main thread, and put a k_msleep() in it, it will wake up from idle in the specified k_msleep() interval, which then can be used do periodic tasks. A better way would be to use the workqueue to queue the tasks you want to do, or to set up periodic tasks.

    For the button press, you would create a GPIO interrupt with a callback function. I guess you can check out this example: https://github.com/nrfconnect/sdk-zephyr/tree/main/samples/basic/button

    In that example you can see that the button pin and LED is initialized in main(). The button is configured with an interrupt gpio_pin_interrupt_configure_dt(), and then a callback function is added: gpio_init_callback().

    After all the initialization, the main function returns. So now the CPU is in idle waiting for the button interrupt. No actions are needed to put the system in standby/idle. When the button is pressed, the callback function will be run, and when it is finished it will return to the idle thread.

    Best regards,
    Stian

Reply
  • Hi, in Zephyr the "standby/idle state" is handled automatically. This means that whenever a thread is finished it will go back to the idle thread, as long as no other threads are being queued. You can even return the main() function, and it will stay in idle.

    k_msleep() is used if you want to pause the thread for a give time. It will wait in the idle thread until the k_msleep() timer runs out, and continue the thread. So if you for example have a for-loop in the main thread, and put a k_msleep() in it, it will wake up from idle in the specified k_msleep() interval, which then can be used do periodic tasks. A better way would be to use the workqueue to queue the tasks you want to do, or to set up periodic tasks.

    For the button press, you would create a GPIO interrupt with a callback function. I guess you can check out this example: https://github.com/nrfconnect/sdk-zephyr/tree/main/samples/basic/button

    In that example you can see that the button pin and LED is initialized in main(). The button is configured with an interrupt gpio_pin_interrupt_configure_dt(), and then a callback function is added: gpio_init_callback().

    After all the initialization, the main function returns. So now the CPU is in idle waiting for the button interrupt. No actions are needed to put the system in standby/idle. When the button is pressed, the callback function will be run, and when it is finished it will return to the idle thread.

    Best regards,
    Stian

Children
Related