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

Commit

Permalink
Add esp-wifi support (#113)
Browse files Browse the repository at this point in the history
* feat: Add support for esp-wifi

* feat: Implement feedback

* feat: Implement feedback

* feat: Add conditionals for timers and radio split

* ci: Remove wifi for H2

* ci: Allow empty loops

* fix: Fix imports for ESP32

* style: Format imports

* fix: Set wifi default

* feat: Add post rhai script

* feat: Simplify esp-wifi code

* fix: Fix rhai script for h2

* Simplify post-script.rhai

Co-authored-by: Scott Mabin <scott@mabez.dev>

* revert: Revert removing h2 check

---------

Co-authored-by: Scott Mabin <scott@mabez.dev>
  • Loading branch information
SergioGasquez and MabezDev authored Sep 20, 2023
1 parent 81ebada commit 5061dbc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
6 changes: 4 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ runner = "espflash flash --monitor"

{% if logging -%}
[env]
ESP_LOGLEVEL="DEBUG"
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
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SSID: SSID
PASSWORD: PASSWORD

# Cancel any currently running workflows from the same PR, branch, or
# tag when a new workflow is triggered.
Expand All @@ -24,21 +26,22 @@ concurrency:

jobs:
rust-checks:
name: cargo ${{ matrix.action.command }} - ${{ matrix.board }} (alloc=${{ matrix.alloc }} logging=${{ matrix.logging }})
name: cargo ${{ matrix.action.command }} - ${{ matrix.board }} (alloc=${{ matrix.alloc }} wifi=${{ matrix.wifi }} logging=${{ matrix.logging }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
board: ["esp32", "esp32c2", "esp32c3", "esp32c6", "esp32h2", "esp32s2", "esp32s3"]
alloc: ["true", "false"]
logging: ["true", "false"]
wifi: ["true", "false"]
action:
- command: build
args: --release
- command: fmt
args: -- --check
- command: clippy
args: --no-deps -- -D warnings -A dead_code
args: --no-deps -- -D warnings -A dead_code -A clippy::empty_loop
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -69,6 +72,10 @@ jobs:
if: steps.binaries.outcome != 'success'
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 --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=false -d wokwi=false -d alloc=${{ matrix.alloc }} -d ci=false -d logging=${{ matrix.logging }}
- name: Generate
if: matrix.board != 'esp32h2'
run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=false -d wokwi=false -d alloc=${{ matrix.alloc }} -d wifi=${{ matrix.wifi }} -d ci=false -d logging=${{ matrix.logging }}
- name: cargo ${{ matrix.action.command }}
run: cd test; cargo ${{ matrix.action.command }} ${{ matrix.action.args }}
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ esp-println = { version = "0.6.0", features = ["{{ mcu }}"] }
{% if alloc -%}
esp-alloc = { version = "0.3.0" }
{% endif -%}
{% if wifi -%}
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi/", rev = "fbb8417", features = ["{{ mcu }}", "wifi"] }
smoltcp = { version = "0.10.0", default-features=false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }
embedded-svc = { version = "0.25.0", default-features = false, features = [] }
embedded-io = "0.4.0"
heapless = { version = "0.7.14", default-features = false }
{% endif -%}
6 changes: 6 additions & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ignore = [".git", ".github/dependabot.yml", ".github/workflows/ci_docker.yml", "

[hooks]
pre = ["pre-script.rhai"]
post = ["post-script.rhai"]

[placeholders.mcu]
type = "string"
Expand All @@ -21,6 +22,11 @@ type = "bool"
prompt = "Enable allocations via the esp-alloc crate?"
default = false

[conditional.'advanced && mcu != "esp32h2"'.placeholders.wifi]
type = "bool"
prompt = "Enable WiFi/Bluetooth/ESP-NOW via the esp-wifi crate?"
default = false

[conditional.'advanced'.placeholders.devcontainer]
type = "bool"
prompt = "Configure project to use Dev Containers (VS Code and GitHub Codespaces)?"
Expand Down
3 changes: 3 additions & 0 deletions post-script.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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");
}
1 change: 1 addition & 0 deletions pre-script.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ if !advanced {
variable::set("devcontainer", false);
variable::set("wokwi", false);
variable::set("logging", false);
variable::set("wifi", false);
}
48 changes: 39 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ use core::mem::MaybeUninit;
use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Delay};
{% if logging -%}
use log::info;

{% if wifi -%}
use esp_wifi::{initialize, EspWifiInitFor};

{% if arch == "riscv" -%}
use hal::{systimer::SystemTimer, Rng};
{% else -%}
use hal::{timer::TimerGroup, Rng};
{% endif -%}
{% endif -%}

{%- if alloc %}
{% if alloc -%}
#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

Expand All @@ -24,15 +31,20 @@ fn init_heap() {
ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
}
}
{% endif %}
{% endif -%}
#[entry]
fn main() -> ! {
{%- if alloc %}
init_heap();
{%- endif %}
{%- endif %}
let peripherals = Peripherals::take();
{% if arch == "xtensa" and wifi-%}
let mut system = peripherals.{{ sys_peripheral }}.split();
{% else -%}
let system = peripherals.{{ sys_peripheral }}.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
{% endif -%}

let clocks = ClockControl::max(system.clock_control).freeze();
let mut delay = Delay::new(&clocks);

{% if logging -%}
Expand All @@ -41,11 +53,29 @@ fn main() -> ! {
// 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();
info!("Logger is setup");
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,
&mut system.peripheral_clock_control,
)
.timer0;
{% endif -%}
let _init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
&clocks,
)
.unwrap();
{% endif -%}
loop {
println!("Loop...");
delay.delay_ms(500u32);
Expand Down

0 comments on commit 5061dbc

Please sign in to comment.