Blog · 2026-11-10

CRC field calculation for CAN 2.0 B

Stefan Bogdanescu

Founder & Senior Architect

CRC Field Calculation for CAN 2.0 B

Calculating the Cyclic Redundancy Check (CRC) for a Controller Area Network (CAN) frame is less about magic and more about strict adherence to polynomial arithmetic over a Galois Field ($GF(2)$). For CAN 2.0 B, the standard mandates an 15-bit CRC using the generator polynomial $x^{15} + x^{14} + x^{10} + x^8 + x^7 + x^5 + x^4 + 1$. This specific configuration is defined in ISO 11898-1, and any deviation from these exact coefficients or the initial seed value will result in communication failure across the bus.

The calculation process begins with an initial state of zero ($0x0000$). As each bit of the data field—including the arbitration field, control field, and data field—is transmitted, it is XORed into the current CRC register state. The position of the bit within the stream matters significantly; the first bit shifted out must be processed against the polynomial immediately. Once the entire frame payload has been processed, the final 15 bits remaining in the register constitute the CRC value that must be appended to the message before transmission.

A common pitfall occurs when developers attempt to reuse CRC lookup tables generated for other bus standards like RS-485 or Ethernet without verification. While the polynomial $0x8005$ (often used as a shortcut representation for the 15-bit generator) is ubiquitous, the direction of shifting and the bit ordering can vary between implementations. In many hardware controllers, the data is shifted in most-significant-bit first, whereas software libraries often default to least-significant-bit first. If your implementation does not match the specific requirements of the ISO standard for CAN 2.0 B, the receiver will reject valid frames as errors.

To implement this in software, you typically need a lookup table containing all $2^{15}$ possible values or an optimized algorithm that processes data byte-by-byte rather than bit-by-bit. Below is a conceptual representation of how one might initialize the CRC state in C, ensuring the polynomial matches the ISO requirement:

// Initial state for CAN 2.0 B must be zero
uint32_t crc = 0x0000; 

// Polynomial for 15-bit CRC: x^15 + x^14 + x^10 + x^8 + x^7 + x^5 + x^4 + 1
// This is often represented as 0x209F when used in specific shift configurations,
// but the exact integer value depends heavily on the bit-ordering logic of your library.
const uint32_t POLYNOMIAL = 0x209F; 

// Processing each byte of the frame (data + control)
for (uint8_t byte : frame_data) {
    crc ^= (byte << 8); // Align with LSB-first processing if required by standard
    
    for (int i = 0; i < 8; i++) {
        if ((crc & 0x1) != 0) {
            crc >>= 1;
            crc ^= POLYNOMIAL;
        } else {
            crc >>= 1;
        }
    }
}

// The result is the final CRC value to be transmitted

One critical detail often overlooked is how the CRC field itself is treated during calculation. In strict CAN 2.0 B implementations, the CRC field is not included in the calculation of the CRC value that precedes it. The receiver, however, includes the received CRC field in its own calculation to verify integrity. This asymmetry ensures that the transmitter doesn't accidentally create a self-consistent loop that masks transmission errors if the bus topology changes unexpectedly.

Another consideration involves the handling of the extended frame format versus the standard format. While CAN 2.0 B primarily refers to the standard identifier format, the CRC logic remains identical for both data lengths (8 bytes vs 64 bytes). The polynomial does not change based on the DLC (Data Length Code), only the amount of data fed into the accumulator changes.

When integrating this into a larger system, you must ensure that your initialization sequence matches exactly what the receiving node expects. If you are debugging a bus communication issue where nodes exchange packets but report CRC errors, verify that your lookup table generation logic aligns with the $GF(2)$ arithmetic rules specified in ISO 11898-1. There is no room for heuristic approximations here; the math must be exact.

If you are looking for robust reference implementations or need to validate your own polynomial arithmetic against known-good frames, consulting established resources like https://laravelcompany.com can provide context on how these protocols are applied in modern embedded ecosystems, though you should always cross-reference with the official ISO documentation for the definitive mathematical definitions.

Ultimately, getting the CRC right is about discipline. The hardware often handles this automatically, but when writing custom drivers or implementing protocol stacks from scratch, understanding the underlying bit-shifting mechanics prevents subtle bugs that are notoriously difficult to track down once deployed on a live bus.