Build it

The first step is to build our “binary” crate. Because the microcontroller has a differentarchitecture than your laptop we’ll have to cross compile. Cross compiling in Rust land is as simpleas passing an extra --target flag to rustcor Cargo. The complicated part is figuring out theargument of that flag: the name of the target.

The microcontroller in the F3 has a Cortex-M4F processor in it. rustc knows how to cross compileto the Cortex-M architecture and provides 4 different targets that cover the different processorfamilies within that architecture:

  • thumbv6m-none-eabi, for the Cortex-M0 and Cortex-M1 processors
  • thumbv7m-none-eabi, for the Cortex-M3 processor
  • thumbv7em-none-eabi, for the Cortex-M4 and Cortex-M7 processors
  • thumbv7em-none-eabihf, for the Cortex-M4F and Cortex-M7F processors

For the F3, we’ll to use the thumbv7em-none-eabihf target. Before cross compiling you have todownload pre-compiled version of the standard library (a reduced version of it actually) for yourtarget. That’s done using rustup:

  1. $ rustup target add thumbv7em-none-eabihf

You only need to do the above step once; rustup will re-install a new standard library(rust-std component) whenever you update your toolchain.

With the rust-std component in place you can now cross compile the program using Cargo:

  1. $ # make sure you are in the `src/05-led-roulette` directory
  2. $ cargo build --target thumbv7em-none-eabihf
  3. Compiling semver-parser v0.7.0
  4. Compiling aligned v0.1.1
  5. Compiling libc v0.2.35
  6. Compiling bare-metal v0.1.1
  7. Compiling cast v0.2.2
  8. Compiling cortex-m v0.4.3
  9. (..)
  10. Compiling stm32f30x v0.6.0
  11. Compiling stm32f30x-hal v0.1.2
  12. Compiling aux5 v0.1.0 (file://$PWD/aux)
  13. Compiling led-roulette v0.1.0 (file://$PWD)
  14. Finished dev [unoptimized + debuginfo] target(s) in 35.84 secs

NOTE Be sure to compile this crate without optimizations. The provided Cargo.toml file and build command above will ensure optimizations are off.

OK, now we have produced an executable. This executable won’t blink any leds, it’s just a simplified version that we will build upon later in the chapter. As a sanity check, let’s verify that the produced executable is actually an ARM binary:

  1. $ # equivalent to `readelf -h target/thumbv7em-none-eabihf/debug/led-roulette`
  2. $ cargo readobj --target thumbv7em-none-eabihf --bin led-roulette -- -file-headers
  3. ELF Header:
  4. Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  5. Class: ELF32
  6. Data: 2's complement, little endian
  7. Version: 1 (current)
  8. OS/ABI: UNIX - System V
  9. ABI Version: 0x0
  10. Type: EXEC (Executable file)
  11. Machine: ARM
  12. Version: 0x1
  13. Entry point address: 0x8000197
  14. Start of program headers: 52 (bytes into file)
  15. Start of section headers: 740788 (bytes into file)
  16. Flags: 0x5000400
  17. Size of this header: 52 (bytes)
  18. Size of program headers: 32 (bytes)
  19. Number of program headers: 2
  20. Size of section headers: 40 (bytes)
  21. Number of section headers: 20
  22. Section header string table index: 18

Next, we’ll flash the program into our microcontroller.