Skip to content

Commit

Permalink
Create the esp-metadata package, update esp-hal build script to u…
Browse files Browse the repository at this point in the history
…se it
  • Loading branch information
jessebraham committed Mar 11, 2024
1 parent e4ea62a commit f6b7b99
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 105 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exclude = [
"esp-hal-procmacros",
"esp-hal-smartled",
"esp-lp-hal",
"esp-metadata",
"esp-riscv-rt",
"examples",
]
7 changes: 4 additions & 3 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ esp-riscv-rt = { version = "0.7.0", optional = true, path = "../esp-riscv-rt" }
xtensa-lx-rt = { version = "0.16.0", optional = true }

[build-dependencies]
basic-toml = "0.1.8"
cfg-if = "1.0.0"
serde = { version = "1.0.197", features = ["derive"] }
basic-toml = "0.1.8"
cfg-if = "1.0.0"
esp-metadata = { version = "0.1.0", path = "../esp-metadata" }
serde = { version = "1.0.197", features = ["derive"] }

[features]
default = ["rt", "vectored"]
Expand Down
108 changes: 12 additions & 96 deletions esp-hal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::{
fs::{self, File},
io::{BufRead, Write},
path::{Path, PathBuf},
str::FromStr,
};

use serde::Deserialize;
use esp_metadata::{Arch, Chip, Config};

// Macros taken from:
// https://github.com/TheDan64/inkwell/blob/36c3b10/src/lib.rs#L81-L110
Expand Down Expand Up @@ -45,61 +46,6 @@ macro_rules! assert_unique_used_features {
}
}

#[derive(Debug, Deserialize, PartialEq)]
enum Arch {
#[serde(rename = "riscv")]
RiscV,
#[serde(rename = "xtensa")]
Xtensa,
}

impl std::fmt::Display for Arch {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Arch::RiscV => "riscv",
Arch::Xtensa => "xtensa",
}
)
}
}

#[derive(Debug, Deserialize)]
enum CoreCount {
#[serde(rename = "single_core")]
Single,
#[serde(rename = "multi_core")]
Multi,
}

impl std::fmt::Display for CoreCount {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
CoreCount::Single => "single_core",
CoreCount::Multi => "multi_core",
}
)
}
}

#[derive(Debug, Deserialize)]
struct Device {
pub arch: Arch,
pub cores: CoreCount,
pub peripherals: Vec<String>,
pub symbols: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct Config {
pub device: Device,
}

fn main() -> Result<(), Box<dyn Error>> {
// NOTE: update when adding new device support!
// Ensure that exactly one chip has been specified:
Expand Down Expand Up @@ -157,59 +103,29 @@ fn main() -> Result<(), Box<dyn Error>> {
}

// Load the configuration file for the configured device:
let chip_toml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("devices")
.join(device_name)
.join("device.toml")
.canonicalize()?;

let config = fs::read_to_string(chip_toml_path)?;
let config: Config = basic_toml::from_str(&config)?;
let device = &config.device;
let chip = Chip::from_str(device_name)?;
let config = Config::for_chip(&chip);

// Check PSRAM features are only given if the target supports PSRAM:
if !&device.symbols.contains(&String::from("psram"))
if !config.contains(&String::from("psram"))
&& (cfg!(feature = "psram-2m") || cfg!(feature = "psram-4m") || cfg!(feature = "psram-8m"))
{
panic!("The target does not support PSRAM");
}

// Don't support "interrupt-preemption" and "direct-vectoring" on Xtensa and
// RISC-V with CLIC:
if (device.symbols.contains(&String::from("clic")) || device.arch == Arch::Xtensa)
if (config.contains(&String::from("clic")) || config.arch() == Arch::Xtensa)
&& (cfg!(feature = "direct-vectoring") || cfg!(feature = "interrupt-preemption"))
{
panic!("The target does not support interrupt-preemption and direct-vectoring");
}

// Define all necessary configuration symbols for the configured device:
println!("cargo:rustc-cfg={}", device_name);
println!("cargo:rustc-cfg={}", device.arch);
println!("cargo:rustc-cfg={}", device.cores);

for peripheral in &device.peripherals {
println!("cargo:rustc-cfg={peripheral}");
}

for symbol in &device.symbols {
println!("cargo:rustc-cfg={symbol}");
}

let mut config_symbols = Vec::new();
let arch = device.arch.to_string();
let cores = device.cores.to_string();
config_symbols.push(device_name);
config_symbols.push(&arch);
config_symbols.push(&cores);

for peripheral in &device.peripherals {
config_symbols.push(peripheral);
}

for symbol in &device.symbols {
config_symbols.push(symbol);
}
config.define_symbols();

#[allow(unused_mut)]
let mut config_symbols = config.all();
#[cfg(feature = "flip-link")]
config_symbols.push("flip-link");

Expand Down Expand Up @@ -260,7 +176,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn copy_dir_all(
config_symbols: &Vec<&str>,
config_symbols: &Vec<String>,
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
) -> std::io::Result<()> {
Expand All @@ -287,7 +203,7 @@ fn copy_dir_all(

/// A naive pre-processor for linker scripts
fn preprocess_file(
config: &[&str],
config: &[String],
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
) -> std::io::Result<()> {
Expand All @@ -302,7 +218,7 @@ fn preprocess_file(
let trimmed = line.trim();

if let Some(stripped) = trimmed.strip_prefix("#IF ") {
let condition = stripped;
let condition = stripped.to_string();
let should_take = take.iter().all(|v| *v);
let should_take = should_take && config.contains(&condition);
take.push(should_take);
Expand Down
14 changes: 14 additions & 0 deletions esp-metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "esp-metadata"
version = "0.1.0"
edition = "2021"
rust-version = "1.60.0"
description = "Metadata for Espressif devices"
repository = "https://github.com/esp-rs/esp-hal"
license = "MIT OR Apache-2.0"

[dependencies]
basic-toml = "0.1.8"
lazy_static = "1.4.0"
serde = { version = "1.0.197", features = ["derive"] }
strum = { version = "0.26.1", features = ["derive"] }
30 changes: 30 additions & 0 deletions esp-metadata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# esp-metadata

[![Crates.io](https://img.shields.io/crates/v/esp-metadata?color=C96329&logo=Rust&style=flat-square)](https://crates.io/crates/esp-metadata)
[![docs.rs](https://img.shields.io/docsrs/esp-metadata?color=C96329&logo=rust&style=flat-square)](https://docs.rs/esp-metadata)
![MSRV](https://img.shields.io/badge/MSRV-1.60-blue?style=flat-square)
![Crates.io](https://img.shields.io/crates/l/esp-metadata?style=flat-square)

Metadata for Espressif devices, primarily intended for use in build scripts.

## [Documentation](https://docs.rs/crate/esp-metadata)

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It _might_
compile with older versions but that may change in any new patch release.

## License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](../LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without
any additional terms or conditions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[device]
name = "esp32"
arch = "xtensa"
cores = "multi_core"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "riscv"
name = "esp32c2"
arch = "riscv"
cores = "single_core"

peripherals = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "riscv"
name = "esp32c3"
arch = "riscv"
cores = "single_core"

peripherals = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "riscv"
name = "esp32c6"
arch = "riscv"
cores = "single_core"

peripherals = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "riscv"
name = "esp32h2"
arch = "riscv"
cores = "single_core"

peripherals = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "riscv"
name = "esp32p4"
arch = "riscv"
cores = "multi_core"

peripherals = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[device]
name = "esp32s2"
arch = "xtensa"
cores = "single_core"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[device]
arch = "xtensa"
name = "esp32s3"
arch = "xtensa"
cores = "multi_core"

peripherals = [
Expand Down
Loading

0 comments on commit f6b7b99

Please sign in to comment.