From 3993a0dc881f0ac8d850bb9f7c343a6486bcf651 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 30 Mar 2026 23:34:25 +0800 Subject: [PATCH] 'fix' for timeout issue on SPIM during display writes occassionally the hardware block just goes unresponsive. the 'fix' is to reset it, and then re-initialize the interface. I suspect this has to do with the UDMA silently dropping transactions when there's too much contention on the bus. --- libs/bao1x-hal/src/sh1107.rs | 63 +++++++++++++++++------------- libs/bao1x-hal/src/udma/spim.rs | 2 +- libs/ux-api/src/minigfx/mod.rs | 4 +- loader/src/platform/bao1x/bao1x.rs | 10 ++--- services/bao-video/src/main.rs | 47 ++++++++++++++++------ services/bao-video/src/panic.rs | 5 ++- 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/libs/bao1x-hal/src/sh1107.rs b/libs/bao1x-hal/src/sh1107.rs index a2fdda791..46acf7004 100644 --- a/libs/bao1x-hal/src/sh1107.rs +++ b/libs/bao1x-hal/src/sh1107.rs @@ -183,6 +183,7 @@ pub struct Oled128x128<'a> { powerdown: bool, power_port: IoxPort, power_pin: u8, + clk_div: u8, } impl<'a> Oled128x128<'a> { @@ -267,9 +268,19 @@ impl<'a> Oled128x128<'a> { power_port, power_pin, powerdown: false, + clk_div: ((perclk_freq / 2) / 2_000_000) as u8, } } + // used to recover a wedged SPI interface + pub fn reinit_spi(&mut self) { + self.spim.send_cmd_list(&[crate::udma::SpimCmd::Config( + SpimClkPol::LeadingEdgeRise, + SpimClkPha::CaptureOnLeading, + self.clk_div, + )]); + } + /// This should only be called to initialize the panic handler with its own /// copy of hardware registers. /// @@ -300,8 +311,9 @@ impl<'a> Oled128x128<'a> { Option, ), bool, + u8, ) { - (self.spim.into_raw_parts(), self.powerdown) + (self.spim.into_raw_parts(), self.powerdown, self.clk_div) } /// Creates a clone of the display handle. This is only safe if the handles are used in a @@ -331,6 +343,7 @@ impl<'a> Oled128x128<'a> { Option, ), bool, + u8, ), iox: &'a T, ) -> Self @@ -354,6 +367,7 @@ impl<'a> Oled128x128<'a> { command_set, ), powerdown, + clk_div, ) = display_parts; // compile them into a new object let mut spim = unsafe { @@ -398,6 +412,7 @@ impl<'a> Oled128x128<'a> { powerdown, power_pin, power_port, + clk_div, } } @@ -407,7 +422,7 @@ impl<'a> Oled128x128<'a> { pub fn screen_size(&self) -> Point { Point::new(WIDTH, LINES) } - pub fn redraw(&mut self) { self.draw(); } + pub fn redraw(&mut self) -> Result<(), xous::Error> { self.draw() } pub fn blit_screen(&mut self, bmp: &[u32]) { self.buffer.copy_from_slice(bmp); } @@ -417,21 +432,21 @@ impl<'a> Oled128x128<'a> { pub fn stash(&mut self) { self.stash.copy_from_slice(&self.buffer); } - pub fn pop(&mut self) { + pub fn pop(&mut self) -> Result<(), xous::Error> { self.buffer.copy_from_slice(&self.stash); - self.redraw(); + self.redraw() } fn set_data(&self) { self.iox.set_gpio_pin_value(self.cd_port, self.cd_pin, IoxValue::High); } fn set_command(&self) { self.iox.set_gpio_pin_value(self.cd_port, self.cd_pin, IoxValue::Low); } - pub fn send_command<'b, U>(&'b mut self, cmd: U) + pub fn send_command<'b, U>(&'b mut self, cmd: U) -> Result<(), xous::Error> where U: IntoIterator + 'b, { if self.powerdown { - return; + return Ok(()); } self.set_command(); let total_buf_len = self.buffer.len() * size_of::(); @@ -451,16 +466,12 @@ impl<'a> Oled128x128<'a> { .txrx_data_async_from_parts::(total_buf_len, len, true, false) .expect("Couldn't initiate oled command"); } - self.spim.txrx_await(false).unwrap_or_else(|_e| { - #[cfg(feature = "std")] - log::error!("txrx err {:?}", _e); - &[] - }); + self.spim.txrx_await(false).map(|_| ()) } - pub fn init(&mut self) { + pub fn init(&mut self) -> Result<(), xous::Error> { if self.powerdown { - return; + return Ok(()); } use Command::*; let init_sequence = [ @@ -483,17 +494,18 @@ impl<'a> Oled128x128<'a> { for command in init_sequence { let bytes = command.encode(); - self.send_command(bytes); + self.send_command(bytes)?; } // clear the frame buffer self.buffer_mut().fill(0xFFFF_FFFF); - self.draw(); + self.draw()?; let display_on = [DisplayOnOff(DisplayState::On)]; for command in display_on { let bytes = command.encode(); - self.send_command(bytes); + self.send_command(bytes)?; } + Ok(()) } pub fn powerdown(&mut self) { @@ -516,9 +528,9 @@ impl<'a> Oled128x128<'a> { self.powerdown = false; } - pub fn brightness(&mut self, level: u8) { + pub fn brightness(&mut self, level: u8) -> Result<(), xous::Error> { let bytes = Command::SetContrastControl(level).encode(); - self.send_command(bytes); + self.send_command(bytes) } #[cfg(feature = "std")] @@ -574,10 +586,10 @@ impl<'a> Oled128x128<'a> { impl<'a> FrameBuffer for Oled128x128<'a> { /// Copies the SRAM buffer to IFRAM and then transfers that over SPI - fn draw(&mut self) { + fn draw(&mut self) -> Result<(), xous::Error> { self.hw_buf.copy_from_slice(&self.buffer); if self.powerdown { - return; + return Ok(()); } let chunk_size = 16; let chunks = self.buffer().len() * size_of::() / chunk_size; @@ -588,8 +600,8 @@ impl<'a> FrameBuffer for Oled128x128<'a> { // the transaction is done before the data is done transmitting, and we have to // toggle set_data() only after the physical transaction is done, not after the // the last UDMA action has been queued. - self.send_command(Command::SetPageAddress(0).encode()); - self.send_command(Command::SetColumnAddress(page as u8).encode()); + self.send_command(Command::SetPageAddress(0).encode())?; + self.send_command(Command::SetColumnAddress(page as u8).encode())?; // wait for commands to finish before toggling set_data // self.spim.tx_data_await(false); // crate::println!("Send page {}, offset {:x}", page, page * chunk_size); @@ -600,12 +612,9 @@ impl<'a> FrameBuffer for Oled128x128<'a> { .txrx_data_async_from_parts::(page * chunk_size, chunk_size, true, false) .expect("Couldn't initiate oled data transfer"); } - self.spim.txrx_await(false).unwrap_or_else(|_e| { - #[cfg(feature = "std")] - log::error!("txrx err {:?}", _e); - &[] - }); + self.spim.txrx_await(false)?; } + Ok(()) } fn clear(&mut self) { self.buffer_mut().fill(0xFFFF_FFFF); } diff --git a/libs/bao1x-hal/src/udma/spim.rs b/libs/bao1x-hal/src/udma/spim.rs index 23703d130..efb55164c 100644 --- a/libs/bao1x-hal/src/udma/spim.rs +++ b/libs/bao1x-hal/src/udma/spim.rs @@ -476,7 +476,7 @@ impl Spim { &self.ifram.as_phys_slice()[..self.tx_buf_len_bytes / size_of::()] } - fn send_cmd_list(&mut self, cmds: &[SpimCmd]) { + pub fn send_cmd_list(&mut self, cmds: &[SpimCmd]) { for cmd_chunk in cmds.chunks(SPIM_CMD_BUF_LEN_BYTES / size_of::()) { for (src, dst) in cmd_chunk.iter().zip(self.cmd_buf_mut().iter_mut()) { *dst = (*src).into(); diff --git a/libs/ux-api/src/minigfx/mod.rs b/libs/ux-api/src/minigfx/mod.rs index ef4e60604..cc6945147 100644 --- a/libs/ux-api/src/minigfx/mod.rs +++ b/libs/ux-api/src/minigfx/mod.rs @@ -47,7 +47,7 @@ pub trait FrameBuffer { /// and background colors for a color theme. fn xor_pixel(&mut self, p: Point); /// Swaps the drawable buffer to the screen and sends it to the hardware - fn draw(&mut self); + fn draw(&mut self) -> Result<(), xous::Error>; /// Clears the drawable buffer fn clear(&mut self); /// Returns the size of the frame buffer as a Point @@ -68,7 +68,7 @@ impl FrameBuffer for DynFb<'_> { fn get_pixel(&self, p: Point) -> Option { self.0.get_pixel(p) } - fn draw(&mut self) { self.0.draw(); } + fn draw(&mut self) -> Result<(), xous::Error> { self.0.draw() } fn clear(&mut self) { self.0.clear(); } diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index 5a7b2031c..5220e28a4 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -168,10 +168,10 @@ pub fn early_init_hw() -> u32 { &mut iox, &mut udma_global, ); - sh1107.init(); + sh1107.init().ok(); sh1107.buffer_mut().fill(0xFFFF_FFFF); sh1107.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP); - sh1107.draw(); + sh1107.draw().ok(); } #[cfg(feature = "board-dabao")] @@ -228,10 +228,10 @@ pub fn oled_init<'a>( iox, udma_global, ); - sh1107.init(); + sh1107.init().ok(); sh1107.buffer_mut().fill(0xFFFF_FFFF); sh1107.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP); - sh1107.draw(); + sh1107.draw().ok(); sh1107 } @@ -415,5 +415,5 @@ pub fn progress_bar(fb: &mut dyn FrameBuffer, progress: usize) { false, ); } - fb.0.draw(); + fb.0.draw().ok(); } diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 175a19920..4400ddab8 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -227,9 +227,10 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { let hal = Hal::new(); let mut display = Oled128x128::new(main_thread_token, bao1x_api::PERCLK, &iox, &udma_global); - display.init(); + // these unwrap because if we have a time-out at this stage, it's likely a hardware problem + display.init().unwrap(); display.clear(); - display.draw(); + display.draw().unwrap(); // ---- panic handler - set up early so we can see panics quickly // install the graphical panic handler. It won't catch really early panics, or panics in this crate, @@ -522,7 +523,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ) }; response.replace(acquisition).unwrap(); - display.pop(); + display + .pop() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); hal.set_preemption(true); } // forward messages on to listeners iff we don't have an active modal @@ -603,7 +606,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Mono::White.into(), Mono::Black.into(), ); - display.draw(); + display + .draw() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); let mut img = rqrr::PreparedImage::prepare_from_greyscale(IMAGE_WIDTH, IMAGE_HEIGHT, |x, y| { frame[y * IMAGE_WIDTH + x] @@ -641,7 +646,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ) }; response.replace(acquisition).unwrap(); - display.pop(); + display.pop().unwrap_or_else(|_| { + display_timeout_handler(&udma_global, &mut display) + }); #[cfg(not(feature = "hosted-baosec"))] hal.set_preemption(true); } else { @@ -685,7 +692,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ); } - display.draw(); + display.draw().unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); // clear the front buffer display.clear(); @@ -762,7 +769,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { GfxOpcode::Flush => { if qr_request.is_none() { log::trace!("***gfx flush*** redraw##"); - display.redraw(); + display + .redraw() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); } } GfxOpcode::Clear => { @@ -797,7 +806,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { GfxOpcode::DrawSleepScreen => { if let Some(_scalar) = msg.body.scalar_message() { display.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP); - display.redraw(); + display + .redraw() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); } else { panic!("Incorrect message type"); } @@ -805,7 +816,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { GfxOpcode::DrawBootLogo => { if let Some(_scalar) = msg.body.scalar_message() { display.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP); - display.redraw(); + display + .redraw() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); } else { panic!("Incorrect message type"); } @@ -833,7 +846,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // no failure if it's not } GfxOpcode::Pop => { - display.pop(); + display.pop().unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); if let Some(scalar) = msg.body.scalar_message_mut() { // ack the message if it's a blocking scalar scalar.arg1 = 1; @@ -857,8 +870,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // safety: this is safe because we call init() a prescribed delay after power-up unsafe { display.powerup() }; tt.sleep_ms(5).ok(); - display.init(); - display.pop(); + display.init().unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); + display.pop().unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); if let Some(scalar) = msg.body.scalar_message_mut() { // ack the message if it's a blocking scalar scalar.arg1 = 1; @@ -874,7 +887,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { GfxOpcode::Brightness => { if let Some(scalar) = msg.body.scalar_message_mut() { let brightness = scalar.arg1.min(255) as u8; - display.brightness(brightness); + display + .brightness(brightness) + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); } } GfxOpcode::Quit => break, @@ -894,3 +909,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { log::trace!("quitting"); xous::terminate_process(0) } + +fn display_timeout_handler(udma_global: &UdmaGlobal, display: &mut Oled128x128) { + log::info!("resetting display spim block"); + udma_global.reset(PeriphId::from(bao1x_hal::board::get_display_pins().0)); + display.reinit_spi(); +} diff --git a/services/bao-video/src/panic.rs b/services/bao-video/src/panic.rs index 7b3ccadbf..7b7422c94 100644 --- a/services/bao-video/src/panic.rs +++ b/services/bao-video/src/panic.rs @@ -57,6 +57,7 @@ pub(crate) fn panic_handler_thread( Option, ), bool, + u8, ), ) { thread::spawn({ @@ -111,7 +112,7 @@ pub(crate) fn panic_handler_thread( } } - display.draw(); + display.draw().ok(); append_string("~Guru Meditation~", &mut display); } let body = match msg.body.memory_message() { @@ -138,7 +139,7 @@ pub(crate) fn panic_handler_thread( &mut display, ); } - display.draw(); + display.draw().ok(); } } });