CAN bus protocol and filter settings of SJA1000

 Controller Area Network (CAN) is a serial communication protocol bus used for real-time applications. It can use twisted pairs to transmit signals and is one of the most widely used field buses in the world. The CAN protocol is used to communicate between various different components in cars, replacing expensive and bulky distribution wiring harnesses. The protocol's robustness extends its use to other automation and industrial applications. Features of the CAN protocol include complete serial data communication, real-time support, transmission rates up to 1Mb/s, and 11-bit addressing and error detection capabilities.

1.CAN terms, definitions and abbreviations

1.1 Terms and Definitions

1.1.1

 dominant dominant
A logical state in CAN bus communication, the dominant bit represents logic 0.

1.1.2

 recessive
A logical state in CAN bus communication. The recessed bit represents logic 1 and can be overwritten by the dominant bit.

1.1.3

data frame data frame
CAN frame carrying a data field containing up to 8 bytes of user data.

1.1.4

 remote frame remote frame
Like CAN frames that other nodes request to send specific data frames, remote frames have no data field.

1.2 Abbreviations

 CAN: controller area network (controller area network)
OSI: open system interconnect reference model
CRC: cyclic redundancy check
DLC: data length code
IDE: identifier extension
RTR: remote transmission request (remote transmission request)
SRR: substitute remote requset

2. Frame format

 CAN bus frame types include data frames, remote frames, etc. The data frame consists of seven different bit fields: start of frame (SOF), arbitration field (frame ID), control field, data field, CRC field, response field, and end of frame (EOF).
The CAN bus communication protocol supports two frame formats: standard frame and extended frame. The standard frame format is shown in Figure 2.1, and the extended frame format is shown in Figure 2.2:
![Figure 2.1 Standard frame format](https://img-blog.csdnimg.cn/4acd7566a6c1447bb02f0f4099d8eecf.png)
![Figure 2.2 Extended frame format](https://img-blog.csdnimg.cn/70012f7a65c347c885c4c755fadef82e.png)
The specific description of the frame format is as follows:
1. Frame start: In fact, it marks the data frame and remote frame and consists of a single dominant bit.
2. Arbitration field: composed of identifier and RTR:
a) Standard frames have 11-bit identifiers ID10~ID0, and extended frames have 29-bit identifiers ID28~ID0;
b) The RTR bit forces the bit to be dominant, and RTR = 0, indicating a data frame and disabling remote frames;
c) The arbitration field determines the priority of data frame transmission, with higher priority identifiers having lower binary values.
3. Control field: consists of 6 bits, including 2-bit RB1, RB0 and 4-bit data length code DLC. In the standard frame, RB1 is the identifier extended IDE, RB0 is the reserved bit, and in the extended frame, RB1 and RB0 are reserved bits. ;Data length codes DLC3~DLC0 indicate the number of bytes of the data field 0~8.
4. Data field: can include 0~8 bytes of data.
5. CRC field: It is a cyclic redundancy check code including the frame start, arbitration field, control field, and data field (if any). The generating polynomial is X^15 + X^14 + X^10 + X^8 + X^7 + X^4 + X^3 + 1.
6. Response field: The sending node sends two recessive bits in the response field, and all nodes that receive a matching CRC overwrite the recessive bits of the transmitter with dominant bits to indicate response.
7. End of frame: Each data frame and remote frame are defined by a series of flags composed of 7 recessive bits.

3.CAN bus signal

3.1 Physical Properties

 On the CAN bus, the signal is expressed in the form of bit voltage. The CAN signal is represented by the potential difference on the CANZ_H and CAN_L lines, which is divided into two types: recessive level and dominant level. Among them, the recessive level is specified as logic 1, and the dominant level is specified as logic 0. The specific definition can be understood through the following figure:
![Figure 3.1 CAN signal description](https://img-blog.csdnimg.cn/41ed745b8b8f4fccafac3884b4d5f325.png)
As shown in the figure, when the voltages of CAN_H and CAN_L are both 2.52v and the voltage difference between the two is 0, the CAN signal is a recessive level; when the voltage signal of CAN_H is 3.5v and the signal of CAN_L is 1.5v, The voltage difference between the two is 2v, which stipulates that the CAN signal is a dominant level. The actual regulation is: if the voltage difference meets a defined range, it can be considered a dominant level or a recessive level. For example, if the voltage difference is in the range of 1.5v~2.5v, it is considered a dominant level.

3.2 Working method

 All nodes on the CAN bus are not divided into masters and slaves. The reflection in data transmission is: when the bus is idle, any node can send messages to the bus. In addition: the node that sends information to the bus first gets the right to send the bus; when multiple nodes send messages to the bus at the same time, the node with the highest priority of the sent message gets the right to send the bus.
In fact, the so-called priority is the ID arbitration of the CAN bus. The smaller the binary number of the CAN ID bit, the higher the priority of the message or instruction sent by the node. (It can also be understood that in the arbitration process of the CAN bus, the dominant level takes precedence over the recessive level)
The arbitration process is as follows:
![Figure 3.2 CAN bus arbitration diagram](https://img- blog.csdnimg.cn/6567c4d809df40508944a638c157b952.png)
Since ID6 of Node_A is dominant, Node_A has a higher priority than Node_B.

4.Data frame type

 The frame types of the CAN bus protocol generally include the following five types:
Frame Frame purpose
Data frame Frame used by the sending unit to transmit data to the receiving unit
Remote control frame Used by the receiving unit to request data from the sending unit with the same ID Frame
Error frame A frame used to notify other units of an error when an error is detected
Overload frame Used by the receiving unit to notify the frame that it is not ready yet
Frame interval Used to transfer data Frames and remote control frames are separated from the previous frame
 The difference between data frame and remote frame:
Compare content Data frame Remote frame
ID The ID of the sending node The ID of the requested sending node
SRR 0 1
RTR 0 1
DLC Send data length Request data length
Whether there is a data segment Yes No
CRC check range Frame start + arbitration section + control section + data section Frame start + arbitration section + control section

4. Filter settings

 ACR: Acceptance code register;
AMR: Acceptance Mask Register;
The purpose of the CAN bus filter is to filter node information, and only CAN information frames that meet the requirements can pass. With the help of the acceptance filter, the CAN controller allows the received information to be stored in the RXFIFO only when the identification bit in the received information is equal to the value defined by the acceptance filter.
The acceptance filter is defined by the acceptance code register (ACRn) and the acceptance mask register (AMRn). The bit pattern of the information to be received is defined in the acceptance code register. The acceptance mask register of the response allows defining certain bits as "do not affect", It can be any value.

4.1CAN mode selection

 In the CAN bus controller SJA1000, the highest bit CDR.7 of the clock division register (CDR) can be used to select the CAN bus mode. If CDR.7 is 0, the CAN controller works in BasicCAN mode. If CDR.7 If it is 1, the CAN controller works in PeliCAN mode, and this register can only be written in reset mode.
![Figure 4.1 CDR register](https://img-blog.csdnimg.cn/ede4c689454a49fdbee194240917d423.png)

4.2 Filter mode selection

 In the CAN bus controller SJA1000, the mode register (MOD) can be used to change the behavior of the CAN controller. MOD.3 can be used to select the filter mode. If MOD.3 is 0, it means single filter mode. , On the contrary, if MOD.3 is 1, it means double filter mode.

4.3 Single filter mode

4.3.1 BasicCAN mode

 For standard frames, the 11-bit identifier, RTR bit, and the first two bytes of the data field participate in filtering;
For data participating in filtering, the ACR bits corresponding to all AMR bits of 0 must be the same as the corresponding bits participating in filtering data to be accepted;
If there is no data byte due to setting bit RTR = 1, or if there is no or only one data byte information because the corresponding data length code is set. Messages will also be received. For a successfully received message, the comparison result of all single bits in the filter must be "accept";
![Figure 4.2 Single filter setting when receiving BasicCAN](https://img-blog.csdnimg.cn/ca88549dd3c44cee8f569162591cf305.png)

4.3.2 PeliCAN mode

 For extended frames, all identifiers including the RTR bit will be filtered.
For data participating in filtering, the ACR bits corresponding to all AMR bits of "0" and the corresponding bits participating in filtering data must be the same before the filtering is accepted;
![Figure 4.3 Single filter settings when receiving PeliCAN](https://img-blog.csdnimg.cn/dc3389f7f12e4c80b963e34cc18c9cc3.png)

4.4 Dual filter mode

 This configuration can define two short filters, consisting of 4 ACRs and 4 AMRs. The information on the bus will be received as long as it passes through any one.

4.4.1 BasicCAN mode

 If standard frame information is received, the two filters defined are different.
The first filter consists of ACR0, ACR1, AMR0, AMR1 and the lower four bits of ACR3 and AMR3. The comparison content of this filter includes: ID10~ID0 + RTR + the first byte information of the data segment.
The second filter consists of ACR2, AMR2 and the high four bits of ACR3 and AMR3. The comparison content of this filter includes: ID10~ID0 + RTR.
![Figure 4.4 Dual filter settings when receiving BasicCAN](https://img-blog.csdnimg.cn/f0511d79e9534ec484eca132bd735184.png)

4.4.2 PeliCAN mode

 If extended frame information is received, the two filters defined are the same. Both filters only compare the first 16 ID bits of the extended frame identification code: ID28~ID13.
The first filter consists of ACR0, AMR0, ACR1, and AMR1;
The second filter consists of ACR2, AMR2, ACR3, and AMR3;
In order to successfully receive information, all individual bits must be compared with at least one filter during reception.
![Figure 4.5 Dual filter settings when receiving PeliCAN](https://img-blog.csdnimg.cn/4bafab02a18c46b79356d465bdba9c3f.png)