aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorey Schuhen <[email protected]>2024-03-02 12:57:03 +1000
committerCorey Schuhen <[email protected]>2024-03-02 14:18:12 +1000
commitb693ab9b34fad5844401bc80b9968940e750680e (patch)
tree623afd3119d8efbd11b9bd2fe25b2b911452d84c
parentbf06d10534fcf6e6f2fd34c1517500a59ed4b626 (diff)
Restore init order to restore H7.
Previous commit broke H7 support in HIL farm. Restore previous order by moving a bunch of config from new and into_config_mode to apply_config. This is a cleanup that I had considered to move more register access into peripheral.rs.
-rw-r--r--embassy-stm32/src/can/fd/peripheral.rs133
-rw-r--r--embassy-stm32/src/can/fdcan.rs92
2 files changed, 113 insertions, 112 deletions
diff --git a/embassy-stm32/src/can/fd/peripheral.rs b/embassy-stm32/src/can/fd/peripheral.rs
index f0aab132b..8ec09ac12 100644
--- a/embassy-stm32/src/can/fd/peripheral.rs
+++ b/embassy-stm32/src/can/fd/peripheral.rs
@@ -22,6 +22,7 @@ enum LoopbackMode {
22pub struct Registers { 22pub struct Registers {
23 pub regs: &'static crate::pac::can::Fdcan, 23 pub regs: &'static crate::pac::can::Fdcan,
24 pub msgram: &'static crate::pac::fdcanram::Fdcanram, 24 pub msgram: &'static crate::pac::fdcanram::Fdcanram,
25 pub msg_ram_offset: usize,
25} 26}
26 27
27impl Registers { 28impl Registers {
@@ -294,7 +295,6 @@ impl Registers {
294 pub fn into_config_mode(mut self, _config: FdCanConfig) { 295 pub fn into_config_mode(mut self, _config: FdCanConfig) {
295 self.set_power_down_mode(false); 296 self.set_power_down_mode(false);
296 self.enter_init_mode(); 297 self.enter_init_mode();
297
298 self.reset_msg_ram(); 298 self.reset_msg_ram();
299 299
300 // check the FDCAN core matches our expections 300 // check the FDCAN core matches our expections
@@ -307,27 +307,6 @@ impl Registers {
307 "Error reading endianness test value from FDCAN core" 307 "Error reading endianness test value from FDCAN core"
308 ); 308 );
309 309
310 // set standard filters list size to 28
311 // set extended filters list size to 8
312 // REQUIRED: we use the memory map as if these settings are set
313 // instead of re-calculating them.
314 #[cfg(not(stm32h7))]
315 {
316 self.regs.rxgfc().modify(|w| {
317 w.set_lss(crate::can::fd::message_ram::STANDARD_FILTER_MAX);
318 w.set_lse(crate::can::fd::message_ram::EXTENDED_FILTER_MAX);
319 });
320 }
321 #[cfg(stm32h7)]
322 {
323 self.regs
324 .sidfc()
325 .modify(|w| w.set_lss(crate::can::fd::message_ram::STANDARD_FILTER_MAX));
326 self.regs
327 .xidfc()
328 .modify(|w| w.set_lse(crate::can::fd::message_ram::EXTENDED_FILTER_MAX));
329 }
330
331 /* 310 /*
332 for fid in 0..crate::can::message_ram::STANDARD_FILTER_MAX { 311 for fid in 0..crate::can::message_ram::STANDARD_FILTER_MAX {
333 self.set_standard_filter((fid as u8).into(), StandardFilter::disable()); 312 self.set_standard_filter((fid as u8).into(), StandardFilter::disable());
@@ -353,6 +332,51 @@ impl Registers {
353 #[inline] 332 #[inline]
354 pub fn apply_config(&mut self, config: FdCanConfig) { 333 pub fn apply_config(&mut self, config: FdCanConfig) {
355 self.set_tx_buffer_mode(config.tx_buffer_mode); 334 self.set_tx_buffer_mode(config.tx_buffer_mode);
335
336 // set standard filters list size to 28
337 // set extended filters list size to 8
338 // REQUIRED: we use the memory map as if these settings are set
339 // instead of re-calculating them.
340 #[cfg(not(stm32h7))]
341 {
342 self.regs.rxgfc().modify(|w| {
343 w.set_lss(crate::can::fd::message_ram::STANDARD_FILTER_MAX);
344 w.set_lse(crate::can::fd::message_ram::EXTENDED_FILTER_MAX);
345 });
346 }
347 #[cfg(stm32h7)]
348 {
349 self.regs
350 .sidfc()
351 .modify(|w| w.set_lss(crate::can::fd::message_ram::STANDARD_FILTER_MAX));
352 self.regs
353 .xidfc()
354 .modify(|w| w.set_lse(crate::can::fd::message_ram::EXTENDED_FILTER_MAX));
355 }
356
357 self.configure_msg_ram();
358
359 // Enable timestamping
360 #[cfg(not(stm32h7))]
361 self.regs
362 .tscc()
363 .write(|w| w.set_tss(stm32_metapac::can::vals::Tss::INCREMENT));
364 #[cfg(stm32h7)]
365 self.regs.tscc().write(|w| w.set_tss(0x01));
366
367 // this isn't really documented in the reference manual
368 // but corresponding txbtie bit has to be set for the TC (TxComplete) interrupt to fire
369 self.regs.txbtie().write(|w| w.0 = 0xffff_ffff);
370 self.regs.ie().modify(|w| {
371 w.set_rfne(0, true); // Rx Fifo 0 New Msg
372 w.set_rfne(1, true); // Rx Fifo 1 New Msg
373 w.set_tce(true); // Tx Complete
374 });
375 self.regs.ile().modify(|w| {
376 w.set_eint0(true); // Interrupt Line 0
377 w.set_eint1(true); // Interrupt Line 1
378 });
379
356 self.set_data_bit_timing(config.dbtr); 380 self.set_data_bit_timing(config.dbtr);
357 self.set_nominal_bit_timing(config.nbtr); 381 self.set_nominal_bit_timing(config.nbtr);
358 self.set_automatic_retransmit(config.automatic_retransmit); 382 self.set_automatic_retransmit(config.automatic_retransmit);
@@ -600,6 +624,71 @@ impl Registers {
600 w.set_rrfe(filter.reject_remote_extended_frames); 624 w.set_rrfe(filter.reject_remote_extended_frames);
601 }); 625 });
602 } 626 }
627
628 #[cfg(not(stm32h7))]
629 fn configure_msg_ram(&mut self) {}
630
631 #[cfg(stm32h7)]
632 fn configure_msg_ram(&mut self) {
633 let r = self.regs;
634
635 use crate::can::fd::message_ram::*;
636 //use fdcan::message_ram::*;
637 let mut offset_words = self.msg_ram_offset as u16;
638
639 // 11-bit filter
640 r.sidfc().modify(|w| w.set_flssa(offset_words));
641 offset_words += STANDARD_FILTER_MAX as u16;
642
643 // 29-bit filter
644 r.xidfc().modify(|w| w.set_flesa(offset_words));
645 offset_words += 2 * EXTENDED_FILTER_MAX as u16;
646
647 // Rx FIFO 0 and 1
648 for i in 0..=1 {
649 r.rxfc(i).modify(|w| {
650 w.set_fsa(offset_words);
651 w.set_fs(RX_FIFO_MAX);
652 w.set_fwm(RX_FIFO_MAX);
653 });
654 offset_words += 18 * RX_FIFO_MAX as u16;
655 }
656
657 // Rx buffer - see below
658 // Tx event FIFO
659 r.txefc().modify(|w| {
660 w.set_efsa(offset_words);
661 w.set_efs(TX_EVENT_MAX);
662 w.set_efwm(TX_EVENT_MAX);
663 });
664 offset_words += 2 * TX_EVENT_MAX as u16;
665
666 // Tx buffers
667 r.txbc().modify(|w| {
668 w.set_tbsa(offset_words);
669 w.set_tfqs(TX_FIFO_MAX);
670 });
671 offset_words += 18 * TX_FIFO_MAX as u16;
672
673 // Rx Buffer - not used
674 r.rxbc().modify(|w| {
675 w.set_rbsa(offset_words);
676 });
677
678 // TX event FIFO?
679 // Trigger memory?
680
681 // Set the element sizes to 16 bytes
682 r.rxesc().modify(|w| {
683 w.set_rbds(0b111);
684 for i in 0..=1 {
685 w.set_fds(i, 0b111);
686 }
687 });
688 r.txesc().modify(|w| {
689 w.set_tbds(0b111);
690 })
691 }
603} 692}
604 693
605fn make_id(id: u32, extended: bool) -> embedded_can::Id { 694fn make_id(id: u32, extended: bool) -> embedded_can::Id {
diff --git a/embassy-stm32/src/can/fdcan.rs b/embassy-stm32/src/can/fdcan.rs
index 77db774fc..20d00ccb5 100644
--- a/embassy-stm32/src/can/fdcan.rs
+++ b/embassy-stm32/src/can/fdcan.rs
@@ -184,43 +184,20 @@ impl<'d, T: Instance> FdcanConfigurator<'d, T> {
184 T::enable_and_reset(); 184 T::enable_and_reset();
185 185
186 let mut config = crate::can::fd::config::FdCanConfig::default(); 186 let mut config = crate::can::fd::config::FdCanConfig::default();
187 config.timestamp_source = TimestampSource::Prescaler(TimestampPrescaler::_1);
187 T::registers().into_config_mode(config); 188 T::registers().into_config_mode(config);
188 189
189 rx.set_as_af(rx.af_num(), AFType::Input); 190 rx.set_as_af(rx.af_num(), AFType::Input);
190 tx.set_as_af(tx.af_num(), AFType::OutputPushPull); 191 tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
191 192
192 T::configure_msg_ram();
193 unsafe { 193 unsafe {
194 // Enable timestamping
195 #[cfg(not(stm32h7))]
196 T::regs()
197 .tscc()
198 .write(|w| w.set_tss(stm32_metapac::can::vals::Tss::INCREMENT));
199 #[cfg(stm32h7)]
200 T::regs().tscc().write(|w| w.set_tss(0x01));
201 config.timestamp_source = TimestampSource::Prescaler(TimestampPrescaler::_1);
202
203 T::IT0Interrupt::unpend(); // Not unsafe 194 T::IT0Interrupt::unpend(); // Not unsafe
204 T::IT0Interrupt::enable(); 195 T::IT0Interrupt::enable();
205 196
206 T::IT1Interrupt::unpend(); // Not unsafe 197 T::IT1Interrupt::unpend(); // Not unsafe
207 T::IT1Interrupt::enable(); 198 T::IT1Interrupt::enable();
208
209 // this isn't really documented in the reference manual
210 // but corresponding txbtie bit has to be set for the TC (TxComplete) interrupt to fire
211 T::regs().txbtie().write(|w| w.0 = 0xffff_ffff);
212 } 199 }
213 200
214 T::regs().ie().modify(|w| {
215 w.set_rfne(0, true); // Rx Fifo 0 New Msg
216 w.set_rfne(1, true); // Rx Fifo 1 New Msg
217 w.set_tce(true); // Tx Complete
218 });
219 T::regs().ile().modify(|w| {
220 w.set_eint0(true); // Interrupt Line 0
221 w.set_eint1(true); // Interrupt Line 1
222 });
223
224 Self { 201 Self {
225 config, 202 config,
226 instance: FdcanInstance(peri), 203 instance: FdcanInstance(peri),
@@ -869,71 +846,6 @@ pub(crate) mod sealed {
869 fn state() -> &'static State; 846 fn state() -> &'static State;
870 unsafe fn mut_state() -> &'static mut State; 847 unsafe fn mut_state() -> &'static mut State;
871 fn calc_timestamp(ns_per_timer_tick: u64, ts_val: u16) -> Timestamp; 848 fn calc_timestamp(ns_per_timer_tick: u64, ts_val: u16) -> Timestamp;
872
873 #[cfg(not(stm32h7))]
874 fn configure_msg_ram() {}
875
876 #[cfg(stm32h7)]
877 fn configure_msg_ram() {
878 let r = Self::regs();
879
880 use crate::can::fd::message_ram::*;
881 //use fdcan::message_ram::*;
882 let mut offset_words = Self::MSG_RAM_OFFSET as u16;
883
884 // 11-bit filter
885 r.sidfc().modify(|w| w.set_flssa(offset_words));
886 offset_words += STANDARD_FILTER_MAX as u16;
887
888 // 29-bit filter
889 r.xidfc().modify(|w| w.set_flesa(offset_words));
890 offset_words += 2 * EXTENDED_FILTER_MAX as u16;
891
892 // Rx FIFO 0 and 1
893 for i in 0..=1 {
894 r.rxfc(i).modify(|w| {
895 w.set_fsa(offset_words);
896 w.set_fs(RX_FIFO_MAX);
897 w.set_fwm(RX_FIFO_MAX);
898 });
899 offset_words += 18 * RX_FIFO_MAX as u16;
900 }
901
902 // Rx buffer - see below
903 // Tx event FIFO
904 r.txefc().modify(|w| {
905 w.set_efsa(offset_words);
906 w.set_efs(TX_EVENT_MAX);
907 w.set_efwm(TX_EVENT_MAX);
908 });
909 offset_words += 2 * TX_EVENT_MAX as u16;
910
911 // Tx buffers
912 r.txbc().modify(|w| {
913 w.set_tbsa(offset_words);
914 w.set_tfqs(TX_FIFO_MAX);
915 });
916 offset_words += 18 * TX_FIFO_MAX as u16;
917
918 // Rx Buffer - not used
919 r.rxbc().modify(|w| {
920 w.set_rbsa(offset_words);
921 });
922
923 // TX event FIFO?
924 // Trigger memory?
925
926 // Set the element sizes to 16 bytes
927 r.rxesc().modify(|w| {
928 w.set_rbds(0b111);
929 for i in 0..=1 {
930 w.set_fds(i, 0b111);
931 }
932 });
933 r.txesc().modify(|w| {
934 w.set_tbds(0b111);
935 })
936 }
937 } 849 }
938} 850}
939 851
@@ -957,7 +869,7 @@ macro_rules! impl_fdcan {
957 &crate::pac::$inst 869 &crate::pac::$inst
958 } 870 }
959 fn registers() -> Registers { 871 fn registers() -> Registers {
960 Registers{regs: &crate::pac::$inst, msgram: &crate::pac::$msg_ram_inst} 872 Registers{regs: &crate::pac::$inst, msgram: &crate::pac::$msg_ram_inst, msg_ram_offset: Self::MSG_RAM_OFFSET}
961 } 873 }
962 fn ram() -> &'static crate::pac::fdcanram::Fdcanram { 874 fn ram() -> &'static crate::pac::fdcanram::Fdcanram {
963 &crate::pac::$msg_ram_inst 875 &crate::pac::$msg_ram_inst