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

  • Hi Stian,

    Thanks for your reply! It's more clear for me now. Now I have a question, regarding this I have an if/else that is looking for a change in a pin. If the pin is 1 then I turn on a LED and if the pin is 0 then I turn off the LED. Based on this, when I reach the pin to 1 and turn on the LED that works fine but after that when I set the pin to 0 the LED stays on for 2 or 3 secs. This can be some error regarding the idle state?

    Best regards

  • Hi, usually you do not want to use an if/else that is looking for a pin change. You configure the pin interrupt for edge detection and in the callback function you can check the provided arguments if the pin is 0 or 1. This callback function will be called whenever there is a level change on the pin. This happens without interaction from the application code, i.e. no if/else, polling, etc.

  • Hi, ok, then I'll change it to use interrupt.

    Many thanks for your help.

    Best regards.

Reply Children
No Data
Related