Skip to content

Possible miscompilation error? #164

Closed
@vojty

Description

@vojty

Hello, I've found a strange error and I'm not sure what the problem might be. I've been trying to use ESP32 + SCD41 with no luck. I tracked down the problem to this piece of code:

// source https://github.com/Sensirion/sensirion-i2c-rs/blob/master/src/crc8.rs#L11-L25
pub fn calculate(data: &[u8]) -> u8 {
    const CRC8_POLYNOMIAL: u8 = 0x31;
    let mut crc: u8 = 0xff;
    for byte in data {
        crc ^= byte;
        for _ in 0..8 {
            if (crc & 0x80) > 0 {
                crc = (crc << 1) ^ CRC8_POLYNOMIAL;
            } else {
                crc <<= 1;
            }
        }
    }
    crc
}

For the given example data [9, 94] the correct result should be 35 but I'm getting 55. It works correctly on the blank Rust project using cargo new but it fails when I deploy the code to my ESP32.

I've created an example here https://wokwi.com/projects/345876589283639891 with 2 identical calculate functions, the only difference is that one of them has println! macros inside. However, the result differs.

Does anyone know what's going on? What am I missing?

Configuration:

➜  rust-esp32-std git:(master) ✗ rustup show         
Default host: aarch64-apple-darwin
rustup home:  /Users/tomas.vojtasek/.rustup

installed toolchains
--------------------

stable-aarch64-apple-darwin (default)
nightly-aarch64-apple-darwin
esp
esp-1.64.0.0

active toolchain
----------------

esp (directory override for '/Users/tomas.vojtasek/fun/rust-esp32-std')
rustc 1.64.0-nightly (f53d2361e 2022-09-20)

----------------

esp (directory override for '/Users/tomas.vojtasek/fun/rust-esp32-std')
rustc 1.64.0-nightly (f53d2361e 2022-09-20)
➜  rust-esp32-std git:(master) ✗ ./install-rust-toolchain.sh 
Processing configuration:
--build-target          = esp32,esp32s2,esp32s3
--cargo-home            = /Users/tomas.vojtasek/.cargo
--clear-cache           = YES
--esp-idf-version       = 
--export-file           = export-esp.sh
--extra-crates          = ldproxy cargo-espflash
--installation-mode     = install
--llvm-version          = esp-14.0.0-20220415
--minified-esp-idf      = NO
--minified-llvm         = YES
--nightly-version       = nightly
--rustup-home           = /Users/tomas.vojtasek/.rustup
--toolchain-version     = 1.64.0.0
--toolchain-destination = /Users/tomas.vojtasek/.rustup/toolchains/esp
/Users/tomas.vojtasek/.cargo/bin/rustup
nightly-aarch64-apple-darwin

Chip: Espressif ESP32-WROOM-32 (https://www.laskakit.cz/laskakit-esp-vindriktning-esp-32-i2c/)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions