From e14a7ced8f64e653176d5bba2f5e66a06d96f506 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 26 Jan 2024 22:51:00 +0800 Subject: [PATCH] pull is the refactor from the HAL --- services/cram-hal-service/src/api.rs | 10 ++++ services/cram-hal-service/src/iox_lib.rs | 3 +- services/cram-hal-service/src/lib.rs | 59 +++++++++++++++++++++++- services/cram-hal-service/src/main.rs | 37 ++++++++++++++- 4 files changed, 105 insertions(+), 4 deletions(-) diff --git a/services/cram-hal-service/src/api.rs b/services/cram-hal-service/src/api.rs index b3fe3ca0d..8b300bcbb 100644 --- a/services/cram-hal-service/src/api.rs +++ b/services/cram-hal-service/src/api.rs @@ -1,5 +1,9 @@ use cramium_hal::iox; +/// The Opcode numbers here should not be changed. You can add new ones, +/// but do not re-use old numbers or repurpose them. This is because the +/// numbers are hard-coded in other libraries in order to break circular +/// dependencies on this file. #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] #[repr(usize)] pub enum Opcode { @@ -29,6 +33,12 @@ pub enum Opcode { /// Return the value of a GPIO bank (blocking scalar) GetGpioBank = 6, + /// Configure UDMA clocks & events + // blocking scalar + ConfigureUdmaClock = 7, + // blocking scalar + ConfigureUdmaEvent = 8, + /// Exit server Quit = 255, diff --git a/services/cram-hal-service/src/iox_lib.rs b/services/cram-hal-service/src/iox_lib.rs index d1553af73..39c414210 100644 --- a/services/cram-hal-service/src/iox_lib.rs +++ b/services/cram-hal-service/src/iox_lib.rs @@ -1,3 +1,5 @@ +use core::sync::atomic::Ordering; + use cramium_hal::iox::{IoxDir, IoxDriveStrength, IoxEnable, IoxFunction, IoxPort, IoxValue}; use num_traits::*; @@ -136,7 +138,6 @@ impl IoxHal { } } -use core::sync::atomic::Ordering; impl Drop for IoxHal { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the diff --git a/services/cram-hal-service/src/lib.rs b/services/cram-hal-service/src/lib.rs index 979451529..41e050e6e 100644 --- a/services/cram-hal-service/src/lib.rs +++ b/services/cram-hal-service/src/lib.rs @@ -1,9 +1,64 @@ pub mod api; -pub mod ifram_lib; pub mod iox_lib; pub use api::*; -pub use ifram_lib::*; +use cramium_hal::udma::{EventChannel, PeriphEventType, PeriphId}; pub use iox_lib::*; +use num_traits::*; +/// Do not change this constant, it is hard-coded into libraries in order to break +/// circular dependencies on the IFRAM block. pub const SERVER_NAME_CRAM_HAL: &str = "_Cramium-SoC HAL_"; + +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); + +pub struct UdmaGlobal { + conn: xous::CID, +} + +impl UdmaGlobal { + pub fn new() -> Self { + REFCOUNT.fetch_add(1, Ordering::Relaxed); + let xns = xous_api_names::XousNames::new().unwrap(); + let conn = + xns.request_connection(SERVER_NAME_CRAM_HAL).expect("Couldn't connect to Cramium HAL server"); + UdmaGlobal { conn } + } + + pub fn udma_clock_config(&self, peripheral: PeriphId, enable: bool) { + xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + Opcode::ConfigureUdmaClock.to_usize().unwrap(), + peripheral as u32 as usize, + if enable { 1 } else { 0 }, + 0, + 0, + ), + ) + .expect("Couldn't setup UDMA clock"); + } + + /// Safety: this event does no checking if an event has been previously mapped. It is up + /// to the caller to ensure that no events are being stomped on. + pub unsafe fn udma_event_map( + &self, + peripheral: PeriphId, + event_type: PeriphEventType, + to_channel: EventChannel, + ) { + let et_u32: u32 = event_type.into(); + xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + Opcode::ConfigureUdmaEvent.to_usize().unwrap(), + peripheral as u32 as usize, + et_u32 as usize, + to_channel as u32 as usize, + 0, + ), + ) + .expect("Couldn't setup UDMA event mapping"); + } +} diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index 5fcfa1369..cf602f2d8 100644 --- a/services/cram-hal-service/src/main.rs +++ b/services/cram-hal-service/src/main.rs @@ -2,7 +2,10 @@ mod api; use api::*; #[cfg(feature = "pio")] mod pio; -use cramium_hal::iox; +use cramium_hal::{ + iox, + udma::{EventChannel, GlobalConfig, PeriphEventId, PeriphEventType, PeriphId}, +}; use xous::sender::Sender; fn try_alloc(ifram_allocs: &mut Vec>, size: usize, sender: Sender) -> Option { @@ -80,6 +83,15 @@ fn main() { .expect("couldn't claim the IOX hardware page"); let mut iox = iox::Iox::new(iox_page.as_mut_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 mut udma_global = GlobalConfig::new(udma_global_csr.as_mut_ptr() as *mut u32); + let mut msg_opt = None; log::debug!("Starting main loop"); loop { @@ -203,6 +215,29 @@ fn main() { scalar.arg1 = iox.get_gpio_bank(port) as usize; } } + Opcode::ConfigureUdmaClock => { + if let Some(scalar) = msg.body.scalar_message() { + let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let enable = if scalar.arg2 != 0 { true } else { false }; + if enable { + udma_global.clock_on(periph); + } else { + udma_global.clock_off(periph); + } + } + } + Opcode::ConfigureUdmaEvent => { + if let Some(scalar) = msg.body.scalar_message() { + let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let event_offset = scalar.arg2 as u32; + let to_channel: EventChannel = + num_traits::FromPrimitive::from_usize(scalar.arg3).unwrap(); + // note: no "air traffic control" is done to prevent mapping other + // events. Maybe this should be done? but for now, let's leave it + // as bare iron. + udma_global.map_event_with_offset(periph, event_offset, to_channel); + } + } Opcode::InvalidCall => { log::error!("Invalid opcode received: {:?}", msg); }