Skip to content

Commit

Permalink
Add FlexCAN pin trait, FlexCAN1 and 2 1060 impls
Browse files Browse the repository at this point in the history
1060 pins implementations checked against section 44.3 in the 1060
reference manual. See imxrt-rs/imxrt-hal#122 for a related FlexCAN
driver.
  • Loading branch information
dstric-aqueduct authored and mciantyre committed Dec 21, 2022
1 parent 7c3f4e0 commit c12adda
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/can.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Can pad configurations
/// A Can signal
pub trait Signal: private::Sealed {}

/// A tag that indicates a Can TX pad
pub enum Tx {}
/// A tag that indicates a Can Rx pad
pub enum Rx {}

impl Signal for Tx {}
impl Signal for Rx {}
mod private {
pub trait Sealed {}
impl Sealed for super::Tx {}
impl Sealed for super::Rx {}
}

/// A Can pin
pub trait Pin: super::IOMUX {
/// Alternate value for this pin
const ALT: u32;
/// Daisy register
const DAISY: Option<super::Daisy>;
/// CAN signal
type Signal: Signal;
/// CAN module; `U1` for `CAN1`
type Module: super::consts::Unsigned;
}

/// Prepare a Can pin
///
/// # Safety
///
/// `prepare()` inherits all the unsafety that comes from the `IOMUX` supertrait.
pub fn prepare<P: Pin>(pin: &mut P) {
super::alternate(pin, P::ALT);
super::set_sion(pin);
if let Some(daisy) = P::DAISY {
unsafe { daisy.write() };
}
}

#[allow(unused)] // Used in chip-specific modules...
macro_rules! can {
(module: $module:ty, alt: $alt:expr, pad: $pad:ty, signal: $signal:ty, daisy: $daisy:expr) => {
impl Pin for $pad {
const ALT: u32 = $alt;
const DAISY: Option<Daisy> = $daisy;
type Signal = $signal;
type Module = $module;
}
};
}
52 changes: 52 additions & 0 deletions src/imxrt106x/can.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Can pin implementation
use super::pads::{ad_b0::*, ad_b1::*, b0::*, b1::*, emc::*, sd_b1::*};

use crate::{
can::{Pin, Rx, Tx},
consts::*,
Daisy,
};

//
// CAN1
//
can!(module: U1, alt: 2, pad: AD_B1_08, signal: Tx, daisy: None);
can!(module: U1, alt: 2, pad: B0_02, signal: Tx, daisy: None);
can!(module: U1, alt: 3, pad: EMC_17, signal: Tx, daisy: None);
can!(module: U1, alt: 4, pad: SD_B1_02, signal: Tx, daisy: None);
can!(module: U1, alt: 2, pad: AD_B1_09, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_AD_B1_09));
can!(module: U1, alt: 2, pad: B0_03, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_B0_03));
can!(module: U1, alt: 3, pad: EMC_18, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_EMC_18));
can!(module: U1, alt: 4, pad: SD_B1_03, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_SD_B1_03));

//
// CAN2
//
can!(module: U2, alt: 0, pad: AD_B0_02, signal: Tx, daisy: None);
can!(module: U2, alt: 3, pad: EMC_09, signal: Tx, daisy: None);
can!(module: U2, alt: 6, pad: B1_08, signal: Tx, daisy: None);
can!(module: U2, alt: 6, pad: AD_B0_14, signal: Tx, daisy: None);
can!(module: U2, alt: 0, pad: AD_B0_03, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_AD_B0_03));
can!(module: U2, alt: 3, pad: EMC_10, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_EMC_10));
can!(module: U2, alt: 6, pad: AD_B0_15, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_AD_B0_15));
can!(module: U2, alt: 6, pad: B1_09, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_B1_09));

/// Auto-generated DAISY values
mod daisy {
#![allow(unused)]

use super::Daisy;

pub const DAISY_CAN1_RX_GPIO_SD_B1_03: Daisy = Daisy::new(0x401F_844C as *mut u32, 0);
pub const DAISY_CAN1_RX_GPIO_EMC_18: Daisy = Daisy::new(0x401F_844C as *mut u32, 1);
pub const DAISY_CAN1_RX_GPIO_AD_B1_09: Daisy = Daisy::new(0x401F_844C as *mut u32, 2);
pub const DAISY_CAN1_RX_GPIO_B0_03: Daisy = Daisy::new(0x401F_844C as *mut u32, 3);

pub const DAISY_CAN2_RX_GPIO_EMC_10: Daisy = Daisy::new(0x401F_8450 as *mut u32, 0);
pub const DAISY_CAN2_RX_GPIO_AD_B0_03: Daisy = Daisy::new(0x401F_8450 as *mut u32, 1);
pub const DAISY_CAN2_RX_GPIO_AD_B0_15: Daisy = Daisy::new(0x401F_8450 as *mut u32, 2);
pub const DAISY_CAN2_RX_GPIO_B1_09: Daisy = Daisy::new(0x401F_8450 as *mut u32, 3);
}

use daisy::*;
1 change: 1 addition & 0 deletions src/imxrt106x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
//! ```
mod adc;
mod can;
mod i2c;
mod pwm;
mod sai;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@

#[macro_use]
pub mod adc;
#[macro_use]
pub mod can;
mod config;
#[macro_use]
pub mod i2c;
Expand Down

0 comments on commit c12adda

Please sign in to comment.