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
  • 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