From 900b1048606989bd3c8aba69b1488611d15ed64d Mon Sep 17 00:00:00 2001 From: Corey Schuhen Date: Sun, 2 Jun 2024 21:45:35 +1000 Subject: [PATCH] Remove generic argument from CanBuilder. --- embassy-stm32/src/can/fd/peripheral.rs | 53 +++++++++++++------------- embassy-stm32/src/can/fdcan.rs | 50 ++++++++++-------------- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/embassy-stm32/src/can/fd/peripheral.rs b/embassy-stm32/src/can/fd/peripheral.rs index 7321ab230..07e3dddad 100644 --- a/embassy-stm32/src/can/fd/peripheral.rs +++ b/embassy-stm32/src/can/fd/peripheral.rs @@ -73,7 +73,6 @@ impl Registers { pub fn put_tx_frame(&self, bufidx: usize, header: &Header, buffer: &[u8]) { let mailbox = self.tx_buffer_element(bufidx); - mailbox.reset(); put_tx_header(mailbox, header); put_tx_data(mailbox, &buffer[..header.len() as usize]); @@ -245,12 +244,12 @@ impl Registers { } #[inline] - fn reset_msg_ram(&mut self) { + fn reset_msg_ram(&self) { self.msg_ram_mut().reset(); } #[inline] - fn enter_init_mode(&mut self) { + fn enter_init_mode(&self) { self.regs.cccr().modify(|w| w.set_init(true)); while false == self.regs.cccr().read().init() {} self.regs.cccr().modify(|w| w.set_cce(true)); @@ -259,7 +258,7 @@ impl Registers { /// Enables or disables loopback mode: Internally connects the TX and RX /// signals together. #[inline] - fn set_loopback_mode(&mut self, mode: LoopbackMode) { + fn set_loopback_mode(&self, mode: LoopbackMode) { let (test, mon, lbck) = match mode { LoopbackMode::None => (false, false, false), LoopbackMode::Internal => (true, true, true), @@ -274,34 +273,34 @@ impl Registers { /// Enables or disables silent mode: Disconnects the TX signal from the pin. #[inline] - fn set_bus_monitoring_mode(&mut self, enabled: bool) { + fn set_bus_monitoring_mode(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_mon(enabled)); } #[inline] - fn set_restricted_operations(&mut self, enabled: bool) { + fn set_restricted_operations(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_asm(enabled)); } #[inline] - fn set_normal_operations(&mut self, _enabled: bool) { + fn set_normal_operations(&self, _enabled: bool) { self.set_loopback_mode(LoopbackMode::None); } #[inline] - fn set_test_mode(&mut self, enabled: bool) { + fn set_test_mode(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_test(enabled)); } #[inline] - fn set_power_down_mode(&mut self, enabled: bool) { + fn set_power_down_mode(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_csr(enabled)); while self.regs.cccr().read().csa() != enabled {} } /// Moves out of PoweredDownMode and into ConfigMode #[inline] - pub fn into_config_mode(mut self, _config: FdCanConfig) { + pub fn into_config_mode(self, _config: FdCanConfig) { self.set_power_down_mode(false); self.enter_init_mode(); self.reset_msg_ram(); @@ -328,7 +327,7 @@ impl Registers { /// Applies the settings of a new FdCanConfig See [`FdCanConfig`] #[inline] - pub fn apply_config(&mut self, config: FdCanConfig) { + pub fn apply_config(&self, config: FdCanConfig) { self.set_tx_buffer_mode(config.tx_buffer_mode); // set standard filters list size to 28 @@ -389,7 +388,7 @@ impl Registers { } #[inline] - fn leave_init_mode(&mut self, config: FdCanConfig) { + fn leave_init_mode(&self, config: FdCanConfig) { self.apply_config(config); self.regs.cccr().modify(|w| w.set_cce(false)); @@ -399,7 +398,7 @@ impl Registers { /// Moves out of ConfigMode and into specified mode #[inline] - pub fn into_mode(mut self, config: FdCanConfig, mode: crate::can::_version::OperatingMode) { + pub fn into_mode(&self, config: FdCanConfig, mode: crate::can::_version::OperatingMode) { match mode { crate::can::OperatingMode::InternalLoopbackMode => self.set_loopback_mode(LoopbackMode::Internal), crate::can::OperatingMode::ExternalLoopbackMode => self.set_loopback_mode(LoopbackMode::External), @@ -423,7 +422,7 @@ impl Registers { /// Then copy the `CAN_BUS_TIME` register value from the table and pass it as the `btr` /// parameter to this method. #[inline] - pub fn set_nominal_bit_timing(&mut self, btr: NominalBitTiming) { + pub fn set_nominal_bit_timing(&self, btr: NominalBitTiming) { self.regs.nbtp().write(|w| { w.set_nbrp(btr.nbrp() - 1); w.set_ntseg1(btr.ntseg1() - 1); @@ -435,7 +434,7 @@ impl Registers { /// Configures the data bit timings for the FdCan Variable Bitrates. /// This is not used when frame_transmit is set to anything other than AllowFdCanAndBRS. #[inline] - pub fn set_data_bit_timing(&mut self, btr: DataBitTiming) { + pub fn set_data_bit_timing(&self, btr: DataBitTiming) { self.regs.dbtp().write(|w| { w.set_dbrp(btr.dbrp() - 1); w.set_dtseg1(btr.dtseg1() - 1); @@ -451,39 +450,39 @@ impl Registers { /// /// Automatic retransmission is enabled by default. #[inline] - pub fn set_automatic_retransmit(&mut self, enabled: bool) { + pub fn set_automatic_retransmit(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_dar(!enabled)); } /// Configures the transmit pause feature. See /// [`FdCanConfig::set_transmit_pause`] #[inline] - pub fn set_transmit_pause(&mut self, enabled: bool) { + pub fn set_transmit_pause(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_txp(!enabled)); } /// Configures non-iso mode. See [`FdCanConfig::set_non_iso_mode`] #[inline] - pub fn set_non_iso_mode(&mut self, enabled: bool) { + pub fn set_non_iso_mode(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_niso(enabled)); } /// Configures edge filtering. See [`FdCanConfig::set_edge_filtering`] #[inline] - pub fn set_edge_filtering(&mut self, enabled: bool) { + pub fn set_edge_filtering(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_efbi(enabled)); } /// Configures TX Buffer Mode #[inline] - pub fn set_tx_buffer_mode(&mut self, tbm: TxBufferMode) { + pub fn set_tx_buffer_mode(&self, tbm: TxBufferMode) { self.regs.txbc().write(|w| w.set_tfqm(tbm.into())); } /// Configures frame transmission mode. See /// [`FdCanConfig::set_frame_transmit`] #[inline] - pub fn set_frame_transmit(&mut self, fts: FrameTransmissionConfig) { + pub fn set_frame_transmit(&self, fts: FrameTransmissionConfig) { let (fdoe, brse) = match fts { FrameTransmissionConfig::ClassicCanOnly => (false, false), FrameTransmissionConfig::AllowFdCan => (true, false), @@ -501,14 +500,14 @@ impl Registers { /// Sets the protocol exception handling on/off #[inline] - pub fn set_protocol_exception_handling(&mut self, enabled: bool) { + pub fn set_protocol_exception_handling(&self, enabled: bool) { self.regs.cccr().modify(|w| w.set_pxhd(!enabled)); } /// Configures and resets the timestamp counter #[inline] #[allow(unused)] - pub fn set_timestamp_counter_source(&mut self, select: TimestampSource) { + pub fn set_timestamp_counter_source(&self, select: TimestampSource) { #[cfg(can_fdcan_h7)] let (tcp, tss) = match select { TimestampSource::None => (0, 0), @@ -532,7 +531,7 @@ impl Registers { #[cfg(not(can_fdcan_h7))] /// Configures the global filter settings #[inline] - pub fn set_global_filter(&mut self, filter: GlobalFilter) { + pub fn set_global_filter(&self, filter: GlobalFilter) { let anfs = match filter.handle_standard_frames { crate::can::fd::config::NonMatchingFilter::IntoRxFifo0 => stm32_metapac::can::vals::Anfs::ACCEPT_FIFO_0, crate::can::fd::config::NonMatchingFilter::IntoRxFifo1 => stm32_metapac::can::vals::Anfs::ACCEPT_FIFO_1, @@ -555,7 +554,7 @@ impl Registers { #[cfg(can_fdcan_h7)] /// Configures the global filter settings #[inline] - pub fn set_global_filter(&mut self, filter: GlobalFilter) { + pub fn set_global_filter(&self, filter: GlobalFilter) { let anfs = match filter.handle_standard_frames { crate::can::fd::config::NonMatchingFilter::IntoRxFifo0 => 0, crate::can::fd::config::NonMatchingFilter::IntoRxFifo1 => 1, @@ -577,10 +576,10 @@ impl Registers { } #[cfg(not(can_fdcan_h7))] - fn configure_msg_ram(&mut self) {} + fn configure_msg_ram(&self) {} #[cfg(can_fdcan_h7)] - fn configure_msg_ram(&mut self) { + fn configure_msg_ram(&self) { let r = self.regs; use crate::can::fd::message_ram::*; diff --git a/embassy-stm32/src/can/fdcan.rs b/embassy-stm32/src/can/fdcan.rs index a838d0412..f3ad718ae 100644 --- a/embassy-stm32/src/can/fdcan.rs +++ b/embassy-stm32/src/can/fdcan.rs @@ -141,13 +141,16 @@ pub enum OperatingMode { //TestMode, } -fn calc_ns_per_timer_tick(mode: crate::can::fd::config::FrameTransmissionConfig) -> u64 { +fn calc_ns_per_timer_tick( + info: &'static Info, + freq: crate::time::Hertz, + mode: crate::can::fd::config::FrameTransmissionConfig, +) -> u64 { match mode { // Use timestamp from Rx FIFO to adjust timestamp reported to user crate::can::fd::config::FrameTransmissionConfig::ClassicCanOnly => { - let freq = T::frequency(); - let prescale: u64 = ({ T::registers().regs.nbtp().read().nbrp() } + 1) as u64 - * ({ T::registers().regs.tscc().read().tcp() } + 1) as u64; + let prescale: u64 = ({ info.regs.regs.nbtp().read().nbrp() } + 1) as u64 + * ({ info.regs.regs.tscc().read().tcp() } + 1) as u64; 1_000_000_000 as u64 / (freq.0 as u64 * prescale) } // For VBR this is too hard because the FDCAN timer switches clock rate you need to configure to use @@ -158,28 +161,28 @@ fn calc_ns_per_timer_tick(mode: crate::can::fd::config::FrameTransm /// FDCAN Configuration instance instance /// Create instance of this first -pub struct CanConfigurator<'d, T: Instance> { +pub struct CanConfigurator<'d> { + _phantom: PhantomData<&'d ()>, config: crate::can::fd::config::FdCanConfig, info: &'static Info, state: &'static State, /// Reference to internals. - _instance: FdcanInstance<'d, T>, properties: Properties, periph_clock: crate::time::Hertz, } -impl<'d, T: Instance> CanConfigurator<'d, T> { +impl<'d> CanConfigurator<'d> { /// Creates a new Fdcan instance, keeping the peripheral in sleep mode. /// You must call [Fdcan::enable_non_blocking] to use the peripheral. - pub fn new( - peri: impl Peripheral

+ 'd, + pub fn new( + _peri: impl Peripheral

+ 'd, rx: impl Peripheral

> + 'd, tx: impl Peripheral

> + 'd, _irqs: impl interrupt::typelevel::Binding> + interrupt::typelevel::Binding> + 'd, - ) -> CanConfigurator<'d, T> { - into_ref!(peri, rx, tx); + ) -> CanConfigurator<'d> { + into_ref!(_peri, rx, tx); rx.set_as_af(rx.af_num(), AFType::Input); tx.set_as_af(tx.af_num(), AFType::OutputPushPull); @@ -201,10 +204,10 @@ impl<'d, T: Instance> CanConfigurator<'d, T> { T::IT1Interrupt::enable(); } Self { + _phantom: PhantomData, config, info: T::info(), state: T::state(), - _instance: FdcanInstance(peri), properties: Properties::new(T::info()), periph_clock: T::frequency(), } @@ -255,7 +258,7 @@ impl<'d, T: Instance> CanConfigurator<'d, T> { /// Start in mode. pub fn start(self, mode: OperatingMode) -> Can<'d> { - let ns_per_timer_tick = calc_ns_per_timer_tick::(self.config.frame_transmit); + let ns_per_timer_tick = calc_ns_per_timer_tick(self.info, self.periph_clock, self.config.frame_transmit); critical_section::with(|_| { let state = self.state as *const State; unsafe { @@ -263,15 +266,14 @@ impl<'d, T: Instance> CanConfigurator<'d, T> { (*mut_state).ns_per_timer_tick = ns_per_timer_tick; } }); - T::registers().into_mode(self.config, mode); + self.info.regs.into_mode(self.config, mode); Can { _phantom: PhantomData, config: self.config, info: self.info, state: self.state, - instance: T::info().regs.regs, _mode: mode, - properties: Properties::new(T::info()), + properties: Properties::new(self.info), } } @@ -297,7 +299,6 @@ pub struct Can<'d> { config: crate::can::fd::config::FdCanConfig, info: &'static Info, state: &'static State, - instance: crate::pac::can::Fdcan, _mode: OperatingMode, properties: Properties, } @@ -360,14 +361,12 @@ impl<'d> Can<'d> { info: self.info, state: self.state, config: self.config, - _instance: self.instance, _mode: self._mode, }, CanRx { _phantom: PhantomData, info: self.info, state: self.state, - _instance: self.instance, _mode: self._mode, }, self.properties, @@ -380,7 +379,6 @@ impl<'d> Can<'d> { config: tx.config, info: tx.info, state: tx.state, - instance: tx._instance, _mode: rx._mode, properties: Properties::new(tx.info), } @@ -392,7 +390,7 @@ impl<'d> Can<'d> { tx_buf: &'static mut TxBuf, rxb: &'static mut RxBuf, ) -> BufferedCan<'d, TX_BUF_SIZE, RX_BUF_SIZE> { - BufferedCan::new(self.info, self.state, self.info.regs.regs, self._mode, tx_buf, rxb) + BufferedCan::new(self.info, self.state, self._mode, tx_buf, rxb) } /// Return a buffered instance of driver with CAN FD support. User must supply Buffers @@ -401,7 +399,7 @@ impl<'d> Can<'d> { tx_buf: &'static mut TxFdBuf, rxb: &'static mut RxFdBuf, ) -> BufferedCanFd<'d, TX_BUF_SIZE, RX_BUF_SIZE> { - BufferedCanFd::new(self.info, self.state, self.info.regs.regs, self._mode, tx_buf, rxb) + BufferedCanFd::new(self.info, self.state, self._mode, tx_buf, rxb) } } @@ -416,7 +414,6 @@ pub struct BufferedCan<'d, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> { _phantom: PhantomData<&'d ()>, info: &'static Info, state: &'static State, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, tx_buf: &'static TxBuf, rx_buf: &'static RxBuf, @@ -427,7 +424,6 @@ impl<'c, 'd, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> BufferedCan<'d, fn new( info: &'static Info, state: &'static State, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, tx_buf: &'static TxBuf, rx_buf: &'static RxBuf, @@ -436,7 +432,6 @@ impl<'c, 'd, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> BufferedCan<'d, _phantom: PhantomData, info, state, - _instance, _mode, tx_buf, rx_buf, @@ -549,7 +544,6 @@ pub struct BufferedCanFd<'d, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> _phantom: PhantomData<&'d ()>, info: &'static Info, state: &'static State, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, tx_buf: &'static TxFdBuf, rx_buf: &'static RxFdBuf, @@ -560,7 +554,6 @@ impl<'c, 'd, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> BufferedCanFd<' fn new( info: &'static Info, state: &'static State, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, tx_buf: &'static TxFdBuf, rx_buf: &'static RxFdBuf, @@ -569,7 +562,6 @@ impl<'c, 'd, const TX_BUF_SIZE: usize, const RX_BUF_SIZE: usize> BufferedCanFd<' _phantom: PhantomData, info, state, - _instance, _mode, tx_buf, rx_buf, @@ -646,7 +638,6 @@ pub struct CanRx<'d> { _phantom: PhantomData<&'d ()>, info: &'static Info, state: &'static State, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, } @@ -668,7 +659,6 @@ pub struct CanTx<'d> { info: &'static Info, state: &'static State, config: crate::can::fd::config::FdCanConfig, - _instance: crate::pac::can::Fdcan, _mode: OperatingMode, }