hauju / scd4x-rs

Rust driver for the Sensirion SCD4x sensors.
Apache License 2.0
5 stars 12 forks source link

CRC error #6

Closed vojty closed 1 year ago

vojty commented 2 years ago

Hey, I'm trying to use SCD41 + ESP32 with this library, however, I'm getting a CRC error using this piece of code:

use esp_idf_hal::delay;
use esp_idf_hal::i2c;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
use scd4x::scd4x::Scd4x;
...
    let peripherals = Peripherals::take().unwrap();
    let i2c = peripherals.i2c1;
    let sda = peripherals.pins.gpio21;
    let scl = peripherals.pins.gpio22;

    println!("Starting I2C test");

    let config = <i2c::config::MasterConfig as Default>::default().baudrate(100.kHz().into());
    let i2c = i2c::Master::<i2c::I2C1, _, _>::new(i2c, i2c::MasterPins { sda, scl }, config)?;

    let mut delay = delay::FreeRtos;
    let mut sensor = Scd4x::new(i2c, delay);

    sensor.wake_up();
    sensor.stop_periodic_measurement().unwrap();

    let serial = sensor.serial_number().unwrap(); <------------------- this panics
    println!("serial: {:#04x}", serial);

If I comment out this line https://github.com/Sensirion/sensirion-i2c-rs/blob/master/src/i2c.rs#L68 then it starts working, including reading the measures. Am I missing something? Can I opt-out of the CRC validation somehow?

vojty commented 2 years ago

so after more debugging, I've got this output:

CRC8 validation failed for [9, 171, 84] (expected 54, calculated e9)
CRC8 validation failed for [109, 59, 187] (expected bb, calculated 41)
CRC8 validation failed for [124, 40, 49] (expected 31, calculated c1)
CO2: 2475, Temperature: 29.67 °C, Humidity: 48.50 RH,

The CRC calculation is somehow wrong but if I try the calculation from https://github.com/Sensirion/sensirion-i2c-rs/blob/master/src/crc8.rs#L11-L25 in the Rust playground, it works well. Don't really know what's going on here 🤷‍♂️

hauju commented 2 years ago

Hmm i have no clue. I will look at it. Which ESP board do you use?

vojty commented 2 years ago

I dug a little more and it looks like a problem with Rust + ESP32. I've raised an issue here https://github.com/esp-rs/rust-build/issues/162 I don't think it's related to this library, so feel free to close it.

hauju commented 1 year ago
    This should now be fixed with the 1.65.0.0 release, grab a compiler here: https://github.com/esp-rs/rust-build/releases/tag/v1.65.0.0

Originally posted by @MabezDev in https://github.com/esp-rs/rust/issues/134#issuecomment-1302269376