Skip to content

Commit

Permalink
SFT-4358: Add stm32h7xx-hal-driver-sys crate.
Browse files Browse the repository at this point in the history
* extmod/foundation-rust/Cargo.lock: Update file.
* extmod/foundation-rust/Cargo.toml [workspace] <members>: Add stm32h7xx-hal-driver-sys.
* extmod/foundation-rust/Justfile (generate): Add stm32h7xx-hal-driver-sys bindgen.
(lint): Check stm32h7xx-hal-driver-sys bindgen.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/Cargo.toml: New manifest.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/bindgen.sh: New script.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/bindings.h: New header file.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/includes.awk: New AWK script.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/gen.rs: Generate bindings.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/gen.rs.license: New license information file.
* extmod/foundation-rust/stm32h7xx-hal-driver-sys/lib.rs: New file.
  • Loading branch information
jeandudey committed Oct 31, 2024
1 parent 2719b95 commit 012f18f
Show file tree
Hide file tree
Showing 10 changed files with 25,803 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extmod/foundation-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions extmod/foundation-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later

[workspace]
members = ["stm32h7xx-hal-driver-sys"]

[package]
name = "foundation"
version = "0.1.0"
Expand Down
2 changes: 2 additions & 0 deletions extmod/foundation-rust/Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ alias l := lint

# Generate C bindings header.
generate:
./stm32h7xx-hal-driver-sys/bindgen.sh
cbindgen --config cbindgen.toml \
--crate foundation \
--output include/foundation.h
Expand All @@ -19,6 +20,7 @@ build:
lint:
CC=arm-none-eabi-gcc cargo clippy --target thumbv7em-none-eabihf
cargo fmt --check
./stm32h7xx-hal-driver-sys/bindgen.sh -c
cbindgen --config cbindgen.toml \
--crate foundation \
--output include/foundation.h \
Expand Down
8 changes: 8 additions & 0 deletions extmod/foundation-rust/stm32h7xx-hal-driver-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later

[package]
name = "stm32h7xx-hal-driver-sys"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-or-later"
100 changes: 100 additions & 0 deletions extmod/foundation-rust/stm32h7xx-hal-driver-sys/bindgen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
#
# SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Generate bindings of STM32H7xx_HAL_Driver using rust-bindgen.
#
# To generate bindings:
#
# ./bindgen.sh
#
# To check if bindings need to be re-generated:
#
# ./bindgen.sh -c

CC=arm-none-eabi-gcc

usage() {
echo "Usage: $0 [-c]"
exit 1
}

check=false
while getopts "c" o; do
case "${o}" in
c)
check=true
;;
*)
usage
;;
esac
done

# From: https://stackoverflow.com/a/246128
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]
do
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )

system_include_directories=$($CC -v -xc -E /dev/null 2>&1 | awk -f "$DIR/includes.awk")

clang_arguments='--target=arm-none-eabi -mcpu=cortex-m7'
for includedir in ${system_include_directories}
do
clang_arguments+=" -isystem $includedir"
done

clang_arguments+=" \
-I$DIR/../../../lib/cmsis/inc \
-I$DIR/../../../lib/stm32lib/CMSIS/STM32H7xx/Include \
-I$DIR/../../../lib/stm32lib/STM32H7xx_HAL_Driver/Inc \
-I$DIR/../../../ports/stm32/boards/Passport \
-DSTM32H753xx \
"

if [ "$check" = true ]
then
output=$(mktemp)
else
output=$DIR/src/gen.rs
fi

# NOTE:
#
# --allowlist-item '^.*_BASE$': this is for the peripheral base addresses.
# Should be a cmsis-device-h7-sys crate.
echo "BINDGEN_EXTRA_CLANG_ARGS=$clang_arguments"
echo "generating bindings: $output"
BINDGEN_EXTRA_CLANG_ARGS="$clang_arguments" bindgen $DIR/bindings.h \
--verbose \
--rust-target 1.73 \
--use-core \
--no-doc-comments \
--default-enum-style moduleconsts \
--sort-semantically \
--allowlist-item '^.*_BASE$' \
--allowlist-function '^HAL_.*$' \
--output $output

# Format the resulting file.
rustfmt --config-path "$DIR/.." $output

if [ "$check" = true ]
then
echo "verifying if bindings need to be re-generated."
if diff "$DIR/src/gen.rs" "$output" >/dev/null
then
echo "done."
else
echo "error: bindings need to be re-generated!"
exit 1
fi
else
echo "bindings up to date."
fi
13 changes: 13 additions & 0 deletions extmod/foundation-rust/stm32h7xx-hal-driver-sys/bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. <[email protected]>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This file is passed to cbindgen to generate the Rust bindings.
*/

#ifndef BINDINGS_H
#define BINDINGS_H

#include <stm32h7xx_hal.h>

#endif /* BINDNIGS_H */
18 changes: 18 additions & 0 deletions extmod/foundation-rust/stm32h7xx-hal-driver-sys/includes.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Prints a space separalted list of GCC system include paths.
#
# For example:
#
# gcc -v -xc -E /dev/null 2>&1 | awk -f includes.awk

/#include <...> search starts here:/ {
inside_search_paths = 1; next
}

/End of search list./ {
inside_search_paths = 0
}

inside_search_paths { print }
Loading

0 comments on commit 012f18f

Please sign in to comment.