diff --git a/libs/cramium-api/src/api.rs b/libs/cramium-api/src/api.rs index 3eed7e1a8..7d2a073a0 100644 --- a/libs/cramium-api/src/api.rs +++ b/libs/cramium-api/src/api.rs @@ -36,6 +36,8 @@ pub enum HalOpcode { ConfigureUdmaClock = 7, // blocking scalar ConfigureUdmaEvent = 8, + // blocking scalar + UdmaIrqStatusBits = 16, /// I2C operations I2c = 9, diff --git a/libs/cramium-api/src/i2c.rs b/libs/cramium-api/src/i2c.rs index dc6498e4c..ed8e9f6f7 100644 --- a/libs/cramium-api/src/i2c.rs +++ b/libs/cramium-api/src/i2c.rs @@ -1,5 +1,5 @@ pub trait I2cApi { - fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result; + fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result; /// initiate an i2c read. The read buffer is passed during the await. fn i2c_read( @@ -8,7 +8,7 @@ pub trait I2cApi { adr: u8, buf: &mut [u8], repeated_start: bool, - ) -> Result; + ) -> Result; } #[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] @@ -20,7 +20,7 @@ pub enum I2cTransactionType { } #[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum I2cResult { /// For the outbound message holder Pending, @@ -28,6 +28,8 @@ pub enum I2cResult { Ack(usize), /// An error occurred. Nack, + /// An unhandled error has occurred. + InternalError, } #[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] #[cfg(feature = "std")] diff --git a/libs/cramium-api/src/udma.rs b/libs/cramium-api/src/udma.rs index cd23c7a53..1e08f8180 100644 --- a/libs/cramium-api/src/udma.rs +++ b/libs/cramium-api/src/udma.rs @@ -233,6 +233,12 @@ pub enum EventChannel { Channel3 = 24, } +#[derive(Debug, Copy, Clone, num_derive::FromPrimitive)] +pub enum IrqBank { + I2c, + I2cErr, +} + /// Use a trait that will allow us to share code between both `std` and `no-std` implementations pub trait UdmaGlobalConfig { fn clock(&self, peripheral: PeriphId, enable: bool); @@ -243,4 +249,5 @@ pub trait UdmaGlobalConfig { to_channel: EventChannel, ); fn reset(&self, peripheral: PeriphId); + fn irq_status_bits(&self, bank: IrqBank) -> u32; } diff --git a/libs/cramium-hal/src/axp2101.rs b/libs/cramium-hal/src/axp2101.rs index 163331ad3..bc5e4ee29 100644 --- a/libs/cramium-hal/src/axp2101.rs +++ b/libs/cramium-hal/src/axp2101.rs @@ -213,6 +213,8 @@ impl Axp2101 { } } + pub fn get_dcdc(&mut self, which: WhichDcDc) -> (f32, bool) { self.dcdc_v_dvm[which as usize] } + pub fn set_ldo( &mut self, i2c: &mut dyn I2cApi, @@ -237,6 +239,10 @@ impl Axp2101 { } } + pub fn get_ldo(&mut self, which: WhichLdo) -> (f32, bool) { + (self.ldo_v[which as usize], self.ldo_ena[which as usize]) + } + fn encode_dcdc_ena(&self) -> u8 { let mut code = 0; for (i, &ena) in self.dcdc_ena.iter().enumerate() { diff --git a/libs/cramium-hal/src/udma/i2c.rs b/libs/cramium-hal/src/udma/i2c.rs index 264c3f3a5..1c03aab4b 100644 --- a/libs/cramium-hal/src/udma/i2c.rs +++ b/libs/cramium-hal/src/udma/i2c.rs @@ -263,6 +263,50 @@ impl<'a> I2c<'a> { xous::yield_slice(); } } + let nack_bits = self.udma_global.irq_status_bits(IrqBank::I2cErr); + match self.channel { + I2cChannel::Channel0 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel1 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel2 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel3 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + } + let ret = match self.pending.take() { I2cPending::Read(len) => { if let Some(buf) = rx_buf { @@ -458,13 +502,25 @@ impl I2cApi for I2c<'_> { adr: u8, buf: &mut [u8], repeated_start: bool, - ) -> Result { - self.i2c_read_async(dev, adr, buf.len(), repeated_start)?; - self.i2c_await(Some(buf), true) + ) -> Result { + match self.i2c_read_async(dev, adr, buf.len(), repeated_start) { + Err(_) => return Err(xous::Error::InternalError), + _ => (), + }; + match self.i2c_await(Some(buf), true) { + Ok(b) => Ok(I2cResult::Ack(b)), + _ => Ok(I2cResult::Nack), + } } - fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { - self.i2c_write_async(dev, adr, data)?; - self.i2c_await(None, true) + fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { + match self.i2c_write_async(dev, adr, data) { + Err(_) => return Err(xous::Error::InternalError), + _ => (), + }; + match self.i2c_await(None, true) { + Ok(b) => Ok(I2cResult::Ack(b)), + _ => Ok(I2cResult::Nack), + } } } diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 7185e7c6c..33fedc735 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -37,9 +37,53 @@ impl Into for GlobalReg { pub struct GlobalConfig { csr: SharedCsr, + i2c_irq: SharedCsr, + i2c_err_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; + + #[cfg(target_os = "xous")] + let i2c_err_irq_addr = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::generated::HW_IRQARRAY12_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_err_irq_addr = utralib::generated::HW_IRQARRAY12_BASE as *mut u32; + + GlobalConfig { + csr: SharedCsr::new(base_addr), + i2c_irq: SharedCsr::new(i2c_irq_addr), + i2c_err_irq: SharedCsr::new(i2c_err_irq_addr), + } + } pub fn clock_on(&self, peripheral: PeriphId) { // Safety: only safe when used in the context of UDMA registers. @@ -138,6 +182,21 @@ impl UdmaGlobalConfig for GlobalConfig { } fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); } + + fn irq_status_bits(&self, bank: IrqBank) -> u32 { + match bank { + IrqBank::I2c => { + let pending = self.i2c_irq.r(utralib::utra::irqarray7::EV_PENDING); + self.i2c_irq.wo(utralib::utra::irqarray7::EV_PENDING, pending); + pending + } + IrqBank::I2cErr => { + let pending = self.i2c_err_irq.r(utralib::utra::irqarray12::EV_PENDING); + self.i2c_err_irq.wo(utralib::utra::irqarray12::EV_PENDING, pending); + pending + } + } + } } // --------------------------------- DMA channel ------------------------------------ pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer 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/i2c_lib.rs b/services/cram-hal-service/src/i2c_lib.rs index af511538e..609b6f981 100644 --- a/services/cram-hal-service/src/i2c_lib.rs +++ b/services/cram-hal-service/src/i2c_lib.rs @@ -33,7 +33,7 @@ impl I2cApi for I2c { adr: u8, buf: &mut [u8], repeated_start: bool, - ) -> Result { + ) -> Result { let r = I2cTransaction { i2c_type: if repeated_start { I2cTransactionType::ReadRepeatedStart @@ -46,16 +46,14 @@ impl I2cApi for I2c { result: I2cResult::Pending, }; let result = self.i2c_transactions(I2cTransactions::from(vec![r]))?; - match result.transactions[0].result { - I2cResult::Ack(b) => { - buf.copy_from_slice(&result.transactions[0].data); - Ok(b) - } - _ => Err(xous::Error::InternalError), + if result.transactions[0].result == I2cResult::InternalError { + Err(xous::Error::InternalError) + } else { + Ok(result.transactions[0].result) } } - fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { + fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { let w = I2cTransaction { i2c_type: I2cTransactionType::Write, device: dev, @@ -64,9 +62,10 @@ impl I2cApi for I2c { result: I2cResult::Pending, }; let result = self.i2c_transactions(I2cTransactions::from(vec![w]))?; - match result.transactions[0].result { - I2cResult::Ack(b) => Ok(b), - _ => Err(xous::Error::InternalError), + if result.transactions[0].result == I2cResult::InternalError { + Err(xous::Error::InternalError) + } else { + Ok(result.transactions[0].result) } } } diff --git a/services/cram-hal-service/src/lib.rs b/services/cram-hal-service/src/lib.rs index 37082e724..2cda32d56 100644 --- a/services/cram-hal-service/src/lib.rs +++ b/services/cram-hal-service/src/lib.rs @@ -86,4 +86,20 @@ impl UdmaGlobalConfig for UdmaGlobal { } fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); } + + fn irq_status_bits(&self, bank: IrqBank) -> u32 { + match xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + HalOpcode::UdmaIrqStatusBits.to_usize().unwrap(), + bank as u32 as usize, + 0, + 0, + 0, + ), + ) { + Ok(xous::Result::Scalar5(_, value, _, _, _)) => value as u32, + _ => panic!("Unhandled response on irq_status_bits"), + } + } } diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index 759749148..1959fb8ba 100644 --- a/services/cram-hal-service/src/main.rs +++ b/services/cram-hal-service/src/main.rs @@ -5,6 +5,7 @@ use bitfield::*; use cramium_api::*; use cramium_hal::{iox::Iox, udma::GlobalConfig}; use num_traits::*; +use udma::UdmaGlobalConfig; use utralib::CSR; #[cfg(feature = "quantum-timer")] use utralib::utra; @@ -185,14 +186,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 @@ -533,6 +527,13 @@ fn main() { udma_global.map_event_with_offset(periph, event_offset, to_channel); } } + HalOpcode::UdmaIrqStatusBits => { + if let Some(scalar) = msg.body.scalar_message_mut() { + let bank = num_traits::FromPrimitive::from_usize(scalar.arg1) + .expect("Bad argument to UdmaIrqStatusBits"); + scalar.arg1 = udma_global.irq_status_bits(bank) as usize; + } + } HalOpcode::PeriphReset => { if let Some(scalar) = msg.body.scalar_message() { let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); @@ -548,7 +549,7 @@ fn main() { match transaction.i2c_type { I2cTransactionType::Write => { match i2c.i2c_write(transaction.device, transaction.address, &transaction.data) { - Ok(b) => transaction.result = I2cResult::Ack(b), + Ok(result) => transaction.result = result, _ => transaction.result = I2cResult::Nack, } } @@ -559,7 +560,7 @@ fn main() { &mut transaction.data, transaction.i2c_type == I2cTransactionType::ReadRepeatedStart, ) { - Ok(b) => transaction.result = I2cResult::Ack(b), + Ok(result) => transaction.result = result, _ => transaction.result = I2cResult::Nack, } }