By carefully controlling code in these blocks and minimizing their use, we can write safe firmware. This is confined to blocks marked as unsafe. In embedded, we often have to bypass Rust's ownership model to interact with hardware. None of the ownership features slow down your program while it’s running." From that docs page: "Memory is managed through a system of ownership with a set of rules that the compiler checks at compile time. Rust's ownership model is responsible for this. This forces you to write programs in a memory-safe way if you make a mistake, it's caught at compile time. Rust has a unique feature: It can run with the speed and fine control of manually managed, but can guarantee memory safety by using a borrow checker to validate references. Garbage-collected languages don't have this problem, but run comparatively slowly, and with inconsistent performance they're not suitable for embedded. This Wikipedia article provides a overview of this class of error, and specific examples. Languages with memory management run quickly, but care must be taken to prevent subtle errors. C and C++ are examples of manually managed, and Python, Go, and Java are examples of garbage-collected languages. Regarding memory management, most languages fall into one of 2 broad categories: Manually managed, and garbage-collected. (Other alternatives include ADA, SPARK, and Zig.) Advantages of using Rust fall into two broad categories: memory management, and ergonomics. At this time, Rust is one of few viable alternatives to C and C++ in these domains. It's one of a handful of languages capable of writing fast, low-level code: the type used to write drivers, operating systems, and embedded firmware. It's learned from mistakes of existing languages, and has drawn inspiration from other languages, including C, C++, OCaml, and Haskell. This is a simplistic outlook, but often works. If your project has a requirement that the tools used to build and flash your program be available in 10 or 20 years, C may be a better choice: It's long history (arguably) predicts a long future. The Lindy Effect: A phenomenon by which the future life expectancy of some things, like a technology or an idea, is proportional to their current age. Some libraries make heavy use of generics and other verbose syntax. This requires an understanding of the specific hardware capabilities and register layout, requires frequent reference manual (RM) checks, and makes switching MCUs more difficult. Depending on your MCU, you may need to write your own abstractions, or work at the register level. This is changing quickly: Support for Espressif/ESP-32 and RISC-V support are rapidly improving. This includes STM32, Nordic MCUs, and the Raspberry Pi Pico. The Rust embedded infrastructure is new, and rapidly changing.Ĭurrently Rust on embedded works best with ARM Cortex-M-based MCUs. Example code, tools, and libraries released by MCU manufacturers are almost always for C or C++. Hiring programmers proficient in Rust is much more difficult than in C or C++ due to the smaller user base. Rust compared to C and C++ Reasons not to use RustĬ is the industry standard for embedded programming, and most embedded jobs use C, with a minority using C++. For a categorized list of tools and libraries, check out our article on the topic. We wrote the Water Monitor firmware and Stove Thermometer using Rust, and plan to do the same for future devices, due to its memory safety and ergonomics.įor a guide on using Rust in low-level domains like embedded, targetted at C engineers, check out Learn Rust the Dangerous Way. This article's focus is on the architecture of practical firmware, and it describes tools and libraries that have been released after these books. These are great resources for getting started, and go into detail for experienced embedded engineers new to Rust, and people new to embedded respectively. Some topics overlap with the official Rust embedded and Rust embedded discovery books. Its goals: Describe why you should consider Rust for new projects, provide an overview of common libraries, and provide code samples that cover the most important parts. This doesn't intend to be a tutorial, or suitable learning resource on its own. It contains introductory information about embedded programming and Rust that you may already be familiar with you may wish to scroll down and skip to the code examples. This article explores Rust's capabilities on embedded, and provides a starting point for writing firmware with it. Rust is a modern language that's recently become a viable alternative. These combination hardware/software devices are called embedded devices, and their software is usually programmed in C or C++. Microcontrollers (MCUs) are ubiquitous in modern electronics.
0 Comments
Leave a Reply. |