Skip to content
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

FlexCAN peripheral addition #122

Open
wants to merge 14 commits into
base: maint-v0.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
.idea
.vscode
Cargo.lock
8 changes: 4 additions & 4 deletions imxrt-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ log = "0.4.8"
rand_core = { version = "0.5", default-features = false, optional = true }

[dependencies.imxrt-iomuxc]
version = "0.1.2"
git = "https://github.com/imxrt-rs/imxrt-iomuxc.git"
branch = "v0.1"
features = ["imxrt106x"]
git = "https://github.com/dstric-aqueduct/imxrt-iomuxc.git"
tag = "v0.1.5-can"
mciantyre marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies.cortex-m-rt]
version = "0.6"
Expand All @@ -34,7 +34,7 @@ features = ["device"]
bench = false

[features]
default = ["embedded-hal/unproven"] # Allows us to access the new digital pin traits
default = ["embedded-hal/unproven", "imxrt1062"] # Allows us to access the new digital pin traits
dstric-aqueduct marked this conversation as resolved.
Show resolved Hide resolved

# Device specific feature flags
# these need fixes and conditional sections in CCM
Expand Down
105 changes: 105 additions & 0 deletions imxrt-hal/src/can/embedded_hal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//! `embedded_hal` trait impls.

use super::{Data, Frame, NoDataError, CAN};
use crate::iomuxc::consts::Unsigned;

use embedded_hal::can;
pub use embedded_hal::can::{ExtendedId, Id, StandardId};

impl<M> can::Can for CAN<M>
where
M: Unsigned,
{
type Frame = Frame;

type Error = NoDataError;

fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
match self.transmit(frame) {
Ok(_status) => Ok(Some(frame.clone())),
Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
Err(nb::Error::Other(e)) => match e {},
}
}

fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
match self.read_mailboxes() {
Some(d) => Ok(d.frame),
None => Err(nb::Error::Other(NoDataError { _priv: () })),
}
}
}

impl can::Error for NoDataError {
fn kind(&self) -> can::ErrorKind {
can::ErrorKind::Overrun
}
}
mciantyre marked this conversation as resolved.
Show resolved Hide resolved

impl can::Frame for Frame {
fn new(id: impl Into<can::Id>, data: &[u8]) -> Option<Self> {
let id = match id.into() {
can::Id::Standard(id) => unsafe {
Id::Standard(StandardId::new_unchecked(id.as_raw()))
},
can::Id::Extended(id) => unsafe {
Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
},
};

let data = Data::new(data)?;
Some(Frame::new_data(id, data))
}

fn new_remote(id: impl Into<can::Id>, dlc: usize) -> Option<Self> {
let id = match id.into() {
can::Id::Standard(id) => unsafe {
Id::Standard(StandardId::new_unchecked(id.as_raw()))
},
can::Id::Extended(id) => unsafe {
Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
},
};

if dlc <= 8 {
Some(Frame::new_remote(id, dlc as u8))
} else {
None
}
}

#[inline]
fn is_extended(&self) -> bool {
self.is_extended()
}

#[inline]
fn is_remote_frame(&self) -> bool {
self.is_remote_frame()
}

#[inline]
fn id(&self) -> can::Id {
match self.id() {
Id::Standard(id) => unsafe {
can::Id::Standard(can::StandardId::new_unchecked(id.as_raw()))
},
Id::Extended(id) => unsafe {
can::Id::Extended(can::ExtendedId::new_unchecked(id.as_raw()))
},
}
}

#[inline]
fn dlc(&self) -> usize {
self.dlc().into()
}

fn data(&self) -> &[u8] {
if let Some(data) = self.data() {
data
} else {
&[]
}
}
}
27 changes: 27 additions & 0 deletions imxrt-hal/src/can/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Filter bank API.

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum FlexCanIde {
#[default]
None = 0,
Ext = 1,
Rtr = 2,
Std = 3,
Inactive
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum FlexCanFlten {
AcceptAll = 0,
#[default]
RejectAll = 1,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct FlexCanFilter {
pub filter_id: u8,
pub id: u32,
pub ide: FlexCanIde,
pub remote: FlexCanIde
}