diff --git a/esp/rust/04_no-std-aht20/.cargo/config.toml b/esp/rust/04_no-std-aht20/.cargo/config.toml new file mode 100644 index 0000000..dcd155a --- /dev/null +++ b/esp/rust/04_no-std-aht20/.cargo/config.toml @@ -0,0 +1,16 @@ +[build] +target = "xtensa-esp32-espidf" + +[target.xtensa-esp32-espidf] +linker = "ldproxy" +runner = "espflash flash --monitor" +rustflags = [ "--cfg", "espidf_time64"] + +[unstable] +build-std = ["std", "panic_abort"] + +[env] +MCU="esp32" +# Note: this variable is not used by the pio builder (`cargo build --features pio`) +ESP_IDF_VERSION = "v5.3.3" + diff --git a/esp/rust/04_no-std-aht20/.github/workflows/rust_ci.yml b/esp/rust/04_no-std-aht20/.github/workflows/rust_ci.yml new file mode 100644 index 0000000..b69d289 --- /dev/null +++ b/esp/rust/04_no-std-aht20/.github/workflows/rust_ci.yml @@ -0,0 +1,42 @@ +name: Continuous Integration + +on: + push: + branches: + - main + paths-ignore: + - "**/README.md" + pull_request: + workflow_dispatch: + +env: + CARGO_TERM_COLOR: always + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + rust-checks: + name: Rust Checks + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + action: + - command: build + args: --release + - command: fmt + args: --all -- --check --color always + - command: clippy + args: --all-targets --all-features --workspace -- -D warnings + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Setup Rust + uses: esp-rs/xtensa-toolchain@v1.6 + with: + default: true + buildtargets: esp32 + ldproxy: true + - name: Enable caching + uses: Swatinem/rust-cache@v2 + - name: Run command + run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }} diff --git a/esp/rust/04_no-std-aht20/.gitignore b/esp/rust/04_no-std-aht20/.gitignore new file mode 100644 index 0000000..73a638b --- /dev/null +++ b/esp/rust/04_no-std-aht20/.gitignore @@ -0,0 +1,4 @@ +/.vscode +/.embuild +/target +/Cargo.lock diff --git a/esp/rust/04_no-std-aht20/Cargo.toml b/esp/rust/04_no-std-aht20/Cargo.toml new file mode 100644 index 0000000..0a030f6 --- /dev/null +++ b/esp/rust/04_no-std-aht20/Cargo.toml @@ -0,0 +1,52 @@ +[package] +name = "aht20" +version = "0.1.0" +authors = ["shaun"] +edition = "2021" +resolver = "2" +rust-version = "1.77" + +[[bin]] +name = "aht20" +harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors + +[profile.release] +opt-level = "s" + +[profile.dev] +debug = true # Symbols are nice and they don't increase the size on Flash +opt-level = "z" + +[features] +default = [] + +experimental = ["esp-idf-svc/experimental"] + +[dependencies] +log = "0.4" +esp-idf-svc = "0.51" +embedded-hal = "1.0.0" +esp-backtrace = "0.17.0" +esp-hal = { version = "1.0.0-rc.0", features = ["unstable"] } +fugit = "0.3.7" +esp-println = { version = "0.15.0", features = ["log-04"] } + +# --- Optional Embassy Integration --- +# esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] } + +# If you enable embassy-time-driver, you MUST also add one of: + +# a) Standalone Embassy libs ( embassy-time, embassy-sync etc) with a foreign async runtime: +# embassy-time = { version = "0.4.0", features = ["generic-queue-8"] } # NOTE: any generic-queue variant will work + +# b) With embassy-executor: +# embassy-executor = { version = "0.7", features = ["executor-thread", "arch-std"] } + +# NOTE: if you use embassy-time with embassy-executor you don't need the generic-queue-8 feature + +# --- Temporary workaround for embassy-executor < 0.8 --- +# esp-idf-svc = { version = "0.51", features = ["embassy-time-driver", "embassy-sync"] } +# critical-section = { version = "1.1", features = ["std"], default-features = false } + +[build-dependencies] +embuild = "0.33" diff --git a/esp/rust/04_no-std-aht20/Data-Sheet-AHT20-Humidity-and-Temperature-Sensor-ASAIR-V1.0.03.pdf b/esp/rust/04_no-std-aht20/Data-Sheet-AHT20-Humidity-and-Temperature-Sensor-ASAIR-V1.0.03.pdf new file mode 100644 index 0000000..86cc200 Binary files /dev/null and b/esp/rust/04_no-std-aht20/Data-Sheet-AHT20-Humidity-and-Temperature-Sensor-ASAIR-V1.0.03.pdf differ diff --git a/esp/rust/04_no-std-aht20/README.md b/esp/rust/04_no-std-aht20/README.md new file mode 100644 index 0000000..8f7364f --- /dev/null +++ b/esp/rust/04_no-std-aht20/README.md @@ -0,0 +1,3 @@ +https://asairsensors.com/wp-content/uploads/2021/09/Data-Sheet-AHT20-Humidity-and-Temperature-Sensor-ASAIR-V1.0.03.pdf + +https://github.com/rust-dd/embedded-dht-rs/tree/main/src \ No newline at end of file diff --git a/esp/rust/04_no-std-aht20/build.rs b/esp/rust/04_no-std-aht20/build.rs new file mode 100644 index 0000000..112ec3f --- /dev/null +++ b/esp/rust/04_no-std-aht20/build.rs @@ -0,0 +1,3 @@ +fn main() { + embuild::espidf::sysenv::output(); +} diff --git a/esp/rust/04_no-std-aht20/rust-toolchain.toml b/esp/rust/04_no-std-aht20/rust-toolchain.toml new file mode 100644 index 0000000..a2f5ab5 --- /dev/null +++ b/esp/rust/04_no-std-aht20/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "esp" diff --git a/esp/rust/04_no-std-aht20/sdkconfig.defaults b/esp/rust/04_no-std-aht20/sdkconfig.defaults new file mode 100644 index 0000000..c25b89d --- /dev/null +++ b/esp/rust/04_no-std-aht20/sdkconfig.defaults @@ -0,0 +1,10 @@ +# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 + +# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). +# This allows to use 1 ms granularity for thread sleeps (10 ms by default). +#CONFIG_FREERTOS_HZ=1000 + +# Workaround for https://github.com/espressif/esp-idf/issues/7631 +#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n +#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n diff --git a/esp/rust/04_no-std-aht20/src/main.rs b/esp/rust/04_no-std-aht20/src/main.rs new file mode 100644 index 0000000..efc88c0 --- /dev/null +++ b/esp/rust/04_no-std-aht20/src/main.rs @@ -0,0 +1,152 @@ +#![no_std] +#![no_main] + +/// Represents a reading from the sensor. +pub struct SensorReading { + pub humidity: T, + pub temperature: T, +} + +/// Possible errors when interacting with the sensor. +#[derive(Debug)] +pub enum SensorError { + ChecksumMismatch, + Timeout, + PinError, +} + +pub struct Dht20 { + pub i2c: I, + pub delay: D, +} + +impl Dht20 { + const SENSOR_ADDRESS: u8 = 0x38; + + pub fn new(i2c: I, delay: D) -> Self { + Self { i2c, delay } + } + + pub fn read(&mut self) -> Result, SensorError> { + // Check status + let mut status_response: [u8; 1] = [0; 1]; + let _ = self + .i2c + .write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response); + + // Calibration if needed + if status_response[0] & 0x18 != 0x18 { + let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1B, 0, 0]); + let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1C, 0, 0]); + let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1E, 0, 0]); + } + + // Trigger the measurement + self.delay.delay_ms(10); + let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0xAC, 0x33, 0x00]); + + // Read the measurement status + self.delay.delay_ms(80); + loop { + let mut measurement_status_response: [u8; 1] = [0; 1]; + let _ = self + .i2c + .read(Self::SENSOR_ADDRESS, &mut measurement_status_response); + let status_word = measurement_status_response[0]; + if status_word & 0b1000_0000 == 0 { + break; + } + self.delay.delay_ms(1); + } + + // Read the measurement (1 status + 5 data + 1 crc) + let mut measurement_response: [u8; 7] = [0; 7]; + let _ = self + .i2c + .read(Self::SENSOR_ADDRESS, &mut measurement_response); + + // Humidity 20 bits (8 + 8 + 4) + let mut raw_humidity = measurement_response[1] as u32; + raw_humidity = (raw_humidity << 8) + measurement_response[2] as u32; + raw_humidity = (raw_humidity << 4) + (measurement_response[3] >> 4) as u32; + let humidity_percentage = (raw_humidity as f32 / ((1 << 20) as f32)) * 100.0; + + // Temperature 20 bits + let mut raw_temperature = (measurement_response[3] & 0b1111) as u32; + raw_temperature = (raw_temperature << 8) + measurement_response[4] as u32; + raw_temperature = (raw_temperature << 8) + measurement_response[5] as u32; + let temperature_percentage = (raw_temperature as f32 / ((1 << 20) as f32)) * 200.0 - 50.0; + + // Compare the calculated CRC with the received CRC + let data = &measurement_response[..6]; + let received_crc = measurement_response[6]; + let calculated_crc = Self::calculate_crc(data); + if received_crc != calculated_crc { + return Err(SensorError::ChecksumMismatch); + } + + Ok(SensorReading { + humidity: humidity_percentage, + temperature: temperature_percentage, + }) + } + + fn calculate_crc(data: &[u8]) -> u8 { + let polynomial = 0x31u8; // x^8 + x^5 + x^4 + 1 + let mut crc = 0xFFu8; + + for &byte in data { + crc ^= byte; + // CRC8 - process every bit + for _ in 0..8 { + if crc & 0x80 != 0 { + crc = (crc << 1) ^ polynomial; + } else { + crc <<= 1; + } + } + } + + crc + } +} + +use embedded_hal::{delay::DelayNs, i2c::I2c as I2cEmbedded}; +use esp_backtrace as _; +use esp_hal::{ + clock::CpuClock, + delay::Delay, + gpio::{Level, Pull}, + i2c::master::I2c, + xtensa_lx_rt::entry, +}; +use fugit::{ExtU64, HertzU32}; + +#[entry] +fn main() -> ! { + let peripherals = esp_hal::init(esp_hal::Config::default()); + esp_println::logger::init_logger_from_env(); + let mut delay = Delay::new(); + + // TODO: Remove unwrap + let i2c_for_dht20 = + esp_hal::i2c::master::I2c::new(peripherals.I2C0, esp_hal::i2c::master::Config::default()) + .unwrap() + .with_sda(peripherals.pins.gpio21) + .with_scl(peripherals.pins.gpio22); + let mut dht20 = Dht20::new(i2c_for_dht20, delay); + + loop { + delay.delay_ms(5000); + match dht20.read() { + Ok(sensor_reading) => log::info!( + "DHT 20 Sensor - Temperature: {} °C, humidity: {} %", + sensor_reading.temperature, + sensor_reading.humidity + ), + Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), + } + + log::info!("-----"); + } +} diff --git a/esp/rust/README.md b/esp/rust/README.md index 1562c65..f9b4d2c 100644 --- a/esp/rust/README.md +++ b/esp/rust/README.md @@ -5,11 +5,30 @@ shaunrd0/klips/esp/rust ├── 01_esp-idf-std # Template project for using ESP-IDF with std enabled. ├── 02_esp-gen-no-std # Template ESP32 project using no std. ├── 03_no-std-lcd # Drawing to LCD using ESP32 no std. +├── 04_no-std-aht20 # Reading temperature and humidity from a AHT20 sensor using ESP32 no std. └── README.md ``` ## Development Setup +Install Rust + +```bash +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +``` + +You must run these commands to set up ESP before building any of these examples. + +```bash +sudo apt install python3-venv +mkdir /tmp/espup && cd /tmp/espup +cargo install espup --locked +espup install +cargo install ldproxy +sudo usermod -aG dialout $USER +newgrp dialout +``` + Some notes I took while setting this up for the first time: [Knoats](https://knoats.com/books/esp32/page/rust) [Espressif Rust book](https://docs.espressif.com/projects/rust/book/introduction.html)