Skip to content

Commit 621bd09

Browse files
Cor Petersluctius
Cor Peters
authored andcommitted
Transition to the stm32/fdcan crate
1 parent 55ac63a commit 621bd09

16 files changed

+134
-2265
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ static_assertions = "1.1"
2323
version = "0.7.7"
2424
features = ["critical-section-single-core"]
2525

26+
[dependencies.fdcan]
27+
version = "0.1.2"
28+
features = ["fdcan_g0_g4_l5"]
29+
2630
[dependencies.cast]
2731
version = "0.2.7"
2832
default-features = false

examples/can-echo.rs

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
#![no_std]
33

44
use crate::hal::{
5-
fdcan::{
6-
config::NominalBitTiming,
7-
filter::{StandardFilter, StandardFilterSlot},
8-
frame::{FrameFormat, TxFrameHeader},
9-
id::StandardId,
10-
FdCan,
11-
},
5+
can::CanExt,
126
gpio::{GpioExt as _, Speed},
137
nb::block,
148
rcc::{Config, RccExt, SysClockSrc},
159
stm32::Peripherals,
1610
time::U32Ext,
1711
};
12+
use fdcan::{
13+
config::NominalBitTiming,
14+
filter::{StandardFilter, StandardFilterSlot},
15+
frame::{FrameFormat, TxFrameHeader},
16+
id::StandardId,
17+
};
1818
use stm32g4xx_hal as hal;
1919

2020
use core::num::{NonZeroU16, NonZeroU8};
@@ -59,10 +59,7 @@ fn main() -> ! {
5959
let tx = gpiob.pb9.into_alternate().set_speed(Speed::VeryHigh);
6060

6161
info!("-- Create CAN 1 instance");
62-
let can = FdCan::new(dp.FDCAN1, tx, rx, &rcc);
63-
64-
info!("-- Set CAN 1 in Config Mode");
65-
let mut can = can.into_config_mode();
62+
let mut can = dp.FDCAN1.fdcan(tx, rx, &rcc);
6663
can.set_protocol_exception_handling(false);
6764

6865
info!("-- Configure nominal timing");
@@ -87,10 +84,8 @@ fn main() -> ! {
8784
// let tx = gpiob.pb13.into_alternate().set_speed(Speed::VeryHigh);
8885

8986
// info!("-- Create CAN 2 instance");
90-
// let can = FdCan::new(dp.FDCAN2, tx, rx, &rcc);
91-
92-
// info!("-- Set CAN in Config Mode");
93-
// let mut can = can.into_config_mode();
87+
// let mut can = dp.FDCAN2.fdcan(tx, rx, &rcc);
88+
// can.set_protocol_exception_handling(false);
9489

9590
// info!("-- Configure nominal timing");
9691
// can.set_nominal_bit_timing(btr);
@@ -109,7 +104,7 @@ fn main() -> ! {
109104
let mut can = can1;
110105

111106
info!("Create Message Data");
112-
let mut buffer = [0xAAAAAAAA, 0xFFFFFFFF, 0x0, 0x0, 0x0, 0x0];
107+
let mut buffer: [u8; 8] = [0xAA, 0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF];
113108
info!("Create Message Header");
114109
let header = TxFrameHeader {
115110
len: 2 * 4,
@@ -121,30 +116,11 @@ fn main() -> ! {
121116
info!("Initial Header: {:#X?}", &header);
122117

123118
info!("Transmit initial message");
124-
block!(can.transmit(header, &mut |b| {
125-
let len = b.len();
126-
b[..len].clone_from_slice(&buffer[..len]);
127-
},))
128-
.unwrap();
119+
block!(can.transmit(header, &buffer)).unwrap();
129120

130121
loop {
131-
if let Ok(rxheader) = block!(can.receive0(&mut |h, b| {
132-
info!("Received Header: {:#X?}", &h);
133-
info!("received data: {:X?}", &b);
134-
135-
for (i, d) in b.iter().enumerate() {
136-
buffer[i] = *d;
137-
}
138-
h
139-
})) {
140-
block!(
141-
can.transmit(rxheader.unwrap().to_tx_header(None), &mut |b| {
142-
let len = b.len();
143-
b[..len].clone_from_slice(&buffer[..len]);
144-
info!("Transmit: {:X?}", b);
145-
})
146-
)
147-
.unwrap();
122+
if let Ok(rxheader) = block!(can.receive0(&mut buffer)) {
123+
block!(can.transmit(rxheader.unwrap().to_tx_header(None), &mut buffer)).unwrap();
148124
}
149125
}
150126
}

0 commit comments

Comments
 (0)