C755 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 55 · Project

Protecting a data link with a shift register and a lookup table

A temperature sensor sends readings over a long cable to a microcontroller, and every so often a burst of noise flips a bit. The fix used on the sensor's bus, as on Ethernet, USB and every disk, is a cyclic redundancy check: the sender divides its message by a fixed polynomial and appends the remainder, and the receiver repeats the division to see whether anything changed. In this project you'll watch the hardware version run in a 3-bit shift register, then implement the standard 8-bit CRC two ways in Python, bit by bit as the hardware does it and a byte at a time with a lookup table, and verify both against the published check values.

  • A serial CRC register for the generator x3+x+1 (1011). The message 1101 enters one bit per clock on D; EN stops the register after the fourth bit.
  • Each clock, the top bit Q2 XORed with the incoming bit is fed back into the positions where the generator has terms (x1 and x0).
  • After four clocks Q2Q1Q0 = 001: the remainder of lesson 31's long division, computed with three flip-flops and two XOR gates.

Predict

The sender appends a message's CRC to it. What does the receiver get when it computes the CRC of the message plus that appended CRC, if nothing was corrupted?

Name it

The bitwise CRC

The hardware register, in software: keep an n-bit value c (starting from an initial value, often 0). For each data byte, XOR it into the top of c; then, for each of its 8 bits, shift c left one place, and if the bit shifted out was 1, XOR in the generator polynomial (written without its top term: x8+x2+x+1 is 0x07).

The table-driven CRC

The 8 shift-and-XOR steps for a byte depend only on the 8 bits involved, so precompute them once for all 256 possible values: table[i] is the CRC of the single byte i. Then each byte costs one lookup:

c←table[c⊕byte]

for an 8-bit CRC (wider CRCs shift c and XOR in the table entry). That's eight times fewer steps, and it's how CRCs run in software.

Standard CRCs

A CRC is fully specified by its width, generator, initial value and a few bit-order options, and each standard publishes a check value: the CRC of the ASCII string "123456789".

NameWidthGeneratorInitialCheck
CRC-8 (SMBus)80x070x000xF4
CRC-16 (CCITT-FALSE)160x10210xFFFF0x29B1

An n-bit CRC detects every error burst of n bits or fewer, and misses a random corruption with probability 2−n.

Worked example

Step the serial CRC register through the message 1101.

Show the solution
  1. Set up. Start at 000; each clock, the feedback is f=Q2⊕D, then Q2←Q1, Q1←Q0⊕f, Q0←f.
  2. Clock in the message.
DfQ2Q1Q0
11011
11101
01001
11001
  1. Read the remainder. The remainder is 001.

Answer the remainder is 001.

Worked example

What is table[1] for CRC-8 with generator 0x07?

Show the solution
  1. Shift seven times. The byte 00000001 shifts left seven times without anything falling out, reaching 10000000.
  2. The eighth shift. It drops that 1 and XORs in 0x07, so table[1] = 0x07.
  3. In general. table[i] is the remainder of i⋅x8.

Answer table[1] = 0x07.

Check

What is the longest error burst a CRC with a 16-bit remainder is guaranteed to detect?

Check

For random corruption, a 16-bit CRC misses an error with probability 2−n. What is that as a power of ten (the exponent)?

Check

Write crc8(data, poly=0x07, init=0x00) for a bytes object, bitwise, and check it gives 0xF4 for b"123456789".

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write make_table(poly=0x07) returning the 256-entry CRC-8 table, and crc8_fast(data, table, init=0x00) that uses one lookup per byte.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write crc16_ccitt(data): the 16-bit CRC with generator 0x1021 and initial value 0xFFFF, bitwise, which gives 0x29B1 for b"123456789".

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does a CRC catch errors that a simple sum of the bytes would miss?

Practice

Keep practicing

5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.