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 Jan 10, 2023
1 parent 9d64c9d commit e929a47
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/flexcan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! FlexCAN pad configurations

/// A FlexCAN signal
pub trait Signal: private::Sealed {}

/// A tag that indicates a FlexCAN TX pad
pub enum Tx {}
/// A tag that indicates a FlexCAN 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 FlexCAN pin
pub trait Pin: super::Iomuxc {
/// 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 FlexCAN pin.
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;
}
};
}
54 changes: 54 additions & 0 deletions src/imxrt1060/flexcan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! FlexCAN pin implementation

use super::pads::{
gpio_ad_b0::*, gpio_ad_b1::*, gpio_b0::*, gpio_b1::*, gpio_emc::*, gpio_sd_b1::*,
};

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

//
// CAN1
//
can!(module: U1, alt: 2, pad: GPIO_AD_B1_08, signal: Tx, daisy: None);
can!(module: U1, alt: 2, pad: GPIO_B0_02, signal: Tx, daisy: None);
can!(module: U1, alt: 3, pad: GPIO_EMC_17, signal: Tx, daisy: None);
can!(module: U1, alt: 4, pad: GPIO_SD_B1_02, signal: Tx, daisy: None);
can!(module: U1, alt: 2, pad: GPIO_AD_B1_09, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_AD_B1_09));
can!(module: U1, alt: 2, pad: GPIO_B0_03, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_B0_03));
can!(module: U1, alt: 3, pad: GPIO_EMC_18, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_EMC_18));
can!(module: U1, alt: 4, pad: GPIO_SD_B1_03, signal: Rx, daisy: Some(DAISY_CAN1_RX_GPIO_SD_B1_03));

//
// CAN2
//
can!(module: U2, alt: 0, pad: GPIO_AD_B0_02, signal: Tx, daisy: None);
can!(module: U2, alt: 3, pad: GPIO_EMC_09, signal: Tx, daisy: None);
can!(module: U2, alt: 6, pad: GPIO_B1_08, signal: Tx, daisy: None);
can!(module: U2, alt: 6, pad: GPIO_AD_B0_14, signal: Tx, daisy: None);
can!(module: U2, alt: 0, pad: GPIO_AD_B0_03, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_AD_B0_03));
can!(module: U2, alt: 3, pad: GPIO_EMC_10, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_EMC_10));
can!(module: U2, alt: 6, pad: GPIO_AD_B0_15, signal: Rx, daisy: Some(DAISY_CAN2_RX_GPIO_AD_B0_15));
can!(module: U2, alt: 6, pad: GPIO_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/imxrt1060/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! functions are tagged with `imxrt-iomuxc` traits.

mod adc;
mod flexcan;
mod flexpwm;
mod lpi2c;
mod lpspi;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
pub mod adc;
mod config;
#[macro_use]
pub mod flexcan;
#[macro_use]
pub mod flexpwm;
#[macro_use]
pub mod lpi2c;
Expand Down Expand Up @@ -216,7 +218,7 @@ pub mod prelude {
};

pub use crate::{
adc, alternate, ccm, clear_sion, consts, flexpwm, gpio, lpi2c, lpspi, lpuart, sai,
adc, alternate, ccm, clear_sion, consts, flexcan, flexpwm, gpio, lpi2c, lpspi, lpuart, sai,
set_sion, usdhc, Daisy, ErasedPad, Pad, WrongPadError,
};
}
Expand Down

0 comments on commit e929a47

Please sign in to comment.