1 | void can_prepare(void) {
|
2 | /* IRQ pin as input with pullup */
|
3 | DDRB &= ~_BV(PB1);
|
4 | PORTB |= _BV(PB1);
|
5 |
|
6 |
|
7 | /* Reset */
|
8 | can_command(CAN_RESET);
|
9 | _delay_ms(100);
|
10 |
|
11 |
|
12 | /* Tie BF pins */
|
13 | can_write_register(BFPCTRL,
|
14 | _BV(B1BFE) | _BV(B1BFM) | _BV(B0BFE) | _BV(B0BFM)
|
15 | );
|
16 |
|
17 | /* Disable RTS pins */
|
18 | can_write_register(TXRTSCTRL, 0);
|
19 |
|
20 |
|
21 | /* Configure baud rate to 100kbps.
|
22 | Given by the quartz oscillator used in the circuit:
|
23 | Fosc = 8 MHz -> Tosc = 125 ns
|
24 |
|
25 | The nominal baud rate 'NBR' of 100 kbps is also the
|
26 | nominal bit rate. This yields a nominal bit time of
|
27 | Tbit = 1/NBR = 10 us
|
28 |
|
29 | The bit time is divided into time quants 'Tq' by the can
|
30 | bus controller and the bit time is composed out of time
|
31 | quants as
|
32 | Tbit = Tsync + Tprop + Tpha1 + Tpha2
|
33 | where
|
34 | . Tsync is fixed to 1*Tq for synchronization to the bus,
|
35 | . Tprop is the physical propagation delay on the bus,
|
36 | . Tpha1 and Tpha2 divide the remaining bit time into
|
37 | two phase segments. The bit is sampled between these
|
38 | phase segments.
|
39 |
|
40 | The time quant must be choosen short enough to allow for
|
41 | a sensible choice of the propagation sement Tprop and large
|
42 | enough to be able to divide the total bit time at a ratio
|
43 | of about 60..70% : 40..30%.
|
44 |
|
45 | The time quant is determined by a prescaler 'BRP'.
|
46 | Tq = 2*(BRP+1) * Tosc
|
47 | Choosing 'BRP' = 2 yields Tq = 1 us.
|
48 |
|
49 | The propagation delay is programmed by 'PRSEG' as
|
50 | Tprop = (PRSEG+1) * Tq
|
51 | and 'PRSEG' = 1 gives a propagation delay of 2 us, which
|
52 | is enough for a tiny bus.
|
53 |
|
54 | After Tsync and Tprop, there are 6 time quants left.
|
55 | Splitting the total bit time at a ratio of 70% : 30% makes
|
56 | 7 time quants before and 3 after the sampling point. As
|
57 | 3 time quants have already been used up by synchronization
|
58 | and propagation, there are 4 time quants left for the first
|
59 | phase segment and another 3 for the second one.
|
60 | Tpha1 = (PHSEG1+1) * Tq
|
61 | Tpha2 = (PHSEG2+1) * Tq
|
62 | Choosing 'PHSEG1' = 3 yields Tpha1 = 4 us and choosing
|
63 | 'PHSEG2' = 2 yields Tpha2 = 3 us.
|
64 |
|
65 | Summarizing:
|
66 | Tq = 1 us
|
67 | Tbit = Tsync + Tprop + Tpha1 + Tpha2
|
68 | = ( 1 + 2 + 4 + 3 ) * Tq = 10 us
|
69 | ^
|
70 | sampling point
|
71 | */
|
72 | can_write_register(CNF1,
|
73 | _BV(SJW0) |
|
74 | _BV(BRP1) | _BV(BRP0) /* 'BRP' = 3 */
|
75 | );
|
76 | can_write_register(CNF2,
|
77 | _BV(SAM) |
|
78 | _BV(BTLMODE) |
|
79 | _BV(PHSEG11) | _BV(PHSEG10) | /* 'PHSEG1' = 3 */
|
80 | _BV(PRSEG0) /* 'PRSEG' = 1 */
|
81 | );
|
82 | can_write_register(CNF3,
|
83 | _BV(PHSEG21) /* 'PHSEG2' = 2 */
|
84 | );
|
85 |
|
86 |
|
87 |
|
88 | /* Interrupts on reception into any of the two RX buffers and on
|
89 | completing transmission of any of the three TX buffers. */
|
90 | can_write_register(CANINTE,
|
91 | _BV(RX1IE) | _BV(RX0IE) |
|
92 | _BV(TX2IE) | _BV(TX1IE) | _BV(TX0IE)
|
93 | );
|
94 |
|
95 |
|
96 | /* Accept anything in first buffer */
|
97 | can_write_register(RXB0CTRL, _BV(RXM1) | _BV(RXM0));
|
98 |
|
99 |
|
100 |
|
101 | /* Leave configuration mode and speed up clock output. */
|
102 | can_write_register(CANCTRL, _BV(CLKEN));
|
103 | }
|