remove double-free path in I2C driver

This commit is contained in:
bunnie 2023-03-23 15:30:55 +08:00
parent e32a22655b
commit 4af7699f0d
2 changed files with 13 additions and 9 deletions

View file

@ -109,6 +109,8 @@ impl I2cStateMachine {
(&mut i2c) as *mut I2cStateMachine as *mut usize, (&mut i2c) as *mut I2cStateMachine as *mut usize,
) )
.expect("couldn't claim I2C irq"); .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 // initialize i2c clocks
// set the prescale assuming 100MHz cpu operation: 100MHz / ( 5 * 100kHz ) - 1 = 199 // set the prescale assuming 100MHz cpu operation: 100MHz / ( 5 * 100kHz ) - 1 = 199

View file

@ -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 // 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. // of continuous uptime service out of the RTC.
let mut settings = [0u8; 8]; let mut settings = [0u8; 8];
let mut success = false; loop {
while !success {
// retry loop is necessary because this function can get called during "congested" periods // retry loop is necessary because this function can get called during "congested" periods
i2c.i2c_mutex_acquire(); i2c.i2c_mutex_acquire();
match i2c.i2c_read_no_repeated_start(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings) { match i2c.i2c_read_no_repeated_start(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings) {
Ok(llio::I2cStatus::ResponseReadOk) => { Ok(llio::I2cStatus::ResponseReadOk) => {
i2c.i2c_mutex_release(); i2c.i2c_mutex_release();
success = true break;
}, },
Err(xous::Error::ServerQueueFull) => { Err(xous::Error::ServerQueueFull) => {
i2c.i2c_mutex_release(); i2c.i2c_mutex_release();
success = false;
// give it a short pause before trying again, to avoid hammering the I2C bus at busy times // give it a short pause before trying again, to avoid hammering the I2C bus at busy times
tt.sleep_ms(38).unwrap(); 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!"); 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"); xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller");
break; continue;
}, },
}; };
} }
if !success {
continue
}
log::debug!("GetRtcValue regs: {:?}", settings); log::debug!("GetRtcValue regs: {:?}", settings);
let total_secs: u64; let total_secs: u64;
let mut retries = 0; let mut retries = 0;