Skip to content

Commit 900e586

Browse files
committed
Init repo
0 parents  commit 900e586

File tree

8 files changed

+155
-0
lines changed

8 files changed

+155
-0
lines changed

.cargo/config.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[build]
2+
target = "riscv32imc-esp-espidf"
3+
4+
[target.riscv32imc-esp-espidf]
5+
linker = "ldproxy"
6+
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
7+
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
8+
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
9+
10+
[unstable]
11+
build-std = ["std", "panic_abort"]
12+
13+
[env]
14+
MCU="esp32c3"
15+
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
16+
ESP_IDF_VERSION = "v5.1.2"
17+

.github/workflows/rust_ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- "**/README.md"
7+
pull_request:
8+
workflow_dispatch:
9+
10+
env:
11+
CARGO_TERM_COLOR: always
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
14+
jobs:
15+
rust-checks:
16+
name: Rust Checks
17+
runs-on: ubuntu-latest
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
action:
22+
- command: build
23+
args: --release
24+
- command: fmt
25+
args: --all -- --check --color always
26+
- command: clippy
27+
args: --all-targets --all-features --workspace -- -D warnings
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v4
31+
- name: Enable caching
32+
uses: Swatinem/rust-cache@v2
33+
- name: Setup Rust
34+
uses: dtolnay/rust-toolchain@v1
35+
with:
36+
toolchain: nightly
37+
components: rust-src
38+
- name: Run command
39+
run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.vscode
2+
/.embuild
3+
/target
4+
/Cargo.lock

Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "rust-esp32-bme280"
3+
version = "0.1.0"
4+
authors = ["Wei-ying Chen <[email protected]>"]
5+
edition = "2021"
6+
resolver = "2"
7+
rust-version = "1.71"
8+
9+
[profile.release]
10+
opt-level = "s"
11+
12+
[profile.dev]
13+
debug = true # Symbols are nice, and they don't increase the size on Flash
14+
opt-level = "z"
15+
16+
[features]
17+
default = ["std", "embassy", "esp-idf-svc/native"]
18+
19+
pio = ["esp-idf-svc/pio"]
20+
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
21+
alloc = ["esp-idf-svc/alloc"]
22+
nightly = ["esp-idf-svc/nightly"]
23+
experimental = ["esp-idf-svc/experimental"]
24+
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
25+
26+
[dependencies]
27+
log = { version = "0.4", default-features = false }
28+
esp-idf-svc = { version = "0.48", default-features = false }
29+
bme280 = "0.5.0"
30+
31+
[build-dependencies]
32+
embuild = "0.31.3"
33+
34+
[patch.crates-io]
35+
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal" }

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
embuild::espidf::sysenv::output();
3+
}

rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "nightly"
3+
components = ["rust-src"]

sdkconfig.defaults

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
2+
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
3+
4+
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
5+
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
6+
#CONFIG_FREERTOS_HZ=1000
7+
8+
# Workaround for https://github.com/espressif/esp-idf/issues/7631
9+
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
10+
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n

src/main.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use esp_idf_svc::hal::{
2+
delay,
3+
i2c::{I2cConfig, I2cDriver},
4+
peripherals::Peripherals,
5+
prelude::*,
6+
};
7+
8+
use bme280::i2c::BME280;
9+
10+
fn main() {
11+
// It is necessary to call this function once. Otherwise some patches to the runtime
12+
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
13+
esp_idf_svc::sys::link_patches();
14+
15+
// Bind the log crate to the ESP Logging facilities
16+
esp_idf_svc::log::EspLogger::initialize_default();
17+
18+
let peripherals = Peripherals::take().unwrap();
19+
let sda = peripherals.pins.gpio2;
20+
let scl = peripherals.pins.gpio3;
21+
let config = I2cConfig::new().baudrate(400.kHz().into());
22+
let i2c = I2cDriver::new(peripherals.i2c0, sda, scl, &config).unwrap();
23+
let mut bme280 = BME280::new_primary(i2c);
24+
let mut delay = delay::FreeRtos;
25+
26+
match bme280.init(&mut delay) {
27+
Ok(_) => log::info!("Successfully initialized BME280 device"),
28+
Err(_) => log::error!("Failed to initialize BME280 device"),
29+
}
30+
31+
loop {
32+
match bme280.measure(&mut delay) {
33+
Ok(measurements) => {
34+
log::info!("Relative Humidity = {}%", measurements.humidity);
35+
log::info!("Temperature = {} deg C", measurements.temperature);
36+
log::info!("Pressure = {} pascals", measurements.pressure);
37+
}
38+
39+
Err(e) => log::error!("Failed to get measurements: {:?}", e),
40+
}
41+
42+
delay::FreeRtos::delay_ms(1000u32);
43+
}
44+
}

0 commit comments

Comments
 (0)