Rust for Embedded Development
Published: 2021-08-18 03:41:02

## What is [Rust](https://www.rust-lang.org/)?


Rust is a language that empowers everyone to build reliable and efficient software. It features

- 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? 

Rust is designed to guarantee both security and high performance. The design philosophy is right into what embedded development requires.
While embedded software has running problems that are mostly due to its memory. The Rust language can be regarded as a compiler-oriented language, so you can be sure that you are using memory safely while compiling. Here are some of the benefits of Rust developing on embedded devices:

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

Now, let's get it on the Open Source [RT-Thread operating system](https://github.com/RT-Thread/rt-thread) to demonstrate how rust can be used for embedded development.

## Call Rust in C

When we're calling the Rust code in C code, requires us to package the Rust source code as a static library file. When the C code compiles, link it in.

### Create lib Library

1.Use `cargo init --lib rust_to_c` to build a lib library in Clion. Add the following code to the lib.rs. The following function evaluates the sum of two values of type i32 and returns the result  : 

```rust
#![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

1.Install armv7 target:

   ```rust
   rustup target add armv7a-none-eabi
   ```

2.Generate static library file:

   ```rust
   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

1.Install [cbindgen](https://github.com/eqrion/cbindgen), cbindgen generates C/C++11 header file from the rust library:

   ```
   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

1.Put the generated `sum.h` and `sum.a` files into the  `rt-thread\bsp\qemu-vexpress-a9\applications` directory

2.Modify the SConscript file and add static library:

   ```rust
   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.

   ```rust
   #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](https://www.rt-thread.io/download.html?download=Env) environment, use scons to compile the project and run:

   ```shell
      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

1.We can implement some complicated math in rust. In the lib.rs file, use the rust language to implement add, subtract, multiply and divide:

   ```rust
     #![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](https://github.com/rust-lang/compiler-builtins/issues/353).

   ```rust
      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`:

   ```rust
       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

Great to know that Rust can be called in C code, let's give a try to call the  rt_kprintf function in Rust:

### Modify the lib.rs file

```rust
    // 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

```rust
    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

Copy the library files that generated by rust into the application directory.

```shell
    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](https://rt-thread.medium.com/rt-thread-iot-os-tech-conference-324182c7403c) 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


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atbqs90mzaodcic9m5vg.png)