Rust Embedded

BMR

Bare-Metal Rust

A practical course for computer science and systems engineering students


Computing (or the "cyber-world") is becoming more and more ubiquitous and intertwined with our physical world—leading to tiny, resource-constrained devices that form the Internet-of-Things. Programming these systems is quite different than programming desktop or web applications. As they do not come with an operating system or other conveniences, they are often refered to as "bare-metal". This is a book developed to teach "Bare-Metal Rust" (BMR), a course designed to introduce computer science and systems engineering students to two things: a) foundations of embedded, bare-metal systems (or cyber-physical systems) and b) the "Embedded Rust" ecosystem.

Author

Andreas Schmidt, @netzdoktor

Contributors

TbA

License

© Copyright 2022 – 2023 Andreas Schmidt

Changelog

Currently rendered version: {{ version }}.

Format:
  • CalVer (youtube-dl style)
  • COMMITS: No. of commits since last tag
  • HASH: current Git commit hash

2022-04-16

Initial version.

Units

U00: Down to the Metal

    ____  _____               ______          __    __  __
   / __ \/ ___/__  _______   / ____/___ ___  / /_  / / / /
  / / / /\__ \/ / / / ___/  / / __/ __ `__ \/ __ \/ /_/ /
 / /_/ /___/ / /_/ (__  )  / /_/ / / / / / / /_/ / __  /
/_____//____/\__, /____/   \____/_/ /_/ /_/_.___/_/ /_/
            /____/

Welcome back at DSys GmbH, we are happy to have you as a new embedded engineer.

Cyber-Physical Systems vs. Embedded Systems

CPS is the newer term (2006), Embedded has been around for a while.

Core Practical & Academic Challenge: "Passing of time is inexorable". [Lee08]

We talk about safety-critical systems, when they impose risk of loss.
loss related to human health, property, environment

As of today, development of embedded / CPS is quite unalike development of, e.g., desktop / web applications, partly due to differences in...

  • Requirements
    time-awareness, fail-safe, operation in adverse environments, constrained resources, focus on sense/control/act over compute, ...
  • Methodology
    V-model, verification & validation, regulation, certification, ...
  • Ecosystem
    proprietary platforms & tools, closed source, distribution of compiled artifacts, ...

Why Embedded Rust?

Considering the existing embedded ecosystems, C(++) would be a better choice, right?

Embedded Rust aims to be...

  • Safe: Thread-, and Memory-Safety, enforce correct API usage via type-system
    embedded-hal, rtic, embassy, uom, ...

  • Productive: ergonomic frameworks as well as tools
    probe-rs, defmt, ...

  • Open: many common crates provided under permissive license via crates.io
    ."everything" presented today should be under a permissive license

Rust: A language empowering everyone to build reliable and efficient software...

... for embedded / cyber-physical systems.

An Abstract System Map

largely simplified — real systems are orders of magnitude more complicated


Latency-, Reliability-, and Energy-Awareness

Dependability

Hands-On

The Hardware

Use https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 7

Setup

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 26

  • knurling tools
  • probe-rs

knurling-rs

Designed to "Get a handle on bare-metal Rust".

  • probe-run
    • Install
    • .cargo/config
  • defmt

Minimal Application

First, we build a minimal application and setup our project for this. We start with a new Rust project. The Cargo.toml should look like this:

[package]
name = "embedded_world"
version = "0.1.0"
edition = "2021"

[dependencies]
cortex-m = "=0.6.7"
cortex-m-rt = "=0.7.1"
nrf52840-hal = "0.14.0"

Hence, crate has the following dependencies:

  • cortex-m crate provides an API to talk with the Cortex-M processor.
  • cortex-m-rt is a runtime crate, allowing us to write an application.
  • nrf52840-hal is a hardware-abstraction layer (HAL) of the board we use (Board Support Package, BSP).

As we are writing an application, we also write a main.rs:

#![no_std]
#![no_main]

use core::fmt::Write;
use hal::{gpio, uarte, uarte::Uarte};
use nrf52840_hal as hal;

#[cortex_m_rt::entry]
fn main() -> ! {
    let mut core = cortex_m::peripheral::Peripherals::take().unwrap();
    core.DWT.enable_cycle_counter();

    let p = hal::pac::Peripherals::take().unwrap();

    let (uart0, cdc_pins) = {
        let p0 = gpio::p0::Parts::new(p.P0);
        (
            p.UARTE0,
            uarte::Pins {
                txd: p0.p0_06.into_push_pull_output(gpio::Level::High).degrade(),
                rxd: p0.p0_08.into_floating_input().degrade(),
                cts: None,
                rts: None,
            },
        )
    };
    let mut uarte = Uarte::new(
        uart0,
        cdc_pins,
        uarte::Parity::EXCLUDED,
        uarte::Baudrate::BAUD115200,
    );

    write!(uarte, "Hello Embedded World\r\n").unwrap();

    loop {
        cortex_m::asm::wfi();
    }
}

#[panic_handler] // panicking behavior
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {
        cortex_m::asm::bkpt();
    }
}

Hardware

Which one?

  • nRF52840: Development Kit is expensive
  • ESP32: what does the DK cost?
  • RP2040: DK available and what does it cost?
  • Calliope Mini / MicroBit

Docker

Renode

At work, you use Renode to develop IoT products.

Download Renode here.

Afterwards, use the following Renode Script (run.resc)

emulation CreateServerSocketTerminal 3456 "term"
mach create "hello-node"
machine LoadPlatformDescription @platforms/cpus/nrf52840.repl
connector Connect sysbus.uart0 term
sysbus LoadELF @target/thumbv7em-none-eabihf/release/embedded_world
showAnalyzer sysbus.uart0
s

Start the script via:

cargo build --release # create the binary
renode -e "s @run.resc" # run the script

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 01 + 03
  • Rust Embedded Books ???

W00: Work Sheet

TODO

S00: Sample Solution

TODO

U01: Applications without an Operating System

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 9 - 10

#![no_std] Land

Formatting with defmt

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 11 - 12

BSPs, PACs, HALs

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 15

probe-rs

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 02 + 03
  • Rust Embedded Books ???

U02: Advanced Embedded Applications

  • UART / Debugging Interface
  • LEDs
  • Buttons
  • Clock

Concurrency

RTIC https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 16 - 18,

Embassy 24 - 25

Data Structures

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 29

Serialization

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 30 - 32

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 04, 05, 06
  • Rust Embedded Books ???

U03: Dependability Engineering

Typestate & State Machines

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 19 - 20

Dimensional Analysis

https://gitlab.com/rust-saar/material/-/blob/main/2022_01_11/02-Embedded-Rust.pdf Slide 28

Fault Trees

  • TODO

Model Checking

  • TODO

Summary

U04: Control Engineering

Control Theory

Feedback Control for Computer Systems:

  • Ch. 1 - 6
  • Ch 12 as intro to use case
  • Ch 17 as relevant use case "cooling fan speed

Embedded Systems Course by Martina Maggio

Rust

  • nalgebra Matrices

U05: Network Engineering

  • Information Theory
  • Bluetooth
  • Bits & Bytes in Protocols
  • ProfiNET
  • MQTT/ROS2: Pub-Sub

U06: Advanced Concurrency

U07: Advanced Control Engineering

U08: Optimizations

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 08, 10
  • Rust Embedded Books ???

U09: Writing Drivers

  • Svd2rust
  • Writing an unsafe Driver

U10: Real-Time Operating Systems

Zephyr

  • Striving for functional safety verification.

microROS

  • TODO

U11: Embedded Machine Learning

Deep Learning Basics and Use Case

  • TODO: Use Case

Embedded Math

  • Common Algorithms
  • Working with low amounts of RAM

TensorFlow

  • TODO

TensorFlow Lite

  • TODO

TensorFlow Micro

  • TODO

AI4ES

  • TODO

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 09
  • Rust Embedded Books ???

U12: Advanced Network Engineering

U13: Advanced Dependability Engineering

Reliability / Availability

Correct by Construction

  • TODO

Code Generation

  • syn, quote, proc-macro

const

  • TODO

Energy-Awareness

  • TODO

U14: Continuous Bare-Metal

Git + GitLab

Test-First Development

Renode Simulated Tests

Hardware-in-Loop Tests

  • defmt-test

Summary

What did you learn?

  • TODO

Where can you learn more?

  • Making Embedded Systems: Ch. 07
  • Rust Embedded Books ???

Projects

P01: EmbeddedWorld

  • Write a minimal #![no_std] application that runs in Renode and outputs "Hello Embedded World" on UART.

P02

  • Deploy P02 to a physical chip.

P03

  • Implement a Simple Control Loop

P04

  • Implement a Networked Control Loop

P05

  • Implement a Concurrent Control Loop w/ Manual Intervention

Colophon

This course has been inspired by:

The course material is created using open tools:

The course material has been regularly checked with Grammarly.

Glossary

TODO