From 5a6e0edafa4cbfaa701d0509ee310379c911fddd Mon Sep 17 00:00:00 2001 From: "Jonathan Pallant (Ferrous Systems)" Date: Tue, 28 Nov 2023 15:16:21 +0000 Subject: [PATCH] Use bindgen to process ThreadX headers. --- .github/workflows/build.yml | 15 ++ .github/workflows/clippy.yml | 17 +- .github/workflows/format.yml | 27 ++- demo-app/Cargo.lock | 397 ++++++++++++++++++++++++++++++++- demo-app/Cargo.toml | 1 + demo-app/src/main.rs | 123 ++++------ threadx | 2 +- threadx-sys/.gitignore | 5 + threadx-sys/Cargo.lock.license | 3 + threadx-sys/Cargo.toml | 16 ++ threadx-sys/build.rs | 61 +++++ threadx-sys/include/stdlib.h | 2 + threadx-sys/include/string.h | 3 + threadx-sys/src/lib.rs | 60 +++++ threadx-sys/wrapper.h | 4 + 15 files changed, 643 insertions(+), 93 deletions(-) create mode 100644 threadx-sys/.gitignore create mode 100644 threadx-sys/Cargo.lock.license create mode 100644 threadx-sys/Cargo.toml create mode 100644 threadx-sys/build.rs create mode 100644 threadx-sys/include/stdlib.h create mode 100644 threadx-sys/include/string.h create mode 100644 threadx-sys/src/lib.rs create mode 100644 threadx-sys/wrapper.h diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a34a9e1..e7db193 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -66,3 +66,18 @@ jobs: with: name: demo-app path: demo-app/target/thumbv7em-none-eabi/release/demo-app + job-build-threadx-sys: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + submodules: 'true' + - name: Check threadx-sys + run: | + cd threadx-sys + cargo check + - name: Build threadx-sys + run: | + cd threadx-sys + cargo build diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 8354c5a..0001b48 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -10,15 +10,30 @@ jobs: steps: - name: Checkout repo uses: actions/checkout@v4 + with: + submodules: 'true' - name: Install tools run: | sudo apt-get update -y && sudo apt-get -y install gcc-arm-none-eabi - name: Add rustup target run: | rustup target add thumbv7em-none-eabi - - name: Check Clippy + - name: Check Clippy on Demo App env: RUSTFLAGS: "-Dwarnings" run: | cd demo-app cargo clippy --all-features + job-clippy-threadx-sys: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + submodules: 'true' + - name: Check Clippy on threadx-sys + env: + RUSTFLAGS: "-Dwarnings" + run: | + cd threadx-sys + cargo clippy --all-features diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 552ecb9..e59a9ae 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -6,11 +6,22 @@ run-name: Check code formatting on: [push] jobs: job-format-demo-app: - runs-on: ubuntu-latest - steps: - - name: Checkout repo - uses: actions/checkout@v4 - - name: Check Formatting - run: | - cd demo-app - cargo fmt -- --check + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Check Formatting + run: | + cd demo-app + cargo fmt -- --check + job-format-threadx-sys: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + submodules: 'true' + - name: Check Formatting + run: | + cd threadx-sys + cargo fmt -- --check diff --git a/demo-app/Cargo.lock b/demo-app/Cargo.lock index c1b6d55..2848663 100644 --- a/demo-app/Cargo.lock +++ b/demo-app/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "atomic-polyfill" version = "0.1.11" @@ -32,6 +41,29 @@ dependencies = [ "rustc_version 0.2.3", ] +[[package]] +name = "bindgen" +version = "0.69.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ffcebc3849946a7170a05992aac39da343a90676ab392c51a4280981d6379c2" +dependencies = [ + "bitflags 2.4.1", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.37", + "which", +] + [[package]] name = "bitfield" version = "0.13.2" @@ -44,6 +76,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "bytemuck" version = "1.14.0" @@ -71,12 +109,32 @@ dependencies = [ "libc", ] +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "cortex-m" version = "0.7.7" @@ -128,7 +186,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8a2d011b2fee29fb7d659b83c43fce9a2cb4df453e16d441a51448e448f3f98" dependencies = [ - "bitflags", + "bitflags 1.3.2", "defmt-macros", ] @@ -176,8 +234,15 @@ dependencies = [ "heapless", "nrf52840-hal", "panic-probe", + "threadx-sys", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "embedded-dma" version = "0.2.0" @@ -203,6 +268,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "156d7a2fdd98ebbf9ae579cbceca3058cff946e13f8e17b90e3511db0508c723" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "fixed" version = "1.24.0" @@ -215,6 +290,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "half" version = "2.3.1" @@ -247,12 +328,49 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "libloading" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" + [[package]] name = "lock_api" version = "0.4.10" @@ -263,6 +381,24 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "nb" version = "0.1.3" @@ -278,6 +414,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nrf-hal-common" version = "0.16.0" @@ -332,6 +478,12 @@ dependencies = [ "vcell", ] +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + [[package]] name = "panic-probe" version = "0.3.1" @@ -342,6 +494,22 @@ dependencies = [ "defmt", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + +[[package]] +name = "prettyplease" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" +dependencies = [ + "proc-macro2", + "syn 2.0.37", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -390,6 +558,41 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.2.3" @@ -408,6 +611,19 @@ dependencies = [ "semver 1.0.19", ] +[[package]] +name = "rustix" +version = "0.38.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc99bc2d4f1fed22595588a013687477aedf3cdcfb26558c559edb67b4d9b22e" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.48.0", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -435,6 +651,12 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "shlex" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" + [[package]] name = "spin" version = "0.9.8" @@ -492,6 +714,13 @@ dependencies = [ "syn 2.0.37", ] +[[package]] +name = "threadx-sys" +version = "0.1.0" +dependencies = [ + "bindgen", +] + [[package]] name = "typenum" version = "1.17.0" @@ -536,3 +765,169 @@ checksum = "9ee8f19f9d74293faf70901bc20ad067dc1ad390d2cbf1e3f75f721ffee908b6" dependencies = [ "vcell", ] + +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/demo-app/Cargo.toml b/demo-app/Cargo.toml index 46680b1..9fcb5b1 100644 --- a/demo-app/Cargo.toml +++ b/demo-app/Cargo.toml @@ -17,6 +17,7 @@ heapless = "0.7" panic-probe = { version = "0.3", features = ["print-defmt"] } defmt = "0.3.5" defmt-rtt = "0.4" +threadx-sys = { path = "../threadx-sys" } # optimize code in both profiles [profile.dev] diff --git a/demo-app/src/main.rs b/demo-app/src/main.rs index c3d5ea6..12e1cae 100644 --- a/demo-app/src/main.rs +++ b/demo-app/src/main.rs @@ -6,120 +6,79 @@ #![no_main] #![no_std] -use core::mem::MaybeUninit; +use core::{ffi::CStr, mem::MaybeUninit}; use cortex_m_rt::entry; use defmt_rtt as _; use nrf52840_hal::prelude::OutputPin; use panic_probe as _; -#[repr(C)] -struct TxThread { - storage: [u32; 128], -} - -#[repr(C)] -struct TxBytePool { - storage: [u32; 128], -} - -static mut THREAD_0: MaybeUninit = MaybeUninit::uninit(); - -static mut THREAD_1: MaybeUninit = MaybeUninit::uninit(); - -static mut BYTE_POOL: MaybeUninit = MaybeUninit::uninit(); - -static mut BYTE_POOL_STORAGE: MaybeUninit<[u8; 32768]> = MaybeUninit::uninit(); - static BUILD_SLUG: Option<&str> = option_env!("BUILD_SLUG"); -type TxThreadFunc = extern "C" fn(entry_input: core::ffi::c_ulong); - -extern "C" { - fn _tx_initialize_kernel_enter(); - // UINT _tx_thread_create(TX_THREAD *thread_ptr, CHAR *name_ptr, - // VOID (*entry_function)(ULONG entry_input), ULONG entry_input, - // VOID *stack_start, ULONG stack_size, - // UINT priority, UINT preempt_threshold, - // ULONG time_slice, UINT auto_start); - - fn _tx_thread_create( - thread_ptr: *mut TxThread, - name_ptr: *const core::ffi::c_char, - entry_function: TxThreadFunc, - entry_input: core::ffi::c_ulong, - stack_start: *mut core::ffi::c_void, - stack_size: core::ffi::c_ulong, - priority: core::ffi::c_uint, - preempt_threshold: core::ffi::c_uint, - time_slice: core::ffi::c_ulong, - auto_start: core::ffi::c_uint, - ) -> u32; - - // _tx_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, - // ULONG wait_option); - fn _tx_byte_allocate( - pool_ptr: *mut TxBytePool, - memory_ptr: *mut *mut core::ffi::c_void, - memory_size: core::ffi::c_ulong, - wait_option: core::ffi::c_ulong, - ); - - // UINT _tx_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, - // ULONG pool_size); - fn _tx_byte_pool_create( - pool_ptr: *mut TxBytePool, - name_ptr: *const core::ffi::c_char, - pool_start: *mut core::ffi::c_void, - pool_size: core::ffi::c_ulong, - ); - // UINT _tx_thread_sleep(ULONG timer_ticks); - fn _tx_thread_sleep(timer_ticks: core::ffi::c_ulong); -} - -const DEMO_STACK_SIZE: core::ffi::c_ulong = 1024; +const DEMO_STACK_SIZE: usize = 1024; #[no_mangle] extern "C" fn tx_application_define(_first_unused_memory: *mut core::ffi::c_void) { + static mut THREAD_0: MaybeUninit = MaybeUninit::uninit(); + static mut THREAD_1: MaybeUninit = MaybeUninit::uninit(); + static mut BYTE_POOL: MaybeUninit = MaybeUninit::uninit(); + static mut BYTE_POOL_STORAGE: MaybeUninit<[u8; 32768]> = MaybeUninit::uninit(); + defmt::println!("In tx_application_define()..."); + // ThreadX requires a non-const pointer to char for the names, which it + // wil hold on to in the object, so it must have static lifetime. So we + // cast-away-const on a static string slice to appease the API. unsafe { - _tx_byte_pool_create( + let pool_name = CStr::from_bytes_with_nul(b"byte-pool0\0").unwrap(); + threadx_sys::_tx_byte_pool_create( BYTE_POOL.as_mut_ptr(), - "byte pool 0\0".as_ptr() as *const core::ffi::c_char, + pool_name.as_ptr() as *mut threadx_sys::CHAR, BYTE_POOL_STORAGE.as_mut_ptr() as *mut _, core::mem::size_of_val(&BYTE_POOL_STORAGE) as u32, ); let mut pointer = core::ptr::null_mut(); - _tx_byte_allocate(BYTE_POOL.as_mut_ptr(), &mut pointer, DEMO_STACK_SIZE, 0); + threadx_sys::_tx_byte_allocate( + BYTE_POOL.as_mut_ptr(), + &mut pointer, + DEMO_STACK_SIZE as _, + threadx_sys::TX_NO_WAIT, + ); defmt::println!("Stack allocated @ {:08x}", pointer); - _tx_thread_create( + let thread_name = CStr::from_bytes_with_nul(b"thread0\0").unwrap(); + threadx_sys::_tx_thread_create( THREAD_0.as_mut_ptr(), - "thread 0\0".as_ptr() as *const core::ffi::c_char, - my_thread, + thread_name.as_ptr() as *mut threadx_sys::CHAR, + Some(my_thread), 0x12345678, pointer, - DEMO_STACK_SIZE, - 1, + DEMO_STACK_SIZE as _, 1, - 0, 1, + threadx_sys::TX_NO_TIME_SLICE, + threadx_sys::TX_AUTO_START, ); defmt::println!("Thread spawned ({:08x})", 0x12345678); let mut pointer = core::ptr::null_mut(); - _tx_byte_allocate(BYTE_POOL.as_mut_ptr(), &mut pointer, DEMO_STACK_SIZE, 0); + threadx_sys::_tx_byte_allocate( + BYTE_POOL.as_mut_ptr(), + &mut pointer, + DEMO_STACK_SIZE as _, + threadx_sys::TX_NO_WAIT, + ); defmt::println!("Stack allocated @ {:08x}", pointer); - _tx_thread_create( + let thread_name = CStr::from_bytes_with_nul(b"thread1\0").unwrap(); + threadx_sys::_tx_thread_create( THREAD_1.as_mut_ptr(), - "thread 1\0".as_ptr() as *const core::ffi::c_char, - my_thread, + thread_name.as_ptr() as *mut threadx_sys::CHAR, + Some(my_thread), 0xAABBCCDD, pointer, - DEMO_STACK_SIZE, - 1, + DEMO_STACK_SIZE as _, 1, - 0, 1, + threadx_sys::TX_NO_TIME_SLICE, + threadx_sys::TX_AUTO_START, ); defmt::println!("Thread spawned ({:08x})", 0xAABBCCDD); } @@ -132,7 +91,7 @@ extern "C" fn my_thread(value: u32) { thread_counter += 1; unsafe { - _tx_thread_sleep(100); + threadx_sys::_tx_thread_sleep(100); } defmt::println!("I am my_thread({:08x}), count = {}", value, thread_counter); @@ -165,7 +124,7 @@ fn main() -> ! { defmt::println!("Entering ThreadX kernel..."); unsafe { - _tx_initialize_kernel_enter(); + threadx_sys::_tx_initialize_kernel_enter(); } panic!("Kernel exited"); diff --git a/threadx b/threadx index a8e5d09..13b700f 160000 --- a/threadx +++ b/threadx @@ -1 +1 @@ -Subproject commit a8e5d0946c31385ff97bb27b67b2cdd905b9a58b +Subproject commit 13b700fd3ed763cedcba7dbd17b859077a01aa9c diff --git a/threadx-sys/.gitignore b/threadx-sys/.gitignore new file mode 100644 index 0000000..15e1e8a --- /dev/null +++ b/threadx-sys/.gitignore @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +# SPDX-License-Identifier: CC0-1.0 + +target/ +Cargo.lock diff --git a/threadx-sys/Cargo.lock.license b/threadx-sys/Cargo.lock.license new file mode 100644 index 0000000..0db3018 --- /dev/null +++ b/threadx-sys/Cargo.lock.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +# SPDX-License-Identifier: CC0-1.0 + diff --git a/threadx-sys/Cargo.toml b/threadx-sys/Cargo.toml new file mode 100644 index 0000000..9a1e4b4 --- /dev/null +++ b/threadx-sys/Cargo.toml @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +# SPDX-License-Identifier: MIT OR Apache-2.0 + +[package] +name = "threadx-sys" +version = "0.1.0" +edition = "2021" +authors = ["Jonathan Pallant "] +license-file = "../LICENCES/LicenseRef-Azure.txt" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[build-dependencies] +bindgen = "0.69.1" diff --git a/threadx-sys/build.rs b/threadx-sys/build.rs new file mode 100644 index 0000000..0df1079 --- /dev/null +++ b/threadx-sys/build.rs @@ -0,0 +1,61 @@ +//! Build Script for threadx-sys +//! +//! Calls out to bindgen to generate a Rust crate from the ThreadX header +//! files. + +// SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use std::env; +use std::path::PathBuf; + +fn main() { + let threadx_path = PathBuf::from("../threadx"); + // The bindgen::Builder is the main entry point + // to bindgen, and lets you build up options for + // the resulting bindings. + let bindings = bindgen::Builder::default() + // The input header we would like to generate + // bindings for. + .header("wrapper.h") + // Point to ThreadX headers + .clang_arg(format!("-I{}", threadx_path.join("common/inc").display())) + .clang_arg(format!( + "-I{}", + threadx_path.join("ports/cortex_m4/gnu/inc").display() + )) + // Some fake local include files + .clang_arg("-I./include") + // Disable standard includes (they belong to the host) + .clang_arg("-nostdinc") + // Set the target + .clang_arg("--target=arm") + .clang_arg("-mthumb") + .clang_arg("-mcpu=cortex-m4") + // Use softfp + .clang_arg("-mfloat-abi=soft") + // We're no_std + .use_core() + // Include only the useful stuff + .allowlist_function("tx_.*") + .allowlist_function("_tx_.*") + .allowlist_type("TX_.*") + .allowlist_var("TX_.*") + .allowlist_var("TX_AUTO_START") + // Format the output + .formatter(bindgen::Formatter::Rustfmt) + // Finish the builder and generate the bindings. + .generate() + // Unwrap the Result and panic on failure. + .expect("Unable to generate bindings"); + + // Write the bindings to the $OUT_DIR/bindings.rs file. + let rust_source = bindings.to_string(); + + let bindings_out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs"); + std::fs::write(bindings_out_path, rust_source).expect("Couldn't write updated bindgen output"); + + // The user will have to specify the path to the library themselves because + // they have to compile ThreadX themselves (or use a pre-compiled version). + // We only generate the API here. +} diff --git a/threadx-sys/include/stdlib.h b/threadx-sys/include/stdlib.h new file mode 100644 index 0000000..7cb4b7c --- /dev/null +++ b/threadx-sys/include/stdlib.h @@ -0,0 +1,2 @@ +// SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +// SPDX-License-Identifier: CC0-1.0 diff --git a/threadx-sys/include/string.h b/threadx-sys/include/string.h new file mode 100644 index 0000000..25ae4cf --- /dev/null +++ b/threadx-sys/include/string.h @@ -0,0 +1,3 @@ +// SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +// SPDX-License-Identifier: CC0-1.0 + diff --git a/threadx-sys/src/lib.rs b/threadx-sys/src/lib.rs new file mode 100644 index 0000000..138c39c --- /dev/null +++ b/threadx-sys/src/lib.rs @@ -0,0 +1,60 @@ +//! Auto-generated bindings for ThreadX 6.3 +//! +//! ```quote +//! /**************************************************************************/ +//! /* */ +//! /* Copyright (c) Microsoft Corporation. All rights reserved. */ +//! /* */ +//! /* This software is licensed under the Microsoft Software License */ +//! /* Terms for Microsoft Azure RTOS. Full text of the license can be */ +//! /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ +//! /* and in the root directory of this software. */ +//! /* */ +//! /**************************************************************************/ +//! ``` + +// The notice above applies to the build output of this crate, including the +// bindings.rs file. +// +// The notice below applies to this file, without the bindings.rs file. +// +// SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +// SPDX-License-Identifier: CC0-1.0 + +#![no_std] +#![allow(non_snake_case)] +#![allow(non_camel_case_types)] +#![allow(non_upper_case_globals)] +#![allow(clippy::missing_safety_doc)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + +// Bindgen couldn't pick these constants out of the header file +// So I added them manually. + +pub const TX_NO_WAIT: ULONG = 0; +pub const TX_WAIT_FOREVER: ULONG = 0xFFFFFFFF; +pub const TX_AND: UINT = 2; +pub const TX_AND_CLEAR: UINT = 3; +pub const TX_OR: UINT = 0; +pub const TX_OR_CLEAR: UINT = 1; +pub const TX_1_ULONG: UINT = 1; +pub const TX_2_ULONG: UINT = 2; +pub const TX_4_ULONG: UINT = 4; +pub const TX_8_ULONG: UINT = 8; +pub const TX_16_ULONG: UINT = 16; +pub const TX_NO_TIME_SLICE: ULONG = 0; +pub const TX_AUTO_START: UINT = 1; +pub const TX_DONT_START: UINT = 0; +pub const TX_AUTO_ACTIVATE: UINT = 1; +pub const TX_NO_ACTIVATE: UINT = 0; +pub const TX_TRUE: UINT = 1; +pub const TX_FALSE: UINT = 0; +pub const TX_INHERIT: UINT = 1; +pub const TX_NO_INHERIT: UINT = 0; +pub const TX_THREAD_ENTRY: UINT = 0; +pub const TX_THREAD_EXIT: UINT = 1; +pub const TX_NO_SUSPENSIONS: UINT = 0; +pub const TX_NO_MESSAGES: UINT = 0; +pub const TX_EMPTY: ULONG = 0; +pub const TX_CLEAR_ID: ULONG = 0; diff --git a/threadx-sys/wrapper.h b/threadx-sys/wrapper.h new file mode 100644 index 0000000..37ae595 --- /dev/null +++ b/threadx-sys/wrapper.h @@ -0,0 +1,4 @@ +// SPDX-FileCopyrightText: Copyright (c) 2023 Ferrous Systems +// SPDX-License-Identifier: CC0-1.0 + +#include