From 15062deb48a57ecfd7a538cac1c44d46fb71c6ca Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Tue, 17 Oct 2023 07:18:49 +0000 Subject: [PATCH] Revert "Revert "Update to new crates; simplify advanced options"" This reverts commit 574e57b03927fe6c302458cb9c98452933732d59. --- cargo/Cargo.toml | 36 ++++++++----------- cargo/build.rs | 9 +++-- cargo/cargo-generate.toml | 13 ++++--- cargo/pre-script.rhai | 2 +- cargo/sdkconfig.defaults | 2 +- cargo/src/main.rs | 20 +++++------ cmake/cargo-generate.toml | 25 ++++++++++--- .../rust-{{project-name}}/Cargo.toml | 36 ++++++++----------- .../components/rust-{{project-name}}/build.rs | 8 ++--- .../rust-{{project-name}}/rust-toolchain.toml | 10 +++--- .../rust-{{project-name}}/src/lib.rs | 20 +++++------ cmake/sdkconfig.defaults | 2 +- 12 files changed, 89 insertions(+), 94 deletions(-) diff --git a/cargo/Cargo.toml b/cargo/Cargo.toml index e598d5c..97b58da 100644 --- a/cargo/Cargo.toml +++ b/cargo/Cargo.toml @@ -3,8 +3,7 @@ name = "{{project-name}}" version = "0.1.0" authors = ["{{authors}}"] edition = "2021" -resolver = "2" -rust-version = "1.66" +rust-version = "1.71" [profile.release] opt-level = "s" @@ -13,32 +12,25 @@ opt-level = "s" debug = true # Symbols are nice and they don't increase the size on Flash opt-level = "z" +{% if hal %} [features] -{% if std and hal == "Yes (default features)" %} -default = ["std", "hal", "esp-idf-sys/native"] -{% elsif std and hal == "Yes (all features)" %} -default = ["all", "hal", "esp-idf-sys/native"] -{% elsif std %} -default = ["std", "esp-idf-sys/native"] +{% if std %} +default = ["std", "embassy", "esp-idf-sys/native"] {% else %} -default = ["esp-idf-sys/native", "esp-idf-sys/panic_handler", "esp-idf-sys/alloc_handler", "esp-idf-sys/libstart"] +default = ["alloc", "embassy", "esp-idf-svc/native", "esp-idf-svc/panic_handler", "esp-idf-svc/alloc_handler", "esp-idf-svc/libstart"] {% endif %} -pio = ["esp-idf-sys/pio"] -all = ["std", "nightly", "experimental", "embassy"] -hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] -std = ["alloc", "esp-idf-sys/std", "esp-idf-sys/binstart", "embedded-svc?/std", "esp-idf-hal?/std", "esp-idf-svc?/std"] -alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] -nightly = ["embedded-svc?/nightly", "esp-idf-svc?/nightly"] # Future: "esp-idf-hal?/nightly" -experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] -embassy = ["esp-idf-hal?/embassy-sync", "esp-idf-hal?/critical-section", "esp-idf-hal?/edge-executor", "esp-idf-svc?/embassy-time-driver", "esp-idf-svc?/embassy-time-isr-queue"] +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] log = { version = "0.4.17", default-features = false } -esp-idf-sys = { version = "0.33", default-features = false } -esp-idf-hal = { version = "0.41", optional = true, default-features = false } -esp-idf-svc = { version = "0.46", optional = true, default-features = false } -embedded-svc = { version = "0.25", optional = true, default-features = false } +esp-idf-svc = { version = "0.47", default-features = false } [build-dependencies] -embuild = "0.31.2" +embuild = "0.31.3" +{% endif %} diff --git a/cargo/build.rs b/cargo/build.rs index ccb6e75..d6f8760 100644 --- a/cargo/build.rs +++ b/cargo/build.rs @@ -1,6 +1,5 @@ -// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 -fn main() -> Result<(), Box> { - embuild::build::CfgArgs::output_propagated("ESP_IDF")?; - embuild::build::LinkArgs::output_propagated("ESP_IDF")?; - Ok(()) +{%- if hal %} +fn main() { + embuild::espidf::sysenv::output(); } +{%- endif %} diff --git a/cargo/cargo-generate.toml b/cargo/cargo-generate.toml index cb4c67a..2aab8cf 100644 --- a/cargo/cargo-generate.toml +++ b/cargo/cargo-generate.toml @@ -16,7 +16,12 @@ type = "bool" prompt = "Configure advanced template options?" default = false -[conditional.'advanced'.placeholders.std] +[conditional.'advanced'.placeholders.hal] +type = "bool" +prompt = "Enable HAL support?" +default = true + +[conditional.'hal'.placeholders.std] type = "bool" prompt = "Enable STD support?" default = true @@ -43,12 +48,6 @@ type = "bool" prompt = "Configure project to support Wokwi simulation with Wokwi VS Code extension?" default = false -[conditional.'std'.placeholders.hal] -type = "string" -prompt = "Include esp-idf-hal/esp-idf-svc?" -choices = ["No", "Yes (default features)", "Yes (all features)"] -default = "Yes (default features)" - [conditional.'advanced'.placeholders.ci] type = "bool" prompt = "Add CI files for GitHub Action?" diff --git a/cargo/pre-script.rhai b/cargo/pre-script.rhai index 9aecd95..07dd5f8 100644 --- a/cargo/pre-script.rhai +++ b/cargo/pre-script.rhai @@ -51,6 +51,7 @@ for key in target_properties.keys() { let advanced = variable::get("advanced"); if !advanced { + variable::set("hal", true); variable::set("std", true); if target == "esp32" || target == "esp32s2" || target == "esp32s3" || target == "esp32c3" { variable::set("espidfver", "v4.4"); @@ -60,5 +61,4 @@ if !advanced { } variable::set("devcontainer", false); variable::set("wokwi", false); - variable::set("hal", "Yes (default features)"); } diff --git a/cargo/sdkconfig.defaults b/cargo/sdkconfig.defaults index 3ca3b5d..9ea5d73 100644 --- a/cargo/sdkconfig.defaults +++ b/cargo/sdkconfig.defaults @@ -1,5 +1,5 @@ # 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=7000 +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). diff --git a/cargo/src/main.rs b/cargo/src/main.rs index 055cfe8..e0601a8 100644 --- a/cargo/src/main.rs +++ b/cargo/src/main.rs @@ -2,24 +2,24 @@ #![no_std] #![no_main] {% endunless -%} -use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported -{%- if std and hal != "No" %} -use log::*; -{% endif %} +{%- if hal %} {%- unless std %} #[no_mangle] {%- endunless %} 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_sys::link_patches(); -{%- if std and hal == "No" %} - println!("Hello, world!"); -{%- elsif std and hal != "No" %} + esp_idf_svc::sys::link_patches(); + // Bind the log crate to the ESP Logging facilities esp_idf_svc::log::EspLogger::initialize_default(); - info!("Hello, world!"); -{%- endif %} + log::info!("Hello, world!"); } +{%- else %} +#[no_mangle] +extern "C" fn app_main() -> i32 { + 42 +} +{%- endif %} diff --git a/cmake/cargo-generate.toml b/cmake/cargo-generate.toml index 973e615..f579cc8 100644 --- a/cmake/cargo-generate.toml +++ b/cmake/cargo-generate.toml @@ -1,6 +1,23 @@ [template] -cargo_generate_version = ">=0.10.0" +cargo_generate_version = ">=0.17.4" -[placeholders] -toolchain = { type = "string", prompt = "Rust toolchain (beware: nightly works only for esp32c3!)", choices = ["esp", "nightly"], default = "esp" } -std = { type = "bool", prompt = "STD support", default = true } +[placeholders.advanced] +type = "bool" +prompt = "Configure advanced template options?" +default = false + +[conditional.'advanced'.placeholders.toolchain] +type = "string" +prompt = "Rust toolchain (beware: nightly works only for riscv MCUs!)" +choices = ["esp", "nightly"] +default = "esp" + +[conditional.'advanced'.placeholders.hal] +type = "bool" +prompt = "Enable HAL support?" +default = true + +[conditional.'advanced'.placeholders.std] +type = "bool" +prompt = "Enable STD support?" +default = true diff --git a/cmake/components/rust-{{project-name}}/Cargo.toml b/cmake/components/rust-{{project-name}}/Cargo.toml index ef179b8..768c54f 100644 --- a/cmake/components/rust-{{project-name}}/Cargo.toml +++ b/cmake/components/rust-{{project-name}}/Cargo.toml @@ -3,8 +3,7 @@ name = "rust-{{project-name}}" version = "0.1.0" authors = ["{{authors}}"] edition = "2021" -resolver = "2" -rust-version = "1.66" +rust-version = "1.71" [lib] crate-type = ["staticlib"] @@ -16,32 +15,25 @@ opt-level = "s" debug = true # Symbols are nice and they don't increase the size on Flash opt-level = "z" +{% if hal %} [features] -{% if std and hal == "Yes (default features)" %} -default = ["std", "hal", "esp-idf-sys/native"] -{% elsif std and hal == "Yes (all features)" %} -default = ["all", "hal", "esp-idf-sys/native"] -{% elsif std %} -default = ["std", "esp-idf-sys/native"] +{% if std %} +default = ["std", "embassy", "esp-idf-sys/native"] {% else %} -default = ["esp-idf-sys/native", "esp-idf-sys/panic_handler", "esp-idf-sys/alloc_handler"] +default = ["alloc", "embassy", "esp-idf-svc/native", "esp-idf-svc/panic_handler", "esp-idf-svc/alloc_handler", "esp-idf-svc/libstart"] {% endif %} -pio = ["esp-idf-sys/pio"] -all = ["std", "nightly", "experimental", "embassy"] -hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] -std = ["alloc", "esp-idf-sys/std", "embedded-svc?/std", "esp-idf-hal?/std", "esp-idf-svc?/std"] -alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] -nightly = ["embedded-svc?/nightly", "esp-idf-svc?/nightly"] # Future: "esp-idf-hal?/nightly" -experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] -embassy = ["esp-idf-hal?/embassy-sync", "esp-idf-hal?/critical-section", "esp-idf-hal?/edge-executor", "esp-idf-svc?/embassy-time-driver", "esp-idf-svc?/embassy-time-isr-queue"] +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] log = { version = "0.4.17", default-features = false } -esp-idf-sys = { version = "0.33", default-features = false } -esp-idf-hal = { version = "0.41", optional = true, default-features = false } -esp-idf-svc = { version = "0.46", optional = true, default-features = false } -embedded-svc = { version = "0.25", optional = true, default-features = false } +esp-idf-svc = { version = "0.47", default-features = false } [build-dependencies] -embuild = "0.31.2" +embuild = "0.31.3" +{% endif %} diff --git a/cmake/components/rust-{{project-name}}/build.rs b/cmake/components/rust-{{project-name}}/build.rs index bc16914..d6f8760 100644 --- a/cmake/components/rust-{{project-name}}/build.rs +++ b/cmake/components/rust-{{project-name}}/build.rs @@ -1,5 +1,5 @@ -// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 -fn main() -> Result<(), Box> { - embuild::build::CfgArgs::output_propagated("ESP_IDF")?; - Ok(()) +{%- if hal %} +fn main() { + embuild::espidf::sysenv::output(); } +{%- endif %} diff --git a/cmake/components/rust-{{project-name}}/rust-toolchain.toml b/cmake/components/rust-{{project-name}}/rust-toolchain.toml index f35bc08..4d10085 100644 --- a/cmake/components/rust-{{project-name}}/rust-toolchain.toml +++ b/cmake/components/rust-{{project-name}}/rust-toolchain.toml @@ -1,7 +1,7 @@ [toolchain] -{% if toolchain == "esp" %} -channel = "esp" -{% endif %} -{% if toolchain == "nightly" %} +{% if arch == "riscv" -%} channel = "nightly" -{% endif %} +components = ["rust-src"] +{% else -%} +channel = "esp" +{% endif %} \ No newline at end of file diff --git a/cmake/components/rust-{{project-name}}/src/lib.rs b/cmake/components/rust-{{project-name}}/src/lib.rs index 3a7b640..5050762 100644 --- a/cmake/components/rust-{{project-name}}/src/lib.rs +++ b/cmake/components/rust-{{project-name}}/src/lib.rs @@ -1,24 +1,20 @@ {% unless std -%} #![no_std] {% endunless -%} -use esp_idf_sys as _; // If using the `libstart` feature of `esp-idf-sys`, always keep this module imported -{%- if std and hal != "No" %} -use log::*; -{% endif %} #[no_mangle] extern "C" fn rust_main() -> i32 { - // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, - // or else some patches to the runtime implemented by esp-idf-sys might not link properly. - esp_idf_sys::link_patches(); -{%- if std and hal == "No" %} - println!("Hello world from Rust!"); -{%- elsif std and hal != "No" %} +{%- if hal %} + // 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(); - info!("Hello world from Rust!"); + log::info!("Hello, world!"); +{%- elif std %} + println!("Hello, world from Rust!"); {%- endif %} - 42 } diff --git a/cmake/sdkconfig.defaults b/cmake/sdkconfig.defaults index 6f6cfdd..f19c694 100644 --- a/cmake/sdkconfig.defaults +++ b/cmake/sdkconfig.defaults @@ -1,5 +1,5 @@ # 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=7000 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 # Workaround for https://github.com/espressif/esp-idf/issues/7631 #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n