Rust for Embedded Development

What is Rust?

  • High Performance: Amazing fast speed and high memory utilization
  • Reliability: Memory errors are able to be eliminated during compilation
  • Productivity: Great documentation, a friendly compiler with useful error messages, and top-notch tooling — an integrated package manager and build tool, smart multi-editor support with auto-completion and type inspections, an auto-formatter, and more.

Why use RUST on Embedded Development?

  • Powerful static analysis
  • Flexible memory
  • Fearless concurrency
  • Interoperability
  • Portability
  • Community driven

Now, let’s get it on the Open Source RT-Thread operating system to demonstrate how rust can be used for embedded development.

Call Rust in C

Create lib Library

#![no_std]
use core::panic::PanicInfo;
#[no_mangle]
pub extern "C" fn sum(a: i32, b: i32) -> i32 {
a + b
}
#[panic_handler]
fn panic(_info:&PanicInfo) -> !{
loop{}
}

2.Add the following code to the Cargo.toml file to tell Rustc what type of library to generate.

[lib]
name = "sum"
crate-type = ["staticlib"]
path = "src/lib.rs"

Cross-compilation

rustup target add armv7a-none-eabi

2.Generate static library file:

PS C:\Users\LiuKang\Desktop\RUST\rust_to_c> cargo build --target=armv7a-none-eabi --release --verbose
Fresh rust_to_c v0.1.0 (C:\Users\LiuKang\Desktop\RUST\rust_to_c)
Finished release [optimized] target(s) in 0.01s

Generate Header File

cargo install --force cbindgen

2.Create a new cbindgen.toml file under the project folder:

3.Generate header file:

cbindgen --config cbindgen.toml --crate rust_to_c --output sum.h

Call Rust Library File

2.Modify the SConscript file and add static library:

from building import *   cwd     = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
LIBS = ["libsum.a"]
LIBPATH = [GetCurrentDir()]
group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH, LIBS = LIBS, LIBPATH = LIBPATH) Return('group')

3.Call the sum function in the main function and get the return value and printf the value.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include "sum.h"
int main(void)
{
int32_t tmp;
tmp = sum(1, 2);
printf("call rust sum(1, 2) = %d\n", tmp);
return 0;
}

4.In RT-Thread Env environment, use scons to compile the project and run:

LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9
$ scons -j6
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: warning: you do not seem to have the pywin32 extensions installed;
parallel (-j) builds may not work reliably with open Python files.
File "D:\software\env_released_1.2.0\env\tools\Python27\Scripts\scons.py", line 204, in <module>
scons: Building targets ...
scons: building associated VariantDir targets: build
LINK rtthread.elf
arm-none-eabi-objcopy -O binary rtthread.elf rtthread.bin
arm-none-eabi-size rtthread.elf
text data bss dec hex filename
628220 2148 86700 717068 af10c rtthread.elf
scons: done building targets.
LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9
$ qemu.bat
WARNING: Image format was not specified for 'sd.bin' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
\ | /
- RT - Thread Operating System
/ | \ 4.0.4 build Jul 28 2021
2006 - 2021 Copyright by rt-thread team
lwIP-2.1.2 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to high speed failed!
call rust sum(1, 2) = 3
msh />

Add, subtract, multiply and divide

#![no_std]
use core::panic::PanicInfo;
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
a + b
}
#[no_mangle]
pub extern "C" fn subtract(a: i32, b: i32) -> i32 {
a - b
}
#[no_mangle]
pub extern "C" fn multiply(a: i32, b: i32) -> i32 {
a * b
}
#[no_mangle]
pub extern "C" fn divide(a: i32, b: i32) -> i32 {
a / b
}
#[panic_handler]
fn panic(_info:&PanicInfo) -> !{
loop{}
}

2.Build library files and header files and place them in the application directory

3.Use scons to compile, if errors jumped on link up, find the solution on its official Github.

LINK rtthread.elf
d:/software/env_released_1.2.0/env/tools/gnu_gcc/arm_gcc/mingw/bin/../lib/gcc/arm-none-eabi/5.4.1/armv7-ar/thumb\libgcc.a(_arm_addsubdf3.o): In function `__aeabi_ul2d':
(.text+0x304): multiple definition of `__aeabi_ul2d'
applications\libsum.a(compiler_builtins-9b744f6fddf5e719.compiler_builtins.20m0qzjq-cgu.117.rcgu.o):/cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.35/src/float/conv.rs:143: first defined here
collect2.exe: error: ld returned 1 exit status
scons: *** [rtthread.elf] Error 1
scons: building terminated because of errors.

4.Modify rtconfig.py file, add the link parameter --allow-multiple-definition:

DEVICE = ' -march=armv7-a -marm -msoft-float'
CFLAGS = DEVICE + ' -Wall'
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__ -I.'
LINK_SCRIPT = 'link.lds'
LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,system_vectors,--allow-multiple-definition'+\
' -T %s' % LINK_SCRIPT
CPATH = ''
LPATH = ''

5.Compile and run QEMU:

LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9
$ scons -j6
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: warning: you do not seem to have the pywin32 extensions installed;
parallel (-j) builds may not work reliably with open Python files.
File "D:\software\env_released_1.2.0\env\tools\Python27\Scripts\scons.py", line 204, in <module>
scons: Building targets ...
scons: building associated VariantDir targets: build
LINK rtthread.elf
arm-none-eabi-objcopy -O binary rtthread.elf rtthread.bin
arm-none-eabi-size rtthread.elf
text data bss dec hex filename
628756 2148 86700 717604 af324 rtthread.elf
scons: done building targets.
LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9
$ qemu.bat
WARNING: Image format was not specified for 'sd.bin' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
\ | /
- RT - Thread Operating System
/ | \ 4.0.4 build Jul 28 2021
2006 - 2021 Copyright by rt-thread team
lwIP-2.1.2 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to high speed failed!
call rust sum(1, 2) = 3
call rust subtract(2, 1) = 1
call rust multiply(2, 2) = 4
call rust divide(4, 2) = 2

Call C in Rust

Modify the lib.rs file

// The imported rt-thread functions list
extern "C" {
pub fn rt_kprintf(format: *const u8, ...);
}
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
unsafe {
rt_kprintf(b"this is from rust\n" as *const u8);
}
a + b
}

Generate Library File

cargo build --target=armv7a-none-eabi --release --verbose
Compiling rust_to_c v0.1.0 (C:\Users\LiuKang\Desktop\RUST\rust_to_c)
Running `rustc --crate-name sum --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type staticlib --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=a
0723fa112c78339 -C extra-filename=-a0723fa112c78339 --out-dir C:\Users\LiuKang\Desktop\RUST\rust_to_c\target\armv7a-none-eabi\release\deps --target armv7a-none-eabi -L dependency=C:\Users\LiuKang\Desktop\RUS
T\rust_to_c\target\armv7a-none-eabi\release\deps -L dependency=C:\Users\LiuKang\Desktop\RUST\rust_to_c\target\release\deps`
Finished release [optimized] target(s) in 0.11s

Run

LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9                                                       
$ scons -j6
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: warning: you do not seem to have the pywin32 extensions installed;
parallel (-j) builds may not work reliably with open Python files.
File "D:\software\env_released_1.2.0\env\tools\Python27\Scripts\scons.py", line 204, in <module>
scons: Building targets ...
scons: building associated VariantDir targets: build
LINK rtthread.elf
arm-none-eabi-objcopy -O binary rtthread.elf rtthread.bin
arm-none-eabi-size rtthread.elf
text data bss dec hex filename
628812 2148 90796 721756 b035c rtthread.elf
scons: done building targets.
LiuKang@DESKTOP-538H6DE D:\repo\github\rt-thread\bsp\qemu-vexpress-a9
$ qemu.bat
WARNING: Image format was not specified for 'sd.bin' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
\ | /
- RT - Thread Operating System
/ | \ 4.0.4 build Jul 28 2021
2006 - 2021 Copyright by rt-thread team
lwIP-2.1.2 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to high speed failed!
this is from rust
call rust sum(1, 2) = 3
call rust subtract(2, 1) = 1
call rust multiply(2, 2) = 4
call rust divide(4, 2) = 2
msh />

RT-Thread IoT OS Global Tech Conference is open for registration! We welcome developers from different countries to come together to exchange ideas, showcase projects, identify solutions, discuss future strategies, and provide mutual learning opportunities on a wide variety of topics exploring embedded technologies.

Free Register: https://forms.gle/8W1j2ZhCSLKFfyFQ8

An Open-Source Community-Powered Real-Time Operating System (RTOS) Project! We develop, we DIY, we create, we share, we explore this new IoT World together!