Skip to content

Commit 21b740f

Browse files
committed
Clean up CAN driver warnings. Add in can-send example.
1 parent a3b1299 commit 21b740f

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ features = ["stm32f429", "rt", "usb_fs"]
2626
targets = ["thumbv7em-none-eabihf"]
2727

2828
[dependencies]
29-
bxcan = "0.3.0"
29+
#bxcan = "0.3.0"
30+
bxcan = { git = "https://github.com/timokroeger/bxcan", branch = "embedded-can" }
3031
cortex-m = ">=0.5.8,<0.7"
3132
cortex-m-rt = "0.6.10"
3233
nb = "0.1.2"
@@ -142,3 +143,7 @@ required-features = ["rt", "stm32f407"]
142143
[[example]]
143144
name = "qei"
144145
required-features = ["rt", "stm32f411"]
146+
147+
[[example]]
148+
name = "can-send"
149+
required-features = ["has-can"]

examples/can-send.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//! Simple CAN example.
2+
//! Requires a transceiver connected to PB8, 9 (CAN1) or PB5 PB6 (CAN2).
3+
4+
#![no_main]
5+
#![no_std]
6+
7+
use panic_halt as _;
8+
9+
use bxcan::filter::Mask32;
10+
use bxcan::{Frame, StandardId};
11+
use cortex_m_rt::entry;
12+
use nb::block;
13+
use stm32f4xx_hal::{can::Can, pac, prelude::*};
14+
15+
#[entry]
16+
fn main() -> ! {
17+
let dp = pac::Peripherals::take().unwrap();
18+
19+
let mut rcc = dp.RCC.constrain();
20+
21+
// To meet CAN clock accuracy requirements an external crystal or ceramic
22+
// resonator must be used. The blue pill has a 8MHz external crystal.
23+
// Other boards might have a crystal with another frequency or none at all.
24+
rcc.cfgr.use_hse(8.mhz()).freeze();
25+
26+
let gpiob = dp.GPIOB.split();
27+
let mut can1 = {
28+
let can = Can::new(dp.CAN1, &mut rcc.apb1);
29+
30+
let rx = gpiob.pb9.into_alternate_af9();
31+
let tx = gpiob.pb8.into_alternate_af9();
32+
33+
bxcan::Can::new(can)
34+
};
35+
can1.configure(|config| {
36+
// APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5%
37+
// Value was calculated with http://www.bittiming.can-wiki.info/
38+
config.set_bit_timing(0x001c_0000);
39+
});
40+
41+
// Configure filters so that can frames can be received.
42+
let mut filters = can1.modify_filters();
43+
filters.enable_bank(0, Mask32::accept_all());
44+
45+
let can2 = {
46+
let can = Can::new(dp.CAN2, &mut rcc.apb1);
47+
48+
let rx = gpiob.pb5.into_floating_input();
49+
let tx = gpiob.pb6.into_alternate_af7();
50+
51+
let mut can2 = bxcan::Can::new(can);
52+
can2.configure(|config| {
53+
// APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5%
54+
// Value was calculated with http://www.bittiming.can-wiki.info/
55+
config.set_bit_timing(0x001c_0000);
56+
});
57+
58+
// A total of 28 filters are shared between the two CAN instances.
59+
// Split them equally between CAN1 and CAN2.
60+
filters.set_split(14);
61+
let mut slave_filters = filters.slave_filters();
62+
slave_filters.enable_bank(14, Mask32::accept_all());
63+
can2
64+
};
65+
66+
// Drop filters to leave filter configuraiton mode.
67+
drop(filters);
68+
69+
// Select the interface.
70+
let mut can = can1;
71+
//let mut can = can2;
72+
73+
// Split the peripheral into transmitter and receiver parts.
74+
block!(can.enable()).unwrap();
75+
76+
// Echo back received packages in sequence.
77+
// See the `can-rtfm` example for an echo implementation that adheres to
78+
// correct frame ordering based on the transfer id.
79+
let mut test: [u8; 8] = [0; 8];
80+
let mut count: u8 = 0;
81+
let id: u16 = 0x500;
82+
loop {
83+
test[0] = count;
84+
test[1] = 1;
85+
test[2] = 2;
86+
test[3] = 3;
87+
test[4] = 4;
88+
test[5] = 5;
89+
test[6] = 6;
90+
test[7] = 7;
91+
let test_frame = Frame::new_data(StandardId::new(id).unwrap(), test);
92+
block!(can.transmit(&test_frame)).unwrap();
93+
if count < 255 {
94+
count += 1;
95+
} else {
96+
count = 0;
97+
}
98+
}
99+
}

src/can.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
//! | TX | PB13 |
1919
//! | RX | PB12 |
2020
21-
use crate::gpio::gpiob::{PB12, PB13, PB5, PB6};
2221
use crate::gpio::{
23-
gpioa::{PA11, PA12},
2422
gpiob::{PB8, PB9},
23+
gpiob::{PB12, PB13},
2524
Alternate, Floating, Input, PushPull,
2625
};
2726
use crate::pac::CAN1;

0 commit comments

Comments
 (0)