Rust std on ThreadX


I'm porting the Rust standard library to ThreadX. It's a lot of work for a five minute talk! I'll make it easier by only supporting the MCUs I know pretty well: NXP's i.MX RT series.

Distribution

The threadx branch of my Rust fork contains a standard library built on ThreadX. It's hacky, so treat it like a prototype.

The only-supported target is called

thumbv7em-threadx-eabihf

Building

Before you try building this distribution, make sure you can build libthreadx-sys for a thumbv7em-none-eabihf target.

After that, it's a little different than what's discussed here. After cloning, use x.py to create a bootstrap.toml with library defaults. Within that bootstrap.toml, specify the C toolchain used to build ThreadX for this new target. Additionally, specify any Rust configurations for ThreadX, NetX Duo, and related libraries. My bootstrap.toml includes

[target.thumbv7em-threadx-eabihf]
cc = "arm-none-eabi-gcc"
cxx = "arm-none-eabi-g++"
ar = "arm-none-eabi-ar"
rustflags = [
    # For enhanced NetX Duo bindings, supported
    # by an i.MX RT ENET driver.
    "--cfg", "nx_enable_interface_capability",
]

If you're on a MacOS host, your bootstrap.toml might also need

[rust]
lld = true

(If you're on Linux, you shouldn't need it, but it also shouldn't hurt.)

Additionally, edit .cargo/config.toml to include something like

[env]
# My preferred build settings for NetX Duo, supporting an i.MX RT MCU.
"CFLAGS_thumbv7em-threadx-eabihf" = "-DNX_PACKET_ALIGNMENT=64 -DNX_DISABLE_ICMP_RX_CHECKSUM -DNX_DISABLE_ICMP_TX_CHECKSUM -DNX_DISABLE_IP_RX_CHECKSUM -DNX_DISABLE_IP_TX_CHECKSUM -DNX_DISABLE_UDP_RX_CHECKSUM -DNX_DISABLE_UDP_TX_CHECKSUM"
"CC_thumbv7em-threadx-eabihf" = "arm-none-eabi-gcc"
"AR_thumbv7em-threadx-eabihf" = "arm-none-eabi-ar"

Since you're building NetX Duo with the standard library, the CFLAGS variable shown above should include configurations for that library. My settings for an i.MX RT ENET driver are shown in the example above.

Build a toolchain for the new target, as well as your host. On my Linux workstation, I usually say

./x build --target=thumbv7em-threadx-eabihf,x86_64-unknown-linux-gnu

And on my old Macbook, I say

./x build --target=thumbv7em-threadx-eabihf,x86_64-apple-darwin

Once complete, rustup should know about your stage1 toolchain.

rustc +stage1 --print target-list | grep threadx

Use stage1 to play with Rust std on ThreadX programs!