-
Notifications
You must be signed in to change notification settings - Fork 237
Controller Area Network (CAN) Take 3 #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f323aa
can: `Frame`, `Transmitter` and `Receiver` traits
timokroeger c5b4816
can: Make frame constructors return `Result`
timokroeger 449b801
can: Merge `Transmitter` and `Receiver` traits
timokroeger 285efaa
can: Use enum for the identifier
timokroeger 439242e
can: `try_` prefixes and improved documentation
timokroeger 95b17a3
can: Add blocking API with default implementation
timokroeger 103b41e
Rename blocking trait methods
timokroeger 7daeaba
can: Only allow valid `Id` s to be constructed
timokroeger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! Blocking CAN API | ||
|
||
/// A blocking CAN interface that is able to transmit and receive frames. | ||
pub trait Can { | ||
/// Associated frame type. | ||
type Frame: crate::can::Frame; | ||
|
||
/// Associated error type. | ||
type Error; | ||
|
||
/// Puts a frame in the transmit buffer. Blocks until space is available in | ||
/// the transmit buffer. | ||
fn try_write(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>; | ||
|
||
/// Blocks until a frame was received or an error occured. | ||
fn try_read(&mut self) -> Result<Self::Frame, Self::Error>; | ||
} | ||
|
||
/// Default implementation of `blocking::can::Can` for implementers of `can::Can` | ||
pub trait Default: crate::can::Can {} | ||
|
||
impl<S> crate::blocking::can::Can for S | ||
where | ||
S: Default, | ||
{ | ||
type Frame = S::Frame; | ||
type Error = S::Error; | ||
|
||
fn try_write(&mut self, frame: &Self::Frame) -> Result<(), Self::Error> { | ||
let mut replaced_frame; | ||
let mut frame_to_transmit = frame; | ||
while let Some(f) = nb::block!(self.try_transmit(&frame_to_transmit))? { | ||
replaced_frame = f; | ||
frame_to_transmit = &replaced_frame; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn try_read(&mut self) -> Result<Self::Frame, Self::Error> { | ||
nb::block!(self.try_receive()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//! Controller Area Network | ||
/// Standard 11bit Identifier (0..=0x7FF) | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
pub struct StandardId(u16); | ||
|
||
impl StandardId { | ||
/// Creates a new standard identifier. | ||
pub fn new(id: u16) -> Result<StandardId, ()> { | ||
if id <= 0x7FF { | ||
Ok(StandardId(id)) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
} | ||
|
||
impl core::convert::From<StandardId> for u16 { | ||
fn from(id: StandardId) -> u16 { | ||
id.0 | ||
} | ||
} | ||
|
||
impl core::convert::From<StandardId> for u32 { | ||
fn from(id: StandardId) -> u32 { | ||
id.0 as u32 | ||
} | ||
} | ||
|
||
impl ExtendedId { | ||
/// Creates a new extended identifier. | ||
pub fn new(id: u32) -> Result<ExtendedId, ()> { | ||
if id <= 0x1FFF_FFFF { | ||
Ok(ExtendedId(id)) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
} | ||
|
||
impl core::convert::From<ExtendedId> for u32 { | ||
fn from(id: ExtendedId) -> u32 { | ||
id.0 | ||
} | ||
} | ||
|
||
/// Extended 29bit Identifier (0..=0x1FFF_FFFF) | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
pub struct ExtendedId(u32); | ||
|
||
/// CAN Identifier | ||
/// | ||
/// The variants are wrapped in newtypes so they can only be costructed with valid values. | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
pub enum Id { | ||
/// Standard 11bit Identifier (0..=0x7FF) | ||
Standard(StandardId), | ||
|
||
/// Extended 29bit Identifier (0..=0x1FFF_FFFF) | ||
Extended(ExtendedId), | ||
} | ||
|
||
impl Id { | ||
/// Creates a new standard identifier. | ||
pub fn new_standard(id: u16) -> Result<Id, ()> { | ||
Ok(StandardId::new(id)?.into()) | ||
} | ||
|
||
/// Creates a new extended identifier. | ||
pub fn new_extended(id: u32) -> Result<Id, ()> { | ||
Ok(ExtendedId::new(id)?.into()) | ||
} | ||
} | ||
|
||
impl core::convert::From<StandardId> for Id { | ||
fn from(id: StandardId) -> Id { | ||
Id::Standard(id) | ||
} | ||
} | ||
|
||
impl core::convert::From<ExtendedId> for Id { | ||
fn from(id: ExtendedId) -> Id { | ||
Id::Extended(id) | ||
} | ||
} | ||
|
||
/// A CAN2.0 Frame | ||
pub trait Frame: Sized { | ||
/// Creates a new frame. | ||
/// Returns an error when the data slice is too long. | ||
fn new(id: Id, data: &[u8]) -> Result<Self, ()>; | ||
|
||
/// Creates a new remote frame (RTR bit set). | ||
/// Returns an error when the data length code (DLC) is not valid. | ||
fn new_remote(id: Id, dlc: usize) -> Result<Self, ()>; | ||
|
||
/// Returns true if this frame is a extended frame. | ||
fn is_extended(&self) -> bool; | ||
|
||
/// Returns true if this frame is a standard frame. | ||
fn is_standard(&self) -> bool { | ||
!self.is_extended() | ||
} | ||
|
||
/// Returns true if this frame is a remote frame. | ||
fn is_remote_frame(&self) -> bool; | ||
|
||
/// Returns true if this frame is a data frame. | ||
fn is_data_frame(&self) -> bool { | ||
!self.is_remote_frame() | ||
} | ||
|
||
/// Returns the frame identifier. | ||
fn id(&self) -> Id; | ||
|
||
/// Returns the data length code (DLC) which is in the range 0..8. | ||
/// | ||
/// For data frames the DLC value always matches the length of the data. | ||
/// Remote frames do not carry any data, yet the DLC can be greater than 0. | ||
fn dlc(&self) -> usize; | ||
|
||
/// Returns the frame data (0..8 bytes in length). | ||
fn data(&self) -> &[u8]; | ||
} | ||
|
||
/// A CAN interface that is able to transmit and receive frames. | ||
pub trait Can { | ||
/// Associated frame type. | ||
type Frame: Frame; | ||
ryankurte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Associated error type. | ||
type Error; | ||
|
||
/// Puts a frame in the transmit buffer to be sent on the bus. | ||
/// | ||
/// If the transmit buffer is full, this function will try to replace a pending | ||
/// lower priority frame and return the frame that was replaced. | ||
/// Returns `Err(WouldBlock)` if the transmit buffer is full and no frame can be | ||
/// replaced. | ||
/// | ||
/// # Notes for implementers | ||
/// | ||
/// * Frames of equal identifier shall be transmited in FIFO fashion when more | ||
/// than one transmit buffer is available. | ||
/// * When replacing pending frames make sure the frame is not in the process of | ||
/// being send to the bus. | ||
fn try_transmit(&mut self, frame: &Self::Frame) | ||
-> nb::Result<Option<Self::Frame>, Self::Error>; | ||
|
||
/// Returns a received frame if available. | ||
fn try_receive(&mut self) -> nb::Result<Self::Frame, Self::Error>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -406,6 +406,7 @@ | |
|
||
pub mod adc; | ||
pub mod blocking; | ||
pub mod can; | ||
pub mod capture; | ||
pub mod digital; | ||
pub mod fmt; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.