diff --git a/advanced/firmware/src/lib.rs b/advanced/firmware/src/lib.rs index 3446a8d..a55fec6 100644 --- a/advanced/firmware/src/lib.rs +++ b/advanced/firmware/src/lib.rs @@ -6,5 +6,15 @@ use panic_probe as _; // this prevents the panic message being printed *twice* when `defmt::panic` is invoked #[defmt::panic_handler] fn panic() -> ! { + unsafe { + // turn off the USB D+ pull-up before pausing the device with a breakpoint + // this disconnects the nRF device from the USB host so the USB host won't attempt further + // USB communication (and see an unresponsive device). probe-run will also reset the nRF's + // USBD peripheral when it sees the device in a halted state which has the same effect as + // this line but that can take a while and the USB host may issue a power cycle of the USB + // port / hub / root in the meantime, which can bring down the probe and break probe-run + const USBD_USBPULLUP: *mut u32 = 0x4002_7504 as *mut u32; + USBD_USBPULLUP.write_volatile(0) + } cortex_m::asm::udf() } diff --git a/boards/dk/src/lib.rs b/boards/dk/src/lib.rs index 3ece095..46b15b7 100644 --- a/boards/dk/src/lib.rs +++ b/boards/dk/src/lib.rs @@ -290,6 +290,16 @@ fn RTC0() { /// Exits the application and prints a backtrace when the program is executed through the `probe-run` /// Cargo runner pub fn exit() -> ! { + unsafe { + // turn off the USB D+ pull-up before pausing the device with a breakpoint + // this disconnects the nRF device from the USB host so the USB host won't attempt further + // USB communication (and see an unresponsive device). probe-run will also reset the nRF's + // USBD peripheral when it sees the device in a halted state which has the same effect as + // this line but that can take a while and the USB host may issue a power cycle of the USB + // port / hub / root in the meantime, which can bring down the probe and break probe-run + const USBD_USBPULLUP: *mut u32 = 0x4002_7504 as *mut u32; + USBD_USBPULLUP.write_volatile(0) + } defmt::println!("`dk::exit()` called; exiting ..."); // force any pending memory operation to complete before the BKPT instruction that follows atomic::compiler_fence(Ordering::SeqCst);