一、电源管理子系统是干什么的,有什么用
硬件电路板的CPU和外设的电源集中管理控制中心
1.1 系统电源管理
负责CPU的休眠和唤醒
1.2 设备电源管理
负责外设的电源上下电,休眠和运行模式管理
1.3 设备有几种状态
序号 | 类别 | 状态 | 定义 | 备注 |
1 | 系统 | 运行时 | RUNTIME | |
休眠 | SUSPEND | |||
2 | 外设 | 休眠 | PM_DEVICE_ACTION_SUSPEND | |
恢复 | PM_DEVICE_ACTION_RESUME | |||
关机 | PM_DEVICE_ACTION_TURN_OFF | |||
开机 | PM_DEVICE_ACTION_TURN_ON |
二、在应用层怎么调用
2.1 通过shell 执行电源管理
2.1.1 外设电源管理指令
uart:~$ device list - buttons (active) uart:~$ pm suspend buttons uart:~$ device list uart:~$ devices: - buttons (suspended)
2.2 API汇总
序号 | 分类 | 功能 | API |
1 | 系统PM接口 | 强制改变系统状态,可以让系统休眠。目前我们项目用到了这个接口 | bool pm_state_force(uint8_t cpu, const struct pm_state_info *info) |
系统状态改变的回调函数注册 | pm_notifier_register(struct pm_notifier *notifier) | ||
查询下一个状态 | const struct pm_state_info *pm_state_next_get(uint8_t cpu) | ||
2 | 外设驱动PM接口 | 注册外设名字到PM,把状态的实际执行函数注册进去 | PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) |
通过设备树给PM标识 | PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) | ||
3 | 外设PM接口 | 启动指定外设 | int pm_device_runtime_enable (const struct device *dev) |
关闭指定外设 | int pm_device_runtime_disable(const struct device *dev) | ||
恢复休眠的外设 | int pm_device_runtime_get(const struct device *dev) | ||
让指定的外设进入休眠 | int pm_device_runtime_put(const struct device *dev) | ||
查询指定设备开关状态 | bool pm_device_runtime_is_enabled(const struct device *dev) |
三、驱动和系统需要支持那些功能
3.1 每个外设驱动都需要实现设备的状态切换
#define DT_DRV_COMPAT dummy_device
static int dummy_driver_pm_action(const struct device *dev,
enum pm_device_action action)
{
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
/* suspend the device */
...
break;
case PM_DEVICE_ACTION_RESUME:
/* resume the device 恢复数据*/
...
break;
case PM_DEVICE_ACTION_TURN_ON:
/*
* powered on the device, used when the power
* domain this device belongs is resumed.
*/
...
break;
case PM_DEVICE_ACTION_TURN_OFF:
/*
* power off the device, used when the power
* domain this device belongs is suspended.
*/
...
break;
default:
return -ENOTSUP;
}
return 0;
}
//设备的电源管理
PM_DEVICE_DT_INST_DEFINE(0, dummy_driver_pm_action);
DEVICE_DT_INST_DEFINE(0, &dummy_init,
PM_DEVICE_DT_INST_GET(0), NULL, NULL, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);