diff --git a/.cargo/config.toml b/.cargo/config.toml index 84addce..d204407 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -2,17 +2,11 @@ runner = "espflash flash --monitor" -{% if logging -%} [env] ESP_LOGLEVEL="INFO" -{% endif -%} [build] rustflags = [ - "-C", "link-arg=-Tlinkall.x", -{% if wifi %} - "-C", "link-arg=-Trom_functions.x", -{% endif -%} {%- if arch == "xtensa" %} "-C", "link-arg=-nostartfiles", {% else %} diff --git a/.github/verify.sh b/.github/verify.sh index 528a860..0d15e4c 100644 --- a/.github/verify.sh +++ b/.github/verify.sh @@ -20,19 +20,15 @@ perform_checks() { cd .. } -complex_wifi_arg="" -# H2 has no wifi -if [ "$1" != "esp32h2" ]; then - complex_wifi_arg="-d wifi=true" -fi - # Generate templates cargo generate \ + -a \ --path $template_path --name=test-complex --silent --vcs=none \ -d advanced=true -d ci=false -d devcontainer=false -d wokwi=false \ - -d alloc=true -d logging=true $complex_wifi_arg -d mcu=$1 + -d alloc=true -d wifi=true -d mcu=$1 cargo generate \ + -a \ --path $template_path --name=test-simple --silent --vcs=none \ -d advanced=false -d mcu=$1 diff --git a/.github/workflows/ci_docker.yml b/.github/workflows/ci_docker.yml index d6a93e6..c8b4f18 100644 --- a/.github/workflows/ci_docker.yml +++ b/.github/workflows/ci_docker.yml @@ -47,10 +47,10 @@ jobs: run: cargo install cargo-generate - name: Generate if: matrix.board == 'esp32h2' - run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test-${{ matrix.board }} --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=true -d wokwi=false -d alloc=false -d ci=false -d logging=false + run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test-${{ matrix.board }} --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=true -d wokwi=false -d alloc=false -d ci=false - name: Generate if: matrix.board != 'esp32h2' - run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test-${{ matrix.board }} --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d wifi=false -d devcontainer=true -d wokwi=false -d alloc=false -d ci=false -d logging=false + run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test-${{ matrix.board }} --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d wifi=false -d devcontainer=true -d wokwi=false -d alloc=false -d ci=false - name: Update ownership run: | sudo chown 1000:1000 -R test-${{ matrix.board }} diff --git a/Cargo.toml b/Cargo.toml index 05c0044..6382a68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,24 +12,20 @@ esp-backtrace = { version = "0.11.0", features = [ "panic-handler", "println", ] } -esp-hal = { version = "0.16.0", features = [ "{{ mcu }}" ] } -{% if logging -%} +esp-hal = { version = "0.17.0", features = [ "{{ mcu }}" ] } esp-println = { version = "0.9.0", features = ["{{ mcu }}", "log"] } log = { version = "0.4.20" } -{% else -%} -esp-println = { version = "0.9.0", features = ["{{ mcu }}"] } -{% endif -%} {% if alloc -%} esp-alloc = { version = "0.3.0" } {% endif -%} {% if wifi -%} embedded-svc = { version = "0.26.1", default-features = false, features = [] } embedded-io = "0.6.1" -esp-wifi = { version = "0.4.0", features = [ +esp-wifi = { version = "0.5.0", features = [ "{{ mcu }}", "phy-enable-usb", "utils", - "wifi-default", + "{{ esp_wifi_feature }}", ] } heapless = { version = "0.8.0", default-features = false } smoltcp = { version = "0.11.0", default-features = false, features = [ diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..16e1de5 --- /dev/null +++ b/build.rs @@ -0,0 +1,6 @@ +fn main() { + println!("cargo:rustc-link-arg-bins=-Tlinkall.x"); + {% if wifi %} + println!("cargo:rustc-link-arg-bins=-Trom_functions.x"); + {% endif -%} +} \ No newline at end of file diff --git a/cargo-generate.toml b/cargo-generate.toml index 7b7161c..a4e3332 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -22,7 +22,7 @@ type = "bool" prompt = "Enable allocations via the esp-alloc crate?" default = false -[conditional.'advanced && mcu != "esp32h2"'.placeholders.wifi] +[conditional.'advanced'.placeholders.wifi] type = "bool" prompt = "Enable WiFi/Bluetooth/ESP-NOW via the esp-wifi crate?" default = false @@ -42,11 +42,6 @@ type = "bool" prompt = "Add CI files for GitHub Action?" default = false -[conditional.'advanced'.placeholders.logging] -type = "bool" -prompt = "Setup logging using the log crate?" -default = false - [conditional.'!devcontainer'] ignore = [ ".devcontainer/", diff --git a/post-script.rhai b/post-script.rhai index b15d269..d9fbe96 100644 --- a/post-script.rhai +++ b/post-script.rhai @@ -1,7 +1,14 @@ -if variable::get("mcu") != "esp32h2" && variable::get("wifi"){ - print("\nFor more information and examples of esp-wifi showcasing Wifi,BLE and ESP-NOW, see https://github.com/esp-rs/esp-wifi/blob/main/examples.md\n"); +if variable::get("wifi"){ + print("\nFor more information and examples of esp-wifi showcasing Wifi,BLE and ESP-NOW, see https://github.com/esp-rs/esp-wifi/blob/main/esp-wifi/docs/examples.md\n"); } if variable::get("ci") { file::rename(".github/rust_ci.yml", ".github/workflows/rust_ci.yml"); } + + +try { + system::command("cargo", ["fmt"]); +} catch { + print("Failed to run rustfmt, please ensure rustfmt is installed and in your PATH variable."); +} \ No newline at end of file diff --git a/pre-script.rhai b/pre-script.rhai index 1109057..d4b92fd 100644 --- a/pre-script.rhai +++ b/pre-script.rhai @@ -2,43 +2,64 @@ let metadata = #{ // Xtensa devices: esp32: #{ wokwi_board: "board-esp32-devkit-c-v4", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "timer::TimerGroup::new(peripherals.TIMG1, &clocks, None).timer0" }, esp32s2: #{ wokwi_board: "board-esp32-s2-devkitm-1", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "timer::TimerGroup::new(peripherals.TIMG1, &clocks, None).timer0" }, esp32s3: #{ wokwi_board: "board-esp32-s3-devkitc-1", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "timer::TimerGroup::new(peripherals.TIMG1, &clocks, None).timer0" }, // RISC-V devices: esp32c2: #{ extensions: "imc", wokwi_board: "", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0" }, esp32c3: #{ extensions: "imc", wokwi_board: "board-esp32-c3-devkitm-1", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0" }, esp32c6: #{ extensions: "imac", wokwi_board: "board-esp32-c6-devkitc-1", + esp_wifi_init: "Wifi", + esp_wifi_feature: "wifi", + esp_wifi_timer: "systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0" }, esp32h2: #{ extensions: "imac", wokwi_board: "board-esp32-h2-devkitm-1", + esp_wifi_init: "Ble", + esp_wifi_feature: "ble", + esp_wifi_timer: "systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0" }, }; let mcu = variable::get("mcu"); let meta = metadata.get(mcu); +variable::set("esp_wifi_feature", meta.get("esp_wifi_feature")); variable::set("wokwi_board", meta.get("wokwi_board")); if mcu in ["esp32", "esp32s2", "esp32s3"] { // Xtensa devices: variable::set("arch", "xtensa"); variable::set("gcc_target", `xtensa-${mcu}-elf`); - variable::set("has_swd", false); variable::set("rust_target", `xtensa-${mcu}-none-elf`); variable::set("toolchain", "esp"); } else { @@ -47,7 +68,6 @@ if mcu in ["esp32", "esp32s2", "esp32s3"] { variable::set("arch", "riscv"); variable::set("gcc_target", "riscv32-esp-elf"); - variable::set("has_swd", true); variable::set("rust_target", `riscv32${extensions}-unknown-none-elf`); variable::set("toolchain", "nightly"); } @@ -58,6 +78,43 @@ if !advanced { variable::set("ci", false); variable::set("devcontainer", false); variable::set("wokwi", false); - variable::set("logging", false); variable::set("wifi", false); } + +// +// Snippets - These should be short & self-contained, not depending on other snippets existing where possible. +// + +// dependencies: none +variable::set("alloc_snippet", +` +extern crate alloc; +use core::mem::MaybeUninit; + +#[global_allocator] +static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); + +fn init_heap() { + const HEAP_SIZE: usize = 32 * 1024; + static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit(); + + unsafe { + ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE); + } +} +`); + +// depends on: `peripherals` being in scope +variable::set("esp_wifi_snippet", +` +let timer = esp_hal::${meta.esp_wifi_timer}; +let _init = esp_wifi::initialize( + esp_wifi::EspWifiInitFor::${meta.esp_wifi_init}, + timer, + esp_hal::rng::Rng::new(peripherals.RNG), + system.radio_clock_control, + &clocks, +) +.unwrap(); +` +); diff --git a/src/main.rs b/src/main.rs index a1ab439..255e43b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,74 +1,33 @@ #![no_std] #![no_main] -{% if alloc -%} -extern crate alloc; -use core::mem::MaybeUninit; -{% endif -%} use esp_backtrace as _; -use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Delay}; -use esp_println::println; - -{% if wifi -%} -use esp_wifi::{initialize, EspWifiInitFor}; - -{% if arch == "riscv" -%} -use esp_hal::{systimer::SystemTimer, Rng}; -{% else -%} -use esp_hal::{timer::TimerGroup, Rng}; -{% endif -%} -{% endif -%} +use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, delay::Delay}; {% if alloc -%} -#[global_allocator] -static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); - -fn init_heap() { - const HEAP_SIZE: usize = 32 * 1024; - static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit(); - - unsafe { - ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE); - } -} +{{ alloc_snippet }} {% endif -%} + #[entry] fn main() -> ! { - {%- if alloc %} - init_heap(); - {%- endif %} let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::max(system.clock_control).freeze(); - let mut delay = Delay::new(&clocks); + let delay = Delay::new(&clocks); + + {%- if alloc %} + init_heap(); + {%- endif %} - {% if logging -%} - // setup logger - // To change the log_level change the env section in .cargo/config.toml - // or remove it and set ESP_LOGLEVEL manually before running cargo run - // this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358 esp_println::logger::init_logger_from_env(); - log::info!("Logger is setup"); - {% endif -%} - println!("Hello world!"); + {% if wifi -%} - {% if arch == "riscv" -%} - let timer = SystemTimer::new(peripherals.SYSTIMER).alarm0; - {% else -%} - let timer = TimerGroup::new(peripherals.TIMG1, &clocks).timer0; - {% endif -%} - let _init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); + {{ esp_wifi_snippet }} {% endif -%} + loop { - println!("Loop..."); - delay.delay_ms(500u32); + log::info!("Hello world!"); + delay.delay(500.millis()); } }