Skip to main content

xwrust/xwos/
pm.rs

1//! XWOS RUST:电源管理
2//! ========
3//!
4
5extern crate core;
6use core::ffi::*;
7use core::ptr;
8use core::cmp::{PartialOrd, Ordering};
9
10use crate::types::*;
11
12extern "C" {
13    fn xwrustffi_pm_set_op(cpuid: XwId,
14                           resume_periph: PmOperation,
15                           suspend_periph: PmOperation,
16                           wakeup_cpu: PmOperation,
17                           sleep_cpu: PmOperation,
18                           arg: *mut c_void);
19    fn xwrustffi_pm_suspend();
20    fn xwrustffi_pm_resume();
21    fn xwrustffi_pm_get_stage() -> XwSq;
22}
23
24/// 电源管理回调函数类型
25pub type PmOperation = extern "C" fn(*mut c_void);
26
27/// 设置电源管理的回调函数
28///
29/// # 参数说明
30///
31/// + cpuid: CPU ID
32/// + resume_periph: 恢复外设
33/// + suspend_periph: 暂停外设
34/// + wakeup_cpu: 唤醒
35/// + sleep_cpu: 休眠
36pub fn set_op(cpuid: XwId,
37              resume_periph: PmOperation,
38              suspend_periph: PmOperation,
39              wakeup_cpu: PmOperation,
40              sleep_cpu: PmOperation) {
41    unsafe {
42        xwrustffi_pm_set_op(cpuid,
43                            resume_periph,
44                            suspend_periph,
45                            wakeup_cpu,
46                            sleep_cpu,
47                            ptr::null_mut());
48    }
49}
50
51/// 将系统切换为低功耗状态
52///
53/// 调用此方法后,所有线程都将开始冻结。冻结完成后,系统开始进入低功耗状态。
54///
55/// # 上下文
56///
57/// + 任意
58pub fn suspend() {
59    unsafe {
60        xwrustffi_pm_suspend();
61    }
62}
63
64/// 唤醒系统
65///
66/// 只可在唤醒中断中调用。
67///
68/// # 错误码
69///
70/// + [`XWOK`] 成功
71/// + [`-EALREADY`] 系统正在运行
72///
73/// # 上下文
74///
75/// + 中断
76pub fn resume() {
77    unsafe {
78        xwrustffi_pm_resume();
79    }
80}
81
82pub struct PmStage(XwSq);
83
84impl PmStage {
85    /// 已经暂停
86    pub const SUSPENDED: PmStage = PmStage(0);
87    /// 正在暂停
88    pub const SUSPENDING: PmStage = PmStage(1);
89    /// 正在恢复
90    pub const RESUMING: PmStage = PmStage(1);
91    /// 正在冻结线程
92    pub const FREEZING: PmStage = PmStage(3);
93    /// 正在解冻线程
94    pub const THAWING: PmStage = PmStage(3);
95    /// 正常运行
96    pub const RUNNING: PmStage = PmStage(4);
97}
98
99/// 获取当前电源管理阶段
100///
101/// 电源管理是复杂的异步操作,当系统正在进入低功耗、或从低功耗唤醒时可通过此函数获取进展的阶段。
102///
103/// 返回值是枚举 [`PmStage`] 。
104pub fn get_stage() -> PmStage {
105    unsafe {
106        PmStage(xwrustffi_pm_get_stage())
107    }
108}
109
110impl PartialEq for PmStage {
111    fn eq(&self, other: &Self) -> bool {
112        self.0 == other.0
113    }
114}
115
116impl PartialOrd for PmStage {
117    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
118        if self.0 < other.0 {
119            Some(Ordering::Less)
120        } else if self.0 > other.0 {
121            Some(Ordering::Greater)
122        } else {
123            Some(Ordering::Equal)
124        }
125    }
126}