Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
7 changes: 3 additions & 4 deletions d
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions d.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions stm32-bindings-gen/res/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"]

Expand Down Expand Up @@ -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 = []
Expand Down
3 changes: 2 additions & 1 deletion stm32-bindings-gen/res/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod wpan_wba;
#[cfg(feature = "wba_wpan_mac")]
mod wba_wpan_mac;
174 changes: 95 additions & 79 deletions stm32-bindings-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,119 @@ 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<PathBuf>,
pub library: PathBuf,
}

pub struct Gen {
opts: Options,
libs: Vec<Library>,
}

impl Gen {
pub fn new(opts: Options) -> Self {
Self { opts }
pub fn new(opts: Options, libs: Vec<Library>) -> Self {
Self { opts, libs }
}

pub fn run_gen(&mut self) {
let _ = fs::remove_dir_all(self.opts.out_dir.clone());
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::<Vec<_>>()
.join("\n");

if !file_contents.ends_with('\n') {
file_contents.push('\n');
line.to_owned()
})
.collect::<Vec<_>>()
.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"),
Expand Down
17 changes: 13 additions & 4 deletions stm32-bindings-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> = None;
Expand Down