C12 · Lesson 35 · Embedded lab
CRCs, framing, ring buffers and bus timing, in Python and C
A device that talks over a serial link needs a way to find where each message starts, a check that it arrived intact, a buffer that never loses a byte while the program is busy, and a budget for how long each bus transaction takes. Both ends of the link need the same code: the firmware, and a program on a computer that talks to it. This lab writes the computer's side in Python (a CRC-16, COBS framing, a ring buffer and a bus-timing calculator), then the board's side of a CRC in C, and checks that both agree on the standard test message.
- COBS framing removes every zero byte from a message, so a zero can mark the end of each frame. The curve is the worst-case overhead: one extra byte per 254, plus one, against the message length.
- For short messages the overhead is a single byte, under 1% once a message passes 100 bytes; for long messages it approaches 1/254, about 0.4%.
- Compare with escaping schemes such as SLIP, whose overhead can double a message full of the special byte.
Predict
A 300-byte message containing no zeros is COBS-encoded. How long is the result, not counting the delimiter?
COBS splits zero-free data into blocks of at most 254 bytes, each with a leading length byte: 254 + 1 and 46 + 1, so 302 bytes. A message with zeros in it costs no more, since each zero is replaced by a length byte.
Name it
The pieces
- CRC-16/CCITT-FALSE: polynomial 0x1021, starting value 0xFFFF, bits processed from the top. Its check value, the CRC of the ASCII text "123456789", is 0x29B1; every implementation should be tested against it.
- CRC-8: polynomial 0x07, starting value 0; its check value is 0xF4.
- COBS (consistent overhead byte stuffing): each block of up to 254 nonzero bytes is sent as its length plus one, followed by the block; the zero that ended the block is implied. The encoded frame has no zeros, so 0x00 delimits frames.
- Ring buffer: a power-of-two array with head and tail counters; full when head − tail equals the size; a full buffer drops the byte and counts an overrun.
- I²C timing: each byte is 9 clocks with its acknowledge; a register read is address, register, repeated start, address and the data bytes, plus about 3 clocks for the start, repeated start and stop.
Check
Write crc16(data): CRC-16/CCITT-FALSE of a bytes object (polynomial 0x1021, start 0xFFFF, most significant bit first, no final XOR).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write cobs_encode(data) and cobs_decode(enc) on lists of byte values. Encoding produces no zeros; decoding reverses it exactly.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write a class Ring(size) (size a power of two) with put(b) (returns False and counts an overrun when full), get() (returns None when empty), count() and an overruns attribute. Use free-running head and tail counters masked with size - 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write i2c_read_time(n, f): seconds for an I²C register read of n data bytes at clock f: 9 clocks for each of the address, register, repeated address and n data bytes, plus 3 for the start, repeated start and stop. Then max_reads(n, f, budget): how many whole such reads fit in budget seconds.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Compute the CRC-8 (polynomial 0x07, starting value 0, most significant bit first) of the nine bytes of "123456789" on the board, and print it. The check value is 0xF4, 244.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a framing scheme like COBS make a serial link recover from errors better than sending a length byte at the start of each message?
With a length byte, the receiver finds the next message by counting bytes from the start of this one. If a byte is lost or a length is corrupted, it counts to the wrong place and every message after that is misread, with no way to tell where the real boundaries are. COBS removes every zero from the data and uses zero only as the end-of-frame marker, so the receiver can always resynchronize: whatever went wrong, it discards bytes until the next zero, and the frame after it is read correctly. Combined with a CRC on each frame, a damaged frame is detected and dropped while the rest of the stream carries on.