设备系统电源管理原理
其实之前我们在介绍系统电源管理的时候已经看到看设备电源管理相关的函数,代码路径zephyr/subsys/pm/power.c
:
enum pm_state pm_system_suspend(int32_t ticks)
{
SYS_PORT_TRACING_FUNC_ENTER(pm, system_suspend, ticks);
z_power_state = pm_policy_next_state(ticks);
if (z_power_state.state == PM_STATE_ACTIVE) {
LOG_DBG("No PM operations done.");
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend, ticks, z_power_state.state);
return z_power_state.state;
}
post_ops_done = 0;
if (ticks != K_TICKS_FOREVER) {
/*
* Just a sanity check in case the policy manager does not
* handle this error condition properly.
*/
__ASSERT(z_power_state.min_residency_us >=
z_power_state.exit_latency_us,
"min_residency_us < exit_latency_us");
/*
* We need to set the timer to interrupt a little bit early to
* accommodate the time required by the CPU to fully wake up.
*/
z_set_timeout_expiry(ticks -
k_us_to_ticks_ceil32(z_power_state.exit_latency_us), true);
}
// 设备电源管理
#if CONFIG_PM_DEVICE
bool should_resume_devices = true;
switch (z_power_state.state) {
case PM_STATE_RUNTIME_IDLE:
__fallthrough;
case PM_STATE_SUSPEND_TO_IDLE:
__fallthrough;
case PM_STATE_STANDBY:
/* low power peripherals. */
if (pm_low_power_devices()) {
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend,
ticks, _handle_device_abort(z_power_state));
return _handle_device_abort(z_power_state);
}
break;
case PM_STATE_SUSPEND_TO_RAM:
__fallthrough;
case PM_STATE_SUSPEND_TO_DISK:
__fallthrough;
case PM_STATE_SOFT_OFF:
if (pm_suspend_devices()) {
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend,
ticks, _handle_device_abort(z_power_state));
return _handle_device_abort(z_power_state);
}
break;
default:
should_resume_devices = false;
break;
}
#endif
/*
* This function runs with interruptions locked but it is
* expected the SoC to unlock them in
* pm_power_state_exit_post_ops() when returning to active
* state. We don't want to be scheduled out yet, first we need
* to send a notification about leaving the idle state. So,
* we lock the scheduler here and unlock just after we have
* sent the notification in pm_system_resume().
*/
k_sched_lock();
pm_debug_start_timer();
/* Enter power state */
pm_state_notify(true);
pm_state_set(z_power_state);
pm_debug_stop_timer();
/* Wake up sequence starts here */
// 设备电源管理
#if CONFIG_PM_DEVICE
if (should_resume_devices) {
/* Turn on peripherals and restore device states as necessary */
pm_resume_devices();
}
#endif
pm_log_debug_info(z_power_state.state);
pm_system_resume();
k_sched_unlock();
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend, ticks, z_power_state.state);
return z_power_state.state;
}
从上面可以看出来,设备电源管理在进入休眠的时候会根据系统电源管理状态
调用两个函数,pm_low_power_devices
或pm_suspend_devices
pm_low_power_devices函数
int pm_low_power_devices(void)
{
return _pm_devices(PM_DEVICE_STATE_LOW_POWER);
}
static int _pm_devices(enum pm_device_state state)
{
const struct device *devs;
size_t devc;
devc = z_device_get_all_static(&devs);
num_susp = 0;
for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) {
int ret;
/* ignore busy devices */
// 判定设备时忙碌或设备时系统唤醒源
if (pm_device_is_busy(dev) || pm_device_wakeup_is_enabled(dev)) {
continue;
}
// 执行设备的电源管理动作
ret = pm_device_state_set(dev, state);
/* ignore devices not supporting or already at the given state */
if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
continue;
} else if (ret < 0) {
LOG_ERR("Device %s did not enter %s state (%d)",
dev->name, pm_device_state_str(state), ret);
return ret;
}
__pm_device_slots_start[num_susp] = dev;
num_susp++;
}
return 0;
}
从上面可以看出,当设备是忙碌或者设备是系统的唤醒源是那就不执行设备休眠。
真正执行设备电源管理动作的还是通过pm_device_state_set
函数。
pm_suspend_devices函数
int pm_suspend_devices(void)
{
return _pm_devices(PM_DEVICE_STATE_SUSPENDED);
}
static int _pm_devices(enum pm_device_state state)
{
const struct device *devs;
size_t devc;
devc = z_device_get_all_static(&devs);
num_susp = 0;
for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) {
int ret;
/* ignore busy devices */
// 判定设备是否忙碌或设备是否是系统唤醒源
if (pm_device_is_busy(dev) || pm_device_wakeup_is_enabled(dev)) {
continue;
}
// 执行设备电源管理动作
ret = pm_device_state_set(dev, state);
/* ignore devices not supporting or already at the given state */
if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
continue;
} else if (ret < 0) {
LOG_ERR("Device %s did not enter %s state (%d)",
dev->name, pm_device_state_str(state), ret);
return ret;
}
__pm_device_slots_start[num_susp] = dev;
num_susp++;
}
return 0;
}
从上面可以看出,当设备是忙碌或者设备是系统的唤醒源是那就不执行设备休眠。
真正执行设备电源管理动作的还是通过pm_device_state_set
函数。
设备系统电源管理示例
Kconfig定义:
CONFIG_GPIO=y
//启动电源管理
CONFIG_PM=y
//启动设备电源管理
CONFIG_PM_DEVICE=y
设备树:
/{
power-states {
stop0: state0 {
compatible = "zephyr,power-state";
power-state-name = "suspend-to-idle";
substate-id = <1>;
min-residency-us = <500>;
};
stop1: state1 {
compatible = "zephyr,power-state";
power-state-name = "suspend-to-idle";
substate-id = <2>;
min-residency-us = <700>;
};
stop2: state2 {
compatible = "zephyr,power-state";
power-state-name = "suspend-to-idle";
substate-id = <3>;
min-residency-us = <1000>;
};
};
leds {
compatible = "gpio-leds";
green_led_2: led_2 {
gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
label = "User LD2";
};
};
aliases {
led0 = &green_led_2;
sw0 = &user_button;
};
}
代码:
/*
* Copyright (c) 2021 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <sys/printk.h>
#define SLEEP_TIME_MS 2000
static const struct gpio_dt_spec led =
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
void main(void)
{
bool led_is_on = true;
__ASSERT_NO_MSG(device_is_ready(led.port));
printk("Device ready\n");
// 标记设备时忙碌的,打开这个注释设备就无法休眠
// pm_device_busy_set(led.port);
while (true) {
gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
gpio_pin_set(led.port, led.pin, (int)led_is_on);
if (led_is_on == false) {
/* Release resource to release device clock */
gpio_pin_configure(led.port, led.pin, GPIO_DISCONNECTED);
}
k_msleep(SLEEP_TIME_MS);
if (led_is_on == true) {
/* Release resource to release device clock */
gpio_pin_configure(led.port, led.pin, GPIO_DISCONNECTED);
}
led_is_on = !led_is_on;
}
}