Merge pull request #842 from betrusted-io/ui-fixes

UI fixes
This commit is contained in:
bunnie 2026-04-03 23:12:04 +08:00 committed by GitHub
commit 53dccf0df2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 150 additions and 28 deletions

View file

@ -461,8 +461,8 @@ pub fn kpc_sr0_to_key(raw_event: u32) -> [KeyPress; 4] {
0 => KeyPress::Down, 0 => KeyPress::Down,
1 => KeyPress::Select, 1 => KeyPress::Select,
2 => KeyPress::Up, 2 => KeyPress::Up,
4 => KeyPress::Right, 4 => KeyPress::Left,
5 => KeyPress::Left, 5 => KeyPress::Right,
6 => KeyPress::Center, 6 => KeyPress::Center,
#[cfg(feature = "accel-kbd")] #[cfg(feature = "accel-kbd")]
9 => KeyPress::Accel, 9 => KeyPress::Accel,
@ -489,8 +489,8 @@ pub fn kpc_sr1_to_key(raw_event: u32) -> KeyPress {
0x10 => KeyPress::Down, 0x10 => KeyPress::Down,
0x11 => KeyPress::Select, 0x11 => KeyPress::Select,
0x12 => KeyPress::Up, 0x12 => KeyPress::Up,
0x14 => KeyPress::Right, 0x14 => KeyPress::Left,
0x15 => KeyPress::Left, 0x15 => KeyPress::Right,
0x16 => KeyPress::Center, 0x16 => KeyPress::Center,
#[cfg(feature = "accel-kbd")] #[cfg(feature = "accel-kbd")]
0x19 => KeyPress::Accel, 0x19 => KeyPress::Accel,

View file

@ -2,6 +2,8 @@ use bao1x_api::find_pll_params;
#[cfg(all(feature = "std", feature = "board-baosec"))] #[cfg(all(feature = "std", feature = "board-baosec"))]
use bao1x_api::{IoxHal, IoxPort}; use bao1x_api::{IoxHal, IoxPort};
use bitbybit::bitfield; use bitbybit::bitfield;
#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;
use utralib::*; use utralib::*;
#[cfg(all(feature = "board-baosec", not(feature = "oem-baosec-lite")))] #[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 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")] #[cfg(feature = "std")]
pub struct ClockManagerImpl { pub struct ClockManagerImpl {
pub vco_freq: u32, pub vco_freq: u32,
@ -414,6 +441,56 @@ impl ClockManagerImpl {
self.perclk = divide_by_fd(perclk_fd, pll0_freq) / 2; 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 /// 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 /// address must manually manage all concurrency issues that could happen with respect to
/// using the susres block. /// using the susres block.

View file

@ -239,7 +239,10 @@ impl Gc2145 {
} }
/// TODO: figure out how to length-bound this to...the frame slice size? line size? idk... /// TODO: figure out how to length-bound this to...the frame slice size? line size? idk...
pub fn rx_buf<T: UdmaWidths>(&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<T: UdmaWidths>(&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... /// TODO: figure out how to length-bound this to...the frame slice size? line size? idk...
pub unsafe fn rx_buf_phys<T: UdmaWidths>(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_phys_slice() } pub unsafe fn rx_buf_phys<T: UdmaWidths>(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_phys_slice() }

View file

@ -424,28 +424,23 @@ pub fn render_qr<T: FrameBuffer>(
let total_width = qr_render.width as isize + QUIET_MODULES * 2; let total_width = qr_render.width as isize + QUIET_MODULES * 2;
let module_width = (window_width / total_width).max(1) as isize; let module_width = (window_width / total_width).max(1) as isize;
let left_edge = if module_width * total_width < window_width { 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 { } else {
0 0
}; };
const USE_TOP_LEFT: bool = false; let tl = qr_render.top_left;
let tl = if USE_TOP_LEFT { qr_render.top_left } else { Point::new(0, 0) };
// draw a white background over the rendering area // draw a white background over the rendering area
let r = Rectangle::new_with_style( let mut r = screensize.clone();
Point::from((left_edge, tl.y)), r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1);
screen_clip.unwrap_or(screensize).br,
DrawStyle::new(PixelColor::Light, PixelColor::Light, 1),
);
op::rectangle(display, r, screen_clip.into(), false); op::rectangle(display, r, screen_clip.into(), false);
// now raster in the modules // 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 = let mut line_top_left =
Point::new(left_edge + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES); Point::new(left_edge + tl.x + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES);
let dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 0); 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 line in qr_render.modules.chunks(qr_render.width) {
for (i, &module) in line.iter().enumerate() { for (i, &module) in line.iter().enumerate() {
if module { if module {

View file

@ -444,7 +444,10 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
GfxOpcode::AcquireQr => { GfxOpcode::AcquireQr => {
if qr_request.is_none() { if qr_request.is_none() {
display.stash(); // save a copy of the UI // reset camera UDMA block
udma_global.reset(PeriphId::Cam);
// display.stash(); // save a copy of the UI
// this will defer response until later // this will defer response until later
qr_request = msg_opt.take(); qr_request = msg_opt.take();
@ -589,6 +592,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
None, None,
None, None,
); );
continue;
} }
} }
@ -624,10 +628,18 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
Mono::White.into(), Mono::White.into(),
Mono::Black.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 // 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 // the Message `Drop`s. It will also cause the sampling of the camera to
// stop on the next frame. // stop on the next frame.
if let Some(mut envelope) = qr_request.take() { if let Some(mut envelope) = qr_request.take() {
/*
display.pop().unwrap_or_else(|_| {
display_timeout_handler(&udma_global, &mut display)
})
*/
let metadata = format!("{:?}", meta); let metadata = format!("{:?}", meta);
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
if content.starts_with("test://") { if content.starts_with("test://") {
@ -646,11 +658,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
) )
}; };
response.replace(acquisition).unwrap(); response.replace(acquisition).unwrap();
display.pop().unwrap_or_else(|_| {
display_timeout_handler(&udma_global, &mut display)
});
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
hal.set_preemption(true); hal.set_preemption(true);
continue;
} else { } else {
log::info!("meta: {:?}", meta); log::info!("meta: {:?}", meta);
log::info!("************ {} ***********", content); log::info!("************ {} ***********", content);

View file

@ -52,6 +52,7 @@ pub fn handler(_irq_no: usize, arg: *mut usize) {
} }
#[cfg(feature = "board-baosec")] #[cfg(feature = "board-baosec")]
#[derive(Debug)]
struct KeypressTimestamp { struct KeypressTimestamp {
pub kp: KeyPress, pub kp: KeyPress,
pub next_repeat_time: u64, pub next_repeat_time: u64,
@ -64,7 +65,7 @@ struct KeyTracker {
} }
#[cfg(feature = "board-baosec")] #[cfg(feature = "board-baosec")]
impl KeyTracker { 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) { pub fn register_key_down(&mut self, key: KeyPress, ts: u64) {
if let Some(entry) = self.keys.iter_mut().find(|e| e.kp == key) { if let Some(entry) = self.keys.iter_mut().find(|e| e.kp == key) {
@ -90,8 +91,10 @@ impl KeyTracker {
for key in keys { for key in keys {
// search for key not in keys vector; create a new entry with next repeat equal // search for key not in keys vector; create a new entry with next repeat equal
// to now, so it gets emitted immediately // to now, so it gets emitted immediately
if !self.keys.iter().any(|e| e.kp == *key) { if *key != KeyPress::Invalid && *key != KeyPress::None {
self.keys.push(KeypressTimestamp { kp: *key, next_repeat_time: now }); 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<char> = Vec::new(); let mut kc: Vec<char> = Vec::new();
let now = tt.elapsed_ms(); let now = tt.elapsed_ms();
if now - last_key_event > KEYUP_DELAY_MS { if now - last_key_event > KEYUP_DELAY_MS {
log::debug!("clearing key tracker");
key_tracker.clear_keys(); key_tracker.clear_keys();
} }
last_key_event = now; last_key_event = now;
@ -468,9 +472,9 @@ fn keyboard_service() {
if kpc_aoint.kpc.r(utra::dkpc::SFR_SR1) != 0 { if kpc_aoint.kpc.r(utra::dkpc::SFR_SR1) != 0 {
let sr1 = unsafe { kpc_aoint.kpc.base().add(8).read_volatile() }; let sr1 = unsafe { kpc_aoint.kpc.base().add(8).read_volatile() };
let key_down = bao1x_hal::board::kpc_sr1_to_key(sr1); let key_down = bao1x_hal::board::kpc_sr1_to_key(sr1);
log::info!("{:?}", key_down); log::debug!("{:?}", key_down);
key_tracker.register_key_down(key_down, now); if key_down != KeyPress::Invalid && key_down != KeyPress::None {
if key_down != KeyPress::Invalid { key_tracker.register_key_down(key_down, now);
kc.push(map_keypress(key_down)) kc.push(map_keypress(key_down))
} }
} }

View file

@ -180,15 +180,48 @@ fn susres_service() {
use bao1x_api::bio::BioApi; use bao1x_api::bio::BioApi;
let mut bio = bao1x_hal::bio::Bio::new(); let mut bio = bao1x_hal::bio::Bio::new();
// hard-coded to the value of the external crystal - this is a hardware // 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 // reference value that should never change; eg, USB doesn't work if this
// isn't 48 MHz. // 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(); clk_mgr.wfi();
// ~~~ time passes, but we're on carbonite so we don't notice ~~~ // ~~~ time passes, but we're on carbonite so we don't notice ~~~
clk_mgr.restore_wfi(); 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); bio.update_bio_freq(clk_mgr.fclk);
// when wfi() returns, it means we've resumed // when wfi() returns, it means we've resumed