From cfe9b3c8458006c16eb4cc87e6644c2bd7201e10 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 01:52:25 +0800 Subject: [PATCH 01/12] 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 From c24bb3a4505fd552e41d2e61f464dc1c742d5c2b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 02:42:25 +0800 Subject: [PATCH 02/12] add irq_status_bits() trait and enum for selecting Irq bank multiple IRQs related to the UDMA will eventually get mapped into the global IRQ handler for UDMA. Create an enum type that will help us differentiate this and add a trait that returns the raw bits for a given IRQ type. Probably a fancier wrapper should be made to interptet the IRQ bits but unfortunately there isn't a uniform pattern to the IRQ mappings, it's pretty much whatever fit into the IRQ space based on the underlying function. At the moment not even sure if this is the right register to be chasing, so we'll use this for probing that aspect and then get fancy later, maybe. --- libs/cramium-api/src/udma.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/cramium-api/src/udma.rs b/libs/cramium-api/src/udma.rs index cd23c7a53..b11550aad 100644 --- a/libs/cramium-api/src/udma.rs +++ b/libs/cramium-api/src/udma.rs @@ -233,6 +233,11 @@ pub enum EventChannel { Channel3 = 24, } +#[derive(Debug, Copy, Clone, num_derive::FromPrimitive)] +pub enum IrqBank { + I2c, +} + /// 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 +248,5 @@ pub trait UdmaGlobalConfig { to_channel: EventChannel, ); fn reset(&self, peripheral: PeriphId); + fn irq_status_bits(&self, bank: IrqBank) -> u32; } From c48c97ebdda92eeaafe036048cf952aca8aeb32b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 02:45:59 +0800 Subject: [PATCH 03/12] add the hal-side implementation this call works either on bare iron or in a xous context --- libs/cramium-hal/src/udma/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 3bd8b60ad..8cb6ca337 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -165,6 +165,12 @@ impl UdmaGlobalConfig for GlobalConfig { } fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); } + + fn irq_status_bits(&self, bank: IrqBank) -> u32 { + match bank { + IrqBank::I2c => self.i2c_irq.r(utralib::utra::irqarray7::EV_STATUS), + } + } } // --------------------------------- DMA channel ------------------------------------ pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer From 2c99b32e5afb301d1ef42a849ef26d377f20ecd7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 02:46:42 +0800 Subject: [PATCH 04/12] add an API opcode for messaging the cramium_hal_service in the Xous context, because the IRQ banks are owned by a global object, not every server can simply refer to the IRQ bits by just dereferencing a pointer. Instead, it needs to send a message to the cram_hal_service with an API number which then catches this and does the dereference on behalf of the caller. --- libs/cramium-api/src/api.rs | 2 ++ 1 file changed, 2 insertions(+) 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, From f3e7eec6cfb3dc930cdaa89505927ad88ba430cd Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 02:48:10 +0800 Subject: [PATCH 05/12] add 'lib' side code for sending the API call to cramium_hal_service this is the code that exists on the caller's side. It creates a message with the correct opcode and decodes the result. --- services/cram-hal-service/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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"), + } + } } From 76a45ff47dee470a6dc0b199899b131f59e20a1b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 02:48:58 +0800 Subject: [PATCH 06/12] add code to the server this is the actual handler inside the Xous service which, when receiving the opcode, dereferences its exclusive pointer to the IRQ bank and then returns the result to the caller. --- services/cram-hal-service/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index 41bce7648..bce8a2e0c 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; @@ -526,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(); From 73a171477277d1dc197c6de09e49b75a8948c148 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:46:14 +0800 Subject: [PATCH 07/12] Modify I2C API to accommodate Nack responses specifically --- libs/cramium-api/src/i2c.rs | 8 +++++--- libs/cramium-api/src/udma.rs | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) 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 b11550aad..1e08f8180 100644 --- a/libs/cramium-api/src/udma.rs +++ b/libs/cramium-api/src/udma.rs @@ -236,6 +236,7 @@ pub enum EventChannel { #[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 From 34ae2011b22f3e5a7e941133c8a6414714d79923 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:47:16 +0800 Subject: [PATCH 08/12] sidequest - add some accessors to axp2101 object --- libs/cramium-hal/src/axp2101.rs | 6 ++++++ 1 file changed, 6 insertions(+) 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() { From 09644f435a39b78af1e5ddd6a3fa0bdce670c3ee Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:54:10 +0800 Subject: [PATCH 09/12] add CSR bank for I2C error signals --- libs/cramium-hal/src/udma/mod.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 8cb6ca337..33fedc735 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -38,6 +38,7 @@ impl Into for GlobalReg { pub struct GlobalConfig { csr: SharedCsr, i2c_irq: SharedCsr, + i2c_err_irq: SharedCsr, } impl GlobalConfig { pub fn new() -> Self { @@ -65,7 +66,23 @@ impl GlobalConfig { #[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) } + #[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) { @@ -168,7 +185,16 @@ impl UdmaGlobalConfig for GlobalConfig { fn irq_status_bits(&self, bank: IrqBank) -> u32 { match bank { - IrqBank::I2c => self.i2c_irq.r(utralib::utra::irqarray7::EV_STATUS), + 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 + } } } } From 1abf0430054f0eaf29fa440c529f3881f379092f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:54:52 +0800 Subject: [PATCH 10/12] check NACK status in i2c_await routine; update bare metal API NACK bit is now checked after running the transaction, and if it is set an appropriate error code is returned. The trait implementation for i2c baremetal calls is also updated in this commit --- libs/cramium-hal/src/udma/i2c.rs | 68 +++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 6 deletions(-) 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), + } } } From 63c1d975b44dca0cf41eccafd25c808b5315fe90 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:56:29 +0800 Subject: [PATCH 11/12] modify result interpretation to allow i2c nacks --- services/cram-hal-service/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/cram-hal-service/src/main.rs b/services/cram-hal-service/src/main.rs index bce8a2e0c..1959fb8ba 100644 --- a/services/cram-hal-service/src/main.rs +++ b/services/cram-hal-service/src/main.rs @@ -549,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, } } @@ -560,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, } } From d17f9881b2269ff5270a055245613abd39847c38 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:56:51 +0800 Subject: [PATCH 12/12] modify user mode i2c lib calls to return nacks Note that a NACK is returned as an Ok(I2cResult::Nack) - this is because the Err() path is still reserved for "OS failure" errors. A Nack isn't a terminal failure, it is a recoverable fault, and so it's handled in the Ok() path. --- services/cram-hal-service/src/i2c_lib.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) 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) } } }