diff --git a/imxrt-hal/src/can/filter.rs b/imxrt-hal/src/can/filter.rs index 947c3b26..4010cc58 100644 --- a/imxrt-hal/src/can/filter.rs +++ b/imxrt-hal/src/can/filter.rs @@ -25,3 +25,10 @@ pub struct FlexCanFilter { pub remote: FlexCanIde } +impl FlexCanFilter { + /// Create a new [`FlexCanFilter`]. + pub fn new(filter_id: u8, id: u32, ide: FlexCanIde, remote: FlexCanIde) -> Self { + Self { filter_id, id, ide, remote } + } +} + diff --git a/imxrt-hal/src/can/mod.rs b/imxrt-hal/src/can/mod.rs index 2befb898..c2d2c10b 100644 --- a/imxrt-hal/src/can/mod.rs +++ b/imxrt-hal/src/can/mod.rs @@ -17,7 +17,7 @@ //! ); //! //! let mut can1 = can1_builder.build( -//! peripherals.iomuxc.ad_b1.p08, +//! peripherals.iomuxc.ad_b1.p08, //! peripherals.iomuxc.ad_b1.p09 //! ); //! @@ -88,15 +88,16 @@ impl Unclocked { divider: ccm::can::PrescalarSelect, ) -> (Builder, Builder) { let (ccm, _) = handle.raw(); - // First, disable clocks + + // First, disable the clocks for Can1 and Can2 ral::modify_reg!( ral::ccm, ccm, - CCGR1, - CG0: 0, - CG1: 0, - CG2: 0, - CG3: 0 + CCGR0, + CG7: 0b00, + CG8: 0b00, + CG9: 0b00, + CG10: 0b00 ); let clk_sel = match clock_select { @@ -112,15 +113,15 @@ impl Unclocked { CAN_CLK_SEL: clk_sel ); - // Enable clocks + // Enable the clocks for Can1 and Can2 ral::modify_reg!( ral::ccm, ccm, - CCGR1, - CG0: 0b11, - CG1: 0b11, - CG2: 0b11, - CG3: 0b11 + CCGR0, + CG7: 0b11, + CG8: 0b11, + CG9: 0b11, + CG10: 0b11 ); let source_clock = ccm::Frequency::from(clock_select) / ccm::Divider::from(divider); @@ -150,8 +151,7 @@ where } } - /// Builds a Can peripheral. The return - /// is a configured FlexCan peripheral running at 24MHz. + /// Builds a Can peripheral. pub fn build(self, mut tx: TX, mut rx: RX) -> CAN where TX: can::Pin, @@ -196,10 +196,6 @@ where can } - pub fn instance(&mut self) -> &mut ral::can::Instance { - &mut self.reg - } - pub fn print_registers(&self) { log::info!("MCR: {:X}", ral::read_reg!(ral::can, self.reg, MCR)); log::info!("CTRL1: {:X}", ral::read_reg!(ral::can, self.reg, CTRL1)); @@ -240,8 +236,6 @@ where } pub fn begin(&mut self) { - self.set_ccm_ccg(); - ral::modify_reg!(ral::can, self.reg, MCR, MDIS: MDIS_0); self.enter_freeze_mode(); @@ -328,20 +322,6 @@ where ral::modify_reg!(ral::can, self.reg, CTRL2, RRS: rrs as u32) } - fn set_ccm_ccg(&mut self) { - match self.instance_number() { - 1 => { - unsafe { modify_reg!(ral::ccm, CCM, CCGR0, |reg| reg | 0x3C000) }; - } - 2 => { - unsafe { modify_reg!(ral::ccm, CCM, CCGR0, |reg| reg | 0x3C0000) }; - } - u => { - log::error!("Invalid Can instance (set_ccm_ccg): {:?}", u); - } - } - } - pub fn get_clock(&self) -> u32 { self.source_clock.0 } @@ -612,16 +592,16 @@ where }) } - pub fn set_fifo_filter( - &mut self, - filter_id: u8, - id: u32, - ide: filter::FlexCanIde, - remote: filter::FlexCanIde, - ) { + pub fn set_fifo_filter(&mut self, filter: filter::FlexCanFilter) { if !self.fifo_enabled() { return; } + let filter::FlexCanFilter { + filter_id, + id, + ide, + remote, + } = filter; let max_fifo_filters = (read_reg!(ral::can, self.reg, CTRL2, RFFN) + 1) * 8; if filter_id as u32 >= max_fifo_filters { return; @@ -710,10 +690,6 @@ where }) } - pub fn set_fifo_filter_2(&mut self, filter: filter::FlexCanFilter) { - self.set_fifo_filter(filter.filter_id, filter.id, filter.ide, filter.remote) - } - pub fn set_fifo_interrupt(&mut self, enabled: bool) { /* FIFO must be enabled first */ if !self.fifo_enabled() { @@ -754,23 +730,6 @@ where } } - #[allow(dead_code)] - fn read_fifo(&self) -> Option<()> { - // if FIFO enabled and interrupt not enabled - if !self.fifo_enabled() { - return None; - }; - // FIFO interrupt enabled, polling blocked - if (ral::read_reg!(ral::can, self.reg, IMASK1) & (0x00000020 as u32)) != 0 { - return None; - } - // message available - if (ral::read_reg!(ral::can, self.reg, IFLAG1) & (0x00000020 as u32)) != 0 { - return None; - } - Some(()) - } - #[inline(always)] fn mailbox_number_to_address(&self, mailbox_number: u8) -> u32 { self.base_address() + 0x80_u32 + (mailbox_number as u32 * 0x10_u32) @@ -1033,8 +992,3 @@ where Err(nb::Error::Other(Error::NoTxMailbox)) } } - -/// Interface to the Can transmitter part. -pub struct Tx { - _can: PhantomData, -}