Skip to content

Commit

Permalink
wip, hello_world using esp-rs as a package
Browse files Browse the repository at this point in the history
  • Loading branch information
brian horakh committed Oct 1, 2023
1 parent 66e8749 commit 40bf1e9
Show file tree
Hide file tree
Showing 16 changed files with 1,133 additions and 34 deletions.
131 changes: 131 additions & 0 deletions examples/esp32c3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
[package]
name = "esp32c3-hal"
version = "0.12.0"
authors = [
"Jesse Braham <[email protected]>",
"Björn Quentin <[email protected]>",
]
edition = "2021"
rust-version = "1.67.0"
description = "HAL for ESP32-C3 microcontrollers"
repository = "https://github.com/esp-rs/esp-hal"
license = "MIT OR Apache-2.0"

keywords = [
"embedded",
"embedded-hal",
"esp",
"esp32c3",
"no-std",
]
categories = [
"embedded",
"hardware-support",
"no-std",
]

[dependencies]
cfg-if = "1.0.0"
esp-hal-common = { version = "0.12.0", features = ["esp-riscv-rt","esp32c3"], git="https://github.com/esp-rs/esp-hal" }
embassy-time = { version = "0.1.3", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-1_000_000"], optional = true }

[dev-dependencies]
aes = "0.8.3"
critical-section = "1.1.2"
crypto-bigint = { version = "0.5.3", default-features = false }
embassy-sync = { version = "0.3.0", path = "../../embassy-sync", features = ["defmt"] }
embassy-executor = { version = "0.3.0", path = "../../embassy-executor", features = ["nightly","integrated-timers","arch-riscv32","executor-thread"] }
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }

embedded-can = "0.4.1"
embedded-graphics = "0.8.1"
embedded-hal = { version = "0.2.7", features = ["unproven"] }
embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" }
embedded-hal-async = "=1.0.0-rc.1"
embedded-io-async = "0.5.0"
esp-backtrace = { version = "0.8.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
esp-hal-smartled = { version = "0.5.0", features = ["esp32c3"], git="https://github.com/esp-rs/esp-hal" }
esp-println = { version = "0.6.0", features = ["esp32c3", "log"] }
heapless = "0.7.16"
hmac = { version = "0.12.1", default-features = false }
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.7", default-features = false }
#smart-leds = "0.3.0"
#ssd1306 = "0.8.1"
static_cell = { version = "1.2.0", features = ["nightly"] }

[features]
default = ["rt", "vectored", "esp-hal-common/rv-zero-rtc-bss"]
async = ["esp-hal-common/async"]
debug = ["esp-hal-common/debug"]
defmt = ["esp-hal-common/defmt"]
direct-boot = ["esp-hal-common/rv-init-data", "esp-hal-common/rv-init-rtc-data"]
direct-vectoring = ["esp-hal-common/direct-vectoring"]
eh1 = ["esp-hal-common/eh1"]
interrupt-preemption = ["esp-hal-common/interrupt-preemption"]
log = ["esp-hal-common/log"]
mcu-boot = []
rt = []
ufmt = ["esp-hal-common/ufmt"]
vectored = ["esp-hal-common/vectored"]

# Embassy support
embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[profile.release]
debug = true

# [[example]]
# name = "spi_eh1_loopback"
# required-features = ["eh1"]

# [[example]]
# name = "spi_eh1_device_loopback"
# required-features = ["eh1"]

[[example]]
name = "embassy_hello_world"
required-features = ["embassy"]

# [[example]]
# name = "embassy_wait"
# required-features = ["embassy", "async"]

# [[example]]
# name = "interrupt_preemption"
# required-features = ["interrupt-preemption"]

# [[example]]
# name = "embassy_spi"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_serial"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_i2c"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_rmt_tx"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_rmt_rx"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_i2s_sound"
# required-features = ["embassy", "async"]

# [[example]]
# name = "embassy_i2s_read"
# required-features = ["embassy", "async"]

# [[example]]
# name = "direct-vectoring"
# required-features = ["direct-vectoring"]
41 changes: 7 additions & 34 deletions examples/esp32c3/README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
# esp32-c3 example
# 👋🏻

this is an WIP signpost, to inform & guide to better examples.
This code was borrowed from [esp32c3-hal](https://github.com/esp-rs/esp-hal/tree/main/esp32c3-hal)

👋🏻 I'm concurrently proposing basic signpost for README-embassy.md this is in conjunction with my proposed uplift:
* https://github.com/esp-rs/esp-hal/pull/834
* https://github.com/drogue-iot/drogue-device/pull/388

## The Basics:
* embassy-rs uses esp-rs/esp-hal
* esp-hal: Hardware Abstraction Layer crates for the ESP32, ESP32-C2/C3/C6, ESP32-H2, and ESP32-S2/S3 from Espressif.
* The Minimum Supported Rust Version is 1.67.0 for all packages.
* 🔗 https://docs.rs/esp32c3-hal/latest/esp32c3_hal/

## Quickstart:
https://github.com/esp-rs/esp-hal/blob/main/esp32c3-hal/examples/embassy_hello_world.rs
The Cargo.toml file has been modified to use the embassy repo in path

```
gh repo clone esp-rs/esp-hal
rustup target add riscv32imc-unknown-none-elf
# when targeting RISC-V arch. necessary to set:
RUSTC_BOOTSTRAP=1
cd esp-hal/esp32c3-hal
```

## 🥰 more github.com/esp-rs/esp-hal/esp32c3-hal/examples
* embassy_hello_world.rs
* embassy_i2s_read.rs
* embassy_rmt_rx.rs
* embassy_serial.rs
* embassy_wait.rs
* embassy_i2c.rs
* embassy_i2s_sound.rs
* embassy_rmt_tx.rs
* embassy_spi.rs

## coming soon:
* embassy_blink (esp32 c3 & s2)
* drogue-iot example with esp32

For now

```
cargo run --example embassy_hello_world --features "default","embassy","embassy-time-timg0"
```
124 changes: 124 additions & 0 deletions examples/esp32c3/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::{env, fs::File, io::Write, path::PathBuf};

// Thanks to kennytm and TheDan64 for the assert_used_features macro.
// Source:
// https://github.com/TheDan64/inkwell/blob/36c3b106e61b1b45295a35f94023d93d9328c76f/src/lib.rs#L81-L110
macro_rules! assert_unique_features {
() => {};
($first:tt $(,$rest:tt)*) => {
$(
#[cfg(all(feature = $first, feature = $rest))]
compile_error!(concat!("Features \"", $first, "\" and \"", $rest, "\" cannot be used together"));
)*
assert_unique_features!($($rest),*);
}
}

assert_unique_features! {"mcu-boot", "direct-boot"}

#[cfg(feature = "direct-boot")]
fn main() {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("ld/db-esp32c3-memory.x"))
.unwrap();

File::create(out.join("esp32c3-link.x"))
.unwrap()
.write_all(include_bytes!("ld/db-esp32c3-link.x"))
.unwrap();

File::create(out.join("riscv-link.x"))
.unwrap()
.write_all(include_bytes!("ld/db-riscv-link.x"))
.unwrap();

File::create(out.join("linkall.x"))
.unwrap()
.write_all(include_bytes!("ld/db-linkall.x"))
.unwrap();

println!("cargo:rustc-link-search={}", out.display());

// Only re-run the build script when memory.x is changed,
// instead of when any part of the source code changes.
println!("cargo:rerun-if-changed=ld/memory.x");

add_defaults();
}

#[cfg(not(any(feature = "mcu-boot", feature = "direct-boot")))]
fn main() {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("ld/bl-esp32c3-memory.x"))
.unwrap();

File::create(out.join("bl-riscv-link.x"))
.unwrap()
.write_all(include_bytes!("ld/bl-riscv-link.x"))
.unwrap();

File::create(out.join("linkall.x"))
.unwrap()
.write_all(include_bytes!("ld/bl-linkall.x"))
.unwrap();

println!("cargo:rustc-link-search={}", out.display());

// Only re-run the build script when memory.x is changed,
// instead of when any part of the source code changes.
println!("cargo:rerun-if-changed=ld/memory.x");

add_defaults();
}

#[cfg(feature = "mcu-boot")]
fn main() {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("ld/mb-esp32c3-memory.x"))
.unwrap();

File::create(out.join("esp32c3-link.x"))
.unwrap()
.write_all(include_bytes!("ld/mb-esp32c3-link.x"))
.unwrap();

File::create(out.join("riscv-link.x"))
.unwrap()
.write_all(include_bytes!("ld/mb-riscv-link.x"))
.unwrap();

File::create(out.join("linkall.x"))
.unwrap()
.write_all(include_bytes!("ld/mb-linkall.x"))
.unwrap();

println!("cargo:rustc-link-search={}", out.display());

// Only re-run the build script when memory.x is changed,
// instead of when any part of the source code changes.
println!("cargo:rerun-if-changed=ld/memory.x");

add_defaults();
}

fn add_defaults() {
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

File::create(out.join("rom-functions.x"))
.unwrap()
.write_all(include_bytes!("ld/rom-functions.x"))
.unwrap();

println!("cargo:rustc-link-search={}", out.display());
}
56 changes: 56 additions & 0 deletions examples/esp32c3/examples/embassy_hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! embassy hello world
//!
//! This is an example of running the embassy executor with multiple tasks
//! concurrently.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use esp32c3_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*};
use esp_backtrace as _;
use static_cell::make_static;

#[embassy_executor::task]
async fn run1() {
loop {
esp_println::println!("Hello world from embassy using esp-hal-async!");
Timer::after(Duration::from_millis(1_000)).await;
}
}

#[embassy_executor::task]
async fn run2() {
loop {
esp_println::println!("Bing!");
Timer::after(Duration::from_millis(5_000)).await;
}
}

#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

#[cfg(feature = "embassy-time-systick")]
embassy::init(
&clocks,
esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);

#[cfg(feature = "embassy-time-timg0")]
embassy::init(
&clocks,
esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0,
);

let executor = make_static!(Executor::new());
executor.run(|spawner| {
spawner.spawn(run1()).ok();
spawner.spawn(run2()).ok();
});
}
33 changes: 33 additions & 0 deletions examples/esp32c3/ld/bl-esp32c3-memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
MEMORY
{
/*
https://github.com/espressif/esptool/blob/ed64d20b051d05f3f522bacc6a786098b562d4b8/esptool/targets/esp32c3.py#L78-L90
MEMORY_MAP = [[0x00000000, 0x00010000, "PADDING"],
[0x3C000000, 0x3C800000, "DROM"],
[0x3FC80000, 0x3FCE0000, "DRAM"],
[0x3FC88000, 0x3FD00000, "BYTE_ACCESSIBLE"],
[0x3FF00000, 0x3FF20000, "DROM_MASK"],
[0x40000000, 0x40060000, "IROM_MASK"],
[0x42000000, 0x42800000, "IROM"],
[0x4037C000, 0x403E0000, "IRAM"],
[0x50000000, 0x50002000, "RTC_IRAM"],
[0x50000000, 0x50002000, "RTC_DRAM"],
[0x600FE000, 0x60100000, "MEM_INTERNAL2"]]
*/
/* 400K of on soc RAM, 16K reserved for cache */
ICACHE : ORIGIN = 0x4037C000, LENGTH = 0x4000
/* Instruction RAM */
IRAM : ORIGIN = 0x4037C000 + 0x4000, LENGTH = 400K - 0x4000
/* Data RAM */
DRAM : ORIGIN = 0x3FC80000, LENGTH = 0x50000


/* External flash */
/* Instruction ROM */
IROM : ORIGIN = 0x42000000 + 0x20, LENGTH = 0x400000 - 0x20
/* Data ROM */
DROM : ORIGIN = 0x3C000000, LENGTH = 0x400000

/* RTC fast memory (executable). Persists over deep sleep. */
RTC_FAST : ORIGIN = 0x50000000, LENGTH = 0x2000 /*- ESP_BOOTLOADER_RESERVE_RTC*/
}
Loading

0 comments on commit 40bf1e9

Please sign in to comment.