From a76c1a5e9b78657a4d88851979e83bcac9139af4 Mon Sep 17 00:00:00 2001 From: Carl Middleton Date: Sun, 25 Feb 2024 16:43:27 -0800 Subject: [PATCH] hello world working with sys --- .cargo/config.toml | 17 +++++++++++++++ .github/workflows/rust_ci.yml | 40 +++++++++++++++++++++++++++++++++++ .gitignore | 5 +++++ Cargo.toml | 35 ++++++++++++++++++++++++++++++ build.rs | 22 +++++++++++++++++++ cfg.toml.example | 3 +++ rust-toolchain.toml | 4 ++++ sdkconfig.defaults | 10 +++++++++ src/main.rs | 24 +++++++++++++++++++++ 9 files changed, 160 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .github/workflows/rust_ci.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 cfg.toml.example create mode 100644 rust-toolchain.toml create mode 100644 sdkconfig.defaults create mode 100644 src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..727a0e4 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,17 @@ +[build] +target = "xtensa-esp32-espidf" + +[target.xtensa-esp32-espidf] +linker = "ldproxy" +# runner = "espflash --monitor" # Select this runner for espflash v1.x.x +runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x +rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 + +[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.1.2" + diff --git a/.github/workflows/rust_ci.yml b/.github/workflows/rust_ci.yml new file mode 100644 index 0000000..6914f4d --- /dev/null +++ b/.github/workflows/rust_ci.yml @@ -0,0 +1,40 @@ +name: Continuous Integration + +on: + push: + 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: Enable caching + uses: Swatinem/rust-cache@v2 + - name: Setup Rust + uses: esp-rs/xtensa-toolchain@v1.5 + with: + default: true + buildtargets: esp32 + ldproxy: true + - name: Run command + run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b696447 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.vscode +/.embuild +/target +/Cargo.lock +/cfg.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..56f22ec --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "sensesp-rs" +version = "0.1.0" +authors = ["Carl Middleton "] +edition = "2021" +resolver = "2" +rust-version = "1.71" + +[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 = ["std", "embassy", "esp-idf-svc/native"] +pio = ["esp-idf-svc/pio"] +std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] +alloc = ["esp-idf-svc/alloc"] +nightly = ["esp-idf-svc/nightly"] +experimental = ["esp-idf-svc/experimental"] +embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] + +[dependencies] +anyhow = "=1.0.75" +esp-idf-svc = { version = "0.48", default-features = false } +log = { version = "0.4", default-features = false } +#rgb-led = { path = "../../common/lib/rgb-led" } +#wifi = { path = "../../common/lib/wifi" } +toml-cfg = "=0.1.3" + +[build-dependencies] +embuild = "0.31.3" +toml-cfg = "=0.1.3" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..8c39435 --- /dev/null +++ b/build.rs @@ -0,0 +1,22 @@ +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, +} + +fn main() { + // Check if the `cfg.toml` file exists and has been filled out. + if !std::path::Path::new("cfg.toml").exists() { + panic!("You need to create a `cfg.toml` file with your Wi-Fi credentials! Use `cfg.toml.example` as a template."); + } + + // The constant `CONFIG` is auto-generated by `toml_config`. + let app_config = CONFIG; + if app_config.wifi_ssid == "FBI Surveillance Van" || app_config.wifi_psk == "hunter2" { + panic!("You need to set the Wi-Fi credentials in `cfg.toml`!"); + } + + embuild::espidf::sysenv::output(); +} diff --git a/cfg.toml.example b/cfg.toml.example new file mode 100644 index 0000000..8763632 --- /dev/null +++ b/cfg.toml.example @@ -0,0 +1,3 @@ +[hardware-check] +wifi_ssid = "FBI Surveillance Van" +wifi_psk = "hunter1" \ No newline at end of file diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..90aabae --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "esp" +components = ["rustfmt", "rustc-dev"] +targets = ["xtensa-esp32-none-elf"] diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..9ea5d73 --- /dev/null +++ b/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 granuality 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/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6089a5d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,24 @@ +use anyhow::{bail, Result}; +use esp_idf_svc::eventloop::EspSystemEventLoop; +use esp_idf_svc::hal::prelude::Peripherals; +use log::info; +use esp_idf_svc::wifi; + +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, +} + +fn main() { + // It is necessary to call this function once. Otherwise some patches to the runtime + // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 + esp_idf_svc::sys::link_patches(); + + // Bind the log crate to the ESP Logging facilities + esp_idf_svc::log::EspLogger::initialize_default(); + + log::info!("Hello, world!"); +}