Eingebettete Systeme
programmieren mit Rust
Jens Siebert (@jens_siebert)
building IoT 2022, 10. Mai 2022
Über mich
• Senior Software Developer bei
doks.innovation in Kassel
• Drohnen-Steuerung, Computer
Vision, Architektur
• Maker, 3D-Drucker, Nerd
Hello... building IoT!
Infos zu Rust
• 2006 als privates Projekt des Entwicklers Graydon Hoare gestartet.
• Ab 2009 von seinem Arbeitgeber (Mozilla) unterstützt.
• Rust 1.0 am 15. Mai 2015.
• 2021 wurde die Rust Foundation gegründet.
• Sponsoren/Nutzer: Amazon (AWS), Microsoft, Google, Mozilla, uvm.
• "Most loved programming language" seit 2016 im Stack Overflow
Developer Survey.
• Stabile Releases alle 6 Wochen, alle 3 Jahre eine neue "Edition".
Besondere Merkmale von Rust
• Sicheres, automatisches Speichermanagement ohne Garbage Collector.
• Nebenläufigkeit ohne Data Races ("Fearless Concurrency").
• Abstraktionen mit möglichst geringem Overhead ("Zero Cost Abstractions").
• Effizientes Interface zu C/C++.
• Aussagekräftige Fehlermeldungen des Compilers.
Lifetimes, Ownership, Copy vs. Move
Referenzen, Borrowing
Veränderbare Referenzen
Structs & Traits
Structs & Traits
Fehlerbehandlung
• panic!
• Result-Datentyp
Nebenläufigkeit
Unsafe Code
Tools, Tools, Tools...
• rustup – Verwaltung der Rust-Installation
• rustc – Der Rust-Compiler
• cargo – Project-/Build-/Test-/Documentation-/Package-Management
• Knurling Tools
• probe-run – cargo runner zum Flashen und Ausführen von Anwendungen (z.B. über JTAG)
• defmt – Logging Framerwork
• flip-link – Stack Protection
• app-template – Vorlagen für eigene Anwendungen
• defmt-test – Test-Framework für On-Device-Tests
rustc – Target Platform Support Policy
• Tier 1: Continuous Integration checks that tier 1 targets will always
build and pass tests.
• Tier 2: Continuous Integration checks that tier 2 targets will always
build, but they may or may not pass tests.
• Tier 3: No guarantees about tier 3 targets; they exist in the codebase,
but may or may not build.
https://doc.rust-lang.org/rustc/target-tier-policy.html
Target Platform Support – Tier 1*
Target
aarch64-unknown-linux-gnu
i686-pc-windows-gnu
i686-pc-windows-msvc
i686-unknown-linux-gnu
x86_64-apple-darwin
x86_64-pc-windows-gnu
x86_64-pc-windows-msvc
x86_64-unknown-linux-gnu
https://doc.rust-lang.org/rustc/platform-support.html#tier-1-with-host-tools
* => Tier 1 with Host Tools
Target Platform Support – Tier 2
Target std
aarch64-unknown-none-softfloat *
aarch64-unknown-none *
armebv7r-none-eabi *
armebv7r-none-eabihf *
armv7a-none-eabi *
armv7r-none-eabi *
armv7r-none-eabihf *
riscv32i-unknown-none-elf *
riscv32imac-unknown-none-elf *
riscv32imc-unknown-none-elf *
riscv64gc-unknown-none-elf *
riscv64imac-unknown-none-elf *
thumbv6m-none-eabi *
thumbv7em-none-eabi *
thumbv7em-none-eabihf *
thumbv7m-none-eabi *
thumbv8m.base-none-eabi *
... ...
https://doc.rust-lang.org/rustc/platform-support.html#tier-2
std vs. no_std
Feature no_std std
heap (dynamic memory) * ✓
collections (Vec, HashMap, etc) ** ✓
stack overflow protection ✘ ✓
runs init code before main ✘ ✓
libstd available ✘ ✓
libcore available ✓ ✓
writing firmware, kernel, or
bootloader code
✓ ✘
* => Nur bei Verwendung des alloc Crates und eines passenden Allocators, wie z.B. alloc-cortex-m
** => Nur bei Verwendung der alloc oder heapless Crates
https://docs.rust-embedded.org/book/intro/no-std.html
Embedded Crates
Application
Board Support Crate Drivers
embedded-hal Crate
embedded-hal impl Crate Drivers
Peripheral Access Crate
Microcontroller
Architecture
Support
Crate
Embedded Crates (Beispiel)
Application
Board Support Crate (rp-pico) Drivers
embedded-hal Crate
embedded-hal impl Crate (rp-2040-hal) Drivers
Peripheral Access Crate (rp-2040-pac)
Microcontroller (Cortex-M0+)
Architecture
Support
Crate
(cortex-m)
Embedded HAL
embedded-hal Serial
I2C SPI
Timer
Watchdog ADC
GPIO
PWM
Beispiel-Projekt (Setup)
rustup update
rustup target add thumbv6m-none-eabi
cargo install elf2uf2-rs
cargo new biot22
Beispiel-Projekt (Layout)
Konfiguration für Build-Tool (Target, Runner, etc.)
Source-Code
Projektinformationen und Abhängigkeiten
.cargo/config für Raspberry Pi Pico
Cargo.toml für Raspberry Pi Pico
Minimale main.rs für Raspberry Pi Pico
Blinky-Projekt (Importe, main.rs)
Blinky-Projekt (Initialisierung, main.rs)
Blinky-Projekt (Pin-Config und blinken, main.rs)
Ergebnis
Fazit
Rust als Sprache für eingebettete Systeme bietet:
• Hohe Performance, geringer Speicherverbrauch.
• Sicheres Speichermanagement.
• Sichere Nebenläufigkeit.
• Viele unterstützte Architekturen (ARM, RISC-V, etc.), Microcontroller und Boards.
• Viele Treiber für externe Hardware bereits vorhanden.
• Komfortables Tooling, welches etablierten Sprachen und Frameworks in nichts nachsteht.
• Teilweise steile Lernkurve (aber es lohnt sich!)
• Freundliche und hilfsbereite Community.
• Open Source (Bitte unterstützen!).
Vielen Dank!
https://www.rust-lang.org/learn
https://docs.rust-embedded.org/discovery/
https://docs.rust-embedded.org/book/
https://github.com/rust-embedded/awesome-embedded-rust
https://knurling.ferrous-systems.com/tools/
Twitter: @jens_siebert

Embedded Rust