Skip to main content

Python reference implementation of Compact Frame Format, a simple, low-overhead frame format designed for fast, reliable serial links.

Project description

Python Reference Implementation of Compact Frame Format

PyPI Changelog Tests License

Overview

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:

  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.
  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.
  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.
  4. Interoperate cleanly with binary serialization formats like FlatBuffers and CBOR.

In CFF, a frame consists of a header, a payload, and a payload CRC.

block-beta
  columns 6
  block:Header["Header<br><br><br>"]:4
    columns 4
    space:4
    Preamble FrameCounter["Frame Counter"] PayloadSize["Payload Size"] HeaderCRC["Header CRC"]
  end
  Payload
  PayloadCRC["Payload CRC"]

The header consists of:

  • 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.
  • 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.
  • 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.
  • 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.

Both CRCs are calculated using CRC-16/CCITT-FALSE, with the following settings:

  • Width: 16
  • Polynomial: 0x1021
  • Init: 0xFFFF
  • RefIn/RefOut: false / false
  • XorOut: 0x0000
  • Check("123456789): 0x29B1l

Setup

Checkout the code:

git clone https://github.com/CompactFrameFormat/cff-python.git
cd cff-python

Create a new virtual environment:

uv venv && .venv\Scripts\activate.ps1

Install dependencies (including test):

uv pip install -e '.[test]'

Run the tests:

python -m pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compact_frame_format-1.0.0rc1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compact_frame_format-1.0.0rc1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file compact_frame_format-1.0.0rc1.tar.gz.

File metadata

  • Download URL: compact_frame_format-1.0.0rc1.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for compact_frame_format-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 770df3f40f2d505089acfffd2310630b753f72fdeadf40ed515fb998f239a7cc
MD5 07555389ab8605edd7fa22212a4e0cdf
BLAKE2b-256 e56c3d9b387b8ebf5193a952e8b2c08fcda970dbfb3a6acda60774e827b84239

See more details on using hashes here.

Provenance

The following attestation bundles were made for compact_frame_format-1.0.0rc1.tar.gz:

Publisher: publish.yml on CompactFrameFormat/cff-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file compact_frame_format-1.0.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for compact_frame_format-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef5dc6379c4bac421bf0d7d82d3f2e3a6f3cc62ccc0cb7f9ff18f3853e2c247e
MD5 a7bbb7c88b1080c4fa8095c7e213cc14
BLAKE2b-256 516fdfafb0e9afd00d0c4f0c6453a9206d0d40dd3f6f04cbbe1fd8247a895422

See more details on using hashes here.

Provenance

The following attestation bundles were made for compact_frame_format-1.0.0rc1-py3-none-any.whl:

Publisher: publish.yml on CompactFrameFormat/cff-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page