#checksum #sm-bus #crc #pec

no-std smbus-pec

Minimal portable implementation of SMBus Packet Error Code calculation algorithm

3 releases (stable)

1.0.1 May 23, 2021
1.0.0 May 13, 2021
0.1.0 Aug 9, 2020

#1012 in Algorithms

Download history 721/week @ 2023-12-16 785/week @ 2023-12-23 2704/week @ 2023-12-30 2016/week @ 2024-01-06 566/week @ 2024-01-13 521/week @ 2024-01-20 1071/week @ 2024-01-27 1027/week @ 2024-02-03 2025/week @ 2024-02-10 1108/week @ 2024-02-17 1965/week @ 2024-02-24 564/week @ 2024-03-02 2286/week @ 2024-03-09 1622/week @ 2024-03-16 1030/week @ 2024-03-23 2519/week @ 2024-03-30

7,735 downloads per month
Used in 2 crates

MIT/Apache

16KB
57 lines

Rust Portable SMBus Packet Error Code Algorithm

crates.io Docs Build Status Coverage Status

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

use smbus_pec::pec;

const ADDRESS: u8 = 0x5A;
const REGISTER: u8 = 0x06;

fn main() {
    let pec_write = pec(&[ADDRESS << 1, REGISTER, 0xAB, 0xCD]);
    println!("PEC: {}", pec_write); // prints 95

    let data = [ADDRESS << 1, REGISTER, (ADDRESS << 1) + 1, 38, 58];
    let pec_write_read = pec(&data);
    println!("PEC: {}", pec_write_read); // prints 102
}

Support

For questions, issues, feature requests, other changes, or just feedback, please file an issue in the github project.

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~18KB