From cfe9b3c8458006c16eb4cc87e6644c2bd7201e10 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 01:52:25 +0800 Subject: [PATCH] modify base API for GlobalConfig to auto-infer the API address the switch based on target is now absorbed into the new() function. This paves the way for us to add more CSR ranges into the global configuration object, i.e. IRQ csrs for I2C. --- libs/cramium-hal/src/udma/mod.rs | 29 +++++++++++++++++++++++++- loader/src/platform/cramium/cramium.rs | 8 +++---- loader/src/platform/cramium/swap.rs | 5 ++--- loader/src/platform/cramium/update.rs | 6 ++---- services/cram-hal-service/src/main.rs | 9 +------- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 7185e7c6c..3bd8b60ad 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -37,9 +37,36 @@ impl Into for GlobalReg { pub struct GlobalConfig { csr: SharedCsr, + i2c_irq: SharedCsr, } impl GlobalConfig { - pub fn new(base_addr: *mut u32) -> Self { GlobalConfig { csr: SharedCsr::new(base_addr) } } + pub fn new() -> Self { + #[cfg(target_os = "xous")] + let base_addr = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::generated::HW_UDMA_CTRL_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map UDMA global control") + .as_mut_ptr() as *mut u32; + #[cfg(not(target_os = "xous"))] + let base_addr = utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32; + + #[cfg(target_os = "xous")] + let i2c_irq_addr = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::generated::HW_IRQARRAY7_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map I2C IRQ range") + .as_mut_ptr() as *mut u32; + #[cfg(not(target_os = "xous"))] + let i2c_irq_addr = utralib::generated::HW_IRQARRAY7_BASE as *mut u32; + + GlobalConfig { csr: SharedCsr::new(base_addr), i2c_irq: SharedCsr::new(i2c_irq_addr) } + } pub fn clock_on(&self, peripheral: PeriphId) { // Safety: only safe when used in the context of UDMA registers. diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs index 14537261d..09231c229 100644 --- a/loader/src/platform/cramium/cramium.rs +++ b/loader/src/platform/cramium/cramium.rs @@ -235,7 +235,7 @@ pub fn early_init() -> u32 { // Set up the UDMA_UART block to the correct baud rate and enable status #[allow(unused_mut)] // some configs require mut - let mut udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + let mut udma_global = GlobalConfig::new(); udma_global.clock_on(PeriphId::Uart1); udma_global.map_event( PeriphId::Uart1, @@ -409,7 +409,7 @@ pub fn early_init() -> u32 { // show the boot logo use ux_api::minigfx::FrameBuffer; - let mut udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + let mut udma_global = GlobalConfig::new(); let mut sh1107 = cramium_hal::sh1107::Oled128x128::new( cramium_hal::sh1107::MainThreadToken::new(), perclk, @@ -456,7 +456,7 @@ pub fn early_init() -> u32 { use crate::platform::cramium::qr; let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - let udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + let udma_global = GlobalConfig::new(); // setup camera pins let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_camera_pins(&iox); @@ -962,7 +962,7 @@ pub fn early_init() -> u32 { // setup the I/O pins let mut iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32); - let mut udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32); + let mut udma_global = GlobalConfig::new(); let channel = cramium_hal::board::setup_memory_pins(&iox); udma_global.clock_off(PeriphId::from(channel)); diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index 1b39a4f89..56af26be6 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -41,7 +41,7 @@ pub struct SwapHal { impl SwapHal { pub fn new(cfg: &BootConfig, perclk_freq: u32) -> Option { if let Some(swap) = cfg.swap { - let udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32); + let udma_global = GlobalConfig::new(); // setup the I/O pins let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32); @@ -413,8 +413,7 @@ pub fn userspace_maps(cfg: &mut BootConfig) { iox.set_gpio_dir(IoxPort::PD, 3, IoxDir::Output); // Set up the UDMA_UART block to the correct baud rate and enable status - let udma_global = - cramium_hal::udma::GlobalConfig::new(utralib::utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + let udma_global = cramium_hal::udma::GlobalConfig::new(); udma_global.clock_on(PeriphId::Uart0); udma_global.map_event( PeriphId::Uart0, diff --git a/loader/src/platform/cramium/update.rs b/loader/src/platform/cramium/update.rs index afb4691f8..614d38f55 100644 --- a/loader/src/platform/cramium/update.rs +++ b/loader/src/platform/cramium/update.rs @@ -110,7 +110,7 @@ pub fn process_update(perclk: u32) { crate::println!("waiting for button press"); let mut iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - let mut udma_global = udma::GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + let mut udma_global = udma::GlobalConfig::new(); let iox_kbd = iox.clone(); let mut sh1107 = cramium_hal::sh1107::Oled128x128::new( @@ -394,9 +394,7 @@ pub fn process_update(perclk: u32) { ); progress_bar(&mut sh1107, 0); - let udma_global = GlobalConfig::new( - utralib::generated::HW_UDMA_CTRL_BASE as *mut u32, - ); + let udma_global = GlobalConfig::new(); // setup the I/O pins let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32); diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index 759749148..41bce7648 100644 --- a/services/cram-hal-service/src/main.rs +++ b/services/cram-hal-service/src/main.rs @@ -185,14 +185,7 @@ fn main() { .expect("couldn't claim the IOX hardware page"); let iox = Iox::new(iox_page.as_ptr() as *mut u32); - let udma_global_csr = xous::syscall::map_memory( - xous::MemoryAddress::new(utralib::generated::HW_UDMA_CTRL_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map UDMA global control"); - let udma_global = GlobalConfig::new(udma_global_csr.as_mut_ptr() as *mut u32); + let udma_global = GlobalConfig::new(); // Note: the I2C handler can be put into a separate thread if we need the main // HAL server to not block while a large I2C transaction is being handled. For