組込み開発の欲望

37598 ワード

さびは何か


錆は、信頼性と効率的なソフトウェアを構築するために誰もが権限を与える言語です.特徴
  • 高性能:驚くべき速い速度と高いメモリ利用
  • 信頼性:メモリエラーはコンパイル中に削除できます
  • 生産性:偉大なドキュメントは、便利なエラーメッセージを使用してフレンドリーなコンパイラ、および最小量のツールを-統合パッケージマネージャとビルドツール、自動補完と型検査、スマートフォーマッタ、スマートフォーマッタをサポートしています.
  • なぜ組み込み開発に錆を使うのか?


    さびは、セキュリティと高性能を保証するように設計されています.デザイン哲学は埋め込まれた開発が必要とするものに正しい.
    組み込みソフトウェアは、ほとんどそのメモリによる問題を実行しています.錆の言語はコンパイラ指向言語と見なされるので、コンパイル中にメモリを安全に使用していることを確認できます.組み込みデバイスでの錆の利点について説明します.
  • 強力静的解析
  • フレキシブルメモリ
  • 大胆不敵な並行性
  • 相互運用性
  • 移植性
  • コミュニティ
  • さあ、オープンソースで入手しましょうRT-Thread operating system 組み込みの開発にどのように錆を使用できるかを示す.

    Cでさびを呼ぶ


    Cコードで錆コードを呼び出しているときには、ソースコードを静的ライブラリファイルとしてパッケージ化する必要があります.Cコードがコンパイルされたとき、それをリンクします.

    ライブラリを作る


    使用法cargo init --lib rust_to_c clionでlibライブラリをビルドします.次のコードをlibに追加します.以下の関数はI 32型の2つの値の和を評価し、結果を返します.
    #![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 .次のコードを貨物に追加します.どのようなタイプのライブラリを生成するかをRustCに伝えるために.
    [lib]
    name = "sum"
    crate-type = ["staticlib"]
    path = "src/lib.rs"
    

    クロスコンピレーション


    ARMv 7ターゲットをインストールする
       rustup target add armv7a-none-eabi
    
    2 .静的ライブラリファイルを生成する
       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
    

    ヘッダーファイルの生成


    インストールcbindgen , cbindgenは、RuustライブラリからC/C++ 11ヘッダファイルを生成します.
       cargo install --force cbindgen
    
    2 .新規作成cbindgen.toml プロジェクトフォルダの下にあるファイル
    3 .ヘッダファイルの生成
       cbindgen --config cbindgen.toml --crate rust_to_c --output sum.h
    

    ライブラリの呼び出し


    1 .生成するsum.h and sum.a ファイルをrt-thread\bsp\qemu-vexpress-a9\applications ディレクトリ
    2 . sconscriptファイルを変更し、静的ライブラリを追加します.
       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')
    
    主関数のsum関数を呼び出し、戻り値とprintf値を取得します.
       #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;
       }
    
    RTスレッドでEnv 環境、SCONSを使用してプロジェクトをコンパイルし、実行します.
          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 />
    

    追加、減算、乗算、除算


    1 .さびに複雑な数学を実装することができます.で.RSファイルは、付加言語を使用して、追加、減算、乗算、および分割を実装します.
         #![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 .ビルドライブラリファイルとヘッダファイルをビルドし、アプリケーションディレクトリに配置する
    3 .コンパイルにsconsを使用します.もしエラーがリンク上にジャンプした場合、そのソリューションを解決して下さい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 .変更rtconfig.py ファイルを追加します--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 = ''
    
    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
    

    さびの呼び出し


    Cコードでさびを呼ぶことができることを知っていて、RTRACK Kprintf関数をRustで呼び出すようにしましょう.

    を変更する。RSファイル


        // 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
        }
    

    ライブラリファイルの生成


        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
    

    ラン


    アプリケーションディレクトリにRustによって生成されたライブラリファイルをコピーします.
        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スレッドIOTのOSのグローバル技術会議は、登録のためのオープンです!我々はさまざまな国からの開発者を交換するアイデア、ショーケースプロジェクト、ソリューションを識別し、将来の戦略を議論し、組み込みの技術を探索するトピックの多種多様な相互学習機会を提供するために一緒に来る歓迎します。


    フリーレジスタ:https://forms.gle/8W1j2ZhCSLKFfyFQ8