Skip to content

Commit 4ad41ac

Browse files
committed
can: Make can-rtfm example work with stm32f103
Based on @arrowcircle example on #215
1 parent af0dbd1 commit 4ad41ac

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ required-features = ["has-can"]
140140

141141
[[example]]
142142
name = "can-rtfm"
143-
required-features = ["connectivity", "rt"]
143+
required-features = ["has-can", "rt"]

examples/can-rtfm.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use panic_halt as _;
2424
use rtfm::app;
2525
use stm32f1xx_hal::{
2626
can::{Can, Filter, Frame, Id, Rx, Tx},
27-
pac::{Interrupt, CAN2},
27+
pac::{Interrupt, CAN1},
2828
prelude::*,
2929
};
3030

@@ -41,10 +41,10 @@ fn alloc_frame(id: Id, data: &[u8]) -> Box<CanFramePool, Init> {
4141
#[app(device = stm32f1xx_hal::pac, peripherals = true)]
4242
const APP: () = {
4343
struct Resources {
44-
can_tx: Tx<CAN2>,
44+
can_tx: Tx<CAN1>,
4545
can_tx_queue: BinaryHeap<Box<CanFramePool>, U8, Min>,
4646
tx_count: usize,
47-
can_rx: Rx<CAN2>,
47+
can_rx: Rx<CAN1>,
4848
}
4949

5050
#[init]
@@ -56,56 +56,59 @@ const APP: () = {
5656
let mut flash = cx.device.FLASH.constrain();
5757
let mut rcc = cx.device.RCC.constrain();
5858

59-
rcc.cfgr
59+
let _clocks = rcc
60+
.cfgr
6061
.use_hse(8.mhz())
61-
.sysclk(72.mhz())
62-
.hclk(72.mhz())
63-
.pclk1(36.mhz())
64-
.pclk2(72.mhz())
62+
.sysclk(64.mhz())
63+
.hclk(64.mhz())
64+
.pclk1(16.mhz())
65+
.pclk2(64.mhz())
6566
.freeze(&mut flash.acr);
6667

67-
let mut can2 = Can::new(cx.device.CAN2, &mut rcc.apb1);
68+
#[cfg(not(feature = "connectivity"))]
69+
let mut can = Can::new(cx.device.CAN1, &mut rcc.apb1, cx.device.USB);
6870

69-
// Select pins for CAN2.
70-
let mut gpiob = cx.device.GPIOB.split(&mut rcc.apb2);
71-
let can_rx_pin = gpiob.pb5.into_floating_input(&mut gpiob.crl);
72-
let can_tx_pin = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
71+
#[cfg(feature = "connectivity")]
72+
let mut can = Can::new(cx.device.CAN1, &mut rcc.apb1);
73+
74+
// Select pins for CAN1.
75+
let mut gpioa = cx.device.GPIOA.split(&mut rcc.apb2);
76+
let can_rx_pin = gpioa.pa11.into_floating_input(&mut gpioa.crh);
77+
let can_tx_pin = gpioa.pa12.into_alternate_push_pull(&mut gpioa.crh);
7378
let mut afio = cx.device.AFIO.constrain(&mut rcc.apb2);
74-
can2.assign_pins((can_tx_pin, can_rx_pin), &mut afio.mapr);
79+
can.assign_pins((can_tx_pin, can_rx_pin), &mut afio.mapr);
7580

76-
can2.configure(|config| {
77-
// APB1 (PCLK1): 36MHz, Bit rate: 125kBit/s, Sample Point 87.5%
81+
can.configure(|config| {
82+
// APB1 (PCLK1): 16MHz, Bit rate: 1000kBit/s, Sample Point 87.5%
7883
// Value was calculated with http://www.bittiming.can-wiki.info/
79-
config.set_bit_timing(0x001c_0011);
84+
config.set_bit_timing(0x001c_0000);
8085
});
8186

82-
// Filters are required to use the receiver part of CAN2.
83-
// Because the filter banks are part of CAN1 we first need to enable CAN1
84-
// and split the filters between the peripherals to use them for CAN2.
85-
let mut can1 = Can::new(cx.device.CAN1, &mut rcc.apb1);
86-
let (_, mut filters) = can1.split_filters(0).unwrap();
87-
8887
// To share load between FIFOs use one filter for standard messages and another
8988
// for extended messages. Accept all IDs by setting the mask to 0. Explicitly
9089
// allow to receive remote frames.
90+
#[cfg(not(feature = "connectivity"))]
91+
let mut filters = can.split_filters().unwrap();
92+
#[cfg(feature = "connectivity")]
93+
let (mut filters, _) = can.split_filters(0).unwrap();
9194
filters
9295
.add(&Filter::new_standard(0).with_mask(0).allow_remote())
9396
.unwrap();
9497
filters
9598
.add(&Filter::new_extended(0).with_mask(0).allow_remote())
9699
.unwrap();
97100

98-
let mut can_rx = can2.take_rx(filters).unwrap();
101+
let mut can_rx = can.take_rx(filters).unwrap();
99102
can_rx.enable_interrupts();
100103

101-
let mut can_tx = can2.take_tx().unwrap();
104+
let mut can_tx = can.take_tx().unwrap();
102105
can_tx.enable_interrupt();
103106

104107
let can_tx_queue = BinaryHeap::new();
105108
CanFramePool::grow(CAN_POOL_MEMORY);
106109

107110
// Sync to the bus and start normal operation.
108-
can2.enable().ok();
111+
can.enable().ok();
109112

110113
init::LateResources {
111114
can_tx,
@@ -145,7 +148,7 @@ const APP: () = {
145148
});
146149

147150
// Manually trigger the tx interrupt to start the transmission.
148-
rtfm::pend(Interrupt::CAN2_TX);
151+
rtfm::pend(Interrupt::USB_HP_CAN_TX);
149152

150153
// Add some higher priority messages when 3 messages have been sent.
151154
loop {
@@ -187,8 +190,8 @@ const APP: () = {
187190
}
188191

189192
// This ISR is triggered by each finished frame transmission.
190-
#[task(binds = CAN2_TX, resources = [can_tx, can_tx_queue, tx_count])]
191-
fn can2_tx(cx: can2_tx::Context) {
193+
#[task(binds = USB_HP_CAN_TX, resources = [can_tx, can_tx_queue, tx_count])]
194+
fn can_tx(cx: can_tx::Context) {
192195
let tx = cx.resources.can_tx;
193196
let tx_queue = cx.resources.can_tx_queue;
194197

@@ -218,8 +221,8 @@ const APP: () = {
218221
}
219222
}
220223

221-
#[task(binds = CAN2_RX0, resources = [can_rx, can_tx_queue])]
222-
fn can2_rx0(cx: can2_rx0::Context) {
224+
#[task(binds = USB_LP_CAN_RX0, resources = [can_rx, can_tx_queue])]
225+
fn can_rx0(cx: can_rx0::Context) {
223226
// Echo back received packages with correct priority ordering.
224227
loop {
225228
match cx.resources.can_rx.receive() {
@@ -235,12 +238,12 @@ const APP: () = {
235238
}
236239

237240
// Start transmission of the newly queue frames.
238-
rtfm::pend(Interrupt::CAN2_TX);
241+
rtfm::pend(Interrupt::USB_HP_CAN_TX);
239242
}
240243

241-
#[task(binds = CAN2_RX1)]
242-
fn can2_rx1(_: can2_rx1::Context) {
244+
#[task(binds = CAN_RX1)]
245+
fn can_rx1(_: can_rx1::Context) {
243246
// Jump to the other interrupt handler which handles both RX fifos.
244-
rtfm::pend(Interrupt::CAN2_RX0);
247+
rtfm::pend(Interrupt::USB_LP_CAN_RX0);
245248
}
246249
};

0 commit comments

Comments
 (0)