Subscribing to power manager events.

Hi!

I am trying to subscribe to the power management events that correspond to the following log lines (which I can clearly see in the log):

I: POWER event ready
I: POWER event removed
I: SUSPEND state detected
I: RESUMING from suspend

I have been trying with this code:

#include "power.h"
#include <zephyr/logging/log.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/state.h>
#include <zephyr/pm/pm.h>

LOG_MODULE_REGISTER(my_module, LOG_LEVEL_INF);

void pm_notifier_cb(enum pm_state state)
{
    printk("PM: %i", state);
}

static struct pm_notifier notifier = {
    .state_entry = pm_notifier_cb,
    .state_exit = pm_notifier_cb,
};

void InitPower(void) {
    printk("Power Init");
    pm_notifier_register(&notifier);
}

I have extended prj.conf with:

CONFIG_PM_DEVICE_RUNTIME=y
CONFIG_PM_DEVICE=y
CONFIG_PM=y


Results:

  • - "Power Init" is getting printed just fine
  • - but the callback is never called.

Setup:

  • - NCS_VERSION=v2.6.0
  • - Running from Ubuntu
  • - Any other info needed?

Any idea what I am doing wrong?

Parents Reply Children
  • (I assume you mean when some version that compiles is released.)

    Sorry, I'm not sure I follow. Are you talking about an internal release on your end?

  • Well, sorry, I am confused, especially as to the anatomy of the zephyr ecosystem and its .release cycle

    Where exactly am I supposed to obtain `usbd_msg_register_cb`?

    Clearly, usbd_msg_register_cb is defined in the `sdk-zephyr` repository master branch, however is not part of the sdk-zephyr repository that I have installed via nrfutil. It seems to be part of some *future* release. I am not sure if it is possible for me to get development version - I have certainly not found instructions on how to anywhere. Just checking out main branch in the repository leads to a large number of kconfig errors...

    Part of what I am confused about is that zephyr sdk is said to not be part of nrf connect sdk, and has listed separate installation instructions. Yet sdk-zephyr is installed during the nrf connect sdk toolchain installation process. Is sdk-zephyr not zephyr sdk? The fact that installation guide points to github.com/.../sdk-ng rather than https://github.com/nrfconnect/sdk-zephyr suggests that too.

  • My bad. I didn't realize that the said function is not available in the current release as I was looking at the latest documentation. Also, I confused the USB device stack API with the USB device controller driver APIs. As far as I can tell, the c2usb library is only using the latter. Either way, I have not found anyone who are familiar with the c2usb library internally, so I'm not sure if there is any easy way to get it to propagate USB state events to the application. 

    The nRF Connect SDK includes this fork of Zephyr: https://github.com/nrfconnect/sdk-zephyr 

    https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.6.1/nrf/dev_model_and_contributions/code_base.html#dm-code-base 

  • Acually, we collaborate with c2usb's creator, so as to its modifications, we should be fairly flexible. (I just didn't realize at first that this is a usb driver/stack issue.) (In this respect, feel free to give just a few pointers and tell me to delegate it to him...)

    In the meanwhile, I have tested Benedek's amend of c2usb, which forwards on some kind of power-related usb events. Maybe the code below tells you more (if not, no worries). Unfortunatelly, it often reports states that are irrelevant to the actual power state.

        usb_manager()
        {
            device_.set_power_event_delegate([](usb::df::device &dev, usb::df::device::event ev) {
                using event = enum usb::df::device::event;
                switch (ev) {
                case event::CONFIGURATION_CHANGE:
                    printk("USB configured: %u, granted current: %uuA\n", dev.configured(), dev.granted_bus_current_uA()); 
                    break;
                case event::POWER_STATE_CHANGE:
                    printk("USB power state: L%u, granted current: %uuA\n", 3 - static_cast<uint8_t>(dev.power_state()), dev.granted_bus_current_uA());
                    break;
                }
            });
        }

    Which further points to github.com/.../device.hpp

    Unlike above benedek's code, the POWER events in the log seem fairly reliable, except...

    When the board is restarted, we get no callback from Benedek's code, neither the POWER event ready event shows up in the log.

    So what we need is to also figure a way to explicitly inquire for the power state...

Related