How to run Rust in MCU

by vaachii in September 22nd, 2021

There are several ways to use a Rust-implemented program from C. One is to compile the Rust-implemented program into WASM and transform it into C using a great tool like wasm2c. The other way is to compile Rust as a static library (.a) and reference it from C. Both methods have advantages and disadvantages. Still, we believe the latter architecture is the better choice due to its "simplicity" and "fewer dependencies." We plan to post more details about this architecture strategy on our blog later. When compiling as a static library, you have to be careful about the definition of compile options. Compilation options need to be appropriately defined for each device on which the program is to run, and if they are defined incorrectly, the program cannot be linked to a bare-metal program. In building a static library for our first target device, the Renesas Electronics RA6M3 (Cortex-M4), trial and error led us to the following custom target definition.

{
  "arch": "arm",
  "data-layout": "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
  "disable-redzone": true,
  "emit-debug-gdb-scripts": false,
  "executables": true,
  "features": "+v7,+thumb2,-neon,+strict-align",
  "is-builtin": false,
  "linker": "rust-lld",
  "linker-flavor": "ld.lld",
  "llvm-target": "armv7a-none-eabihf",
  "os": "none",
  "env": "",
  "cpu": "cortex-m4",
  "panic-strategy": "abort",
  "relocation-model": "static",
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-pointer-width": "32"
}

After writing this definition to a file, you can use it as a custom target in Rust, for example, cargo build --target ra6m3.json. The next thing that needs to be solved is that the stdpackages that some 3rd party libraries depend on are not available for bare metal programs, but we will discuss this in detail in the next blog.

To learn more about UNiD, please visit our GitHub, where we will feature (a demo) and a technical deep dive.

Your cart
    Checkout