Ferrous Systems Rust Training

repository·main·Indexed 18 days ago

https://github.com/ferrous-systems/rust-training

Free, high-quality workshop material for developers ranging from beginners to advanced Rust users. The repository includes lessons organized as an mdbook and reveal.js slides, featuring example code for standard output methods (println!, writeln!, write_fmt, write_str), C library imports, raw syscalls, and no-std environments. It also provides bare-metal demos for Aarch32, AArch64, RISC-V, and Armv7E-M architectures using QEMU and the Ferrocene toolchain.

Tokens
98.8K
Snippets
358
Records
485
Agent score
62%

What's inside ferrous-systems-rust-training

  1. Overview of the Rust standard library (libstd)

    main

    The Rust standard library (libstd) provides essential primitives for high-productivity development:

    • Filesystem & I/O: Path handling, filesystem access, and I/O traits for files, strings, and sockets.
    • Concurrency: Threads, Mutexes, Condition Variables, and Channels.
    • Data Structures: Growable arrays (Vec), hash-tables, B-Trees, and Strings.
    • Text: First-class Unicode support.
    • Networking: IPv4/IPv6, TCP, and UDP support.
    • System: Heap allocation, environment variables, CLI arguments, and time handling (Duration, Instant).
  2. Overview of AArch64 demo binaries

    main

    The demo contains five specific binaries located in ./src/bin designed to run in a QEMU-emulated AArch64 Arm Cortex-A system. All binaries utilize defmt for logging.

    • defmt: Prints defmt logs at various levels.
    • global_uart: Sets up a UART as a global variable and prints to it.
    • panic: Demonstrates panic handling behavior.
    • uart: Prints to the first available UART.
    • with_heap: Demonstrates heap allocation by setting up a heap allocator and using the format! macro to generate and print heap-allocated strings.
  3. What is Rust?

    main

    Rust is a free and open-source systems programming language designed to empower developers to build reliable and efficient software. It focuses on three core pillars:

    1. Safety: Preventing common programming errors.
    2. Performance: Providing high-speed execution comparable to C/C++.
    3. Productivity: Offering modern tooling and developer ergonomics.

    Rust is highly portable and cross-platform, supporting Windows, macOS, Linux, iOS, Android, WebAssembly, and bare-metal embedded systems. It can also import and export C-compatible libraries, making it suitable for integrating into existing C/C++ codebases or creating extensions for languages like Python.

    fn main() {
        println!("Hello, world!");
    }
  4. Understand training terminology

    main

    The following terms are used to describe the structure and components of the Ferrous Systems Rust training program:

    • Training half-day: A 4-hour block of training.
    • Training day: An 8-hour block of training (applicable to non-remote trainings).
    • Lesson: A single set of slides covering a specific topic.
    • Session: A block of content occurring between breaks.
    • Module: A block of consecutive sessions focused on a fixed set of subjects; modules can vary in length.
    • Training: The complete program, consisting of various modules over a series of days or half-days.
    • Opening: The first 15 minutes of a training day or half-day, used for ice-breakers, recaps, and presenting the day's plan.
    • Wash-up: The final 15 minutes of a training day or half-day, used for recaps, open questions, and looking ahead to the next day.
    • Ice Breakers: Brief warm-up activities (usually short questions) to start the training.
    • Quizzes: Mini-tests used to assess understanding of the training material.

    External Resources:

  5. Understand what Ferrocene is

    main

    Ferrocene is a qualified version of the Rust toolchain designed for safety-critical applications. Unlike a subset of Rust, it is a downstream of The Rust Project that uses the actual Rust language.

    Key characteristics include:

    • Qualified: Certified per ISO 26262 (ASIL D) and IEC 61508 (SIL 4).
    • Open Source: Licensed under MIT or Apache-2.0.
    • Long-term Stable: Provides stable releases with support and tracking of known problems.
    • Warranty & Support: Includes a warranty for compiler bugs and access to support/binary downloads via subscription.
    • Independent Testing: Uses a separate, parallel CI pipeline to the Rust Project to produce artifacts required for qualification.
  6. Use mdbook-stripelineno to fix code block line numbering in mdbook

    main

    The mdbook-stripelineno pre-processor is used to convert code block syntax from a format compatible with reveal.js (which includes line ranges like [1|2-3|4]) into a format compatible with mdbook.

    Specifically, it converts syntax like:

    ...

    into the standard mdbook format:

    ...

    This ensures that line-numbering metadata intended for slides does not break the rendering of code blocks in mdbook.

    // Input format (reveal.js style)
    ```rust no_run [1|2-3|4]
    fn example() {}
    ```
    
    // Output format (mdbook compatible)
    ```rust,no_run
    fn example() {}
    ```
  7. Explore Ferrous Systems' Rust Training Modules

    main

    The training consists of several modules and stand-alone courses. Some modules require prerequisite knowledge from others.

    Stand-alone Courses

    • Why Rust?: A half-day tour of Rust for decision-makers, technical leads, and managers.
    • Why Ferrocene?: A 60-minute introduction to Ferrocene.

    Core Modules and Dependencies

    • Rust Fundamentals: Basics including types, functions, and iterators.
    • Applied Rust: Using Rust on Windows, macOS, or Linux. (Prerequisite for Advanced, No-Std, Bare-Metal, Async, and Wasm).
    • Advanced Rust: Deep-dives into specific topics.
    • No-Std Rust: Rust without the Standard Library. (Prerequisite for Ferrocene).
    • Bare-Metal Rust: Rust on a microcontroller. (Prerequisite for Embassy).
    • Async Rust: Covers Futures, Polling, Tokio, etc. (Prerequisite for Embassy).
    • Rust and WebAssembly: Building WASM binaries for sandboxes or HTML pages.
    • Ferrocene: Working with the qualified toolchain.
    • Using Embassy: Async-Rust on a microcontroller.
  8. What is defmt and how does it work?

    main

    defmt (Deferred Formatter) is a logging framework designed for microcontrollers. Unlike classical logging which stores format strings in Flash and transmits them over UART, defmt interns strings into a .defmt section within the ELF file.

    Key mechanics:

    • Strings: Stored in the ELF file on your host machine, not in the microcontroller's Flash.
    • Data: Arguments are packed in a compact binary format and transmitted over the wire.
    • Reconstruction: A host-side tool uses the exact ELF file used during compilation to reconstruct the log messages.

    Benefits:

    • Significantly less Flash usage.
    • Reduced data transfer requirements.

    Requirements:

    • You must use a compatible host-side viewer tool.
    • You must provide the exact ELF file that was used to build the binary running on the chip.
  9. What is Unsafe Rust and what can it do?

    main

    Unsafe Rust allows you to perform operations that the standard Rust type system cannot guarantee are safe. This is necessary for low-level memory manipulation or interfacing with external code.

    Unsafe code is permitted to:

    • Freely access memory.
    • Dereference raw pointers.
    • Call external functions (FFI).
    • Declare values as Send and Sync.
    • Write to unsynced global variables.

    The following are NOT considered unsafe:

    • Conversion to raw pointers.
    • Memory leaks.
  10. What is an Iterator and how does it work?

    main

    An Iterator is an object that produces a sequence of items one at a time by implementing a .next() method. This method returns Some(data) for each item and None once the sequence is exhausted.

    Key characteristics:

    • Stateful: The iterator object holds the current state of the iteration.
    • Lazy: Iterators do not perform work until they are actually polled (e.g., in a loop or by a consumer method).
    • On-the-fly calculation: Iterators can derive values from a collection, calculate items on-the-fly, or wrap and transform other iterators.
  11. What is a Peripheral Access Crate (PAC)?

    main

    A Peripheral Access Crate (PAC) sits at the bottom of the hardware abstraction stack. It provides low-level access to the memory-mapped peripherals of a Microcontroller Unit (MCU).

    Key concepts:

    • Memory Mapped Peripherals: Hardware blocks (like a UART/serial port) that are accessed via specific memory addresses.
    • Registers: The interface to peripherals, comprised of one or more bitfields. The specific layout and bitfield values are defined in the MCU's datasheet.
    • Instances: Multiple peripherals may share the same register layout but exist at different base addresses (e.g., UART0, UART1).