|
| 1 | +# compact-frame-format |
| 2 | + |
| 3 | +[](https://pypi.org/project/compact-frame-format/) |
| 4 | +[](https://github.com/CompactFrameFormat/cff-python/releases) |
| 5 | +[](https://github.com/CompactFrameFormat/cff-python/actions/workflows/test.yml) |
| 6 | +[](https://github.com/CompactFrameFormat/cff-python/blob/master/LICENSE) |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +Compact Frame Format (CFF) is a way of delineating messages (called the _payload_) in a byte stream. It is designed specifically with Microcontrollers (MCUs) in mind, leading to the following design goals: |
| 11 | + |
| 12 | +1. Take advantage of hardware acceleration like Direct Memory Access (DMA) controllers and the CRC peripherals available on most 32-bit MCUs. This precludes the use of delimiter-based packet boundaries that require the CPU to examine every byte. |
| 13 | +2. Exploit the fact that modern serial links are already reliable. Universal Serial Bus (USB), Controller Area Network Bus (CANBus), and Bluetooth Low Energy (BLE), already detect and retransmit lost or corrupted packets. Other serial interfaces like Universal Asynchronous Receiver-Transmitters (UART), Serial Peripheral Interface (SPI), and Inter-Integrated Circuit (I2C) are often reliable in practice, with extremely low error rates when the wiring is short and clean. Therefore, the it's okay for error recovery to be expensive, so long as it's possible and the happy path is cheap. |
| 14 | +3. Easy to implement and debug. Firmware running on MCUs is not amenable to taking dependencies on 3rd party libraries, so the implementation should be small enough to fit comfortably in a single file, and simple enough that you wouldn't mind implementing it yourself if you had to. |
| 15 | +4. Interoperate cleanly with binary serialization formats like [FlatBuffers](https://flatbuffers.dev/) and [CBOR](https://cbor.io/). |
| 16 | + |
| 17 | +In CFF, a frame consists of a header, a payload, and a payload CRC. |
| 18 | + |
| 19 | +```mermaid |
| 20 | +block-beta |
| 21 | + columns 6 |
| 22 | + block:Header["Header<br><br><br>"]:4 |
| 23 | + columns 4 |
| 24 | + space:4 |
| 25 | + Preamble FrameCounter["Frame Counter"] PayloadSize["Payload Size"] HeaderCRC["Header CRC"] |
| 26 | + end |
| 27 | + Payload |
| 28 | + PayloadCRC["Payload CRC"] |
| 29 | +``` |
| 30 | + |
| 31 | +The header consists of: |
| 32 | + |
| 33 | +* A 2-byte preamble: [0xFA, 0xCE]. Note that this is better though of as an array of two bytes rather than an unsigned short (ushort) because it is transmitted as 0xFA, 0xCE (spelling face), whereas the ushort 0xFACE would be transmitted as 0xCE, 0xFA (spelling nothing) in little endian. |
| 34 | +* A little-endian ushort frame counter which increments for every frame sent and rolls over to 0 after 65,535 (2^16 - 1) frames have been sent. |
| 35 | +* A little-endian ushort payload size, in bytes. This gives a theoretical maximum payload size of 65,535, though few MCU-based applications would want to support this. A protocol making use of CFF can enforce smaller maximum payload sizes if desired. Note that this excludes both the header and the payload CRC at the end. In other words, the _frame_ size is `header_size + payload_size + crc_size`. |
| 36 | +* A 16-bit header CRC (see below for details) calculated over the preamble, frame counter, and payload size. This allows the receiver to validate the header and, crucially, the payload size without having to read in the entire frame, as would be the case if there were just one CRC, at the end, covering the entire frame. The problem with having a single CRC is that the if the payload size is corrupted in such a way that it is extremely large (65,535 in the pathological case) the reciever will not detect this until it reads that many bytes, calculates the CRC, and discovers that it doesn't match. Depending on the transmitter's data rate at the time of the error, it could take a long time to receive this many bytes, making the issue look like a dropped link. |
| 37 | + |
| 38 | +Both CRCs are calculated using CRC-16/CCITT-FALSE, with the following settings: |
| 39 | + |
| 40 | +- Width: 16 |
| 41 | +- Polynomial: 0x1021 |
| 42 | +- Init: 0xFFFF |
| 43 | +- RefIn/RefOut: false / false |
| 44 | +- XorOut: 0x0000 |
| 45 | +- Check("123456789): 0x29B1l |
| 46 | + |
| 47 | +## Setup |
| 48 | + |
| 49 | +Checkout the code: |
| 50 | +```powershell |
| 51 | +git clone https://github.com/CompactFrameFormat/cff-python.git |
| 52 | +cd cff-python |
| 53 | +``` |
| 54 | + |
| 55 | +Create a new virtual environment: |
| 56 | +```powershell |
| 57 | +uv venv && .venv\Scripts\activate.ps1 |
| 58 | +``` |
| 59 | + |
| 60 | +Install dependencies (including test): |
| 61 | +```powershell |
| 62 | +uv pip install -e '.[test]' |
| 63 | +``` |
| 64 | + |
| 65 | +Run the tests: |
| 66 | +```powershell |
| 67 | +python -m pytest |
| 68 | +``` |
0 commit comments