feat: Block on twai calls, remove Option<> results

This commit is contained in:
Sergio Gasquez 2025-01-07 13:15:18 +01:00
parent d1c703227e
commit 650e13205d
3 changed files with 7 additions and 22 deletions

View File

@ -1194,10 +1194,7 @@ where
return Err(EspTwaiError::BusOff); return Err(EspTwaiError::BusOff);
} }
// Check that the peripheral is not already transmitting a packet. // Check that the peripheral is not already transmitting a packet.
if !status.tx_buf_st().bit_is_set() { while !status.tx_buf_st().bit_is_set() {}
return Err(EspTwaiError::WouldBlock); // TODO: Is this the right
// error?
}
write_frame(register_block, frame); write_frame(register_block, frame);
@ -1227,9 +1224,7 @@ where
} }
// Check that we actually have packets to receive. // Check that we actually have packets to receive.
if !status.rx_buf_st().bit_is_set() { while !status.rx_buf_st().bit_is_set() {}
return Err(EspTwaiError::WouldBlock);
}
// Check if the packet in the receive buffer is valid or overrun. // Check if the packet in the receive buffer is valid or overrun.
if status.miss_st().bit_is_set() { if status.miss_st().bit_is_set() {
@ -1252,8 +1247,6 @@ pub enum EspTwaiError {
NonCompliantDlc(u8), NonCompliantDlc(u8),
/// Encapsulates errors defined by the embedded-hal crate. /// Encapsulates errors defined by the embedded-hal crate.
EmbeddedHAL(ErrorKind), EmbeddedHAL(ErrorKind),
/// This operation requires blocking behavior to complete
WouldBlock,
} }
#[cfg(any(doc, feature = "unstable"))] #[cfg(any(doc, feature = "unstable"))]

View File

@ -81,7 +81,7 @@ fn main() -> ! {
// Send a frame to the other ESP // Send a frame to the other ESP
// Use `new_self_reception` if you want to use self-testing. // Use `new_self_reception` if you want to use self-testing.
let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap(); let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap();
while twai.transmit(&frame).is_err() {} twai.transmit(&frame).unwrap();
println!("Sent a frame"); println!("Sent a frame");
} }
@ -89,18 +89,14 @@ fn main() -> ! {
let delay = Delay::new(); let delay = Delay::new();
loop { loop {
// Wait for a frame to be received. // Wait for a frame to be received.
let frame = loop { let frame = twai.receive().unwrap();
if let Ok(frame) = twai.receive() {
break frame;
}
};
println!("Received a frame: {frame:?}"); println!("Received a frame: {frame:?}");
delay.delay_millis(250); delay.delay_millis(250);
let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap(); let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap();
// Transmit a new frame back to the other ESP // Transmit a new frame back to the other ESP
while twai.transmit(&frame).is_err() {} twai.transmit(&frame).unwrap();
println!("Sent a frame"); println!("Sent a frame");
} }
} }

View File

@ -51,13 +51,9 @@ mod tests {
#[test] #[test]
fn test_send_receive(mut ctx: Context) { fn test_send_receive(mut ctx: Context) {
let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO, &[1, 2, 3]).unwrap(); let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO, &[1, 2, 3]).unwrap();
while ctx.twai.transmit(&frame).is_err() {} ctx.twai.transmit(&frame).unwrap();
let frame = loop { let frame = ctx.twai.receive().unwrap();
if let Ok(frame) = ctx.twai.receive() {
break frame;
}
};
assert_eq!(frame.data(), &[1, 2, 3]) assert_eq!(frame.data(), &[1, 2, 3])
} }