Crate smbus_pec[][src]

Expand description

This is a portable minimal implementation of the System Management Bus (SMBus) Packet Error Code calculation algorithm intended for use in no_std.

SMBus 1.1 and later defines an optional Packet Error Checking mode. When used, an extra byte is appended to all transmissions containing a Packet Error Code (PEC).

The PEC is calculated over the whole transmission including address and read/write bit. The polynomial used is x^8 + x^2 + x + 1, which corresponds to CRC-8-ATM HEC initialized to zero.

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 a portable and non-configurable implementation of exactly one algorithm: The one used for SMBus PEC (optionally using a pre-calculated lookup table).

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

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

Pre-calculated lookup table

A faster version of the algorithm is provided through the use of a pre-calculated lookup table. This can be enabled through the lookup-table feature.

With this feature enabled a table of 256 pre-calculated u8 values will be included which avoids bit-by-bit calculation at the cost of the space needed to store it.

Usage

Send command with a value to an address using function

A typical use case for this would be writing the value to a register

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

let checksum = pec(&[ADDR << 1, command, value]);
smbus.write(ADDR, &[command, value, checksum]);

Perform request-response using function

A typical use case for this would be reading the value of a register. To do this, a write with the register address is sent, followed by a read.

use smbus_pec::pec;
const ADDR: u8 = 0x5A;
let register = 0x06;
let mut data = [0; 2];

smbus.write_read(ADDR, &[register], &mut data);
let checksum = pec(&[ADDR << 1, register, (ADDR << 1) | 1, data[0]]);
if checksum != data[1] {
  println!("Packet Error Code mismatch.");
}
let value = data[0];

Send command with a value to an address using hasher

A typical use case for this would be writing the value to a register

use core::hash::Hasher;
use smbus_pec::Pec;
const ADDR: u8 = 0x5A;
let command = 0x06;
let value = 0xAB;

let mut hasher = Pec::new();
hasher.write(&[ADDR << 1, command, value]);
let checksum = hasher.finish() as u8;
smbus.write(ADDR, &[command, value, checksum]);

Perform request-response using hasher

A typical use case for this would be reading the value of a register. To do this, a write with the register address is sent, followed by a read.

use core::hash::Hasher;
use smbus_pec::Pec;
const ADDR: u8 = 0x5A;
let register = 0x06;
let mut data = [0; 2];

smbus.write_read(ADDR, &[register], &mut data);
let mut hasher = Pec::new();
hasher.write(&[ADDR << 1, register, (ADDR << 1) | 1, data[0]]);
let checksum = hasher.finish();
if checksum != data[1] as u64 {
  println!("Packet Error Code mismatch.");
}
let value = data[0];

Structs

Pec

Calculate SMBus Packet Error Code over transmitted data. core::hash::Hasher implementation.

Functions

pec

Calculate SMBus Packet Error Code over transmitted data.