docs.rust-embedded.org
Singletons In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. Wikipedia: Singleton Pattern But why can't we just use global variable(s)? We could make every...
The Embedded Rust Book
Book
2024-01-01
~3 min read
docs.rust-embedded.org
Static Guarantees Rust's type system prevents data races at compile time (see Send and Sync traits). The type system can also be used to check other properties at compile time; reducing the need for runtime checks in some cases. When applie...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Typestate Programming The concept of typestates describes the encoding of information about the current state of an object into the type of that object. Although this can sound a little arcane, if you have used the Builder Pattern in Rust,...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Peripherals as State Machines The peripherals of a microcontroller can be thought of as set of state machines. For example, the configuration of a simplified GPIO pin could be represented as the following tree of states: • Disabled • Enable...
The Embedded Rust Book
Book
2024-01-01
~2 min read
docs.rust-embedded.org
Design Contracts In our last chapter, we wrote an interface that didn't enforce design contracts. Let's take another look at our imaginary GPIO configuration register: | Name | Bit Number(s) | Value | Meaning | Notes | | ---: | ------------...
The Embedded Rust Book
Book
2024-01-01
~4 min read
docs.rust-embedded.org
Zero Cost Abstractions Type states are also an excellent example of Zero Cost Abstractions - the ability to move certain behaviors to compile time execution or analysis. These type states contain no actual data, and are instead used as mark...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Portability In embedded environments portability is a very important topic: Every vendor and even each family from a single manufacturer offers different peripherals and capabilities and similarly the ways to interact with the peripherals w...
The Embedded Rust Book
Book
2024-01-01
~3 min read
docs.rust-embedded.org
Concurrency Concurrency happens whenever different parts of your program might execute at different times or out of order. In an embedded context, this includes: • interrupt handlers, which run whenever the associated interrupt happens, • v...
The Embedded Rust Book
Book
2024-01-01
~18 min read
docs.rust-embedded.org
Collections Eventually you'll want to use dynamic data structures (AKA collections) in your program. std provides a set of common collections: Vec, String, HashMap, etc. All the collections implemented in std use a global dynamic memory all...
The Embedded Rust Book
Book
2024-01-01
~6 min read
docs.rust-embedded.org
Design Patterns This chapter aims to collect various useful design patterns for embedded Rust.
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
HAL Design Patterns This is a set of common and recommended patterns for writing hardware abstraction layers (HALs) for microcontrollers in Rust. These patterns are intended to be used in addition to the existing Rust API Guidelines when wr...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
HAL Design Patterns Checklist • Naming (crate aligns with Rust naming conventions) • [ ] The crate is named appropriately (C-CRATE-NAME) • Interoperability (crate interacts nicely with other library functionality) • [ ] Wrapper types provid...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Naming The crate is named appropriately (C-CRATE-NAME) HAL crates should be named after the chip or family of chips they aim to support. Their name should end with -hal to distinguish them from register access crates. The name should not co...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Interoperability Wrapper types provide a destructor method (C-FREE) Any non-Copy wrapper type provided by the HAL should provide a free method that consumes the wrapper and returns back the raw peripheral (and possibly other objects) it was...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Predictability Constructors are used instead of extension traits (C-CTOR) All peripherals to which the HAL adds functionality should be wrapped in a new type, even if no additional fields are required for that functionality. Extension trait...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
Recommendations for GPIO Interfaces Pin types are zero-sized by default (C-ZST-PIN) GPIO Interfaces exposed by the HAL should provide dedicated zero-sized types for each pin on every interface or port, resulting in a zero-cost GPIO abstract...
The Embedded Rust Book
Book
2024-01-01
~3 min read
docs.rust-embedded.org
Tips for embedded C developers This chapter collects a variety of tips that might be useful to experienced embedded C developers looking to start writing Rust. It will especially highlight how things you might already be used to in C are di...
The Embedded Rust Book
Book
2024-01-01
~11 min read
docs.rust-embedded.org
Interoperability Interoperability between Rust and C code is always dependent on transforming data between the two languages. For this purpose, there is a dedicated module in the stdlib called std::ffi. std::ffi provides type definitions fo...
The Embedded Rust Book
Book
2024-01-01
~1 min read
docs.rust-embedded.org
A little C with your Rust Using C or C++ inside of a Rust project consists of two major parts: • Wrapping the exposed C API for use with Rust • Building your C or C++ code to be integrated with the Rust code As C++ does not have a stable AB...
The Embedded Rust Book
Book
2024-01-01
~5 min read
docs.rust-embedded.org
A little Rust with your C Using Rust code inside a C or C++ project mostly consists of two parts. • Creating a C-friendly API in Rust • Embedding your Rust project into an external build system Apart from cargo and meson, most build systems...
The Embedded Rust Book
Book
2024-01-01
~2 min read