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

View File

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

View File

@ -51,13 +51,9 @@ mod tests {
#[test]
fn test_send_receive(mut ctx: Context) {
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 {
if let Ok(frame) = ctx.twai.receive() {
break frame;
}
};
let frame = ctx.twai.receive().unwrap();
assert_eq!(frame.data(), &[1, 2, 3])
}