Difference between revisions of "CAN Bus"
(Reorganize into layers to make more sense) |
(→CAN FD) |
||
| (3 intermediate revisions by one other user not shown) | |||
| Line 6: | Line 6: | ||
* the data link layer, which specifies bit timing, message framing, error detection, etc. | * the data link layer, which specifies bit timing, message framing, error detection, etc. | ||
* the application layer, which specifies things like which identifier to use for what data. | * the application layer, which specifies things like which identifier to use for what data. | ||
| + | |||
==CAN bus physical layer== | ==CAN bus physical layer== | ||
At the physical layer, there are two types of CAN networks: low–speed CAN and high–speed CAN. | At the physical layer, there are two types of CAN networks: low–speed CAN and high–speed CAN. | ||
| Line 16: | Line 17: | ||
Because of its low speed it is not often used in the automotive world, but if you don't have a lot of data to send it could be useful. | Because of its low speed it is not often used in the automotive world, but if you don't have a lot of data to send it could be useful. | ||
| − | ==CAN bus data link layer== | + | ==CAN bus data link layer==<!-- TODO: expand on types of frames: remote request frames, error frames, etc. --> |
The CAN 2.0 protocol is specified in ISO 11898-1. It is split into CAN 2.0A and CAN 2.0B, the original 11 bit identifier version and the extended 29 bit identifier version. | The CAN 2.0 protocol is specified in ISO 11898-1. It is split into CAN 2.0A and CAN 2.0B, the original 11 bit identifier version and the extended 29 bit identifier version. | ||
===CAN 2.0A=== | ===CAN 2.0A=== | ||
CAN 2.0A is effectively the same as CAN 1.0 and 1.2. It uses 11 bit identifiers, so a maximum of 2048 devices can transmit on a network. If a device says it supports CAN, it supports this. | CAN 2.0A is effectively the same as CAN 1.0 and 1.2. It uses 11 bit identifiers, so a maximum of 2048 devices can transmit on a network. If a device says it supports CAN, it supports this. | ||
| − | + | ====Data frame format==== | |
| + | {| class="wikitable" | ||
| + | |- | ||
| + | !Field name | ||
| + | !Length (bits) | ||
| + | !Purpose | ||
| + | |- | ||
| + | |Start of frame | ||
| + | |1 | ||
| + | |Denotes the start of frame transmission | ||
| + | |- | ||
| + | |Identifier | ||
| + | |11 | ||
| + | |A unique identifier which also represents the message priority | ||
| + | |- | ||
| + | |Remote transmission request (RTR) | ||
| + | |1 | ||
| + | |Must be 0 for data frames and 1 for remote request frames | ||
| + | |- | ||
| + | |Identifier extension bit (IDE) | ||
| + | |1 | ||
| + | |Must be 0 for base format with 11 bit identifiers | ||
| + | |- | ||
| + | |Reserved bit (r0) | ||
| + | |1 | ||
| + | |Reserved bit. Must be 0 | ||
| + | |- | ||
| + | |Data length code (DLC) | ||
| + | |4 | ||
| + | |Number of bytes of data (0-8 bytes) | ||
| + | |- | ||
| + | |Data field | ||
| + | |0-64 | ||
| + | |Data to be transmitted (length in bytes determined by DLC field) | ||
| + | |- | ||
| + | |CRC | ||
| + | |15 | ||
| + | |[https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-15-CAN Cyclic redundancy check] | ||
| + | |- | ||
| + | |CRC delimiter | ||
| + | |1 | ||
| + | |Must be 1 | ||
| + | |- | ||
| + | |ACK slot | ||
| + | |1 | ||
| + | |Transmitter sends 1 and any receiver can assert a 0 | ||
| + | |- | ||
| + | |ACK delimiter | ||
| + | |1 | ||
| + | |must be 1 | ||
| + | |- | ||
| + | |End of frame (EOF) | ||
| + | |7 | ||
| + | |all bits must be 1 | ||
| + | |- | ||
| + | |Inter-frame spacing (IFS) | ||
| + | |3 | ||
| + | |all bits must be 1 | ||
| + | |} | ||
===CAN 2.0B=== | ===CAN 2.0B=== | ||
CAN 2.0B builds on CAN 2.0A by allowing 29 bit identifiers. | CAN 2.0B builds on CAN 2.0A by allowing 29 bit identifiers. | ||
| − | <!-- | + | ====Data frame format==== |
| + | {| class="wikitable" | ||
| + | |- | ||
| + | !Field name<br /> | ||
| + | !Length (bits)<br /> | ||
| + | !Purpose<br /> | ||
| + | |- | ||
| + | |Start of frame | ||
| + | |1 | ||
| + | |Denotes the start of frame transmission | ||
| + | |- | ||
| + | |Identifier A | ||
| + | |11 | ||
| + | |First part of the unique identifier which also represents the message priority | ||
| + | |- | ||
| + | |Substitute remote request (SRR) | ||
| + | |1 | ||
| + | |Must be 1 | ||
| + | |- | ||
| + | |Identifier extension bit (IDE) | ||
| + | |1 | ||
| + | |Must be 1 for extended format with 29 bit identifiers | ||
| + | |- | ||
| + | |Identifier B | ||
| + | |18 | ||
| + | |Second part of the unique identifier which also represents the message priority | ||
| + | |- | ||
| + | |Remote transmission request (RTR) | ||
| + | |1 | ||
| + | |Must be 0 for data frames and 1 for remote request frames | ||
| + | |- | ||
| + | |Reserved bits (r1, r0) | ||
| + | |2 | ||
| + | |Reserved bits which must be set 0, but accepted as either 0 or 1 | ||
| + | |- | ||
| + | |Data length code (DLC) | ||
| + | |4 | ||
| + | |Number of bytes of data (0-8 bytes) | ||
| + | |- | ||
| + | |Data field | ||
| + | |0-64 | ||
| + | |Data to be transmitted (length in bytes determined by DLC field) | ||
| + | |- | ||
| + | |CRC | ||
| + | |15 | ||
| + | |[https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-15-CAN Cyclic redundancy check] | ||
| + | |- | ||
| + | |CRC delimiter | ||
| + | |1 | ||
| + | |Must be 1 | ||
| + | |- | ||
| + | |ACK slot | ||
| + | |1 | ||
| + | |Transmitter sends 1 and any receiver can assert a 0 | ||
| + | |- | ||
| + | |ACK delimiter | ||
| + | |1 | ||
| + | |must be 1 | ||
| + | |- | ||
| + | |End of frame (EOF) | ||
| + | |7 | ||
| + | |all bits must be 1 | ||
| + | |- | ||
| + | |Inter-frame spacing (IFS) | ||
| + | |3 | ||
| + | |all bits must be 1 | ||
| + | |} | ||
===CAN FD=== | ===CAN FD=== | ||
CAN FD (Flexible Data-rate) is an extension of the CAN 2.0 protocol. It can dynamically switch between different data rates and longer or shorter messages, resulting in faster data speeds and higher data capacity.<ref>Falch, Martin. "CAN FD Explained - A Simple Intro [2022] - CSS Electronics". Retrieved 2023-01-21. https://www.csselectronics.com/pages/can-fd-flexible-data-rate-intro.</ref> | CAN FD (Flexible Data-rate) is an extension of the CAN 2.0 protocol. It can dynamically switch between different data rates and longer or shorter messages, resulting in faster data speeds and higher data capacity.<ref>Falch, Martin. "CAN FD Explained - A Simple Intro [2022] - CSS Electronics". Retrieved 2023-01-21. https://www.csselectronics.com/pages/can-fd-flexible-data-rate-intro.</ref> | ||
| + | |||
| + | Note that classic CAN devices don't understand CAN FD frames, and your bus will probably break if you mix the two. However, you can use e.g. the STM32 FDCAN peripheral in classic CAN mode, which will work just fine with devices that don't understand FDCAN. | ||
| + | |||
==CAN bus application layer== | ==CAN bus application layer== | ||
===J1939=== | ===J1939=== | ||
===CANopen=== | ===CANopen=== | ||
| − | |||
==References== | ==References== | ||
Latest revision as of 08:05, 28 June 2024
CAN bus is a Communication Protocol and networking standard designed for communicating between devices in vehicles. Because of its versatility and relatively low complexity, it is used in the vast majority of vehicles manufactured today. By letting multiple devices communicate with each-other through a common interface, wiring harness complexity can be reduced.
Computer networks can be thought of as having layers. CAN bus has 3 main layers:
- the physical layer, which contains electrical specifications (cable impedance, voltage levels, etc.)
- the data link layer, which specifies bit timing, message framing, error detection, etc.
- the application layer, which specifies things like which identifier to use for what data.
Contents
CAN bus physical layer
At the physical layer, there are two types of CAN networks: low–speed CAN and high–speed CAN.
High–Speed CAN
High–speed CAN (ISO 11898-2) is probably the most common version of CAN. It can support speeds up to 1 Mb/s, and uses a linear bus. High–speed CAN does not work when hooked up as a star network, as star networks can lead to strange behavior at higher frequencies. The stubs coming off of the linear bus should be kept as short as possible. If you are designing a custom PCB that uses CAN, you can do this branching on the PCB instead of in the wiring harness. A 120 ohm termination resistor is required at either end of the bus, i.e. the DC resistance measurement between the 2 CAN wires should be 60 ohms when hooked up properly. The cable should theoretically have an impedance between 108 and 132 Ohms at the bus frequency, but in practice this isn't necessary and most cable will work just fine.[1]
Low–Speed CAN
Low-speed CAN (ISO 11898-3) is limited to a speed of 125 Kb/s, but it is more flexible in its routing. It can be connected as a star bus with no issues, and each device on the network has two termination resistors. Their value can be calculated based on the method described on page 4-10 of this document from National Instruments: [1].
Because of its low speed it is not often used in the automotive world, but if you don't have a lot of data to send it could be useful.
CAN bus data link layer
The CAN 2.0 protocol is specified in ISO 11898-1. It is split into CAN 2.0A and CAN 2.0B, the original 11 bit identifier version and the extended 29 bit identifier version.
CAN 2.0A
CAN 2.0A is effectively the same as CAN 1.0 and 1.2. It uses 11 bit identifiers, so a maximum of 2048 devices can transmit on a network. If a device says it supports CAN, it supports this.
Data frame format
| Field name | Length (bits) | Purpose |
|---|---|---|
| Start of frame | 1 | Denotes the start of frame transmission |
| Identifier | 11 | A unique identifier which also represents the message priority |
| Remote transmission request (RTR) | 1 | Must be 0 for data frames and 1 for remote request frames |
| Identifier extension bit (IDE) | 1 | Must be 0 for base format with 11 bit identifiers |
| Reserved bit (r0) | 1 | Reserved bit. Must be 0 |
| Data length code (DLC) | 4 | Number of bytes of data (0-8 bytes) |
| Data field | 0-64 | Data to be transmitted (length in bytes determined by DLC field) |
| CRC | 15 | Cyclic redundancy check |
| CRC delimiter | 1 | Must be 1 |
| ACK slot | 1 | Transmitter sends 1 and any receiver can assert a 0 |
| ACK delimiter | 1 | must be 1 |
| End of frame (EOF) | 7 | all bits must be 1 |
| Inter-frame spacing (IFS) | 3 | all bits must be 1 |
CAN 2.0B
CAN 2.0B builds on CAN 2.0A by allowing 29 bit identifiers.
Data frame format
| Field name |
Length (bits) |
Purpose |
|---|---|---|
| Start of frame | 1 | Denotes the start of frame transmission |
| Identifier A | 11 | First part of the unique identifier which also represents the message priority |
| Substitute remote request (SRR) | 1 | Must be 1 |
| Identifier extension bit (IDE) | 1 | Must be 1 for extended format with 29 bit identifiers |
| Identifier B | 18 | Second part of the unique identifier which also represents the message priority |
| Remote transmission request (RTR) | 1 | Must be 0 for data frames and 1 for remote request frames |
| Reserved bits (r1, r0) | 2 | Reserved bits which must be set 0, but accepted as either 0 or 1 |
| Data length code (DLC) | 4 | Number of bytes of data (0-8 bytes) |
| Data field | 0-64 | Data to be transmitted (length in bytes determined by DLC field) |
| CRC | 15 | Cyclic redundancy check |
| CRC delimiter | 1 | Must be 1 |
| ACK slot | 1 | Transmitter sends 1 and any receiver can assert a 0 |
| ACK delimiter | 1 | must be 1 |
| End of frame (EOF) | 7 | all bits must be 1 |
| Inter-frame spacing (IFS) | 3 | all bits must be 1 |
CAN FD
CAN FD (Flexible Data-rate) is an extension of the CAN 2.0 protocol. It can dynamically switch between different data rates and longer or shorter messages, resulting in faster data speeds and higher data capacity.[2]
Note that classic CAN devices don't understand CAN FD frames, and your bus will probably break if you mix the two. However, you can use e.g. the STM32 FDCAN peripheral in classic CAN mode, which will work just fine with devices that don't understand FDCAN.
CAN bus application layer
J1939
CANopen
References
- ↑ "CAN Physical Layers - Kvaser". Retrieved 2023-01-21. https://www.kvaser.com/lesson/can-physical-layers/.
- ↑ Falch, Martin. "CAN FD Explained - A Simple Intro [2022] - CSS Electronics". Retrieved 2023-01-21. https://www.csselectronics.com/pages/can-fd-flexible-data-rate-intro.