From 4af7699f0d50200af65d9948c044dfc0e8962287 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 23 Mar 2023 15:30:55 +0800 Subject: [PATCH] remove double-free path in I2C driver --- services/llio/src/i2c/hardware.rs | 2 ++ services/llio/src/main.rs | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/llio/src/i2c/hardware.rs b/services/llio/src/i2c/hardware.rs index 0cb6a5d5a..a186a2c18 100644 --- a/services/llio/src/i2c/hardware.rs +++ b/services/llio/src/i2c/hardware.rs @@ -109,6 +109,8 @@ impl I2cStateMachine { (&mut i2c) as *mut I2cStateMachine as *mut usize, ) .expect("couldn't claim I2C irq"); + i2c.i2c_csr.wfo(utra::i2c::CORE_RESET_RESET, 1); + i2c.ticktimer.sleep_ms(10).ok(); // initialize i2c clocks // set the prescale assuming 100MHz cpu operation: 100MHz / ( 5 * 100kHz ) - 1 = 199 diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index eb7e9d75d..f5560d700 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -541,32 +541,34 @@ fn main() -> ! { // Note that we start the RTC at somewhere between 0-10 years, so in practice, a user can expect between 90-100 years // of continuous uptime service out of the RTC. let mut settings = [0u8; 8]; - let mut success = false; - while !success { + loop { // retry loop is necessary because this function can get called during "congested" periods i2c.i2c_mutex_acquire(); match i2c.i2c_read_no_repeated_start(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings) { Ok(llio::I2cStatus::ResponseReadOk) => { i2c.i2c_mutex_release(); - success = true + break; }, Err(xous::Error::ServerQueueFull) => { i2c.i2c_mutex_release(); - success = false; // give it a short pause before trying again, to avoid hammering the I2C bus at busy times tt.sleep_ms(38).unwrap(); + + xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); + continue; }, _ => { log::error!("Couldn't read seconds from RTC!"); - i2c.i2c_mutex_release(); + // reset the hardware driver, in case that's the problem + // this will reset the mutex to not acquired, even if someone else is using it. Very dangerous! + unsafe{i2c.i2c_driver_reset();} + tt.sleep_ms(37).unwrap(); // short pause in case the upset was caused by too much activity + xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); - break; + continue; }, }; } - if !success { - continue - } log::debug!("GetRtcValue regs: {:?}", settings); let total_secs: u64; let mut retries = 0;