From ee9bec19a1e303ed71bb7760b8f4172f972ccf58 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 2 Apr 2026 03:47:15 +0800 Subject: [PATCH 1/7] fix key mappings and delay times --- .../bao1x-hal-service/src/servers/keyboard.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/services/bao1x-hal-service/src/servers/keyboard.rs b/services/bao1x-hal-service/src/servers/keyboard.rs index 4864c7c10..7d1a2158b 100644 --- a/services/bao1x-hal-service/src/servers/keyboard.rs +++ b/services/bao1x-hal-service/src/servers/keyboard.rs @@ -52,6 +52,7 @@ pub fn handler(_irq_no: usize, arg: *mut usize) { } #[cfg(feature = "board-baosec")] +#[derive(Debug)] struct KeypressTimestamp { pub kp: KeyPress, pub next_repeat_time: u64, @@ -64,7 +65,7 @@ struct KeyTracker { } #[cfg(feature = "board-baosec")] impl KeyTracker { - pub fn new() -> Self { KeyTracker { rate_ms: KEYUP_DELAY_MS as usize, delay_ms: 500, keys: Vec::new() } } + pub fn new() -> Self { KeyTracker { rate_ms: KEYUP_DELAY_MS as usize, delay_ms: 1000, keys: Vec::new() } } pub fn register_key_down(&mut self, key: KeyPress, ts: u64) { if let Some(entry) = self.keys.iter_mut().find(|e| e.kp == key) { @@ -90,8 +91,10 @@ impl KeyTracker { for key in keys { // search for key not in keys vector; create a new entry with next repeat equal // to now, so it gets emitted immediately - if !self.keys.iter().any(|e| e.kp == *key) { - self.keys.push(KeypressTimestamp { kp: *key, next_repeat_time: now }); + if *key != KeyPress::Invalid && *key != KeyPress::None { + if !self.keys.iter().any(|e| e.kp == *key) { + self.keys.push(KeypressTimestamp { kp: *key, next_repeat_time: now }); + } } } @@ -460,6 +463,7 @@ fn keyboard_service() { let mut kc: Vec = Vec::new(); let now = tt.elapsed_ms(); if now - last_key_event > KEYUP_DELAY_MS { + log::debug!("clearing key tracker"); key_tracker.clear_keys(); } last_key_event = now; @@ -468,9 +472,9 @@ fn keyboard_service() { if kpc_aoint.kpc.r(utra::dkpc::SFR_SR1) != 0 { let sr1 = unsafe { kpc_aoint.kpc.base().add(8).read_volatile() }; let key_down = bao1x_hal::board::kpc_sr1_to_key(sr1); - log::info!("{:?}", key_down); - key_tracker.register_key_down(key_down, now); - if key_down != KeyPress::Invalid { + log::debug!("{:?}", key_down); + if key_down != KeyPress::Invalid && key_down != KeyPress::None { + key_tracker.register_key_down(key_down, now); kc.push(map_keypress(key_down)) } } From 4f9584ec9e8f4ae3b55b19ea62a6f0d84e6f8984 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 2 Apr 2026 03:47:46 +0800 Subject: [PATCH 2/7] cleanup qr code capture behavior goes to blank screen after capture instead of previous ui state also cleans up transitions between camera and run mode --- services/bao-video/src/main.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 92d034e7b..dde9c2e24 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -444,7 +444,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(not(feature = "hosted-baosec"))] GfxOpcode::AcquireQr => { if qr_request.is_none() { - display.stash(); // save a copy of the UI + // display.stash(); // save a copy of the UI // this will defer response until later qr_request = msg_opt.take(); @@ -589,6 +589,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { None, None, ); + continue; } } @@ -624,10 +625,18 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Mono::White.into(), Mono::Black.into(), ); + display.draw().unwrap_or_else(|_| { + display_timeout_handler(&udma_global, &mut display) + }); // this take will cause the QR response to be routed to the sender since // the Message `Drop`s. It will also cause the sampling of the camera to // stop on the next frame. if let Some(mut envelope) = qr_request.take() { + /* + display.pop().unwrap_or_else(|_| { + display_timeout_handler(&udma_global, &mut display) + }) + */ let metadata = format!("{:?}", meta); #[cfg(not(feature = "hosted-baosec"))] if content.starts_with("test://") { @@ -646,11 +655,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ) }; response.replace(acquisition).unwrap(); - display.pop().unwrap_or_else(|_| { - display_timeout_handler(&udma_global, &mut display) - }); #[cfg(not(feature = "hosted-baosec"))] hal.set_preemption(true); + continue; } else { log::info!("meta: {:?}", meta); log::info!("************ {} ***********", content); From 13cdb9d64b71c71a4426f32a179be75aed6dba3b Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 2 Apr 2026 03:48:49 +0800 Subject: [PATCH 3/7] cleanup qr code rendering --- libs/ux-api/src/minigfx/handlers.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/libs/ux-api/src/minigfx/handlers.rs b/libs/ux-api/src/minigfx/handlers.rs index 99836533b..77e4f1b40 100644 --- a/libs/ux-api/src/minigfx/handlers.rs +++ b/libs/ux-api/src/minigfx/handlers.rs @@ -424,28 +424,23 @@ pub fn render_qr( let total_width = qr_render.width as isize + QUIET_MODULES * 2; let module_width = (window_width / total_width).max(1) as isize; let left_edge = if module_width * total_width < window_width { - (window_width - (module_width * total_width)) as isize / 2 + (window_width - module_width * total_width) as isize / 2 } else { 0 }; - const USE_TOP_LEFT: bool = false; - let tl = if USE_TOP_LEFT { qr_render.top_left } else { Point::new(0, 0) }; + let tl = qr_render.top_left; // draw a white background over the rendering area - let r = Rectangle::new_with_style( - Point::from((left_edge, tl.y)), - screen_clip.unwrap_or(screensize).br, - DrawStyle::new(PixelColor::Light, PixelColor::Light, 1), - ); + let mut r = screensize.clone(); + r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); op::rectangle(display, r, screen_clip.into(), false); // now raster in the modules - // TODO: on dense codes, the dark colors are over-sized by a pixel. Figure this out. - // TODO: on smaller than optimal codes, align the QR code into the center of the screen. let mut line_top_left = - Point::new(left_edge + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES); - let dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 0); + Point::new(left_edge + tl.x + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES); + let dark = DrawStyle { fill_color: Some(PixelColor::Dark), stroke_color: None, stroke_width: 0 }; + // log::info!("left edge: {} module width: {}", left_edge, module_width); for line in qr_render.modules.chunks(qr_render.width) { for (i, &module) in line.iter().enumerate() { if module { From 220124a483e466da9df8caca7d0eb10dd794b392 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 2 Apr 2026 03:49:08 +0800 Subject: [PATCH 4/7] fix left/right mapping swap --- libs/bao1x-hal/src/board/baosec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/bao1x-hal/src/board/baosec.rs b/libs/bao1x-hal/src/board/baosec.rs index 827706a73..47061cfea 100644 --- a/libs/bao1x-hal/src/board/baosec.rs +++ b/libs/bao1x-hal/src/board/baosec.rs @@ -461,8 +461,8 @@ pub fn kpc_sr0_to_key(raw_event: u32) -> [KeyPress; 4] { 0 => KeyPress::Down, 1 => KeyPress::Select, 2 => KeyPress::Up, - 4 => KeyPress::Right, - 5 => KeyPress::Left, + 4 => KeyPress::Left, + 5 => KeyPress::Right, 6 => KeyPress::Center, #[cfg(feature = "accel-kbd")] 9 => KeyPress::Accel, @@ -489,8 +489,8 @@ pub fn kpc_sr1_to_key(raw_event: u32) -> KeyPress { 0x10 => KeyPress::Down, 0x11 => KeyPress::Select, 0x12 => KeyPress::Up, - 0x14 => KeyPress::Right, - 0x15 => KeyPress::Left, + 0x14 => KeyPress::Left, + 0x15 => KeyPress::Right, 0x16 => KeyPress::Center, #[cfg(feature = "accel-kbd")] 0x19 => KeyPress::Accel, From 19b83a86505bdccdfe00f49e4521bd83d4baa473 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 01:52:57 +0800 Subject: [PATCH 5/7] fix offset in camera driver --- libs/bao1x-hal/src/gc2145/gc2145.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/bao1x-hal/src/gc2145/gc2145.rs b/libs/bao1x-hal/src/gc2145/gc2145.rs index 8440e4143..2a4c921c3 100644 --- a/libs/bao1x-hal/src/gc2145/gc2145.rs +++ b/libs/bao1x-hal/src/gc2145/gc2145.rs @@ -239,7 +239,10 @@ impl Gc2145 { } /// TODO: figure out how to length-bound this to...the frame slice size? line size? idk... - pub fn rx_buf(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_slice() } + /// The offset on the slice is a "cheater" parameter which is empirically calibrated based on + /// observed data. I don't know the underlying cause of it, but I suspect it probably has to + /// do with data in the pipeline that's not flushed, so the first three elements are "stale" + pub fn rx_buf(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_slice()[3..] } /// TODO: figure out how to length-bound this to...the frame slice size? line size? idk... pub unsafe fn rx_buf_phys(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_phys_slice() } From 2f9a7cd723a1e821fc91d52152d70637b646a738 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 01:53:13 +0800 Subject: [PATCH 6/7] reset the udma block for the camera on every frame improves stability across multiple captures --- services/bao-video/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index dde9c2e24..96632db9e 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -444,6 +444,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(not(feature = "hosted-baosec"))] GfxOpcode::AcquireQr => { if qr_request.is_none() { + // reset camera UDMA block + udma_global.reset(PeriphId::Cam); + // display.stash(); // save a copy of the UI // this will defer response until later qr_request = msg_opt.take(); From 979d00dedf5444ba4d6169d4ccd6dee0bc2d3d09 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 23:00:35 +0800 Subject: [PATCH 7/7] attempt to debug the wfi/bio clock conundrum the commited code works, but there's something weird going on with the clock setting math that indicates i'm not understanding exactly what's going on. Need to move on and solve other problems, but this will be an outstanding issue. --- libs/bao1x-hal/src/clocks.rs | 77 +++++++++++++++++++ .../bao1x-hal-service/src/servers/susres.rs | 35 ++++++++- 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/libs/bao1x-hal/src/clocks.rs b/libs/bao1x-hal/src/clocks.rs index 47c9a6017..dab5bb9a0 100644 --- a/libs/bao1x-hal/src/clocks.rs +++ b/libs/bao1x-hal/src/clocks.rs @@ -2,6 +2,8 @@ use bao1x_api::find_pll_params; #[cfg(all(feature = "std", feature = "board-baosec"))] use bao1x_api::{IoxHal, IoxPort}; use bitbybit::bitfield; +#[cfg(not(feature = "std"))] +use num_traits::float::FloatCore; use utralib::*; #[cfg(all(feature = "board-baosec", not(feature = "oem-baosec-lite")))] @@ -270,6 +272,31 @@ pub fn divide_by_fd(fd: u32, in_freq_hz: u32) -> u32 { out_freq_khz * 1_000 } +/// Returns the floating point number to multiply the VCO frequency by to get the +/// resulting frequency. i.e. if the FCO is at 700 MHz, the net frequency is +/// equal to `(700_000_000f32 * fd_to_ratio()) as u32` +pub fn fd_to_ratio(fd: u32) -> f32 { (fd as f32 + 1.0) / (FD_MAX as f32) } + +/// when `round_freq_up` is `true`, the resulting divider will result in a frequency that is +/// rounded up from the target ratio, if it can't be perfectly achieved. +/// +/// Otherwise, the result will round to the next lowest frequency that can be achieved. +/// +/// Note that any divide ratio > 1.0 will result in a divide ration of 1.0: this is a +/// *divide* ratio, not a multiplier. It can only strictly result in frequency that are lower +/// than the input. +/// +/// This will never return a result that causes the clock to stop. So any divisors smaller +/// than 1/128 will result in a divisor of 1/128. +pub fn divide_ratio_to_fd(ratio: f32, round_freq_up: bool) -> u8 { + let target_div = ((FD_MAX as f32 * ratio) - 1.0).max(1.0); + if round_freq_up { + (target_div.floor() as u32).min(255) as u8 + } else { + (target_div.ceil() as u32).min(255) as u8 + } +} + #[cfg(feature = "std")] pub struct ClockManagerImpl { pub vco_freq: u32, @@ -414,6 +441,56 @@ impl ClockManagerImpl { self.perclk = divide_by_fd(perclk_fd, pll0_freq) / 2; } + pub fn fclk_fd(&self) -> u8 { (self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & 0xFF) as u8 } + + pub fn aclk_fd(&self) -> u8 { (self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & 0xFF) as u8 } + + pub fn hclk_fd(&self) -> u8 { (self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & 0xFF) as u8 } + + pub fn iclk_fd(&self) -> u8 { (self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3) & 0xFF) as u8 } + + pub fn pclk_fd(&self) -> u8 { (self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4) & 0xFF) as u8 } + + pub fn set_fclk_fd(&mut self, fd: u8) { + self.sysctrl.wo( + utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, + self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & !0xFF | fd as u32, + ); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); + } + + pub fn set_aclk_fd(&mut self, fd: u8) { + self.sysctrl.wo( + utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1, + self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & !0xFF | fd as u32, + ); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); + } + + pub fn set_hclk_fd(&mut self, fd: u8) { + self.sysctrl.wo( + utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2, + self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & !0xFF | fd as u32, + ); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); + } + + pub fn set_iclk_fd(&mut self, fd: u8) { + self.sysctrl.wo( + utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3, + self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3) & !0xFF | fd as u32, + ); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); + } + + pub fn set_pclk_fd(&mut self, fd: u8) { + self.sysctrl.wo( + utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4, + self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4) & !0xFF | fd as u32, + ); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); + } + /// Safety: this pulls out the hardware base for the susres block. The receiver of this /// address must manually manage all concurrency issues that could happen with respect to /// using the susres block. diff --git a/services/bao1x-hal-service/src/servers/susres.rs b/services/bao1x-hal-service/src/servers/susres.rs index a68520617..f21e4936a 100644 --- a/services/bao1x-hal-service/src/servers/susres.rs +++ b/services/bao1x-hal-service/src/servers/susres.rs @@ -180,15 +180,48 @@ fn susres_service() { use bao1x_api::bio::BioApi; let mut bio = bao1x_hal::bio::Bio::new(); + // hard-coded to the value of the external crystal - this is a hardware // reference value that should never change; eg, USB doesn't work if this // isn't 48 MHz. - bio.update_bio_freq(48_000_000); + let fclk_fd_backup = clk_mgr.fclk_fd(); + + // TODO: this is a hack to get past some issue with clock rate changes + // + // In theory: + // - the set_clk_fd(0xff) call should reset the fclk divider to 1:1 + // - this would mean the bio_freq is getting 48MHz + // In practice: + // - the fd seems "stuck" at /2 for fd settings from 0xff-0x3f + // - so the pulse width outputs on BIO is the same for 0xff, 0x7f, 0x3f + // - if I reduce reduce fd setting to 0x1f, then, I can see the clock rate is 1/8th + // What has been tried: + // - Forcing the clocks to a fixed 0xFF setting regardless of input - I can see that + // BIO clocks, after wfi, are incorrect in this case. Which means that set_fclk_fd + // is "getting through" + // - Adding wait states, etc. around the CGUSET call does not seem to change the + // situation + // + // The compromise for now is to detect the "fast_bio" setting at init by heuristically + // looking at the fd_backup field, and then specifying an offset frequency to the + // update_bio_freq that results in the correct waveform. + // + // Possible cause: + // - 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... + if fclk_fd_backup == 0xff { + bio.update_bio_freq(48_000_000); + } else { + bio.update_bio_freq(24_000_000); + } + clk_mgr.set_fclk_fd(0xff); // set divider to 1:1 ratio clk_mgr.wfi(); // ~~~ time passes, but we're on carbonite so we don't notice ~~~ clk_mgr.restore_wfi(); + clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD before restoring potentially fast PLL setting bio.update_bio_freq(clk_mgr.fclk); // when wfi() returns, it means we've resumed