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

nrf twim return errors in async_wait instead of waiting indefinitely #3745

Merged
merged 4 commits into from
Jan 8, 2025
Merged
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
17 changes: 11 additions & 6 deletions embassy-nrf/src/twim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<'d, T: Instance> Twim<'d, T> {
}

/// Get Error instance, if any occurred.
fn check_errorsrc(&self) -> Result<(), Error> {
fn check_errorsrc() -> Result<(), Error> {
let r = T::regs();

let err = r.errorsrc().read();
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<'d, T: Instance> Twim<'d, T> {
}

/// Wait for stop or error
fn async_wait(&mut self) -> impl Future<Output = ()> {
fn async_wait(&mut self) -> impl Future<Output = Result<(), Error>> {
poll_fn(move |cx| {
let r = T::regs();
let s = T::state();
Expand All @@ -338,13 +338,18 @@ impl<'d, T: Instance> Twim<'d, T> {
if r.events_suspended().read() != 0 || r.events_stopped().read() != 0 {
r.events_stopped().write_value(0);

return Poll::Ready(());
return Poll::Ready(Ok(()));
}

// stop if an error occurred
if r.events_error().read() != 0 {
r.events_error().write_value(0);
r.tasks_stop().write_value(1);
if let Err(e) = Self::check_errorsrc() {
return Poll::Ready(Err(e));
} else {
panic!("Found events_error bit without an error in errorsrc reg");
}
}

Poll::Pending
Expand Down Expand Up @@ -503,7 +508,7 @@ impl<'d, T: Instance> Twim<'d, T> {

fn check_operations(&mut self, operations: &[Operation<'_>]) -> Result<(), Error> {
compiler_fence(SeqCst);
self.check_errorsrc()?;
Self::check_errorsrc()?;

assert!(operations.len() == 1 || operations.len() == 2);
match operations {
Expand Down Expand Up @@ -626,7 +631,7 @@ impl<'d, T: Instance> Twim<'d, T> {
while !operations.is_empty() {
let ops = self.setup_operations(address, operations, Some(&mut tx_ram_buffer), last_op, true)?;
let (in_progress, rest) = operations.split_at_mut(ops);
self.async_wait().await;
self.async_wait().await?;
self.check_operations(in_progress)?;
last_op = in_progress.last();
operations = rest;
Expand All @@ -644,7 +649,7 @@ impl<'d, T: Instance> Twim<'d, T> {
while !operations.is_empty() {
let ops = self.setup_operations(address, operations, None, last_op, true)?;
let (in_progress, rest) = operations.split_at_mut(ops);
self.async_wait().await;
self.async_wait().await?;
self.check_operations(in_progress)?;
last_op = in_progress.last();
operations = rest;
Expand Down
Loading