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