From 406e39a0d5a9d5fbfb6da091beefddabbf89b780 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Nov 2025 20:09:12 +0800 Subject: [PATCH 1/2] fix timebase of delays in loader should be tied to the system clock frequency --- loader/src/lib.rs | 2 +- loader/src/platform/bao1x/bao1x.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/loader/src/lib.rs b/loader/src/lib.rs index 1ff422382..15a41f17d 100644 --- a/loader/src/lib.rs +++ b/loader/src/lib.rs @@ -54,4 +54,4 @@ pub const GUARD_MEMORY_BYTES: usize = 8 * crate::PAGE_SIZE; // 1 extra page for pub const SWAPPER_PID: u8 = 2; #[cfg(feature = "bao1x")] -pub const SYSTEM_CLOCK_FREQUENCY: u32 = 800_000_000; +pub const SYSTEM_CLOCK_FREQUENCY: u32 = bao1x_hal::board::DEFAULT_FCLK_FREQUENCY / 2; diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index 3db9516ad..a93ee4063 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -36,7 +36,8 @@ pub fn delay(quantum: usize) { use utralib::{CSR, utra}; // abuse the d11ctime timer to create some time-out like thing let mut d11c = CSR::new(utra::d11ctime::HW_D11CTIME_BASE as *mut u32); - d11c.wfo(utra::d11ctime::CONTROL_COUNT, 333_333); // 1.0ms per interval + // 1.0ms per interval + d11c.wfo(utra::d11ctime::CONTROL_COUNT, crate::SYSTEM_CLOCK_FREQUENCY / 1000); let mut polarity = d11c.rf(utra::d11ctime::HEARTBEAT_BEAT); for _ in 0..quantum { while polarity == d11c.rf(utra::d11ctime::HEARTBEAT_BEAT) {} From 3f60e487935ab4c32823ff65325f77590faf438d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Nov 2025 20:09:43 +0800 Subject: [PATCH 2/2] Fix 'boot' command not re-enumerating USB The "press button" path cleanly shuts down the USB stack before going into the next stage. "boot" does not; it missed the additional code added at the end of the main routine. Refactor the "boot" routine to be a function call now, so any future improvements to the boot process are used by both paths. --- bao1x-boot/boot1/src/main.rs | 5 +++++ bao1x-boot/boot1/src/repl.rs | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index e46a7c31e..b45d8280b 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -273,6 +273,10 @@ pub unsafe extern "C" fn rust_entry() -> ! { } } + boot(&iox, oled, se0_port, se0_pin) +} + +pub fn boot(iox: &Iox, mut oled: Option, se0_port: bao1x_api::IoxPort, se0_pin: u8) -> ! { if let Some(ref mut sh1107) = oled { marquee(sh1107, "Booting..."); } @@ -280,6 +284,7 @@ pub unsafe extern "C" fn rust_entry() -> ! { // stop the USB subsystem so it can be re-init'd by the next stage. // without this, USB init will hang later on. glue::shutdown(); + iox.set_gpio_dir(se0_port, se0_pin, bao1x_api::IoxDir::Output); iox.set_gpio_pin(se0_port, se0_pin, bao1x_api::IoxValue::Low); // put the USB port into SE0, so we re-enumerate with the OS stack // check that all pages in the SPI memory page cache have been written out diff --git a/bao1x-boot/boot1/src/repl.rs b/bao1x-boot/boot1/src/repl.rs index 8017f89c6..dec8cb71c 100644 --- a/bao1x-boot/boot1/src/repl.rs +++ b/bao1x-boot/boot1/src/repl.rs @@ -106,7 +106,25 @@ impl Repl { rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); } "boot" => { - crate::secboot::try_boot(true); + use bao1x_hal::iox::Iox; + let one_way = OneWayCounter::new(); + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + let (port, pin) = match one_way.get_decoded::() { + // the default map is baosec in boot1 + Ok(bao1x_api::BoardTypeCoding::Baosec) => bao1x_hal::board::setup_usb_pins(&iox), + // otherwise assume dabao mapping + _ => crate::setup_dabao_se0_pin(&iox), + }; + + // assert SE0 pin here. We add a delay even though crate:boot() calls this, because + // a button press initiated SE0 includes a certain minimum "low"; a direct serial command + // does not. + iox.set_gpio_pin(port, pin, IoxValue::Low); + crate::platform::delay(20); // minimum is 2.5ms + + // note: the SE0 pin is now asserted & configured as an output as it goes to the next stage + // it us up to the next USB stack to de-assert this. + crate::boot(&iox, None, port, pin); } "uf2" => { use base64::{Engine as _, engine::general_purpose};