Jens Siebert, IoT Hessen Meetup (Kassel), 23.11.2023
Embedded Rust
About me
• Senior Software Architect @ CARIAD
• Safety-critical Automotive Software for Pulse Inverters
• Maker, 3D Printing, (Programming Language) Nerd
Hello… IoT Hessen Meetup!
About Rust
• Started in 2006 as a private project of Graydon Hoare.
• Supported and adopted by Mozilla from 2009.
• Released as 1.0 on May 15th 2015.
• Rust Foundation established in 2021.
• Sponsors and Users: Amazon (AWS), Microsoft, Google, Mozilla, Meta, etc.
• „Most loved programming language“ in Stack Over
fl
ow Developer Survey since 2016.
• Stable release every 6 weeks, a new „edition“ (roughly) every 3 years.
Characteristics of Rust
• Multi-paradigm system-level programming language
(high-level ergonomics, low-level control).
• Compiled to binary code (no interpreter, JIT compiler, etc.).
• Automatic memory management without a garbage collector
(“Fearless Memory Management“).
• Concurrency without data races (“Fearless Concurrency“).
• Abstractions with minimal overhead (“Zero Cost Abstractions“).
• E
ffi
cient interfacing to C/C++.
• Expressive and helpful error messages of the compiler.
Ownership
Borrowing
Mutable References
Structs & Traits
Structs & Traits
Enumerations & Pattern Matching
Enumerations & Pattern Matching
Error Handling (panic!, Result Datatype)
Concurrency
Unsafe Code
Crates
cargo add rp-pico
Tools, Tools, Tools…
• rustup — Management of the Rust Installation.
• rustc — The Rust Compiler.
• cargo — Project/Build/Test/Documentation/Package Management
• Knurling Tools
• probe-run — cargo runner for
fl
ashing and execution of embedded applications (e.g. using JTAG)
• defmt — Logging Framework
• flip-link — Stack Protection
• app-template — Project templates for embedded applications
• defmt-test — Framework for 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-soft
fl
oat *
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
* => indicates the target only supports no_std development
std vs. no_std
Feature no_std std
Heap (dynamic memory) * ✓
Collections (Vec, HashMap, etc.) ** ✓
Stack over
fl
ow protection ✗ ✓
Runs init code before main ✗ ✓
libstd available ✗ ✓
libcore available ✓ ✓
Writing
fi
rmware, kernel or bootloader code ✓ ✗
https://docs.rust-embedded.org/book/intro/no-std.html
** => Only if you use the collections crate and con
fi
gure a global default allocator
* => Only if you use the alloc crate and use a suitable allocator like alloc-cortex-m
Embedded Crates
Application
Board Support Crate Drivers
Drivers
embedded-hal Crate
embedded-hal impl Crate
Peripheral Access Crate
Microcontroller
Architecture
Support
Crate
Embedded Crates (Raspberry Pi Pico)
Application
Board Support Crate (rp-pico) Drivers
Drivers
embedded-hal Crate
embedded-hal impl Crate (rp-2040-hal)
Peripheral Access Crate (rp-2040-pac)
Microcontroller (Cortex-M0+)
Architecture
Support
Crate
(cortex-m)
Embedded HAL
I2C
embedded-hal
SPI GPIO
Serial
Timer
Watchdog PWM ADC
Example (Blinky on Raspberry Pi Pico, Setup)
rustup target add thumbv6m-none-eabi
cargo install elf2uf2-rs
cargo new embedded-rust
Project Layout
Build Tool Con
fi
guration (Target, Runner, etc.)
Source Code
Project Con
fi
guration and Dependencies
.cargo/config for Raspberry Pi Pico
Cargo.toml for Raspberry Pi Pico
Minimal main.rs for Raspberry Pi Pico
Blinky Project (Imports, main.rs)
Blinky Project (Initialization, main.rs)
Blinky Project (Pin Config and blink, main.rs)
Blinky Project (Result)
Conclusion
Rust as a language for embedded systems:
• High performance, low memory consumption.
• Safe memory management.
• Safe concurrency.
• Many supported architectures (ARM, RISC-V, etc.), controllers and boards.
• Lots of drivers for external hardware available already.
• Steep learning curve (to some extend, but it's worth it!)
• Friendly, helpful and welcoming community.
• It’s open source! (please support!!!)
Thank you!
www.rust-lang.org/learn
docs.rust-embedded.org/discovery/
docs.rust-embedded.org/book/
github.com/rust-embedded/awesome-embedded-rust
knurling.ferrous-systems.com/tools/
Github: github.com/jens-siebert
SlideShare: slideshare.net/JensSiebert1
LinkedIn: www.linkedin.com/in/jenssiebert/
Mastodon: mastodon.online/@jens_siebert

Embedded Rust

  • 1.
    Jens Siebert, IoTHessen Meetup (Kassel), 23.11.2023 Embedded Rust
  • 2.
    About me • SeniorSoftware Architect @ CARIAD • Safety-critical Automotive Software for Pulse Inverters • Maker, 3D Printing, (Programming Language) Nerd
  • 3.
  • 4.
    About Rust • Startedin 2006 as a private project of Graydon Hoare. • Supported and adopted by Mozilla from 2009. • Released as 1.0 on May 15th 2015. • Rust Foundation established in 2021. • Sponsors and Users: Amazon (AWS), Microsoft, Google, Mozilla, Meta, etc. • „Most loved programming language“ in Stack Over fl ow Developer Survey since 2016. • Stable release every 6 weeks, a new „edition“ (roughly) every 3 years.
  • 5.
    Characteristics of Rust •Multi-paradigm system-level programming language (high-level ergonomics, low-level control). • Compiled to binary code (no interpreter, JIT compiler, etc.). • Automatic memory management without a garbage collector (“Fearless Memory Management“). • Concurrency without data races (“Fearless Concurrency“). • Abstractions with minimal overhead (“Zero Cost Abstractions“). • E ffi cient interfacing to C/C++. • Expressive and helpful error messages of the compiler.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    Error Handling (panic!,Result Datatype)
  • 14.
  • 15.
  • 16.
  • 17.
    Tools, Tools, Tools… •rustup — Management of the Rust Installation. • rustc — The Rust Compiler. • cargo — Project/Build/Test/Documentation/Package Management • Knurling Tools • probe-run — cargo runner for fl ashing and execution of embedded applications (e.g. using JTAG) • defmt — Logging Framework • flip-link — Stack Protection • app-template — Project templates for embedded applications • defmt-test — Framework for on-device tests
  • 18.
    rustc — TargetPlatform 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
  • 19.
    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
  • 20.
    Target Platform Support— Tier 2 Target std aarch64-unknown-none-soft fl oat * 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 * => indicates the target only supports no_std development
  • 21.
    std vs. no_std Featureno_std std Heap (dynamic memory) * ✓ Collections (Vec, HashMap, etc.) ** ✓ Stack over fl ow protection ✗ ✓ Runs init code before main ✗ ✓ libstd available ✗ ✓ libcore available ✓ ✓ Writing fi rmware, kernel or bootloader code ✓ ✗ https://docs.rust-embedded.org/book/intro/no-std.html ** => Only if you use the collections crate and con fi gure a global default allocator * => Only if you use the alloc crate and use a suitable allocator like alloc-cortex-m
  • 22.
    Embedded Crates Application Board SupportCrate Drivers Drivers embedded-hal Crate embedded-hal impl Crate Peripheral Access Crate Microcontroller Architecture Support Crate
  • 23.
    Embedded Crates (RaspberryPi Pico) Application Board Support Crate (rp-pico) Drivers Drivers embedded-hal Crate embedded-hal impl Crate (rp-2040-hal) Peripheral Access Crate (rp-2040-pac) Microcontroller (Cortex-M0+) Architecture Support Crate (cortex-m)
  • 24.
  • 25.
    Example (Blinky onRaspberry Pi Pico, Setup) rustup target add thumbv6m-none-eabi cargo install elf2uf2-rs cargo new embedded-rust
  • 26.
    Project Layout Build ToolCon fi guration (Target, Runner, etc.) Source Code Project Con fi guration and Dependencies
  • 27.
  • 28.
  • 29.
    Minimal main.rs forRaspberry Pi Pico
  • 30.
  • 31.
  • 32.
    Blinky Project (PinConfig and blink, main.rs)
  • 33.
  • 34.
    Conclusion Rust as alanguage for embedded systems: • High performance, low memory consumption. • Safe memory management. • Safe concurrency. • Many supported architectures (ARM, RISC-V, etc.), controllers and boards. • Lots of drivers for external hardware available already. • Steep learning curve (to some extend, but it's worth it!) • Friendly, helpful and welcoming community. • It’s open source! (please support!!!)
  • 35.