Skip to content

Commit 7aaf794

Browse files
committed
embedded-can-04: Add a receive function for an arbitrary embedded_can::Frame.
Classic CAN only, as no CAN-FD support in embedded_can yet.
1 parent 1157869 commit 7aaf794

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

src/lib.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use filter::{
5252
StandardFilterSlot, EXTENDED_FILTER_MAX, STANDARD_FILTER_MAX,
5353
};
5454
use frame::MergeTxFrameHeader;
55-
use frame::{RxFrameInfo, TxFrameHeader};
55+
use frame::{FrameFormat, RxFrameInfo, TxFrameHeader};
5656
use id::{Id, IdReg};
5757
use interrupt::{Interrupt, InterruptLine, Interrupts};
5858

@@ -1140,6 +1140,36 @@ where
11401140
// Safety: We have a `&mut self` and have unique access to the peripheral.
11411141
unsafe { Rx::<I, M, Fifo1>::conjure().receive(buffer) }
11421142
}
1143+
1144+
/// Returns a received Classic CAN frame of a given type from FIFO_0 if available.
1145+
///
1146+
/// # Panics
1147+
///
1148+
/// Panics if an `CAN-FD` frame is received, as embedded_can::Frame has no FD support.
1149+
#[cfg(feature = "embedded-can-04")]
1150+
#[inline]
1151+
pub fn receive0_frame<F>(&mut self) -> nb::Result<ReceiveOverrun<F>, Infallible>
1152+
where
1153+
F: embedded_can::Frame,
1154+
{
1155+
// Safety: We have a `&mut self` and have unique access to the peripheral.
1156+
unsafe { Rx::<I, M, Fifo0>::conjure().receive_frame() }
1157+
}
1158+
1159+
/// Returns a received Classic CAN frame of a given type from FIFO_1 if available.
1160+
///
1161+
/// # Panics
1162+
///
1163+
/// Panics if an `CAN-FD` frame is received, as embedded_can::Frame has no FD support.
1164+
#[cfg(feature = "embedded-can-04")]
1165+
#[inline]
1166+
pub fn receive1_frame<F>(&mut self) -> nb::Result<ReceiveOverrun<F>, Infallible>
1167+
where
1168+
F: embedded_can::Frame,
1169+
{
1170+
// Safety: We have a `&mut self` and have unique access to the peripheral.
1171+
unsafe { Rx::<I, M, Fifo1>::conjure().receive_frame() }
1172+
}
11431173
}
11441174

11451175
/// FdCanControl Struct
@@ -1567,8 +1597,6 @@ where
15671597

15681598
/// Returns a received frame if available.
15691599
///
1570-
/// Returns `Err` when a frame was lost due to buffer overrun.
1571-
///
15721600
/// # Panics
15731601
///
15741602
/// Panics if `buffer` is smaller than the header length.
@@ -1607,6 +1635,33 @@ where
16071635
}
16081636
}
16091637

1638+
/// Returns a received Classic CAN frame of a given type if available.
1639+
///
1640+
/// # Panics
1641+
///
1642+
/// Panics if an `CAN-FD` frame is received, as embedded_can::Frame has no FD support.
1643+
#[cfg(feature = "embedded-can-04")]
1644+
pub fn receive_frame<F>(mut self) -> nb::Result<ReceiveOverrun<F>, Infallible>
1645+
where
1646+
F: embedded_can::Frame,
1647+
{
1648+
let mut buffer = [0_u8; 8];
1649+
let overrun = self.receive(&mut buffer)?;
1650+
let info = overrun.unwrap();
1651+
if info.frame_format != FrameFormat::Standard {
1652+
panic!("Received CAN-FD frame");
1653+
}
1654+
let frame = if info.rtr {
1655+
F::new_remote(info.id, info.len as usize)
1656+
} else {
1657+
F::new(info.id, &buffer[..info.len as usize])
1658+
}.unwrap();
1659+
match overrun {
1660+
ReceiveOverrun::NoOverrun(_) => Ok(ReceiveOverrun::NoOverrun(frame)),
1661+
ReceiveOverrun::Overrun(_) => Ok(ReceiveOverrun::Overrun(frame)),
1662+
}
1663+
}
1664+
16101665
#[inline]
16111666
fn registers(&self) -> &RegisterBlock {
16121667
unsafe { &*I::REGISTERS }

0 commit comments

Comments
 (0)