diff options
| author | Corey Schuhen <[email protected]> | 2024-02-03 09:44:00 +1000 |
|---|---|---|
| committer | Corey Schuhen <[email protected]> | 2024-02-17 18:25:58 +1000 |
| commit | 70b3c4374d57ab638e0cb76013b725e1ea229546 (patch) | |
| tree | 0c0f13b68c6e67d1f2ae49fafb62e5c38754c8c6 /tests | |
| parent | 377e58e408f830f79171a470ba602b7d8bc525e4 (diff) | |
Port FDCAN HAL to use PAC directly instead of fdcan crate.
- Provide separate FDCAN capable and Classic CAN API's
- Don't use fdcan crate dep anymore
- Provide embedded-can traits.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/stm32/Cargo.toml | 1 | ||||
| -rw-r--r-- | tests/stm32/src/bin/fdcan.rs | 109 |
2 files changed, 29 insertions, 81 deletions
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 8554682a4..5b28b5849 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -69,6 +69,7 @@ cortex-m-rt = "0.7.0" | |||
| 69 | embedded-hal = "0.2.6" | 69 | embedded-hal = "0.2.6" |
| 70 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } | 70 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } |
| 71 | embedded-hal-async = { version = "1.0" } | 71 | embedded-hal-async = { version = "1.0" } |
| 72 | embedded-can = { version = "0.4" } | ||
| 72 | micromath = "2.0.0" | 73 | micromath = "2.0.0" |
| 73 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | 74 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } |
| 74 | rand_core = { version = "0.6", default-features = false } | 75 | rand_core = { version = "0.6", default-features = false } |
diff --git a/tests/stm32/src/bin/fdcan.rs b/tests/stm32/src/bin/fdcan.rs index 7363eaa16..76c27d091 100644 --- a/tests/stm32/src/bin/fdcan.rs +++ b/tests/stm32/src/bin/fdcan.rs | |||
| @@ -36,7 +36,7 @@ fn options() -> TestOptions { | |||
| 36 | c.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE; | 36 | c.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE; |
| 37 | TestOptions { | 37 | TestOptions { |
| 38 | config: c, | 38 | config: c, |
| 39 | max_latency: Duration::from_micros(3800), | 39 | max_latency: Duration::from_micros(1200), |
| 40 | second_fifo_working: false, | 40 | second_fifo_working: false, |
| 41 | } | 41 | } |
| 42 | } | 42 | } |
| @@ -53,12 +53,12 @@ fn options() -> TestOptions { | |||
| 53 | c.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE; | 53 | c.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE; |
| 54 | TestOptions { | 54 | TestOptions { |
| 55 | config: c, | 55 | config: c, |
| 56 | max_latency: Duration::from_micros(5500), | 56 | max_latency: Duration::from_micros(1200), |
| 57 | second_fifo_working: false, | 57 | second_fifo_working: false, |
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | #[cfg(any(feature = "stm32g491re"))] | 61 | #[cfg(any(feature = "stm32g491re", feature = "stm32g431cb"))] |
| 62 | fn options() -> TestOptions { | 62 | fn options() -> TestOptions { |
| 63 | info!("G4 config"); | 63 | info!("G4 config"); |
| 64 | TestOptions { | 64 | TestOptions { |
| @@ -80,9 +80,9 @@ async fn main(_spawner: Spawner) { | |||
| 80 | // 250k bps | 80 | // 250k bps |
| 81 | can.set_bitrate(250_000); | 81 | can.set_bitrate(250_000); |
| 82 | 82 | ||
| 83 | can.can.set_extended_filter( | 83 | can.set_extended_filter( |
| 84 | can::filter::ExtendedFilterSlot::_0, | 84 | can::fd::filter::ExtendedFilterSlot::_0, |
| 85 | can::filter::ExtendedFilter::accept_all_into_fifo1(), | 85 | can::fd::filter::ExtendedFilter::accept_all_into_fifo1(), |
| 86 | ); | 86 | ); |
| 87 | 87 | ||
| 88 | let mut can = can.into_internal_loopback_mode(); | 88 | let mut can = can.into_internal_loopback_mode(); |
| @@ -91,31 +91,21 @@ async fn main(_spawner: Spawner) { | |||
| 91 | 91 | ||
| 92 | let mut i: u8 = 0; | 92 | let mut i: u8 = 0; |
| 93 | loop { | 93 | loop { |
| 94 | let tx_frame = can::TxFrame::new( | 94 | let tx_frame = can::frame::ClassicFrame::new_standard(0x123, &[i; 1]).unwrap(); |
| 95 | can::TxFrameHeader { | ||
| 96 | len: 1, | ||
| 97 | frame_format: can::FrameFormat::Standard, | ||
| 98 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 99 | bit_rate_switching: false, | ||
| 100 | marker: None, | ||
| 101 | }, | ||
| 102 | &[i], | ||
| 103 | ) | ||
| 104 | .unwrap(); | ||
| 105 | 95 | ||
| 106 | info!("Transmitting frame..."); | 96 | info!("Transmitting frame..."); |
| 107 | let tx_ts = Instant::now(); | 97 | let tx_ts = Instant::now(); |
| 108 | can.write(&tx_frame).await; | 98 | can.write(&tx_frame).await; |
| 109 | 99 | ||
| 110 | let envelope = can.read().await.unwrap(); | 100 | let (frame, timestamp) = can.read().await.unwrap(); |
| 111 | info!("Frame received!"); | 101 | info!("Frame received!"); |
| 112 | 102 | ||
| 113 | // Check data. | 103 | // Check data. |
| 114 | assert!(i == envelope.data()[0], "{} == {}", i, envelope.data()[0]); | 104 | assert!(i == frame.data()[0], "{} == {}", i, frame.data()[0]); |
| 115 | 105 | ||
| 116 | info!("loopback time {}", envelope.header.time_stamp); | 106 | info!("loopback time {}", timestamp); |
| 117 | info!("loopback frame {=u8}", envelope.data()[0]); | 107 | info!("loopback frame {=u8}", frame.data()[0]); |
| 118 | let latency = envelope.timestamp.saturating_duration_since(tx_ts); | 108 | let latency = timestamp.saturating_duration_since(tx_ts); |
| 119 | info!("loopback latency {} us", latency.as_micros()); | 109 | info!("loopback latency {} us", latency.as_micros()); |
| 120 | 110 | ||
| 121 | // Theoretical minimum latency is 55us, actual is usually ~80us | 111 | // Theoretical minimum latency is 55us, actual is usually ~80us |
| @@ -143,47 +133,26 @@ async fn main(_spawner: Spawner) { | |||
| 143 | // in each FIFO so make sure we write enough to fill them both up before reading. | 133 | // in each FIFO so make sure we write enough to fill them both up before reading. |
| 144 | for i in 0..3 { | 134 | for i in 0..3 { |
| 145 | // Try filling up the RX FIFO0 buffers with standard packets | 135 | // Try filling up the RX FIFO0 buffers with standard packets |
| 146 | let tx_frame = can::TxFrame::new( | 136 | let tx_frame = can::frame::ClassicFrame::new_standard(0x123, &[i; 1]).unwrap(); |
| 147 | can::TxFrameHeader { | ||
| 148 | len: 1, | ||
| 149 | frame_format: can::FrameFormat::Standard, | ||
| 150 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 151 | bit_rate_switching: false, | ||
| 152 | marker: None, | ||
| 153 | }, | ||
| 154 | &[i], | ||
| 155 | ) | ||
| 156 | .unwrap(); | ||
| 157 | info!("Transmitting frame {}", i); | 137 | info!("Transmitting frame {}", i); |
| 158 | can.write(&tx_frame).await; | 138 | can.write(&tx_frame).await; |
| 159 | } | 139 | } |
| 160 | for i in 3..max_buffered { | 140 | for i in 3..max_buffered { |
| 161 | // Try filling up the RX FIFO0 buffers with extended packets | 141 | // Try filling up the RX FIFO0 buffers with extended packets |
| 162 | let tx_frame = can::TxFrame::new( | 142 | let tx_frame = can::frame::ClassicFrame::new_extended(0x1232344, &[i; 1]).unwrap(); |
| 163 | can::TxFrameHeader { | ||
| 164 | len: 1, | ||
| 165 | frame_format: can::FrameFormat::Standard, | ||
| 166 | id: can::ExtendedId::new(0x1232344).unwrap().into(), | ||
| 167 | bit_rate_switching: false, | ||
| 168 | marker: None, | ||
| 169 | }, | ||
| 170 | &[i], | ||
| 171 | ) | ||
| 172 | .unwrap(); | ||
| 173 | |||
| 174 | info!("Transmitting frame {}", i); | 143 | info!("Transmitting frame {}", i); |
| 175 | can.write(&tx_frame).await; | 144 | can.write(&tx_frame).await; |
| 176 | } | 145 | } |
| 177 | 146 | ||
| 178 | // Try and receive all 6 packets | 147 | // Try and receive all 6 packets |
| 179 | for i in 0..max_buffered { | 148 | for i in 0..max_buffered { |
| 180 | let envelope = can.read().await.unwrap(); | 149 | let (frame, _ts) = can.read().await.unwrap(); |
| 181 | match envelope.header.id { | 150 | match frame.id() { |
| 182 | can::Id::Extended(id) => { | 151 | embedded_can::Id::Extended(id) => { |
| 183 | info!("Extended received! {:x} {} {}", id.as_raw(), envelope.data()[0], i); | 152 | info!("Extended received! {:x} {} {}", id.as_raw(), frame.data()[0], i); |
| 184 | } | 153 | } |
| 185 | can::Id::Standard(id) => { | 154 | embedded_can::Id::Standard(id) => { |
| 186 | info!("Standard received! {:x} {} {}", id.as_raw(), envelope.data()[0], i); | 155 | info!("Standard received! {:x} {} {}", id.as_raw(), frame.data()[0], i); |
| 187 | } | 156 | } |
| 188 | } | 157 | } |
| 189 | } | 158 | } |
| @@ -192,48 +161,26 @@ async fn main(_spawner: Spawner) { | |||
| 192 | let (mut tx, mut rx) = can.split(); | 161 | let (mut tx, mut rx) = can.split(); |
| 193 | for i in 0..3 { | 162 | for i in 0..3 { |
| 194 | // Try filling up the RX FIFO0 buffers with standard packets | 163 | // Try filling up the RX FIFO0 buffers with standard packets |
| 195 | let tx_frame = can::TxFrame::new( | 164 | let tx_frame = can::frame::ClassicFrame::new_standard(0x123, &[i; 1]).unwrap(); |
| 196 | can::TxFrameHeader { | ||
| 197 | len: 1, | ||
| 198 | frame_format: can::FrameFormat::Standard, | ||
| 199 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 200 | bit_rate_switching: false, | ||
| 201 | marker: None, | ||
| 202 | }, | ||
| 203 | &[i], | ||
| 204 | ) | ||
| 205 | .unwrap(); | ||
| 206 | |||
| 207 | info!("Transmitting frame {}", i); | 165 | info!("Transmitting frame {}", i); |
| 208 | tx.write(&tx_frame).await; | 166 | tx.write(&tx_frame).await; |
| 209 | } | 167 | } |
| 210 | for i in 3..max_buffered { | 168 | for i in 3..max_buffered { |
| 211 | // Try filling up the RX FIFO0 buffers with extended packets | 169 | // Try filling up the RX FIFO0 buffers with extended packets |
| 212 | let tx_frame = can::TxFrame::new( | 170 | let tx_frame = can::frame::ClassicFrame::new_extended(0x1232344, &[i; 1]).unwrap(); |
| 213 | can::TxFrameHeader { | ||
| 214 | len: 1, | ||
| 215 | frame_format: can::FrameFormat::Standard, | ||
| 216 | id: can::ExtendedId::new(0x1232344).unwrap().into(), | ||
| 217 | bit_rate_switching: false, | ||
| 218 | marker: None, | ||
| 219 | }, | ||
| 220 | &[i], | ||
| 221 | ) | ||
| 222 | .unwrap(); | ||
| 223 | |||
| 224 | info!("Transmitting frame {}", i); | 171 | info!("Transmitting frame {}", i); |
| 225 | tx.write(&tx_frame).await; | 172 | tx.write(&tx_frame).await; |
| 226 | } | 173 | } |
| 227 | 174 | ||
| 228 | // Try and receive all 6 packets | 175 | // Try and receive all 6 packets |
| 229 | for i in 0..max_buffered { | 176 | for i in 0..max_buffered { |
| 230 | let envelope = rx.read().await.unwrap(); | 177 | let (frame, _ts) = rx.read().await.unwrap(); |
| 231 | match envelope.header.id { | 178 | match frame.id() { |
| 232 | can::Id::Extended(id) => { | 179 | embedded_can::Id::Extended(id) => { |
| 233 | info!("Extended received! {:x} {} {}", id.as_raw(), envelope.data()[0], i); | 180 | info!("Extended received! {:x} {} {}", id.as_raw(), frame.data()[0], i); |
| 234 | } | 181 | } |
| 235 | can::Id::Standard(id) => { | 182 | embedded_can::Id::Standard(id) => { |
| 236 | info!("Standard received! {:x} {} {}", id.as_raw(), envelope.data()[0], i); | 183 | info!("Standard received! {:x} {} {}", id.as_raw(), frame.data()[0], i); |
| 237 | } | 184 | } |
| 238 | } | 185 | } |
| 239 | } | 186 | } |
