From 8521d698d78e7ac14b5e8d09109a5e865bd20d18 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 21 Apr 2026 16:27:10 +0800 Subject: [PATCH] add hooks for pre-clock transition on bio this hook allows the application logic to warn the BIO app that a clock transition is coming so that glitches can be suppressed. --- libs/bao1x-api/src/bio.rs | 5 +++++ libs/bao1x-hal/src/bio.rs | 8 ++++++++ libs/bao1x-hal/src/bio_hw.rs | 4 ++++ services/bao1x-hal-service/src/servers/bio.rs | 11 +++++++++++ services/bao1x-hal-service/src/servers/susres.rs | 2 ++ 5 files changed, 30 insertions(+) diff --git a/libs/bao1x-api/src/bio.rs b/libs/bao1x-api/src/bio.rs index e4c31bd6b..01234d36e 100644 --- a/libs/bao1x-api/src/bio.rs +++ b/libs/bao1x-api/src/bio.rs @@ -51,6 +51,10 @@ pub trait BioApi<'a> { /// that the clocks may have changed. fn update_bio_freq(&mut self, freq: u32) -> u32; + /// Called before frequency changes to activate any application-specific callbacks + /// to avoid glitches. + fn prep_freq_change(&mut self); + /// Returns the frequency, in Hz, of the incoming clock to the BIO cores. fn get_bio_freq(&self) -> u32; @@ -192,6 +196,7 @@ pub enum BioOp { InitCore, DeInitCore, UpdateBioFreq, + PrepFreqChange, GetCoreFreq, GetBioFreq, CoreState, diff --git a/libs/bao1x-hal/src/bio.rs b/libs/bao1x-hal/src/bio.rs index c23386102..40dbce042 100644 --- a/libs/bao1x-hal/src/bio.rs +++ b/libs/bao1x-hal/src/bio.rs @@ -134,6 +134,14 @@ impl<'a> BioApi<'a> for Bio { } } + fn prep_freq_change(&mut self) { + send_message( + self.conn, + Message::new_blocking_scalar(BioOp::PrepFreqChange.to_usize().unwrap(), 0, 0, 0, 0), + ) + .ok(); + } + fn get_bio_freq(&self) -> u32 { match send_message( self.conn, diff --git a/libs/bao1x-hal/src/bio_hw.rs b/libs/bao1x-hal/src/bio_hw.rs index fca4086ef..d6c4fc69d 100644 --- a/libs/bao1x-hal/src/bio_hw.rs +++ b/libs/bao1x-hal/src/bio_hw.rs @@ -502,6 +502,10 @@ impl<'a> BioApi<'a> for BioSharedState { prev_freq } + fn prep_freq_change(&mut self) { + // at the hw level, there's nothing to do; a handler at the `std` level is invoked instead + } + unsafe fn get_core_handle(&self, _fifo: Fifo) -> Result, BioError> { unimplemented!("This is managed by the main loop server, not the hardware interface"); } diff --git a/services/bao1x-hal-service/src/servers/bio.rs b/services/bao1x-hal-service/src/servers/bio.rs index 59573599c..77f46d374 100644 --- a/services/bao1x-hal-service/src/servers/bio.rs +++ b/services/bao1x-hal-service/src/servers/bio.rs @@ -15,6 +15,9 @@ fn bio_service(clk_freq: u32) { let mut resource_tracker = ResourceTracker::new(); + #[cfg(feature = "oem-baosec-lite")] + let led_conn = xns.request_connection_blocking("_oem_led_").unwrap(); + let mut bio_ss = bio_hw::BioSharedState::new(clk_freq); let mut msg_opt = None; loop { @@ -125,6 +128,14 @@ fn bio_service(clk_freq: u32) { } } + BioOp::PrepFreqChange => { + if let Some(_scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + // pause rendering while the clock transitions, avoiding 'bright glitches' + #[cfg(feature = "oem-baosec-lite")] + xous::send_message(led_conn, xous::Message::new_blocking_scalar(128, 0, 0, 0, 0)).ok(); + } + } + BioOp::GetBioFreq => { if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { scalar.arg1 = bio_ss.get_bio_freq() as usize; diff --git a/services/bao1x-hal-service/src/servers/susres.rs b/services/bao1x-hal-service/src/servers/susres.rs index fe7568bed..174cfad13 100644 --- a/services/bao1x-hal-service/src/servers/susres.rs +++ b/services/bao1x-hal-service/src/servers/susres.rs @@ -210,6 +210,7 @@ fn susres_service() { // - The update_bio_freq routine is being "too clever" and compensating for the fd // setting. But it seems like the routine just takes the putative fclk setting as // the argument to update_bio_freq(), I don't see any compensation happening... + bio.prep_freq_change(); if fclk_fd_backup == 0xff { bio.update_bio_freq(48_000_000); } else { @@ -222,6 +223,7 @@ fn susres_service() { // clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD before restoring potentially // fast PLL setting + bio.prep_freq_change(); clk_mgr.restore_wfi(); bio.update_bio_freq(clk_mgr.fclk);