Can is short for Controller Area Network. It is mainly used in the automotive field.
In the usual hardware design, there are mainly two controllers to implement the expansion of the CAN device, namely SJA1000 and mcp2515. The SJA1000 is designed to replace the original pca82c200.
SJA1000
The sja10000. The SJA1000 has two independent working modes: basiccan mode (pca82c200 compatible mode) and Pelican
Mode.
The SJA1000 CAN baud rate is calculated as follows:
Assume that we do not consider the sjw bit in btr0 and the Sam bit in btr1. Then, btr0 and btr1 are two frequency division coefficient registers; their product is an extended frequency division coefficient. That is:
Btr0 × btr1 = f_base/fbps (1)
Where:
Internal Frequency reference source f_base = fclk/2, that is, the 2-division of the external crystal oscillator frequency fclk. Note that in any application, when using an external crystal oscillator as the benchmark source, it will first undergo a 2-frequency shaping.
(1) When the crystal oscillator is 16 m, f_base = 8000 K
When the crystal oscillator is 12 m, f_base = 6000 K
Fbps is the expected CAN bus frequency. The Unit is K.
In the (1) type, btr0 = m, btr1 = n, and the external crystal oscillator 16 m are:
N= 8000/fbps (2)
In this way, when fbps gets the expected value, it will get a combination value of M * n. The m value is also unique when n is selected.
The N value can specification specifies 8 ~ 25. (That is, the value of btr1) The basic principle is: the higher the fbps value, the greater the value of N (by setting btr1. The reason is not hard to understand.
I suppose n = 10 is selected in the general application, that is:
Synchronization segment + phase buffer segment 1 + phase buffer segment 2 = 1 + 5 + 4
Then, the formula (2) is simplified
M = 800/fbps
The maximum value of M is 64, and the maximum frequency division coefficient of SJA1000 is M * n = 64x25 = 1600. Therefore, 16 m crystal oscillator is usually used as an example in the standard algorithm. In fact, with the formula (1), any crystal oscillator value (6 m ~ 24 m) is easy to calculate.
Determination of SAM: for low frequencies, select Sam = 1, that is, sampling three times. When the frequency is greater than K, take Sam = 0, that is, sampling 1 time.
Sja re-synchronization hop Width Selection: it is related to the Digital Phase-Locked Loop Technology. If n is selected as a large value, sja can select a large value, that is, multiple pulse shares can be corrected at a time, tscl. If the N value is small or the frequency is low, sja = 1 is selected. That is, btr0.7 and btr0.6 are both set to 0.
The setting in Sam is set to 0 when it is high-speed can and 1 when it is low-speed can;
For more information about bus timers, see the command register section in the SJA1000 manual.
Mcp2515
The connection between mcp2515 and MCU is implemented through the SPI interface.
The baud rate of mcp2515 is calculated as follows:
Tbit = tsyncseg + tprow.g + TPS1 + tps2;
The synchronous segment (syncseq) is the first segment of NBT. It is about synchronizing nodes on the CAN bus. The edge of the input signal occurs in the synchronization segment, and this segment is set to 1tq;
Propseg is used to compensate for the physical transmission delay between nodes. It can be set to 1-8tq;
The phase buffer segments (PS1 and PS2) are used to compensate for edge phase errors on the bus. PS1 can be set to 1-8tq, and PS2 can be set to 2-8tq.
For the TQ settings, refer to the following formula:
TQ = 2 × (BRP + 1)/fosc;
PS1 = (phseg1 + 1) × TQ;
PS2 = (phseg2 + 1) × TQ;
BRP is the pre-division ratio of can.
Refer to Chapter 5 Timing of mcp2515 device Manual
Lpc2xxx with CAN controller
Calculation of the baud rate of the lpc2xxx series arm with can
When the VPB clock is 4 * 11059200hz, the common baud rate is similar to that of the bus sequencer)
BPS = (SAM <23) | (tseg2 <20) | (tseg1 <16) | (sjw <14) | BRP
# Define bps_5k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 879
# Define bps_10k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 439
# Define bps_20k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 219
# Define bps_40k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 109
# Define bps_50k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 87
# Define bps_80k (1 <23) | (1 <20) | (4 <16) | (0 <14) | 68
# Define bps_100k (1 <23) | (1 <20) | (6 <16) | (0 <14) | 43
# Define bps_125k (0 <23) | (1 <20) | (4 <16) | (0 <14) | 43
# Define bps_200k (0 <23) | (1 <20) | (6 <16) | (0 <14) | 21
# Define bps_250k (0 <23) | (1 <20) | (4 <16) | (0 <14) | 21
# Define bps_400k (0 <23) | (1 <20) | (6 <16) | (0 <14) | 10
# Define bps_500k (0 <23) | (1 <20) | (4 <16) | (0 <14) | 10
# Define bps_666k (0 <23) | (1 <20) | (2 <16) | (0 <14) | 10
# Define bps_800k (0 <23) | (1 <20) | (1 <16) | (0 <14) | 10
# Define bps_1000k (0 <23) | (1 <20) | (1 <16) | (0 <14) | 8
The following is my own derivation (for reference only)
Canbtr (0xe00xx014)
Baud Rate BPS =
Sam 0: 125k and above baud rate
1: kb or lower baud rate