diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index bc41a83..97fa8c5 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,6 +29,6 @@ jobs: - name: Load sources run: ./d download-all - name: Run build - run: cargo run --release --bin stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf + run: cargo run --release --bin stm32-bindings-gen - name: Run package build run: cd build/stm32-bindings && cargo build --target=thumbv8m.main-none-eabihf diff --git a/.vscode/settings.json b/.vscode/settings.json index 77ee51e..1177afa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,7 +15,7 @@ // Uncomment the target of your chip. // "rust-analyzer.cargo.target": "thumbv6m-none-eabi", //"rust-analyzer.cargo.target": "thumbv7m-none-eabi", - "rust-analyzer.cargo.target": "thumbv7em-none-eabi", + // "rust-analyzer.cargo.target": "thumbv7em-none-eabi", //"rust-analyzer.cargo.target": "thumbv7em-none-eabihf", // "rust-analyzer.cargo.target": "thumbv8m.main-none-eabihf", "rust-analyzer.cargo.features": [], diff --git a/d b/d index 399f690..cef92fe 100644 --- a/d +++ b/d @@ -4,18 +4,17 @@ set -e cd $(dirname $0) CMD=$1 -REV=8382ecbcc976ab5e91070b386700dbfd1e654275 +REV=v1.8.0 shift case "$CMD" in gen) - cargo run --release stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf + cargo run --release stm32-bindings-gen ;; download-all) rm -rf ./sources/ - git clone https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/ -q + git clone --branch $REV https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/STM32CubeWBA/ --depth 1 -q cd ./sources/ - git checkout $REV ;; *) echo "unknown command" diff --git a/d.ps1 b/d.ps1 index 03a300f..9046c1e 100644 --- a/d.ps1 +++ b/d.ps1 @@ -6,17 +6,15 @@ param ( [string]$peri ) -$REV = "8382ecbcc976ab5e91070b386700dbfd1e654275" +$REV = "v1.8.0" Switch ($CMD) { "gen" { - cargo run --release stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf + cargo run --release stm32-bindings-gen } "download-all" { rm -r -Force ./sources/ -ErrorAction SilentlyContinue - git clone https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/ - cd ./sources/ - git checkout $REV + git clone --branch $REV https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/STM32CubeWBA/ --depth 1 -q cd .. } default { diff --git a/stm32-bindings-gen/inc/wpan-wba.h b/stm32-bindings-gen/inc/wba_wpan_mac.h similarity index 100% rename from stm32-bindings-gen/inc/wpan-wba.h rename to stm32-bindings-gen/inc/wba_wpan_mac.h diff --git a/stm32-bindings-gen/res/Cargo.toml b/stm32-bindings-gen/res/Cargo.toml index b2935df..e1f9b8f 100644 --- a/stm32-bindings-gen/res/Cargo.toml +++ b/stm32-bindings-gen/res/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stm32-bindings" -version = "18.0.0" +version = "0.1.0" edition = "2024" license = "MIT OR Apache-2.0" repository = "https://github.com/embassy-rs/stm32-data" @@ -15,8 +15,8 @@ include = [ ] [package.metadata.docs.rs] -features = ["stm32h755zi-cm7", "pac", "metadata"] -default-target = "thumbv7em-none-eabihf" +features = [] +default-target = "thumbv8m.main-none-eabihf" targets = [] rustdoc-args = ["--cfg", "docsrs"] @@ -53,6 +53,10 @@ defmt = { version = "0.3.0", optional = true } [features] default = ["pac"] +wba_wpan_mac = [] +wba_wpan_ble = [] +n6_ai_runtime = [] + # Build the actual PAC. Set by default. # If you just want the metadata, unset it with `default-features = false`. pac = [] diff --git a/stm32-bindings-gen/res/src/bindings/mod.rs b/stm32-bindings-gen/res/src/bindings/mod.rs index 6443cd1..b5570eb 100644 --- a/stm32-bindings-gen/res/src/bindings/mod.rs +++ b/stm32-bindings-gen/res/src/bindings/mod.rs @@ -1 +1,2 @@ -mod wpan_wba; +#[cfg(feature = "wba_wpan_mac")] +mod wba_wpan_mac; diff --git a/stm32-bindings-gen/src/lib.rs b/stm32-bindings-gen/src/lib.rs index 5f7fe07..f160d63 100644 --- a/stm32-bindings-gen/src/lib.rs +++ b/stm32-bindings-gen/src/lib.rs @@ -19,16 +19,25 @@ impl ParseCallbacks for UppercaseCallbacks { pub struct Options { pub out_dir: PathBuf, pub sources_dir: PathBuf, +} + +pub struct Library { + pub sources_dir: PathBuf, pub target_triple: String, + pub header: &'static [u8], + pub module: &'static str, + pub includes: Vec, + pub library: PathBuf, } pub struct Gen { opts: Options, + libs: Vec, } impl Gen { - pub fn new(opts: Options) -> Self { - Self { opts } + pub fn new(opts: Options, libs: Vec) -> Self { + Self { opts, libs } } pub fn run_gen(&mut self) { @@ -36,86 +45,93 @@ impl Gen { fs::create_dir_all(self.opts.out_dir.join("src/bindings")).unwrap(); fs::create_dir_all(self.opts.out_dir.join("src/lib")).unwrap(); - // Create a named temporary file - let mut header = NamedTempFile::new().unwrap(); - - // Write some data to the first handle - header - .write_all(include_bytes!("../inc/wpan-wba.h")) - .unwrap(); - - header.reopen().unwrap(); - - // The bindgen::Builder is the main entry point - // to bindgen, and lets you build up options for - // the resulting bindings. - let target_flag = format!("--target={}", self.opts.target_triple); - let include_arg = format!( - "-I{}/Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc", - self.opts.sources_dir.to_str().unwrap() - ); - let mut builder = bindgen::Builder::default() - .parse_callbacks(Box::new(UppercaseCallbacks)) - // Force Clang to use the same layout as the selected target. - .clang_arg(&target_flag) - .clang_arg(&include_arg); - if self - .opts - .target_triple - .to_ascii_lowercase() - .starts_with("thumb") - { - builder = builder.clang_arg("-mthumb"); - } - let bindings = builder - // The input header we would like to generate - // bindings for. - .header("stm32-bindings-gen/inc/wpan-wba.h") - // Finish the builder and generate the bindings. - .generate() - // Unwrap the Result and panic on failure. - .expect("Unable to generate bindings"); - - let out_path = self.opts.out_dir.join("src/bindings/wpan_wba.rs"); - - bindings - .write_to_file(&out_path) - .expect("Couldn't write bindings!"); - - let mut file_contents = fs::read_to_string(&out_path).unwrap(); - file_contents = file_contents - .replace("::std::mem::", "::core::mem::") - .replace("::std::os::raw::", "::core::ffi::") - .replace("::std::option::", "::core::option::"); - - file_contents = file_contents - .lines() - .map(|line| { - if let Some(rest) = line.strip_prefix("pub const ") { - if let Some((name, tail)) = rest.split_once(':') { - let upper = name.trim().to_ascii_uppercase(); - return format!("pub const {}:{}", upper, tail); + for lib in &self.libs { + let sources_dir = self.opts.sources_dir.join(&lib.sources_dir); + + // Create a named temporary file + let mut header = NamedTempFile::with_suffix(".h").unwrap(); + + // Write some data to the first handle + header.write_all(lib.header).unwrap(); + + // The bindgen::Builder is the main entry point + // to bindgen, and lets you build up options for + // the resulting bindings. + let target_flag = format!("--target={}", lib.target_triple); + + let mut builder = bindgen::Builder::default() + .parse_callbacks(Box::new(UppercaseCallbacks)) + // Force Clang to use the same layout as the selected target. + .clang_arg(&target_flag); + + for include_arg in &lib.includes { + builder = builder.clang_arg(&format!( + "-I{}", + sources_dir.join(include_arg).to_str().unwrap() + )); + } + + if lib.target_triple.to_ascii_lowercase().starts_with("thumb") { + builder = builder.clang_arg("-mthumb"); + } + let bindings = builder + // The input header we would like to generate + // bindings for. + .header(header.path().to_str().unwrap()) + // Finish the builder and generate the bindings. + .generate() + // Unwrap the Result and panic on failure. + .expect("Unable to generate bindings"); + + let out_path = self + .opts + .out_dir + .join("src") + .join("bindings") + .join(format!("{}.rs", lib.module)); + + bindings + .write_to_file(&out_path) + .expect("Couldn't write bindings!"); + + let mut file_contents = fs::read_to_string(&out_path).unwrap(); + file_contents = file_contents + .replace("::std::mem::", "::core::mem::") + .replace("::std::os::raw::", "::core::ffi::") + .replace("::std::option::", "::core::option::"); + + file_contents = file_contents + .lines() + .map(|line| { + if let Some(rest) = line.strip_prefix("pub const ") { + if let Some((name, tail)) = rest.split_once(':') { + let upper = name.trim().to_ascii_uppercase(); + return format!("pub const {}:{}", upper, tail); + } } - } - line.to_owned() - }) - .collect::>() - .join("\n"); - - if !file_contents.ends_with('\n') { - file_contents.push('\n'); + line.to_owned() + }) + .collect::>() + .join("\n"); + + if !file_contents.ends_with('\n') { + file_contents.push('\n'); + } + + fs::write(&out_path, file_contents).unwrap(); + + // copy misc files + fs::copy( + sources_dir.join(&lib.library), + self.opts + .out_dir + .join("src") + .join("lib") + .join(format!("{}.a", lib.module)), + ) + .unwrap(); } - fs::write(&out_path, file_contents).unwrap(); - - // copy misc files - fs::copy( - self.opts - .sources_dir - .join("Middlewares/ST/STM32_WPAN/mac_802_15_4/lib/wba_mac_lib.a"), - self.opts.out_dir.join("src/lib/wba_mac_lib.a"), - ) - .unwrap(); fs::write( self.opts.out_dir.join("README.md"), include_bytes!("../res/README.md"), diff --git a/stm32-bindings-gen/src/main.rs b/stm32-bindings-gen/src/main.rs index d019910..f7c4d68 100644 --- a/stm32-bindings-gen/src/main.rs +++ b/stm32-bindings-gen/src/main.rs @@ -1,20 +1,29 @@ use std::{env, path::PathBuf, process}; -use stm32_bindings_gen::{Gen, Options}; +use stm32_bindings_gen::{Gen, Library, Options}; fn main() { let out_dir = PathBuf::from("build/stm32-bindings"); let sources_dir = PathBuf::from("sources"); - let target_triple = resolve_target_triple(); let opts = Options { out_dir, sources_dir, - target_triple, }; - Gen::new(opts).run_gen(); + + let libs = Vec::from([Library { + target_triple: String::from("thumbv8m.main-none-eabihf"), + sources_dir: "STM32CubeWBA".into(), + header: include_bytes!("../inc/wba_wpan_mac.h"), + module: "wba_wpan_mac", + includes: Vec::from(["Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc".into()]), + library: "Middlewares/ST/STM32_WPAN/mac_802_15_4/lib/wba_mac_lib.a".into(), + }]); + + Gen::new(opts, libs).run_gen(); } +#[allow(dead_code)] fn resolve_target_triple() -> String { let mut args = env::args().skip(1); let mut positional: Option = None;