You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Options the CAN bus. This is primarily used to set bus timings, but also controls options like enabling loopback or silent mode for debugging.
29
+
/// See http://www.bittiming.can-wiki.info/#bxCAN for generating the timing parameters for different baud rates and clocks.
30
+
///
31
+
/// Use `CanOpts::default()` to get 250kbps at 32mhz system clock
32
+
pubstructCanOpts{
33
+
brp:u16,
34
+
sjw:u8,
35
+
ts1:u8,
36
+
ts2:u8,
37
+
lbkm:LBKM_A,
38
+
}
39
+
40
+
implCanOpts{
41
+
/// Create new `CanOpts` using the default settings from `CanOpts::default()` to get 250kbps at 32mhz system clock.
42
+
pubfnnew() -> CanOpts{
43
+
CanOpts::default()
44
+
}
45
+
46
+
/// Set the Baud Rate Prescaler. See http://www.bittiming.can-wiki.info/#bxCAN for generating the timing parameters for different baud rates and clocks.
47
+
pubfnbrp(mutself,brp:u16) -> Self{
48
+
self.brp = brp;
49
+
self
50
+
}
51
+
52
+
/// Set the Resynchronisation Jump Width. See http://www.bittiming.can-wiki.info/#bxCAN for generating the timing parameters for different baud rates and clocks.
53
+
pubfnsjw(mutself,sjw:u8) -> Self{
54
+
self.sjw = sjw;
55
+
self
56
+
}
57
+
58
+
/// Set Time Segment One. See http://www.bittiming.can-wiki.info/#bxCAN for generating the timing parameters for different baud rates and clocks.
59
+
pubfnts1(mutself,ts1:u8) -> Self{
60
+
self.ts1 = ts1;
61
+
self
62
+
}
63
+
64
+
/// Set Time Segment Two. See http://www.bittiming.can-wiki.info/#bxCAN for generating the timing parameters for different baud rates and clocks.
65
+
pubfnts2(mutself,ts2:u8) -> Self{
66
+
self.ts2 = ts2;
67
+
self
68
+
}
69
+
70
+
/// Enable or disable loopback mode on the CAN device. This is useful for debugging.
71
+
pubfnlbkm(mutself,lbkm:LBKM_A) -> Self{
72
+
self.lbkm = lbkm;
73
+
self
74
+
}
75
+
}
76
+
77
+
implDefaultforCanOpts{
78
+
fndefault() -> Self{
79
+
CanOpts{
80
+
brp:4,
81
+
sjw:0,
82
+
ts1:10,
83
+
ts2:3,
84
+
lbkm:LBKM_A::DISABLED,
85
+
}
86
+
}
87
+
}
88
+
27
89
/// A CAN identifier, which can be either 11 or 27 (extended) bits.
28
90
/// u16 and u32 respectively are used here despite the fact that the upper bits are unused.
29
91
#[derive(Debug,Copy,Clone,Eq,PartialEq)]
@@ -252,42 +314,28 @@ impl CanFilterData {
252
314
}
253
315
254
316
implCan{
255
-
/// Initialize the CAN Peripheral
256
-
pubfnnew(
317
+
/// Initialize the CAN peripheral using the options specified by `opts`.
318
+
pubfnnew_with_opts(
257
319
can: stm32::CAN,
258
320
rx: gpioa::PA11<AF9>,
259
321
tx: gpioa::PA12<AF9>,
260
322
apb1:&mutAPB1,
261
-
) -> Self{
323
+
opts:CanOpts,
324
+
) -> Can{
262
325
apb1.enr().modify(|_, w| w.canen().enabled());
263
326
can.mcr.modify(|_, w| w.sleep().clear_bit());
264
327
can.mcr.modify(|_, w| w.inrq().set_bit());
265
328
266
329
// Wait for INAK to confirm we have entered initialization mode
267
330
while !can.msr.read().inak().bit_is_set(){}
268
331
269
-
// TODO: actually calculate baud params
270
-
271
-
// Our baud rate calc here is aiming for roughly 4000uS total bit time or about 250kbps
272
-
// Though we actually allow closer to 5500uS total given the sjw setting
273
-
// Calculations for timing value from http://www.bittiming.can-wiki.info/#bxCAN
274
-
275
-
// Baud rate prescaler defines time quanta
276
-
// tq = (BRP[9:0]+1) x tPCLK
277
-
let brp:u16 = 4;
278
-
279
-
// Resynchronization jump width: number of quanta segments may be expanded to resync
280
-
// tRJW = tq x (SJW[1:0] + 1)
281
-
let sjw = 0;
282
-
283
-
// Time seg 2
284
-
// tBS2 = tq x (TS2[2:0] + 1)
285
-
let ts2 = 3;
286
-
287
-
// Time seg 1
288
-
// tBS1 = tq x (TS1[3:0] + 1)
289
-
let ts1 = 10;
290
-
332
+
letCanOpts{
333
+
brp,
334
+
sjw,
335
+
ts1,
336
+
ts2,
337
+
lbkm,
338
+
} = opts;
291
339
can.btr.modify(|_, w| unsafe{
292
340
w.brp()
293
341
.bits(brp)
@@ -297,8 +345,8 @@ impl Can {
297
345
.bits(ts1)
298
346
.ts2()
299
347
.bits(ts2)
300
-
//.lbkm()
301
-
//.set_bit()
348
+
.lbkm()
349
+
.variant(lbkm)
302
350
});
303
351
304
352
// Leave initialization mode by clearing INRQ and switch to normal mode
@@ -312,6 +360,15 @@ impl Can {
312
360
_tx: tx,
313
361
}
314
362
}
363
+
/// Initialize the CAN Peripheral using default options from `CanOpts::default()`
0 commit comments