bxcan_ng/
embedded_can.rs

1//! `embedded_hal` trait impls.
2
3use crate::{Can, Data, ExtendedId, Frame, Id, Instance, OverrunError, StandardId};
4
5use embedded_can_04 as can;
6
7impl<I> can::nb::Can for Can<I>
8where
9    I: Instance,
10{
11    type Frame = Frame;
12
13    type Error = OverrunError;
14
15    fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
16        match self.transmit(frame) {
17            Ok(status) => Ok(status.dequeued_frame().cloned()),
18            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
19            Err(nb::Error::Other(e)) => match e {},
20        }
21    }
22
23    fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
24        self.receive()
25    }
26}
27
28impl can::Error for OverrunError {
29    fn kind(&self) -> can::ErrorKind {
30        can::ErrorKind::Overrun
31    }
32}
33
34impl can::Frame for Frame {
35    fn new(id: impl Into<can::Id>, data: &[u8]) -> Option<Self> {
36        let id = match id.into() {
37            can::Id::Standard(id) => unsafe {
38                Id::Standard(StandardId::new_unchecked(id.as_raw()))
39            },
40            can::Id::Extended(id) => unsafe {
41                Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
42            },
43        };
44
45        let data = Data::new(data)?;
46        Some(Frame::new_data(id, data))
47    }
48
49    fn new_remote(id: impl Into<can::Id>, dlc: usize) -> Option<Self> {
50        let id = match id.into() {
51            can::Id::Standard(id) => unsafe {
52                Id::Standard(StandardId::new_unchecked(id.as_raw()))
53            },
54            can::Id::Extended(id) => unsafe {
55                Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
56            },
57        };
58
59        if dlc <= 8 {
60            Some(Frame::new_remote(id, dlc as u8))
61        } else {
62            None
63        }
64    }
65
66    #[inline]
67    fn is_extended(&self) -> bool {
68        self.is_extended()
69    }
70
71    #[inline]
72    fn is_remote_frame(&self) -> bool {
73        self.is_remote_frame()
74    }
75
76    #[inline]
77    fn id(&self) -> can::Id {
78        match self.id() {
79            Id::Standard(id) => unsafe {
80                can::Id::Standard(can::StandardId::new_unchecked(id.as_raw()))
81            },
82            Id::Extended(id) => unsafe {
83                can::Id::Extended(can::ExtendedId::new_unchecked(id.as_raw()))
84            },
85        }
86    }
87
88    #[inline]
89    fn dlc(&self) -> usize {
90        self.dlc().into()
91    }
92
93    fn data(&self) -> &[u8] {
94        if let Some(data) = self.data() {
95            data
96        } else {
97            &[]
98        }
99    }
100}