diff --git a/Cargo.lock b/Cargo.lock index 5b659fd..6f28692 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -952,7 +952,7 @@ dependencies = [ [[package]] name = "guac" -version = "0.3.1" +version = "0.3.2" dependencies = [ "atomic", "bbqueue", @@ -978,7 +978,7 @@ dependencies = [ [[package]] name = "guac-cli" -version = "0.3.1" +version = "0.3.2" dependencies = [ "async-hid", "clap", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index c81bc46..d4fc00a 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "guac-cli" -version = "0.3.1" +version = "0.3.2" edition = "2024" [dependencies] diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 270ba98..5fc55c6 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "guac" -version = "0.3.1" +version = "0.3.2" edition = "2024" [features] diff --git a/firmware/src/config.rs b/firmware/src/config.rs index 2c23fff..b2a8adf 100644 --- a/firmware/src/config.rs +++ b/firmware/src/config.rs @@ -48,6 +48,73 @@ fn slot_address(index: usize) -> usize { config_base() + CONFIG_RESERVED + index * SLOT_SIZE } +// The spare page in the reserved area, used to carry a boot request across a reset. +const BOOT_REQUEST_MAGIC: u32 = u32::from_le_bytes(*b"BOOT"); + +fn boot_request_address() -> usize { + config_base() + FLASH_PAGE_SIZE +} + +/// Writes a boot request and resets. The next boot hands over to the ROM before configuring +/// anything, which is the only way it gets a chip that is still in its reset state. +#[cfg(feature = "hid")] +pub fn request_bootloader(state: &mut FlashState) -> Result<(), ()> { + defmt::info!("config: requesting bootloader"); + + cortex_m::interrupt::free(|_| { + slow_clock_for_flash(); + + let address = boot_request_address(); + let mut page = [0xffu8; FLASH_PAGE_SIZE]; + page[..4].copy_from_slice(&BOOT_REQUEST_MAGIC.to_le_bytes()); + + if state.flash.erase_page(address / FLASH_PAGE_SIZE).is_err() { + defmt::error!("config: boot request erase failed"); + return Err(()); + } + + if state + .flash + .write_native(address, GenericArray::from_slice(&page)) + .is_err() + { + defmt::error!("config: boot request write failed"); + return Err(()); + } + + Ok(()) + })?; + + // Let the probe client drain RTT before the reset tears it down. + cortex_m::asm::delay(15_000_000); + + cortex_m::peripheral::SCB::sys_reset() +} + +/// Consumes a pending boot request, clearing it first so a hand-off that fails to enter ISP cannot +/// cause a boot loop. +pub fn take_bootloader_request(flash: &mut FlashGordon) -> bool { + slow_clock_for_flash(); + + let address = boot_request_address(); + let mut word = [0u8; FLASH_WORD_SIZE]; + + if read_flash(address, &mut word).is_err() { + return false; + } + + if u32::from_le_bytes([word[0], word[1], word[2], word[3]]) != BOOT_REQUEST_MAGIC { + return false; + } + + if flash.erase_page(address / FLASH_PAGE_SIZE).is_err() { + defmt::error!("config: boot request clear failed"); + } + + defmt::info!("config: boot request found"); + true +} + fn config_buf() -> &'static mut [u8; SLOT_SIZE] { // Safety: flash access is not reentrant and the buffer is only borrowed for one operation at a time. unsafe { &mut *core::ptr::addr_of_mut!(CONFIG_BUF) } diff --git a/firmware/src/hid_config.rs b/firmware/src/hid_config.rs index cd865db..6b30ced 100644 --- a/firmware/src/hid_config.rs +++ b/firmware/src/hid_config.rs @@ -242,7 +242,10 @@ impl CommandState { } Action::Bootloader => { ack_delay(); - crate::boot_to_bootloader(); + + if config::request_bootloader(flash).is_err() { + defmt::error!("boot request failed, staying in the application"); + } } Action::Commit => { ack_delay(); diff --git a/firmware/src/main.rs b/firmware/src/main.rs index c84a454..313ab61 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -218,17 +218,32 @@ static PERF: PerfCounters = PerfCounters { // fb: AtomicI32::new(0), }; -fn boot_to_bootloader() -> ! { - cortex_m::interrupt::disable(); +fn boot_to_isp() -> ! { + // Hand the ROM a chip that is still in its reset state, with the ISP pin held low. Peripheral + // resets are left alone: holding them asserted stops the ROM's own USB from coming up. + let mut syscon = unsafe { hal::Syscon::steal() }; + let mut iocon = unsafe { hal::Iocon::steal() }.enabled(&mut syscon); + let mut gpio = unsafe { hal::Gpio::steal() }.enabled(&mut syscon); + let _isp = unsafe { pins::Pio0_5::steal() } + .into_gpio_pin(&mut iocon, &mut gpio) + .into_output_low(); + unsafe { + (*pac::IOCON::ptr()) + .pio0_5 + .modify(|_, w| w.mode().pull_down()) + }; + + // And the boot error counter, in case the pin is not the input the ROM acts on. unsafe { let pmc = &*pac::PMC::ptr(); - // https://community.nxp.com/t5/LPC-Microcontrollers/Configure-LPC55-to-enter-re-invoke-ROM-Bootloader-from/m-p/966990?profile.language=en - // If you write the following register, it will be set to boot from the ROM: - // PMC->AOREG1 |= (0x0A << 16); //Set to boot from ROM - // NVIC_SystemReset(); //Reset‍‍ - pmc.aoreg1.modify(|r, w| w.bits(r.bits() | (0x0a << 16))); + pmc.aoreg1.modify(|r, w| w.bits(r.bits() | (0xa << 16))); } - cortex_m::peripheral::SCB::sys_reset() + + cortex_m::interrupt::disable(); + defmt::info!("bootloader: entering ROM from the reset state"); + cortex_m::asm::delay(15_000_000); + + unsafe { cortex_m::asm::bootload(0x0300_0000 as *const u32) } } fn build_telemetry_report, I>( @@ -838,6 +853,14 @@ fn main() -> ! { debug!("start"); + // A boot request is honoured before any peripheral or clock is configured beyond what reading + // flash requires, so the ROM is entered with the chip still in its reset state. + let mut flash = hal::FlashGordon::new(hal.flash.enabled(&mut syscon)); + + if config::take_bootloader_request(&mut flash) { + boot_to_isp(); + } + debug!("iocon"); let usb0_vbus_pin = pins::Pio0_22::take() .unwrap() @@ -888,8 +911,6 @@ fn main() -> ! { // Flash controller commands require a core clock of at most 100 MHz, so the config is read // before the system PLL takes the core to 150 MHz. - let flash = hal.flash.enabled(&mut syscon); - let mut flash = hal::FlashGordon::new(flash); defmt::info!("config: loading"); let (running_config, active_slot) = config::load(&mut flash); defmt::info!(