add apis to access clock state via the susres manager

this requires a PlatformSpecific extension to the
susres API.

In addition to that, a DeepSleep op is added so we're
no longer overloading the meaning of the SuspendRequest
operation.
This commit is contained in:
bunnie 2026-01-28 15:07:22 +08:00
parent 75f75ebffa
commit 3e4cbe441f
5 changed files with 184 additions and 12 deletions

View file

@ -15,7 +15,7 @@ xous = { version = "0.9.69", features = ["raw-trng"] }
xous-ipc = "0.10.9"
log-server = { package = "xous-api-log", version = "0.1.68" }
log = "0.4.14"
xous-api-susres = "0.9.68"
xous-api-susres = { path = "../../api/xous-api-susres" }
bao1x-hal = { path = "../../libs/bao1x-hal", default-features = false }
bao1x-api = { path = "../../libs/bao1x-api", features = ["derive-rkyv", "std"] }
bio-lib = { path = "../../libs/bio-lib" }
@ -48,7 +48,7 @@ swap = []
mpw = ["bao1x-hal/mpw"]
bmp180 = ["bao1x-hal/bmp180"]
board-baosec = ["bao1x-hal/board-baosec"]
oem-baosec-lite = []
oem-baosec-lite = ["bao1x-hal/oem-baosec-lite"]
board-baosor = ["bao1x-hal/board-baosor"]
board-dabao = ["bao1x-hal/board-dabao"]

View file

@ -20,3 +20,15 @@ pub enum TimeOp {
/// Serialize the internal state for storage across reboots
GetState = 1024,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum ClockOp {
GetVco,
GetFclk,
GetAclk,
GetHclk,
GetIclk,
GetPclk,
GetPer,
DeepSleep,
}

View file

@ -5,6 +5,10 @@ use core::sync::atomic::{AtomicU32, Ordering};
use bao1x_api::*;
use num_traits::*;
use xous::{Message, send_message};
use xous_api_susres::api::Opcode as SusresOp;
use crate::api::ClockOp;
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
pub struct UdmaGlobal {
@ -128,3 +132,128 @@ impl UdmaGlobalConfig for UdmaGlobal {
}
}
}
pub struct ClockManager {
conn: xous::CID,
}
impl ClockManager {
pub fn new() -> Self {
let xns = xous_names::XousNames::new().unwrap();
let conn = xns
.request_connection_blocking(xous_api_susres::api::SERVER_NAME_SUSRES)
.expect("Couldn't connect to susres server");
ClockManager { conn }
}
pub fn get_fclk(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetFclk.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_aclk(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetAclk.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_hclk(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetHclk.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_iclk(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetIclk.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_pclk(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetPclk.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_per(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetPer.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
pub fn get_vco(&self) -> u32 {
match send_message(
self.conn,
Message::new_blocking_scalar(
SusresOp::PlatformSpecific.to_usize().unwrap(),
ClockOp::GetVco.to_usize().unwrap(),
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(clk)) => clk as u32,
_ => unimplemented!(),
}
}
}

View file

@ -273,8 +273,8 @@ fn main() {
servers::susres::start_susres_service();
// TODO: implement a method to fetch the clock setting from susres and put it into here!
servers::bio::start_bio_service(bao1x_hal::board::DEFAULT_FCLK_FREQUENCY);
let clk_mgr = bao1x_hal_service::ClockManager::new();
servers::bio::start_bio_service(clk_mgr.get_fclk());
// claim BIO, based on the platform
servers::trng::start_trng_service();

View file

@ -1,11 +1,14 @@
use core::sync::atomic::{AtomicU32, Ordering};
use bao1x_hal_service::api::ClockOp;
use num_traits::*;
use utralib::*;
use xous::{CID, Message, msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, sender::Sender};
use xous_api_susres::*;
use xous_ipc::Buffer;
use crate::api::ClockOp;
static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(5000);
#[derive(Copy, Clone, Debug)]
@ -31,7 +34,7 @@ fn susres_service() {
let susres_sid = xns.register_name(api::SERVER_NAME_SUSRES, None).expect("can't register server");
let susres_conn = xous::connect(susres_sid).unwrap();
let mut clk_mgr = bao1x_hal::clocks::ClockManager::new().unwrap();
let mut clk_mgr = bao1x_hal::clocks::ClockManagerImpl::new().unwrap();
let measured = clk_mgr.measured_freqs();
log::info!("computed frequencies:");
log::info!(" vco: {}", clk_mgr.vco_freq);
@ -220,13 +223,6 @@ fn susres_service() {
}
}),
Some(Opcode::SuspendRequest) => msg_blocking_scalar_unpack!(msg, deepsleep, 0, 0, 0, {
// catch the case of deepsleep request
if deepsleep == 1 {
log::info!("entering deep sleep");
clk_mgr.deep_sleep();
xous::return_scalar(msg.sender, 1).ok();
// system is shut down after this point, no code runs
}
log::info!("registered suspend listeners:");
for sub in suspend_subscribers.iter() {
log::info!("{:?}", sub);
@ -340,6 +336,41 @@ fn susres_service() {
}
}),
Some(Opcode::Quit) => break,
Some(Opcode::PlatformSpecific) => {
msg_blocking_scalar_unpack!(msg, op, _arg2, _arg3, _arg4, {
let platform_op = FromPrimitive::from_usize(op);
match platform_op {
Some(ClockOp::GetVco) => {
xous::return_scalar(msg.sender, clk_mgr.vco_freq as usize).unwrap()
}
Some(ClockOp::GetFclk) => {
xous::return_scalar(msg.sender, clk_mgr.fclk as usize).unwrap()
}
Some(ClockOp::GetAclk) => {
xous::return_scalar(msg.sender, clk_mgr.aclk as usize).unwrap()
}
Some(ClockOp::GetHclk) => {
xous::return_scalar(msg.sender, clk_mgr.hclk as usize).unwrap()
}
Some(ClockOp::GetIclk) => {
xous::return_scalar(msg.sender, clk_mgr.iclk as usize).unwrap()
}
Some(ClockOp::GetPclk) => {
xous::return_scalar(msg.sender, clk_mgr.pclk as usize).unwrap()
}
Some(ClockOp::GetPer) => {
xous::return_scalar(msg.sender, clk_mgr.perclk as usize).unwrap()
}
Some(ClockOp::DeepSleep) => {
log::info!("entering deep sleep");
clk_mgr.deep_sleep();
xous::return_scalar(msg.sender, 1).ok();
// system is shut down after this point, no code runs
}
_ => panic!("Incorrect PlatformOp"),
}
})
}
None => {
log::error!("couldn't convert opcode");
}