Crate embedded_crc_macros[][src]

Expand description

This crate provides macros that define portable CRC-8 algorithm implementations with the parameters directly provided at compile time and without any dependencies. Intended for use in no_std.

How this crate compares to others

There is a number of crates implementing CRC algorithms but their intention is to be configurable, generic, use acceleration via SIMD instructions, etc.

This crate provides macros that define portable and non-configurable CRC-8 algorithm implementations with the parameters provided at compile time (optionally using a pre-calculated lookup table) and without any dependencies.

This should allow the compiler to make good optimizations and allows for use of the algorithm in any target architecture with minimal code bloat.

Furthermore, this crate provides macros to generate the lookup tables on build time.

This makes this crate specially well suited for use in no_std environments.

Usage

The examples implement the System Management Bus (SMBus) Packet Error Code calculation algorithm, also called CRC-8-ATM HEC.

This algorithm uses the polynomial x^8 + x^2 + x + 1 which corresponds to the value 7 with an initial value of 0.

See here for more information.

Define a function computing the SMBus PEC algorithm

use embedded_crc_macros::crc8;

// 7 corresponds to the polynomial x^8 + x^2 + x + 1
crc8!(fn smbus_pec, 7, 0, "SMBus Packet Error Code");

const ADDR: u8 = 0x5A;
let command = 0x06;
let value = 0xAB;

let checksum = smbus_pec(&[ADDR << 1, command, value]);
println!("PEC: {}", checksum);

Define a function computing the SMBus PEC algorithm using a pre-calculated lookup table

A lookup table constant must be defined in the same environment. This can be generated in the build.rs file with the build_rs_lookup_table_file_generation macro.

use embedded_crc_macros::crc8_lookup_table;

crc8_lookup_table!(fn smbus_pec, 0, LOOKUP_TABLE, "SMBus Packet Error Code");

const ADDR: u8 = 0x5A;
let command = 0x06;
let value = 0xAB;

let checksum = smbus_pec(&[ADDR << 1, command, value]);
println!("PEC: {}", checksum);

Define structure implementing the SMBus PEC algorithm as a core::hash::Hasher

use core::hash::Hasher;
use embedded_crc_macros::crc8_hasher;

crc8_hasher!(struct SmbusPec, 7 /* x^8+x^2+x+1 */, 0, "SMBus Packet Error Code");

let mut hasher = SmbusPec::new();
hasher.write(&[0xAB, 0xCD]);
let pec = hasher.finish();

println!("PEC: {}", pec);

core::hash::Hasher implementation using a pre-calculated lookup table.

A lookup table constant must be defined in the same environment. This can be generated in the build.rs file with the build_rs_lookup_table_file_generation macro.

use core::hash::Hasher;
use embedded_crc_macros::crc8_hasher_lookup_table;

// include!(concat!(env!("OUT_DIR"), "/lookup_table.rs"));
crc8_hasher_lookup_table!(struct SmbusPec, 0, LOOKUP_TABLE, "SMBus Packet Error Code");

let mut hasher = SmbusPec::new();
hasher.write(&[0xAB, 0xCD]);
let pec = hasher.finish();

println!("PEC: {}", pec);

Macros

build_rs_lookup_table_file_generation

Code generation macro for use in build.rs files.

crc8

Define public function implementing the CRC-8 algorithm for the given polynomial and initial value.

crc8_hasher

Define public structure implementing the CRC-8 algorithm for the given polynomial and initial value as a core::hash::Hasher trait implementation.

crc8_hasher_lookup_table

Define public structure implementing the CRC-8 algorithm as a core::hash::Hasher trait implementation using a pre-calculated lookup table.

crc8_lookup_table

Define public function implementing the CRC-8 algorithm for the given polynomial and initial value using a lookup table.