Skip to content

Commit 5dfbd3c

Browse files
committed
can: Frame, Transmitter and Receiver traits
1 parent 9e6ab5a commit 5dfbd3c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/can.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Controller Area Network
2+
3+
/// A CAN2.0 Frame
4+
pub trait Frame {
5+
/// Creates a new frame with a standard identifier.
6+
fn new_standard(id: u32, data: &[u8]) -> Self;
7+
8+
/// Creates a new frame with an extended identifier.
9+
fn new_extended(id: u32, data: &[u8]) -> Self;
10+
11+
/// Marks this frame as a remote frame (by setting the RTR bit).
12+
fn with_rtr(&mut self, dlc: usize) -> &mut Self;
13+
14+
/// Returns true if this frame is a extended frame.
15+
fn is_extended(&self) -> bool;
16+
17+
/// Returns true if this frame is a standard frame.
18+
fn is_standard(&self) -> bool {
19+
!self.is_extended()
20+
}
21+
22+
/// Returns true if this frame is a remote frame.
23+
fn is_remote_frame(&self) -> bool;
24+
25+
/// Returns true if this frame is a data frame.
26+
fn is_data_frame(&self) -> bool {
27+
!self.is_remote_frame()
28+
}
29+
30+
/// Returns the frame identifier.
31+
fn id(&self) -> u32;
32+
33+
/// Returns the data length code (DLC) which is in the range 0..8.
34+
///
35+
/// For data frames the DLC value always matches the lenght of the data.
36+
/// Remote frames no not carry any data, yet the DLC can be greater than 0.
37+
fn dlc(&self) -> usize;
38+
39+
/// Returns the frame data (0..8 bytes in length).
40+
fn data(&self) -> &[u8];
41+
}
42+
43+
/// A CAN interface that is able to transmit frames.
44+
pub trait Transmitter {
45+
/// Associated frame type.
46+
type Frame: Frame;
47+
48+
/// Associated error type.
49+
type Error;
50+
51+
/// Puts a frame in the transmit buffer.
52+
///
53+
/// If the buffer is full, this function will try to replace a lower priority frame
54+
/// and return it. This is to avoid the priority inversion problem.
55+
fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error>;
56+
}
57+
58+
/// A CAN interface that is able to receive frames.
59+
pub trait Receiver {
60+
/// Associated frame type.
61+
type Frame: Frame;
62+
63+
/// Associated error type.
64+
type Error;
65+
66+
/// Returns a received frame if available.
67+
fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error>;
68+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ extern crate void;
692692

693693
pub mod adc;
694694
pub mod blocking;
695+
pub mod can;
695696
pub mod digital;
696697
pub mod fmt;
697698
pub mod prelude;

0 commit comments

Comments
 (0)