diff --git a/services/cram-hal-service/src/api.rs b/services/cram-hal-service/src/api.rs index 0db7e6785..11a4f543a 100644 --- a/services/cram-hal-service/src/api.rs +++ b/services/cram-hal-service/src/api.rs @@ -1,5 +1,5 @@ pub mod keyboard; -use cramium_hal::iox; +use cramium_hal::iox::{self, IoxPort, IoxValue}; pub use keyboard::*; /// The Opcode numbers here should not be changed. You can add new ones, @@ -47,6 +47,10 @@ pub enum Opcode { /// Peripheral reset PeriphReset = 10, + /// Configure Iox IRQ + ConfigureIoxIrq = 11, + IrqLocalHandler = 12, + /// Exit server Quit = 255, @@ -145,3 +149,12 @@ pub struct I2cTransactions { impl From> for I2cTransactions { fn from(value: Vec) -> Self { Self { transactions: value } } } + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct IoxIrqRegistration { + pub server: String, + pub opcode: usize, + pub port: IoxPort, + pub pin: u8, + pub active: IoxValue, +} diff --git a/services/cram-hal-service/src/iox_lib.rs b/services/cram-hal-service/src/iox_lib.rs index 3bc96992a..48389e289 100644 --- a/services/cram-hal-service/src/iox_lib.rs +++ b/services/cram-hal-service/src/iox_lib.rs @@ -1,11 +1,14 @@ use core::sync::atomic::Ordering; use cramium_hal::iox::{ - IoGpio, IoSetup, IoxDir, IoxDriveStrength, IoxEnable, IoxFunction, IoxPort, IoxValue, + IoGpio, IoIrq, IoSetup, IoxDir, IoxDriveStrength, IoxEnable, IoxFunction, IoxPort, IoxValue, }; use num_traits::*; -use crate::{Opcode, SERVER_NAME_CRAM_HAL, api::IoxConfigMessage}; +use crate::{ + Opcode, SERVER_NAME_CRAM_HAL, + api::{IoxConfigMessage, IoxIrqRegistration}, +}; pub struct IoxHal { conn: xous::CID, @@ -176,3 +179,11 @@ impl Drop for IoxHal { } } } + +impl IoIrq for IoxHal { + fn set_irq_pin(&self, port: IoxPort, pin: u8, active: IoxValue, server: &str, opcode: usize) { + let msg = IoxIrqRegistration { server: server.to_owned(), opcode, port, pin, active }; + let buf = xous_ipc::Buffer::into_buf(msg).unwrap(); + buf.lend(self.conn, Opcode::ConfigureIoxIrq.to_u32().unwrap()).expect("Couldn't set up IRQ"); + } +} diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index 91ff6fe27..f62a02365 100644 --- a/services/cram-hal-service/src/main.rs +++ b/services/cram-hal-service/src/main.rs @@ -2,17 +2,20 @@ mod api; mod hw; use api::*; +use bitfield::*; use cramium_hal::{ - iox, + iox::{self, IoxPort, IoxValue}, udma::{EventChannel, GlobalConfig, I2cApi, PeriphId}, }; +use num_traits::*; +use utralib::CSR; #[cfg(feature = "quantum-timer")] use utralib::utra; #[cfg(feature = "quantum-timer")] use utralib::*; #[cfg(feature = "swap")] use xous::SWAPPER_PID; -use xous::sender::Sender; +use xous::{ScalarMessage, sender::Sender}; #[cfg(feature = "pio")] use xous_pio::*; @@ -37,6 +40,57 @@ fn timer_tick(_irq_no: usize, arg: *mut usize) { ptimer.irq_csr.wo(utra::irqarray18::EV_PENDING, ptimer.irq_csr.r(utra::irqarray18::EV_PENDING)); } +#[repr(u32)] +#[allow(dead_code)] +enum IntMode { + RisingEdge = 0, + FallingEdge = 1, + HighLevel = 2, + LowLevel = 3, +} +bitfield! { + #[derive(Copy, Clone, PartialEq, Eq, Default)] + pub struct IntCr(u32); + impl Debug; + pub u32, select, set_select: 6, 0; + pub u32, mode, set_mode: 8, 7; + pub enable, set_enable: 9; + pub wakeup, set_wakeup: 10; +} +fn irq_select_from_port(port: IoxPort, pin: u8) -> u32 { (port as u32) * 16 + pin as u32 } +fn find_first_none(arr: &[Option]) -> Option { arr.iter().position(|item| item.is_none()) } + +#[derive(Debug, Copy, Clone)] +#[allow(dead_code)] +struct IrqLocalRegistration { + pub cid: xous::CID, + pub opcode: usize, + pub port: IoxPort, + pub pin: u8, + pub active: IoxValue, +} + +struct IrqHandler { + pub irq_csr: CSR, + pub cid: xous::CID, +} +fn iox_irq_handler(_irq_no: usize, arg: *mut usize) { + let handler = unsafe { &mut *(arg as *mut IrqHandler) }; + let pending = handler.irq_csr.r(utralib::utra::irqarray10::EV_PENDING); + handler.irq_csr.wo(utralib::utra::irqarray10::EV_PENDING, pending); + xous::try_send_message( + handler.cid, + xous::Message::Scalar(ScalarMessage::from_usize( + Opcode::IrqLocalHandler.to_usize().unwrap(), + pending as usize, + 0, + 0, + 0, + )), + ) + .ok(); +} + fn try_alloc(ifram_allocs: &mut Vec>, size: usize, sender: Sender) -> Option { let mut size_pages = size / 4096; if size % 4096 != 0 { @@ -94,6 +148,7 @@ fn main() { let xns = xous_names::XousNames::new().unwrap(); let sid = xns.register_name(cram_hal_service::SERVER_NAME_CRAM_HAL, None).expect("can't register server"); + let self_cid = xous::connect(sid).expect("couldn't create self-connection"); let mut ifram_allocs = [Vec::new(), Vec::new()]; // code is written assuming the IFRAM blocks have the same size. Since this is fixed in @@ -229,6 +284,28 @@ fn main() { } // -------------------- end timer workaround code + // ---- "own" the Iox IRQ bank. This might need revision once NTO aliasing is available. --- + let irq_page = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::utra::irqarray10::HW_IRQARRAY10_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't claim IRQ control page"); + let irq_csr = CSR::new(irq_page.as_mut_ptr() as *mut u32); + let mut irq = IrqHandler { irq_csr, cid: self_cid }; + xous::claim_interrupt( + utralib::utra::irqarray10::IRQARRAY10_IRQ, + iox_irq_handler, + &mut irq as *mut IrqHandler as *mut usize, + ) + .expect("couldn't claim Iox interrupt"); + irq.irq_csr.wo(utralib::utra::irqarray10::EV_PENDING, 0xFFFF_FFFF); + irq.irq_csr.wfo(utralib::utra::irqarray10::EV_ENABLE_IOXIRQ, 1); + // Up to 8 slots where we can populate interrupt mappings in the hardware + // The index of the array corresponds to the slot. + let mut irq_table: [Option; 8] = [None; 8]; + // start keyboard emulator service hw::keyboard::start_keyboard_service(); @@ -339,6 +416,89 @@ fn main() { iox.set_drive_strength(config.port, config.pin, s); } } + Opcode::ConfigureIoxIrq => { + let buf = + unsafe { xous_ipc::Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let registration = buf.to_original::().unwrap(); + log::info!("Got registration request: {:?}", registration); + if let Some(index) = find_first_none(&irq_table) { + // create the reverse-lookup registration + let local_conn = xns + .request_connection(®istration.server) + .expect("couldn't connect to IRQ registree"); + let local_reg = IrqLocalRegistration { + cid: local_conn, + opcode: registration.opcode, + port: registration.port, + pin: registration.pin, + active: registration.active, + }; + irq_table[index] = Some(local_reg); + + // now activate the hardware register + let select = irq_select_from_port(registration.port, registration.pin); + let mut int_cr = IntCr(0); + int_cr.set_select(select); + match registration.active { + IoxValue::Low => int_cr.set_mode(IntMode::FallingEdge as u32), + IoxValue::High => int_cr.set_mode(IntMode::RisingEdge as u32), + } + int_cr.set_enable(true); + // safety: the index and offset are mapped to the intended range because the index is + // bounded by the size of irq_table, and the offset comes from the generated header file. + log::debug!( + "writing {:x} to {:x} at index {}", + int_cr.0, + unsafe { + iox.csr.base().add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()).add(index) + as usize + }, + index + ); + unsafe { + iox.csr + .base() + .add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()) + .add(index) + .write_volatile(int_cr.0); + } + } else { + panic!("Ran out of Iox interrupt slots: maximum 8 available"); + } + } + Opcode::IrqLocalHandler => { + // Figure out which port(s) caused the IRQ + let irq_flag = iox.csr.r(utralib::utra::iox::SFR_INTFR); + // clear the set bit by writing it back + iox.csr.wo(utralib::utra::iox::SFR_INTFR, irq_flag); + let mut found = false; + for bitpos in 0..8 { + // the bit position is flipped versus register order in memory + if ((irq_flag << (bitpos as u32)) & 0x80) != 0 { + if let Some(local_reg) = irq_table[bitpos] { + found = true; + // interrupts are "Best effort" and can gracefully fail if the receiver has been + // overwhelmed by too many interrupts + xous::try_send_message( + local_reg.cid, + xous::Message::new_scalar(local_reg.opcode, 0, 0, 0, 0), + ) + .ok(); + } else { + log::warn!( + "Got IRQ on position {} but no registration was found, ignoring!", + bitpos + ); + } + } + } + if !found { + log::warn!( + "No handler was found for raw flag: {:x} (note bit order is reversed)", + irq_flag + ); + } + } Opcode::SetGpioBank => { if let Some(scalar) = msg.body.scalar_message() { let port: cramium_hal::iox::IoxPort =