I ran some CAN example code on my Arduino setup, and it worked as expected. Now I'm trying to understand the CAN protocol in depth instead of just using the example code.
I've written down my understanding of how I think it works. I'm fairly confident in my reasoning, but I'd really appreciate a second opinion from someone with more experience working with CAN. Even if my understanding is mostly correct, an experienced person @Papabravo might catch something I've overlooked.
CAN is a communication protocol mainly used in the automotive industry and industrial automation. It uses two communication lines, CAN High (CAN_H) and CAN Low (CAN_L). It works using a differential signalling mechanism, which makes communication more reliable in noisy environments.
On the CAN bus, we have two logic states:
When a sender node wants to transmit a dominant bit (0), it drives CAN High to about 3.5 V and CAN Low to about 1.5 V. The receiver measures the voltage difference between the two lines (about 2 V) and recognizes it as a dominant bit.
When the sender wants to transmit a recessive bit (1), both CAN High and CAN Low remain at approximately 2.5 V, so the differential voltage is almost 0 V. The receiver recognizes this as a recessive bit.
One important property of the CAN bus is that any node can force a dominant bit onto the bus, but no node can force a recessive bit if another node is transmitting a dominant bit.
CAN is a message-based protocol, which means messages are identified by their identifier (Message ID) instead of the destination node address. Any node can transmit a message, and every node receives it. Each receiver decides whether to accept or ignore the message using its acceptance filters and masks.
Whenever a node wants to transmit a message, it must follow the CAN frame format.
A CAN data frame contains the following fields:
Normally, when no node is transmitting, the CAN bus remains in the recessive state, with both CAN High and CAN Low at approximately 2.5 V.
To begin communication, the transmitting node sends the Start of Frame, which is one dominant bit. Therefore, the bus changes from recessive to dominant, indicating that a new message is starting.
The next field is the Arbitration Field, which contains the Message Identifier and the RTR (Remote Transmission Request) bit. This field is used for bus arbitration.
If more than one node starts transmitting at the same time, they all transmit their identifier while simultaneously monitoring the bus. Since dominant (0) always overrides recessive (1), the message with the lowest numerical identifier (highest priority) wins arbitration.
For example, suppose two nodes transmit the following identifier bits
Node A : 10011...
Node B : 10010...
Both nodes transmit the same bits until the last bit shown. At that point, Node A transmits 1 (recessive) while Node B transmits 0 (dominant). Since the bus becomes dominant, Node A realizes that the bus does not match what it transmitted, so it immediately stops transmitting and becomes a receiver. Node B continues sending the rest of the message without interruption.
This arbitration process only occurs during the Arbitration Field.
The next field is the Control Field. One important bit is the IDE (Identifier Extension) bit, which indicates whether the frame uses the Standard 11-bit identifier or the Extended 29-bit identifier. The Control Field also contains the DLC (Data Length Code), which specifies how many data bytes are present in the Data Field. In Classical CAN, the DLC can indicate 0 to 8 data bytes. For example, if the DLC is 2, then the message contains two bytes of data.
Next comes the Data Field, which contains the actual information being transmitted.
After that is the CRC Field. The sender calculates a 15-bit CRC from the transmitted message and appends it to the frame. The receiver performs the same CRC calculation on the received bits and compares the result with the received CRC value. If they do not match, the receiver detects a CRC error, indicating that the message was corrupted during transmission. After the 15 CRC bits, there is a CRC Delimiter, which is one recessive bit. It simply marks the end of the CRC Field
The next field is the ACK (Acknowledgement) Field, which is used to indicate that the message was received correctly. The transmitter sends a recessive bit in the ACK Slot. If at least one receiver successfully receives the frame without detecting any errors, it overwrites that bit with a dominant bit. The transmitter monitors the ACK Slot, and if it reads a dominant bit, it knows that at least one node received the message correctly. It does not know which node acknowledged it. The next bit is the ACK Delimiter and It simply marks the end of the ACK Field and separates it
Finally, the transmitter sends the End of Frame (EOF), which consists of 7 recessive bits, indicating that the frame has finished and the bus returns to the idle (recessive) state.
Sorry for the too long post. I have a few doubts about some parts of the CAN protocol, but before moving on to those, I wanted to make sure my understanding of the frame format is correct
I've written down my understanding of how I think it works. I'm fairly confident in my reasoning, but I'd really appreciate a second opinion from someone with more experience working with CAN. Even if my understanding is mostly correct, an experienced person @Papabravo might catch something I've overlooked.
CAN is a communication protocol mainly used in the automotive industry and industrial automation. It uses two communication lines, CAN High (CAN_H) and CAN Low (CAN_L). It works using a differential signalling mechanism, which makes communication more reliable in noisy environments.
On the CAN bus, we have two logic states:
- Dominant bit (0)
- Recessive bit (1)
When a sender node wants to transmit a dominant bit (0), it drives CAN High to about 3.5 V and CAN Low to about 1.5 V. The receiver measures the voltage difference between the two lines (about 2 V) and recognizes it as a dominant bit.
When the sender wants to transmit a recessive bit (1), both CAN High and CAN Low remain at approximately 2.5 V, so the differential voltage is almost 0 V. The receiver recognizes this as a recessive bit.
One important property of the CAN bus is that any node can force a dominant bit onto the bus, but no node can force a recessive bit if another node is transmitting a dominant bit.
CAN is a message-based protocol, which means messages are identified by their identifier (Message ID) instead of the destination node address. Any node can transmit a message, and every node receives it. Each receiver decides whether to accept or ignore the message using its acceptance filters and masks.
Whenever a node wants to transmit a message, it must follow the CAN frame format.
A CAN data frame contains the following fields:
- Start of Frame (SOF)
- Arbitration Field
- Control Field
- Data Field
- CRC Field
- ACK Field
- End of Frame (EOF)
Normally, when no node is transmitting, the CAN bus remains in the recessive state, with both CAN High and CAN Low at approximately 2.5 V.
To begin communication, the transmitting node sends the Start of Frame, which is one dominant bit. Therefore, the bus changes from recessive to dominant, indicating that a new message is starting.
The next field is the Arbitration Field, which contains the Message Identifier and the RTR (Remote Transmission Request) bit. This field is used for bus arbitration.
If more than one node starts transmitting at the same time, they all transmit their identifier while simultaneously monitoring the bus. Since dominant (0) always overrides recessive (1), the message with the lowest numerical identifier (highest priority) wins arbitration.
For example, suppose two nodes transmit the following identifier bits
Node A : 10011...
Node B : 10010...
Both nodes transmit the same bits until the last bit shown. At that point, Node A transmits 1 (recessive) while Node B transmits 0 (dominant). Since the bus becomes dominant, Node A realizes that the bus does not match what it transmitted, so it immediately stops transmitting and becomes a receiver. Node B continues sending the rest of the message without interruption.
This arbitration process only occurs during the Arbitration Field.
The next field is the Control Field. One important bit is the IDE (Identifier Extension) bit, which indicates whether the frame uses the Standard 11-bit identifier or the Extended 29-bit identifier. The Control Field also contains the DLC (Data Length Code), which specifies how many data bytes are present in the Data Field. In Classical CAN, the DLC can indicate 0 to 8 data bytes. For example, if the DLC is 2, then the message contains two bytes of data.
Next comes the Data Field, which contains the actual information being transmitted.
After that is the CRC Field. The sender calculates a 15-bit CRC from the transmitted message and appends it to the frame. The receiver performs the same CRC calculation on the received bits and compares the result with the received CRC value. If they do not match, the receiver detects a CRC error, indicating that the message was corrupted during transmission. After the 15 CRC bits, there is a CRC Delimiter, which is one recessive bit. It simply marks the end of the CRC Field
The next field is the ACK (Acknowledgement) Field, which is used to indicate that the message was received correctly. The transmitter sends a recessive bit in the ACK Slot. If at least one receiver successfully receives the frame without detecting any errors, it overwrites that bit with a dominant bit. The transmitter monitors the ACK Slot, and if it reads a dominant bit, it knows that at least one node received the message correctly. It does not know which node acknowledged it. The next bit is the ACK Delimiter and It simply marks the end of the ACK Field and separates it
Finally, the transmitter sends the End of Frame (EOF), which consists of 7 recessive bits, indicating that the frame has finished and the bus returns to the idle (recessive) state.
Sorry for the too long post. I have a few doubts about some parts of the CAN protocol, but before moving on to those, I wanted to make sure my understanding of the frame format is correct