From da623430c608760d8995ebd01f695166b0dc8cd1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 30 Oct 2025 04:16:59 +0800 Subject: [PATCH] add debug routines for spim --- baremetal/Cargo.toml | 1 + baremetal/src/repl.rs | 102 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/baremetal/Cargo.toml b/baremetal/Cargo.toml index da64e17b2..6810c29b4 100644 --- a/baremetal/Cargo.toml +++ b/baremetal/Cargo.toml @@ -77,6 +77,7 @@ dabao-selftest = [ "xous-bio-bdma/bao1x", ] test-boot0-writeprotect = [] +spim-tests = [] verilator-only = [] default = ["bao1x-usb", "dabao-selftest"] diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index ef71f7b24..8f04c3fea 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -167,6 +167,106 @@ impl Repl { )); } } + #[cfg(feature = "spim-tests")] + "qe" => { + use bao1x_hal::board::SPIM_FLASH_IFRAM_ADDR; + use bao1x_hal::iox::Iox; + use bao1x_hal::udma::{GlobalConfig, Spim, SpimClkPha, SpimClkPol, SpimCs}; + + let udma_global = GlobalConfig::new(); + + // setup the I/O pins + let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32); + let channel = bao1x_hal::board::setup_memory_pins(&iox); + udma_global.clock_on(PeriphId::from(channel)); + + // safety: this is safe because clocks have been set up + let mut flash_spim = unsafe { + Spim::new_with_ifram( + channel, + // has to be half the clock frequency reaching the block, but run it as fast + // as we can run perclk + 100_000_000 / 4, + 100_000_000 / 2, + SpimClkPol::LeadingEdgeRise, + SpimClkPha::CaptureOnLeading, + SpimCs::Cs0, + 0, + 0, + None, + 16, // just enough space to send commands + 4096, + Some(6), + None, + bao1x_hal::ifram::IframRange::from_raw_parts( + SPIM_FLASH_IFRAM_ADDR, + SPIM_FLASH_IFRAM_ADDR, + 4096 * 2, + ), + ) + }; + flash_spim.mem_qpi_mode(false); + let status = flash_spim.flash_read_status_register(); + crate::println!("status register: {:x}", status); + flash_spim.flash_set_qe(); + let status = flash_spim.flash_read_status_register(); + crate::println!("status register after qe set: {:x}", status); + } + #[cfg(feature = "spim-tests")] + "check_qpi" => { + use bao1x_hal::board::SPIM_FLASH_IFRAM_ADDR; + use bao1x_hal::iox::Iox; + use bao1x_hal::udma::{GlobalConfig, Spim, SpimClkPha, SpimClkPol, SpimCs}; + let udma_global = GlobalConfig::new(); + + // setup the I/O pins + let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32); + let channel = bao1x_hal::board::setup_memory_pins(&iox); + udma_global.clock_on(PeriphId::from(channel)); + // safety: this is safe because clocks have been set up + let mut flash_spim = unsafe { + Spim::new_with_ifram( + channel, + // has to be half the clock frequency reaching the block, but run it as fast + // as we can run perclk + 100_000_000 / 4, + 100_000_000 / 2, + SpimClkPol::LeadingEdgeRise, + SpimClkPha::CaptureOnLeading, + SpimCs::Cs0, + 0, + 0, + None, + 16, // just enough space to send commands + 4096, + Some(6), + None, + bao1x_hal::ifram::IframRange::from_raw_parts( + SPIM_FLASH_IFRAM_ADDR, + SPIM_FLASH_IFRAM_ADDR, + 4096 * 2, + ), + ) + }; + flash_spim.mem_read_id_flash(); + // turn off QPI mode, in case it was set from a reboot in a bad state + flash_spim.mem_qpi_mode(false); + + // sanity check: read ID + let flash_id = flash_spim.mem_read_id_flash(); + crate::println!("flash ID (init): {:x}", flash_id); + flash_spim.mem_qpi_mode(true); + + // re-check the ID to confirm we entered QPI mode correctly + let flash_id = flash_spim.mem_read_id_flash(); + crate::println!("QPI flash ID: {:x}", flash_id); + flash_spim.mem_qpi_mode(false); + let flash_id = flash_spim.mem_read_id_flash(); + crate::println!("SPI flash ID: {:x}", flash_id); + flash_spim.mem_qpi_mode(true); + let flash_id = flash_spim.mem_read_id_flash(); + crate::println!("QPI flash ID: {:x}", flash_id); + } #[cfg(feature = "bao1x")] "rram" => { if args.len() == 2 || args.len() == 3 { @@ -1051,6 +1151,8 @@ impl Repl { crate::print!(", trngro, trngav"); #[cfg(feature = "dabao-selftest")] crate::print!(", dbtest"); + #[cfg(feature = "spim-tests")] + crate::print!(", qe, check_qpi"); crate::println!(""); } }