diff options
| author | Corey Schuhen <[email protected]> | 2025-05-31 09:09:12 +1000 |
|---|---|---|
| committer | Corey Schuhen <[email protected]> | 2025-05-31 09:09:12 +1000 |
| commit | 010744802fc4a1fda51f259cce874aac7294a79e (patch) | |
| tree | 23f04536c9246a80dbe53ac755d7a7c5a40e6854 /embassy-stm32 | |
| parent | f5658d6833cb140296a0b6f25b7eb6d16f06c520 (diff) | |
Remove duplicate calc_timestamp and move to 'Registers' struct.
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/src/can/fd/peripheral.rs | 17 | ||||
| -rw-r--r-- | embassy-stm32/src/can/fdcan.rs | 49 |
2 files changed, 23 insertions, 43 deletions
diff --git a/embassy-stm32/src/can/fd/peripheral.rs b/embassy-stm32/src/can/fd/peripheral.rs index 4873ee444..0a22f2c91 100644 --- a/embassy-stm32/src/can/fd/peripheral.rs +++ b/embassy-stm32/src/can/fd/peripheral.rs | |||
| @@ -71,6 +71,23 @@ impl Registers { | |||
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | #[cfg(feature = "time")] | ||
| 75 | pub fn calc_timestamp(&self, ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 76 | let now_embassy = embassy_time::Instant::now(); | ||
| 77 | if ns_per_timer_tick == 0 { | ||
| 78 | return now_embassy; | ||
| 79 | } | ||
| 80 | let cantime = { self.regs.tscv().read().tsc() }; | ||
| 81 | let delta = cantime.overflowing_sub(ts_val).0 as u64; | ||
| 82 | let ns = ns_per_timer_tick * delta as u64; | ||
| 83 | now_embassy - embassy_time::Duration::from_nanos(ns) | ||
| 84 | } | ||
| 85 | |||
| 86 | #[cfg(not(feature = "time"))] | ||
| 87 | pub fn calc_timestamp(&self, _ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 88 | ts_val | ||
| 89 | } | ||
| 90 | |||
| 74 | pub fn put_tx_frame(&self, bufidx: usize, header: &Header, buffer: &[u8]) { | 91 | pub fn put_tx_frame(&self, bufidx: usize, header: &Header, buffer: &[u8]) { |
| 75 | let mailbox = self.tx_buffer_element(bufidx); | 92 | let mailbox = self.tx_buffer_element(bufidx); |
| 76 | mailbox.reset(); | 93 | mailbox.reset(); |
diff --git a/embassy-stm32/src/can/fdcan.rs b/embassy-stm32/src/can/fdcan.rs index 4495280c4..97d22315a 100644 --- a/embassy-stm32/src/can/fdcan.rs +++ b/embassy-stm32/src/can/fdcan.rs | |||
| @@ -713,10 +713,10 @@ impl RxMode { | |||
| 713 | //async fn read_classic<T: Instance>(&self) -> Result<Envelope, BusError> { | 713 | //async fn read_classic<T: Instance>(&self) -> Result<Envelope, BusError> { |
| 714 | fn try_read<T: Instance>(&self, ns_per_timer_tick: u64) -> Option<Result<Envelope, BusError>> { | 714 | fn try_read<T: Instance>(&self, ns_per_timer_tick: u64) -> Option<Result<Envelope, BusError>> { |
| 715 | if let Some((frame, ts)) = T::registers().read(0) { | 715 | if let Some((frame, ts)) = T::registers().read(0) { |
| 716 | let ts = T::calc_timestamp(ns_per_timer_tick, ts); | 716 | let ts = T::registers().calc_timestamp(ns_per_timer_tick, ts); |
| 717 | Some(Ok(Envelope { ts, frame })) | 717 | Some(Ok(Envelope { ts, frame })) |
| 718 | } else if let Some((frame, ts)) = T::registers().read(1) { | 718 | } else if let Some((frame, ts)) = T::registers().read(1) { |
| 719 | let ts = T::calc_timestamp(ns_per_timer_tick, ts); | 719 | let ts = T::registers().calc_timestamp(ns_per_timer_tick, ts); |
| 720 | Some(Ok(Envelope { ts, frame })) | 720 | Some(Ok(Envelope { ts, frame })) |
| 721 | } else if let Some(err) = T::registers().curr_error() { | 721 | } else if let Some(err) = T::registers().curr_error() { |
| 722 | // TODO: this is probably wrong | 722 | // TODO: this is probably wrong |
| @@ -728,10 +728,10 @@ impl RxMode { | |||
| 728 | 728 | ||
| 729 | fn try_read_fd<T: Instance>(&self, ns_per_timer_tick: u64) -> Option<Result<FdEnvelope, BusError>> { | 729 | fn try_read_fd<T: Instance>(&self, ns_per_timer_tick: u64) -> Option<Result<FdEnvelope, BusError>> { |
| 730 | if let Some((frame, ts)) = T::registers().read(0) { | 730 | if let Some((frame, ts)) = T::registers().read(0) { |
| 731 | let ts = T::calc_timestamp(ns_per_timer_tick, ts); | 731 | let ts = T::registers().calc_timestamp(ns_per_timer_tick, ts); |
| 732 | Some(Ok(FdEnvelope { ts, frame })) | 732 | Some(Ok(FdEnvelope { ts, frame })) |
| 733 | } else if let Some((frame, ts)) = T::registers().read(1) { | 733 | } else if let Some((frame, ts)) = T::registers().read(1) { |
| 734 | let ts = T::calc_timestamp(ns_per_timer_tick, ts); | 734 | let ts = T::registers().calc_timestamp(ns_per_timer_tick, ts); |
| 735 | Some(Ok(FdEnvelope { ts, frame })) | 735 | Some(Ok(FdEnvelope { ts, frame })) |
| 736 | } else if let Some(err) = T::registers().curr_error() { | 736 | } else if let Some(err) = T::registers().curr_error() { |
| 737 | // TODO: this is probably wrong | 737 | // TODO: this is probably wrong |
| @@ -743,10 +743,10 @@ impl RxMode { | |||
| 743 | 743 | ||
| 744 | fn read<F: CanHeader>(info: &'static Info, ns_per_timer_tick: u64) -> Option<Result<(F, Timestamp), BusError>> { | 744 | fn read<F: CanHeader>(info: &'static Info, ns_per_timer_tick: u64) -> Option<Result<(F, Timestamp), BusError>> { |
| 745 | if let Some((msg, ts)) = info.regs.read(0) { | 745 | if let Some((msg, ts)) = info.regs.read(0) { |
| 746 | let ts = info.calc_timestamp(ns_per_timer_tick, ts); | 746 | let ts = info.regs.calc_timestamp(ns_per_timer_tick, ts); |
| 747 | Some(Ok((msg, ts))) | 747 | Some(Ok((msg, ts))) |
| 748 | } else if let Some((msg, ts)) = info.regs.read(1) { | 748 | } else if let Some((msg, ts)) = info.regs.read(1) { |
| 749 | let ts = info.calc_timestamp(ns_per_timer_tick, ts); | 749 | let ts = info.regs.calc_timestamp(ns_per_timer_tick, ts); |
| 750 | Some(Ok((msg, ts))) | 750 | Some(Ok((msg, ts))) |
| 751 | } else if let Some(err) = info.regs.curr_error() { | 751 | } else if let Some(err) = info.regs.curr_error() { |
| 752 | // TODO: this is probably wrong | 752 | // TODO: this is probably wrong |
| @@ -947,32 +947,12 @@ struct Info { | |||
| 947 | state: SharedState, | 947 | state: SharedState, |
| 948 | } | 948 | } |
| 949 | 949 | ||
| 950 | impl Info { | ||
| 951 | #[cfg(feature = "time")] | ||
| 952 | fn calc_timestamp(&self, ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 953 | let now_embassy = embassy_time::Instant::now(); | ||
| 954 | if ns_per_timer_tick == 0 { | ||
| 955 | return now_embassy; | ||
| 956 | } | ||
| 957 | let cantime = { self.regs.regs.tscv().read().tsc() }; | ||
| 958 | let delta = cantime.overflowing_sub(ts_val).0 as u64; | ||
| 959 | let ns = ns_per_timer_tick * delta as u64; | ||
| 960 | now_embassy - embassy_time::Duration::from_nanos(ns) | ||
| 961 | } | ||
| 962 | |||
| 963 | #[cfg(not(feature = "time"))] | ||
| 964 | fn calc_timestamp(&self, _ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 965 | ts_val | ||
| 966 | } | ||
| 967 | } | ||
| 968 | |||
| 969 | trait SealedInstance { | 950 | trait SealedInstance { |
| 970 | const MSG_RAM_OFFSET: usize; | 951 | const MSG_RAM_OFFSET: usize; |
| 971 | 952 | ||
| 972 | fn info() -> &'static Info; | 953 | fn info() -> &'static Info; |
| 973 | fn registers() -> crate::can::fd::peripheral::Registers; | 954 | fn registers() -> crate::can::fd::peripheral::Registers; |
| 974 | fn internal_operation(val: InternalOperation); | 955 | fn internal_operation(val: InternalOperation); |
| 975 | fn calc_timestamp(ns_per_timer_tick: u64, ts_val: u16) -> Timestamp; | ||
| 976 | } | 956 | } |
| 977 | 957 | ||
| 978 | /// Instance trait | 958 | /// Instance trait |
| @@ -1045,23 +1025,6 @@ macro_rules! impl_fdcan { | |||
| 1045 | Registers{regs: crate::pac::$inst, msgram: crate::pac::$msg_ram_inst, msg_ram_offset: Self::MSG_RAM_OFFSET} | 1025 | Registers{regs: crate::pac::$inst, msgram: crate::pac::$msg_ram_inst, msg_ram_offset: Self::MSG_RAM_OFFSET} |
| 1046 | } | 1026 | } |
| 1047 | 1027 | ||
| 1048 | #[cfg(feature = "time")] | ||
| 1049 | fn calc_timestamp(ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 1050 | let now_embassy = embassy_time::Instant::now(); | ||
| 1051 | if ns_per_timer_tick == 0 { | ||
| 1052 | return now_embassy; | ||
| 1053 | } | ||
| 1054 | let cantime = { Self::registers().regs.tscv().read().tsc() }; | ||
| 1055 | let delta = cantime.overflowing_sub(ts_val).0 as u64; | ||
| 1056 | let ns = ns_per_timer_tick * delta as u64; | ||
| 1057 | now_embassy - embassy_time::Duration::from_nanos(ns) | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | #[cfg(not(feature = "time"))] | ||
| 1061 | fn calc_timestamp(_ns_per_timer_tick: u64, ts_val: u16) -> Timestamp { | ||
| 1062 | ts_val | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | } | 1028 | } |
| 1066 | 1029 | ||
| 1067 | #[allow(non_snake_case)] | 1030 | #[allow(non_snake_case)] |
