Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix i2c_driver panic/stack overflow + I2C resilience #1437

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 23 additions & 60 deletions drv/i2c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ impl I2cDevice {
}

impl I2cDevice {
fn response_code<V>(&self, code: u32, val: V) -> Result<V, ResponseCode> {
if code != 0 {
if let Some(_g) = userlib::extract_new_generation(code) {
panic!("i2c reset");
}

Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(val)
}
}

///
/// Reads a register, with register address of type R and value of type V.
///
Expand Down Expand Up @@ -370,12 +383,7 @@ impl I2cDevice {
&[Lease::from(reg.as_bytes()), Lease::from(val.as_bytes_mut())],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(val)
}
self.response_code(code, val)
}

///
Expand Down Expand Up @@ -403,12 +411,7 @@ impl I2cDevice {
&[Lease::from(reg.as_bytes()), Lease::from(buf)],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(response)
}
self.response_code(code, response)
}

///
Expand Down Expand Up @@ -438,12 +441,7 @@ impl I2cDevice {
&[Lease::from(reg.as_bytes()), Lease::from(buf)],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(response)
}
self.response_code(code, response)
}

///
Expand All @@ -470,12 +468,7 @@ impl I2cDevice {
&[Lease::read_only(&[]), Lease::from(val.as_bytes_mut())],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(val)
}
self.response_code(code, val)
}

///
Expand All @@ -499,12 +492,7 @@ impl I2cDevice {
&[Lease::read_only(&[]), Lease::from(buf)],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(response)
}
self.response_code(code, response)
}

///
Expand All @@ -527,12 +515,7 @@ impl I2cDevice {
&[Lease::from(buffer), Lease::read_only(&[])],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(())
}
self.response_code(code, ())
}

///
Expand Down Expand Up @@ -569,12 +552,7 @@ impl I2cDevice {
],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(val)
}
self.response_code(code, val)
}

///
Expand Down Expand Up @@ -616,12 +594,7 @@ impl I2cDevice {
],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(response)
}
self.response_code(code, response)
}

///
Expand Down Expand Up @@ -656,12 +629,7 @@ impl I2cDevice {
],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(())
}
self.response_code(code, ())
}

///
Expand Down Expand Up @@ -701,11 +669,6 @@ impl I2cDevice {
],
);

if code != 0 {
Err(ResponseCode::from_u32(code)
.ok_or(ResponseCode::BadResponse)?)
} else {
Ok(val)
}
self.response_code(code, val)
}
}
14 changes: 7 additions & 7 deletions drv/stm32xx-i2c-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ fn reset(
let _ = all_muxes(controller, port, muxes, |mux| {
ringbuf_entry!(Trace::ResetMux(mux.address));
let _ = mux.driver.reset(mux, &sys);

//
// We now consider ourselves to be in an Unknown state: it will
// be up to the next transaction on this bus to properly set the
// mux state.
//
muxmap.insert(bus, MuxState::Unknown);
Ok(())
});

//
// We now consider ourselves to be in an Unknown state: it will
// be up to the next transaction on this bus to properly set the
// mux state.
//
muxmap.insert(bus, MuxState::Unknown);
}

fn reset_needed(code: ResponseCode) -> bool {
Expand Down