Skip to content

Commit

Permalink
PR updates
Browse files Browse the repository at this point in the history
- fixed clock configuration in builder method and removed set_ccm_ccg method

- removed unused `instance` method

- removed duplicated `set_fifo_filter` method in favor of single method that accepts a `FlexCanFilter` as arg

- removed dead code
  • Loading branch information
dstric-aqueduct committed Dec 28, 2022
1 parent c6f055e commit 11daf65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 68 deletions.
7 changes: 7 additions & 0 deletions imxrt-hal/src/can/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

90 changes: 22 additions & 68 deletions imxrt-hal/src/can/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//! );
//!
Expand Down Expand Up @@ -88,15 +88,16 @@ impl Unclocked {
divider: ccm::can::PrescalarSelect,
) -> (Builder<U1>, Builder<U2>) {
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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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<TX, RX>(self, mut tx: TX, mut rx: RX) -> CAN<M>
where
TX: can::Pin<Module = M, Signal = can::Tx>,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1033,8 +992,3 @@ where
Err(nb::Error::Other(Error::NoTxMailbox))
}
}

/// Interface to the Can transmitter part.
pub struct Tx<I> {
_can: PhantomData<I>,
}

0 comments on commit 11daf65

Please sign in to comment.