aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-02-23 00:07:05 +0100
committerGitHub <[email protected]>2024-02-23 00:07:05 +0100
commit9c918f6474cedfa7d6a2d36c416c3c380ee0c747 (patch)
tree0fb53f542831216e6e63a00239df115a780f5d5d /examples
parent2cceeab56481b873d8a1c41ddf244fc6c34f5dcc (diff)
parent3f93105e9fe7c5cbdada46a4367b01448fd61c62 (diff)
Merge pull request #2588 from cschuhen/feature/fdcan_buffered
Add FDCAN Buffered mode.
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32g4/Cargo.toml2
-rw-r--r--examples/stm32g4/src/bin/can.rs182
-rw-r--r--examples/stm32h5/src/bin/can.rs90
-rw-r--r--examples/stm32h7/src/bin/can.rs90
4 files changed, 281 insertions, 83 deletions
diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml
index 895ad3e7c..74ccfa3b0 100644
--- a/examples/stm32g4/Cargo.toml
+++ b/examples/stm32g4/Cargo.toml
@@ -20,9 +20,11 @@ defmt-rtt = "0.4"
20cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 20cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
21cortex-m-rt = "0.7.0" 21cortex-m-rt = "0.7.0"
22embedded-hal = "0.2.6" 22embedded-hal = "0.2.6"
23embedded-can = { version = "0.4" }
23panic-probe = { version = "0.3", features = ["print-defmt"] } 24panic-probe = { version = "0.3", features = ["print-defmt"] }
24futures = { version = "0.3.17", default-features = false, features = ["async-await"] } 25futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
25heapless = { version = "0.8", default-features = false } 26heapless = { version = "0.8", default-features = false }
27static_cell = "2.0.0"
26 28
27[profile.release] 29[profile.release]
28debug = 2 30debug = 2
diff --git a/examples/stm32g4/src/bin/can.rs b/examples/stm32g4/src/bin/can.rs
index 727921fba..affa97039 100644
--- a/examples/stm32g4/src/bin/can.rs
+++ b/examples/stm32g4/src/bin/can.rs
@@ -5,6 +5,7 @@ use embassy_executor::Spawner;
5use embassy_stm32::peripherals::*; 5use embassy_stm32::peripherals::*;
6use embassy_stm32::{bind_interrupts, can, Config}; 6use embassy_stm32::{bind_interrupts, can, Config};
7use embassy_time::Timer; 7use embassy_time::Timer;
8use static_cell::StaticCell;
8use {defmt_rtt as _, panic_probe as _}; 9use {defmt_rtt as _, panic_probe as _};
9 10
10bind_interrupts!(struct Irqs { 11bind_interrupts!(struct Irqs {
@@ -18,39 +19,190 @@ async fn main(_spawner: Spawner) {
18 19
19 let peripherals = embassy_stm32::init(config); 20 let peripherals = embassy_stm32::init(config);
20 21
21 let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); 22 let mut can = can::FdcanConfigurator::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs);
23
24 can.set_extended_filter(
25 can::fd::filter::ExtendedFilterSlot::_0,
26 can::fd::filter::ExtendedFilter::accept_all_into_fifo1(),
27 );
22 28
23 // 250k bps 29 // 250k bps
24 can.set_bitrate(250_000); 30 can.set_bitrate(250_000);
25 31
32 let use_fd = false;
33
34 // 1M bps
35 if use_fd {
36 can.set_fd_data_bitrate(1_000_000, false);
37 }
38
26 info!("Configured"); 39 info!("Configured");
27 40
28 //let mut can = can.into_external_loopback_mode(); 41 let mut can = can.start(match use_fd {
29 let mut can = can.into_normal_mode(); 42 true => can::FdcanOperatingMode::InternalLoopbackMode,
43 false => can::FdcanOperatingMode::NormalOperationMode,
44 });
30 45
31 let mut i = 0; 46 let mut i = 0;
47 let mut last_read_ts = embassy_time::Instant::now();
48
32 loop { 49 loop {
33 let frame = can::TxFrame::new( 50 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
34 can::TxFrameHeader {
35 len: 1,
36 frame_format: can::FrameFormat::Standard,
37 id: can::StandardId::new(0x123).unwrap().into(),
38 bit_rate_switching: false,
39 marker: None,
40 },
41 &[i],
42 )
43 .unwrap();
44 info!("Writing frame"); 51 info!("Writing frame");
52
45 _ = can.write(&frame).await; 53 _ = can.write(&frame).await;
46 54
47 match can.read().await { 55 match can.read().await {
48 Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), 56 Ok((rx_frame, ts)) => {
57 let delta = (ts - last_read_ts).as_millis();
58 last_read_ts = ts;
59 info!(
60 "Rx: {} {:02x} --- {}ms",
61 rx_frame.header().len(),
62 rx_frame.data()[0..rx_frame.header().len() as usize],
63 delta,
64 )
65 }
49 Err(_err) => error!("Error in frame"), 66 Err(_err) => error!("Error in frame"),
50 } 67 }
51 68
52 Timer::after_millis(250).await; 69 Timer::after_millis(250).await;
53 70
54 i += 1; 71 i += 1;
72 if i > 2 {
73 break;
74 }
75 }
76
77 // Use the FD API's even if we don't get FD packets.
78
79 loop {
80 if use_fd {
81 let frame = can::frame::FdFrame::new_extended(0x123456F, &[i; 16]).unwrap();
82 info!("Writing frame using FD API");
83 _ = can.write_fd(&frame).await;
84 } else {
85 let frame = can::frame::FdFrame::new_extended(0x123456F, &[i; 8]).unwrap();
86 info!("Writing frame using FD API");
87 _ = can.write_fd(&frame).await;
88 }
89
90 match can.read_fd().await {
91 Ok((rx_frame, ts)) => {
92 let delta = (ts - last_read_ts).as_millis();
93 last_read_ts = ts;
94 info!(
95 "Rx: {} {:02x} --- using FD API {}ms",
96 rx_frame.header().len(),
97 rx_frame.data()[0..rx_frame.header().len() as usize],
98 delta,
99 )
100 }
101 Err(_err) => error!("Error in frame"),
102 }
103
104 Timer::after_millis(250).await;
105
106 i += 1;
107 if i > 4 {
108 break;
109 }
110 }
111 i = 0;
112 let (mut tx, mut rx) = can.split();
113 // With split
114 loop {
115 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
116 info!("Writing frame");
117 _ = tx.write(&frame).await;
118
119 match rx.read().await {
120 Ok((rx_frame, ts)) => {
121 let delta = (ts - last_read_ts).as_millis();
122 last_read_ts = ts;
123 info!(
124 "Rx: {} {:02x} --- {}ms",
125 rx_frame.header().len(),
126 rx_frame.data()[0..rx_frame.header().len() as usize],
127 delta,
128 )
129 }
130 Err(_err) => error!("Error in frame"),
131 }
132
133 Timer::after_millis(250).await;
134
135 i += 1;
136
137 if i > 2 {
138 break;
139 }
140 }
141
142 let can = can::Fdcan::join(tx, rx);
143
144 info!("\n\n\nBuffered\n");
145 if use_fd {
146 static TX_BUF: StaticCell<can::TxFdBuf<8>> = StaticCell::new();
147 static RX_BUF: StaticCell<can::RxFdBuf<10>> = StaticCell::new();
148 let mut can = can.buffered_fd(
149 TX_BUF.init(can::TxFdBuf::<8>::new()),
150 RX_BUF.init(can::RxFdBuf::<10>::new()),
151 );
152 loop {
153 let frame = can::frame::FdFrame::new_extended(0x123456F, &[i; 16]).unwrap();
154 info!("Writing frame");
155
156 _ = can.write(frame).await;
157
158 match can.read().await {
159 Ok((rx_frame, ts)) => {
160 let delta = (ts - last_read_ts).as_millis();
161 last_read_ts = ts;
162 info!(
163 "Rx: {} {:02x} --- {}ms",
164 rx_frame.header().len(),
165 rx_frame.data()[0..rx_frame.header().len() as usize],
166 delta,
167 )
168 }
169 Err(_err) => error!("Error in frame"),
170 }
171
172 Timer::after_millis(250).await;
173
174 i += 1;
175 }
176 } else {
177 static TX_BUF: StaticCell<can::TxBuf<8>> = StaticCell::new();
178 static RX_BUF: StaticCell<can::RxBuf<10>> = StaticCell::new();
179 let mut can = can.buffered(
180 TX_BUF.init(can::TxBuf::<8>::new()),
181 RX_BUF.init(can::RxBuf::<10>::new()),
182 );
183 loop {
184 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
185 info!("Writing frame");
186
187 _ = can.write(frame).await;
188
189 match can.read().await {
190 Ok((rx_frame, ts)) => {
191 let delta = (ts - last_read_ts).as_millis();
192 last_read_ts = ts;
193 info!(
194 "Rx: {} {:02x} --- {}ms",
195 rx_frame.header().len(),
196 rx_frame.data()[0..rx_frame.header().len() as usize],
197 delta,
198 )
199 }
200 Err(_err) => error!("Error in frame"),
201 }
202
203 Timer::after_millis(250).await;
204
205 i += 1;
206 }
55 } 207 }
56} 208}
diff --git a/examples/stm32h5/src/bin/can.rs b/examples/stm32h5/src/bin/can.rs
index 2906d1576..e5ccfe4f7 100644
--- a/examples/stm32h5/src/bin/can.rs
+++ b/examples/stm32h5/src/bin/can.rs
@@ -16,54 +16,76 @@ bind_interrupts!(struct Irqs {
16#[embassy_executor::main] 16#[embassy_executor::main]
17async fn main(_spawner: Spawner) { 17async fn main(_spawner: Spawner) {
18 let mut config = Config::default(); 18 let mut config = Config::default();
19 19 config.rcc.hse = Some(rcc::Hse {
20 // configure FDCAN to use PLL1_Q at 64 MHz 20 freq: embassy_stm32::time::Hertz(25_000_000),
21 config.rcc.pll1 = Some(rcc::Pll { 21 mode: rcc::HseMode::Oscillator,
22 source: rcc::PllSource::HSI,
23 prediv: rcc::PllPreDiv::DIV4,
24 mul: rcc::PllMul::MUL8,
25 divp: None,
26 divq: Some(rcc::PllDiv::DIV2),
27 divr: None,
28 }); 22 });
29 config.rcc.fdcan_clock_source = rcc::FdCanClockSource::PLL1_Q; 23 config.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE;
30 24
31 let peripherals = embassy_stm32::init(config); 25 let peripherals = embassy_stm32::init(config);
32 26
33 let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); 27 let mut can = can::FdcanConfigurator::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs);
34 28
35 can.can.apply_config( 29 // 250k bps
36 can::config::FdCanConfig::default().set_nominal_bit_timing(can::config::NominalBitTiming { 30 can.set_bitrate(250_000);
37 sync_jump_width: 1.try_into().unwrap(),
38 prescaler: 8.try_into().unwrap(),
39 seg1: 13.try_into().unwrap(),
40 seg2: 2.try_into().unwrap(),
41 }),
42 );
43 31
44 info!("Configured"); 32 //let mut can = can.into_internal_loopback_mode();
33 let mut can = can.into_normal_mode();
45 34
46 let mut can = can.into_external_loopback_mode(); 35 info!("CAN Configured");
47 //let mut can = can.into_normal_mode();
48 36
49 let mut i = 0; 37 let mut i = 0;
38 let mut last_read_ts = embassy_time::Instant::now();
39
50 loop { 40 loop {
51 let frame = can::TxFrame::new( 41 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
52 can::TxFrameHeader {
53 len: 1,
54 frame_format: can::FrameFormat::Standard,
55 id: can::StandardId::new(0x123).unwrap().into(),
56 bit_rate_switching: false,
57 marker: None,
58 },
59 &[i],
60 )
61 .unwrap();
62 info!("Writing frame"); 42 info!("Writing frame");
63 _ = can.write(&frame).await; 43 _ = can.write(&frame).await;
64 44
65 match can.read().await { 45 match can.read().await {
66 Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), 46 Ok((rx_frame, ts)) => {
47 let delta = (ts - last_read_ts).as_millis();
48 last_read_ts = ts;
49 info!(
50 "Rx: {:x} {:x} {:x} {:x} --- NEW {}",
51 rx_frame.data()[0],
52 rx_frame.data()[1],
53 rx_frame.data()[2],
54 rx_frame.data()[3],
55 delta,
56 )
57 }
58 Err(_err) => error!("Error in frame"),
59 }
60
61 Timer::after_millis(250).await;
62
63 i += 1;
64 if i > 3 {
65 break;
66 }
67 }
68
69 let (mut tx, mut rx) = can.split();
70 // With split
71 loop {
72 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
73 info!("Writing frame");
74 _ = tx.write(&frame).await;
75
76 match rx.read().await {
77 Ok((rx_frame, ts)) => {
78 let delta = (ts - last_read_ts).as_millis();
79 last_read_ts = ts;
80 info!(
81 "Rx: {:x} {:x} {:x} {:x} --- NEW {}",
82 rx_frame.data()[0],
83 rx_frame.data()[1],
84 rx_frame.data()[2],
85 rx_frame.data()[3],
86 delta,
87 )
88 }
67 Err(_err) => error!("Error in frame"), 89 Err(_err) => error!("Error in frame"),
68 } 90 }
69 91
diff --git a/examples/stm32h7/src/bin/can.rs b/examples/stm32h7/src/bin/can.rs
index 2906d1576..e5ccfe4f7 100644
--- a/examples/stm32h7/src/bin/can.rs
+++ b/examples/stm32h7/src/bin/can.rs
@@ -16,54 +16,76 @@ bind_interrupts!(struct Irqs {
16#[embassy_executor::main] 16#[embassy_executor::main]
17async fn main(_spawner: Spawner) { 17async fn main(_spawner: Spawner) {
18 let mut config = Config::default(); 18 let mut config = Config::default();
19 19 config.rcc.hse = Some(rcc::Hse {
20 // configure FDCAN to use PLL1_Q at 64 MHz 20 freq: embassy_stm32::time::Hertz(25_000_000),
21 config.rcc.pll1 = Some(rcc::Pll { 21 mode: rcc::HseMode::Oscillator,
22 source: rcc::PllSource::HSI,
23 prediv: rcc::PllPreDiv::DIV4,
24 mul: rcc::PllMul::MUL8,
25 divp: None,
26 divq: Some(rcc::PllDiv::DIV2),
27 divr: None,
28 }); 22 });
29 config.rcc.fdcan_clock_source = rcc::FdCanClockSource::PLL1_Q; 23 config.rcc.fdcan_clock_source = rcc::FdCanClockSource::HSE;
30 24
31 let peripherals = embassy_stm32::init(config); 25 let peripherals = embassy_stm32::init(config);
32 26
33 let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); 27 let mut can = can::FdcanConfigurator::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs);
34 28
35 can.can.apply_config( 29 // 250k bps
36 can::config::FdCanConfig::default().set_nominal_bit_timing(can::config::NominalBitTiming { 30 can.set_bitrate(250_000);
37 sync_jump_width: 1.try_into().unwrap(),
38 prescaler: 8.try_into().unwrap(),
39 seg1: 13.try_into().unwrap(),
40 seg2: 2.try_into().unwrap(),
41 }),
42 );
43 31
44 info!("Configured"); 32 //let mut can = can.into_internal_loopback_mode();
33 let mut can = can.into_normal_mode();
45 34
46 let mut can = can.into_external_loopback_mode(); 35 info!("CAN Configured");
47 //let mut can = can.into_normal_mode();
48 36
49 let mut i = 0; 37 let mut i = 0;
38 let mut last_read_ts = embassy_time::Instant::now();
39
50 loop { 40 loop {
51 let frame = can::TxFrame::new( 41 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
52 can::TxFrameHeader {
53 len: 1,
54 frame_format: can::FrameFormat::Standard,
55 id: can::StandardId::new(0x123).unwrap().into(),
56 bit_rate_switching: false,
57 marker: None,
58 },
59 &[i],
60 )
61 .unwrap();
62 info!("Writing frame"); 42 info!("Writing frame");
63 _ = can.write(&frame).await; 43 _ = can.write(&frame).await;
64 44
65 match can.read().await { 45 match can.read().await {
66 Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), 46 Ok((rx_frame, ts)) => {
47 let delta = (ts - last_read_ts).as_millis();
48 last_read_ts = ts;
49 info!(
50 "Rx: {:x} {:x} {:x} {:x} --- NEW {}",
51 rx_frame.data()[0],
52 rx_frame.data()[1],
53 rx_frame.data()[2],
54 rx_frame.data()[3],
55 delta,
56 )
57 }
58 Err(_err) => error!("Error in frame"),
59 }
60
61 Timer::after_millis(250).await;
62
63 i += 1;
64 if i > 3 {
65 break;
66 }
67 }
68
69 let (mut tx, mut rx) = can.split();
70 // With split
71 loop {
72 let frame = can::frame::ClassicFrame::new_extended(0x123456F, &[i; 8]).unwrap();
73 info!("Writing frame");
74 _ = tx.write(&frame).await;
75
76 match rx.read().await {
77 Ok((rx_frame, ts)) => {
78 let delta = (ts - last_read_ts).as_millis();
79 last_read_ts = ts;
80 info!(
81 "Rx: {:x} {:x} {:x} {:x} --- NEW {}",
82 rx_frame.data()[0],
83 rx_frame.data()[1],
84 rx_frame.data()[2],
85 rx_frame.data()[3],
86 delta,
87 )
88 }
67 Err(_err) => error!("Error in frame"), 89 Err(_err) => error!("Error in frame"),
68 } 90 }
69 91