Compare commits
6 Commits
aht20
...
no-std-and
| Author | SHA1 | Date | |
|---|---|---|---|
| f29522985d | |||
| 606e61c98e | |||
| d36de58e16 | |||
| acf99da972 | |||
| 10fd633a2f | |||
| 79c6da4c60 |
@@ -2,7 +2,6 @@
|
|||||||
runner = "espflash flash --monitor --chip esp32"
|
runner = "espflash flash --monitor --chip esp32"
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
ESP_LOG = "info"
|
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
rustflags = [
|
rustflags = [
|
||||||
|
|||||||
18
esp/rust/02_esp-gen-no-std/Cargo.lock
generated
18
esp/rust/02_esp-gen-no-std/Cargo.lock
generated
@@ -268,15 +268,12 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "esp-gen-no-std"
|
name = "esp-gen-test"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"critical-section",
|
"critical-section",
|
||||||
"esp-bootloader-esp-idf",
|
"esp-bootloader-esp-idf",
|
||||||
"esp-hal",
|
"esp-hal",
|
||||||
"esp-println",
|
|
||||||
"fugit",
|
|
||||||
"log",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -359,19 +356,6 @@ dependencies = [
|
|||||||
"esp-metadata",
|
"esp-metadata",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "esp-println"
|
|
||||||
version = "0.15.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3e7e3ab41e96093d7fd307e93bfc88bd646a8ff23036ebf809e116b18869f719"
|
|
||||||
dependencies = [
|
|
||||||
"critical-section",
|
|
||||||
"document-features",
|
|
||||||
"esp-metadata-generated",
|
|
||||||
"log",
|
|
||||||
"portable-atomic",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "esp-riscv-rt"
|
name = "esp-riscv-rt"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
name = "esp-gen-no-std"
|
name = "esp-gen-test"
|
||||||
rust-version = "1.86"
|
rust-version = "1.86"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "blinky-i2c-scanner"
|
name = "esp-gen-test"
|
||||||
path = "./src/bin/main.rs"
|
path = "./src/bin/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -13,9 +13,6 @@ esp-bootloader-esp-idf = { version = "0.2.0", features = ["esp32"] }
|
|||||||
esp-hal = { version = "=1.0.0-rc.0", features = ["esp32"] }
|
esp-hal = { version = "=1.0.0-rc.0", features = ["esp32"] }
|
||||||
|
|
||||||
critical-section = "1.2.0"
|
critical-section = "1.2.0"
|
||||||
esp-println = { version = "0.15.0", features = ["esp32", "log-04"] }
|
|
||||||
log = "0.4.28"
|
|
||||||
fugit = "0.3.7"
|
|
||||||
|
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|||||||
@@ -6,14 +6,12 @@
|
|||||||
holding buffers for the duration of a data transfer."
|
holding buffers for the duration of a data transfer."
|
||||||
)]
|
)]
|
||||||
|
|
||||||
use esp_hal::i2c::master::BusTimeout;
|
|
||||||
use esp_hal::{
|
use esp_hal::{
|
||||||
clock::CpuClock,
|
clock::CpuClock,
|
||||||
gpio::{Level, Output, OutputConfig},
|
gpio::{Level, Output, OutputConfig},
|
||||||
main,
|
main,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant}
|
||||||
};
|
};
|
||||||
use fugit::RateExtU32;
|
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||||
@@ -27,29 +25,11 @@ esp_bootloader_esp_idf::esp_app_desc!();
|
|||||||
#[main]
|
#[main]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
// generator version: 0.5.0
|
// generator version: 0.5.0
|
||||||
esp_println::logger::init_logger_from_env();
|
|
||||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||||
let peripherals = esp_hal::init(config);
|
let peripherals = esp_hal::init(config);
|
||||||
let i2c_config = esp_hal::i2c::master::Config::default().with_timeout(BusTimeout::Maximum);
|
|
||||||
let mut i2c = esp_hal::i2c::master::I2c::new(peripherals.I2C0, i2c_config)
|
|
||||||
.unwrap()
|
|
||||||
.with_sda(peripherals.GPIO21)
|
|
||||||
.with_scl(peripherals.GPIO22);
|
|
||||||
|
|
||||||
let mut led = Output::new(peripherals.GPIO2, Level::High, OutputConfig::default());
|
let mut led = Output::new(peripherals.GPIO2, Level::High, OutputConfig::default());
|
||||||
loop {
|
loop {
|
||||||
log::info!("Scanning for I2C devices..");
|
|
||||||
for i in 0..=127u8 {
|
|
||||||
let res = i2c.read(i, &mut [0]);
|
|
||||||
match res {
|
|
||||||
Ok(_) => {
|
|
||||||
log::info!("Device found at address: {}", i);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
log::warn!("Failed to read device address: {:?}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
led.toggle();
|
led.toggle();
|
||||||
let delay_start = Instant::now();
|
let delay_start = Instant::now();
|
||||||
while delay_start.elapsed() < Duration::from_millis(500) {}
|
while delay_start.elapsed() < Duration::from_millis(500) {}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use esp_idf_hal::prelude::*;
|
|||||||
|
|
||||||
const SSD1306_ADDRESS: u8 = 0x3c;
|
const SSD1306_ADDRESS: u8 = 0x3c;
|
||||||
|
|
||||||
// TODO: This is not no-std; This is using ESP-IDF-HAL, which uses std
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
esp_idf_hal::sys::link_patches();
|
esp_idf_hal::sys::link_patches();
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
[target.xtensa-esp32-none-elf]
|
|
||||||
runner = "espflash flash --monitor --chip esp32"
|
|
||||||
|
|
||||||
[env]
|
|
||||||
ESP_LOG = "info"
|
|
||||||
|
|
||||||
[build]
|
|
||||||
rustflags = [
|
|
||||||
"-C", "link-arg=-Wl,-Tlinkall.x",
|
|
||||||
"-C", "link-arg=-nostartfiles",
|
|
||||||
]
|
|
||||||
|
|
||||||
target = "xtensa-esp32-none-elf"
|
|
||||||
|
|
||||||
[unstable]
|
|
||||||
build-std = ["core"]
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
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 }}
|
|
||||||
4
esp/rust/04_no-std-aht20/.gitignore
vendored
4
esp/rust/04_no-std-aht20/.gitignore
vendored
@@ -1,4 +0,0 @@
|
|||||||
/.vscode
|
|
||||||
/.embuild
|
|
||||||
/target
|
|
||||||
/Cargo.lock
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "aht20"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Shaun Reed <shaunrd0@gmail.com>"]
|
|
||||||
edition = "2021"
|
|
||||||
resolver = "2"
|
|
||||||
rust-version = "1.77"
|
|
||||||
description = "Reading from AHT20 sensor and drawing to OLED display"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[[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 = []
|
|
||||||
esp32 = [
|
|
||||||
"esp-backtrace/esp32",
|
|
||||||
"esp-hal/esp32",
|
|
||||||
"esp-bootloader-esp-idf/esp32",
|
|
||||||
]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
log = "0.4"
|
|
||||||
embedded-hal = "1.0.0"
|
|
||||||
esp-backtrace = { version = "0.17.0", features = ["esp32", "println", "panic-handler"] }
|
|
||||||
esp-hal = { version = "1.0.0-rc.0", features = ["esp32", "unstable"] }
|
|
||||||
esp-println = { version = "0.15.0", features = ["auto", "log-04"] }
|
|
||||||
esp-bootloader-esp-idf = { version = "0.2.0", default-features = false, features = ["esp-rom-sys", "log-04", "esp32"] }
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
embuild = "0.33"
|
|
||||||
Binary file not shown.
@@ -1,3 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
[toolchain]
|
|
||||||
channel = "esp"
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
# 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
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![no_main]
|
|
||||||
|
|
||||||
use embedded_hal::i2c::ErrorType;
|
|
||||||
use embedded_hal::{delay::DelayNs, i2c::I2c as I2cEmbedded};
|
|
||||||
use esp_backtrace as _;
|
|
||||||
use esp_hal::{delay::Delay, xtensa_lx_rt::entry};
|
|
||||||
|
|
||||||
/// Represents a reading from the sensor.
|
|
||||||
pub struct SensorReading<T> {
|
|
||||||
pub humidity: T,
|
|
||||||
pub temperature: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Dht20<I: I2cEmbedded, D: DelayNs> {
|
|
||||||
pub i2c: I,
|
|
||||||
pub delay: D,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I: I2cEmbedded, D: DelayNs> Dht20<I, D> {
|
|
||||||
const SENSOR_ADDRESS: u8 = 0x38;
|
|
||||||
|
|
||||||
pub fn new(i2c: I, delay: D) -> Self {
|
|
||||||
Self { i2c, delay }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read(&mut self) -> Result<SensorReading<f32>, <I as ErrorType>::Error> {
|
|
||||||
log::info!("Checking status");
|
|
||||||
let mut status_response: [u8; 1] = [0; 1];
|
|
||||||
self.i2c
|
|
||||||
.write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response)?;
|
|
||||||
|
|
||||||
// Calibration if needed
|
|
||||||
if status_response[0] & 0x18 != 0x18 {
|
|
||||||
log::info!("Calibrating sensor {:?}", status_response);
|
|
||||||
for b in [0x1B, 0x1C, 0x1E] {
|
|
||||||
self.i2c.write(Self::SENSOR_ADDRESS, &[b, 0, 0])?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trigger the measurement
|
|
||||||
self.delay.delay_ms(10);
|
|
||||||
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];
|
|
||||||
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];
|
|
||||||
self.i2c
|
|
||||||
.read(Self::SENSOR_ADDRESS, &mut measurement_response)?;
|
|
||||||
|
|
||||||
// Humidity 20 bits (8 + 8 + 4)
|
|
||||||
let mut raw_humidity = measurement_response[1] as u32;
|
|
||||||
// log::info!("Raw Humidity: {:#018b}", raw_humidity);
|
|
||||||
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;
|
|
||||||
// log::info!("Raw Temperature: {:#018b}", raw_temperature);
|
|
||||||
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;
|
|
||||||
|
|
||||||
// TODO: Do we even need to do this? I think hal handles the checksum in the write / read
|
|
||||||
// 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 {
|
|
||||||
log::error!("Calculated CRC {:#018b}", received_crc);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #[panic_handler]
|
|
||||||
// fn panic(_: &core::panic::PanicInfo) -> ! {
|
|
||||||
// loop {}
|
|
||||||
// }
|
|
||||||
|
|
||||||
esp_bootloader_esp_idf::esp_app_desc!();
|
|
||||||
#[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.GPIO21)
|
|
||||||
.with_scl(peripherals.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!("-----");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,30 +5,11 @@ shaunrd0/klips/esp/rust
|
|||||||
├── 01_esp-idf-std # Template project for using ESP-IDF with std enabled.
|
├── 01_esp-idf-std # Template project for using ESP-IDF with std enabled.
|
||||||
├── 02_esp-gen-no-std # Template ESP32 project using no std.
|
├── 02_esp-gen-no-std # Template ESP32 project using no std.
|
||||||
├── 03_no-std-lcd # Drawing to LCD using ESP32 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
|
└── README.md
|
||||||
```
|
```
|
||||||
|
|
||||||
## Development Setup
|
## 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)
|
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)
|
[Espressif Rust book](https://docs.espressif.com/projects/rust/book/introduction.html)
|
||||||
|
|||||||
Reference in New Issue
Block a user