dbrgn / shtcx-rs

Platform agnostic Rust driver for the Sensirion SHTCx temperature/humidity sensors.
Apache License 2.0
13 stars 4 forks source link

Hardware CRC #17

Closed dbrgn closed 4 years ago

dbrgn commented 4 years ago

Many STM32 MCUs (including the STM32L071) feature a hardware CRC implementation.

As an optimization, it would be nice if the default CRC software-implementation could be swapped with a custom implementation that may be faster.

This could be done using a trait:

public trait Crc {
    fn crc8(data: &[u8]) -> u8;
}

The driver struct would then be parametrized:

pub struct ShtCx<S: ShtSensor, I2C, C: Crc, D> {
    crc: C,
    ...
}

The simplest way to pass in the concrete implementation would be if the default factory functions (like shtc3) use the default implementation, but the ShtCx struct would allow swapping the implementation:

impl ShtCx<S: ShtSensor, I2C, C: Crc, D> {
    pub fn use_crc<CRC: Crc>(crc: CRC) -> Self<S, I2C, CRC, D> {
        self.crc = crc;
        self
    }
}

However, I'm not sure if the software implementation can be optimized out by the compiler or not. Probably not. Maybe a builder would be better.

What do you think, @rnestler?

rnestler commented 4 years ago

I'm not really sure if it is worth it to use hardware CRC if one just calculates the CRC of two bytes. I guess setting up the hardware CRC is overhead as well, but I may be wrong. IMHO the hardware CRC is useful if one wants to calculate the CRC over some larger data in the flash.

dbrgn commented 4 years ago

Let's close this for now, it's probably overkill for those few bytes.