Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Complete esp32h2 support, simplify linker args, create snippets (#141)
Browse files Browse the repository at this point in the history
* Simplify linker arg specification

* Default to logging

* Split alloc and wifi into rhai snippets

* fix ci

* Add back rustfmt, with catch to print error if it fails

* specify snippet deps

* Rebase updates

* enable proper esp-wifi feature

* Update esp-hal & esp-wifi
  • Loading branch information
MabezDev authored Apr 19, 2024
1 parent eb2e11c commit c5064f0
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 87 deletions.
6 changes: 0 additions & 6 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
10 changes: 3 additions & 7 deletions .github/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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 -%}
}
7 changes: 1 addition & 6 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/",
Expand Down
11 changes: 9 additions & 2 deletions post-script.rhai
Original file line number Diff line number Diff line change
@@ -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.");
}
63 changes: 60 additions & 3 deletions pre-script.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
Expand All @@ -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();
`
);
67 changes: 13 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit c5064f0

Please sign in to comment.