I think they gave a decent overview of a couple of tricky parts, but as always, the devil is in the details. To help with all the gritty details, I’ve written up a more complete example.

Overview

In order to actually run this example, you’ll need an STM32L1 Discovery Board. I don’t expect many readers to have this exact board, but it should be relatively easy to port to other targets/boards, and I thought a complete example would be useful as a point of comparison. This is basically just STM’s GPIO_IOToggle example but with the core inner loop replaced with some Rust code.

Essentially what I did was:

Started with STM’s GPIO_IOToggle example

Added a Makefile to build the example

Setup the Cargo configuration necessary to target a Cortex-M3 in .cargo/config

Added rules to the Makefile to build a Rust sysroot containing a valid libcore

Used bindgen to wrap the STM32 HAL libraries for use in Rust

Replaced the core inner loop in main.c of the GPIO_IOToggle example with Rust code

Added rules to the Makefile to build the Rust code and link the resulting static library into the final binary

Thanks for all your great Rust articles! I’m excited too about what Rust might mean for embedded development.

I’m trying to build your example here, but I’m having a problem building rust_src with cargo though. I have the the rust source in a sibling directory, and it matches my nightly rustc installation commit. I’m trying to build rust_src via the build-rust make target.

The sysroot with libcore builds fine, but I’m getting an error when it tries to build rust_src: