From Rust to Riches: Will Embedded Applications Ditch C++ for a Brighter Future?
From Rust to Riches: Will Embedded Applications Ditch C++ for a Brighter Future?
Welcome to the inaugural blog of our exciting series, where we explore technology and innovation alongside our Partners. In this edition, in collaboration with Tronel, we delve into the captivating realm of Embedded Systems. Join us on an anticipatory journey as we predict the future and unveil emerging possibilities within this field.
On the xBerry front, expert Adam Prochownik dives deep into the transformative power of Rust and its predicted impact on the embedded world. If you’re intrigued by his insights, this is a must-read!
The Rise of Rust in Embedded Systems
Rust, a modern programming language developed by Mozilla, has been gaining impressive traction in the past few years. Known for its high-performance capabilities and memory safety features without the need for garbage collection, Rust is proving itself to be an attractive option for various domains, including system programming, game development, and most notably, embedded systems. As Rust matures, it is becoming increasingly stable, offering a viable alternative to traditional languages like C and C++.
Rust’s Notable Integration
Rust’s journey to fame recently took a significant leap forward as it became integrated into some of the most prominent open-source projects in the world. The Linux kernel, known for its dominance in servers and supercomputers, is beginning to incorporate Rust into its codebase. This initiative aims to benefit from Rust’s safety principles, helping to mitigate common bugs that have long plagued the C-based kernel.
In addition, the GStreamer multimedia framework and the Chromium browser engine, powering popular browsers such as Google Chrome, Microsoft Edge, and Opera, are also starting to incorporate Rust. This demonstrates Rust’s versatility and potential to enhance the stability and performance of these popular platforms.
Rust as a Secure Alternative
The National Security Agency (NSA) released a report with recommendations to avoid C and C++ due to lack of built-in memory safety features. Following this, Microsoft decided to promote use of rust instead of C++ in greenfield projects, sparking a significant shift in the software development industry. The software giant opted for Rust, embracing it as the language of choice for system-level tasks that require a high degree of control over system resources but do not necessitate garbage collection.
This decision is indicative of a broader industry trend. As cybersecurity becomes an increasingly critical concern, Rust’s memory safety features and focus on preventing out of bound access and data races make it an appealing choice.
Thriving Embedded Rust Community
Over the past few years, the Rust community focused on embedded systems has flourished. A multitude of projects has sprung up with the goal of writing high-performance, secure code for bare-metal systems. This emerging trend demonstrates Rust’s promise for embedded systems development, where resource constraints and the need for direct hardware access and control are paramount.
Rust’s “zero-cost abstractions” allow it to provide a high-level syntax similar to languages like Python and Ruby, while still allowing low-level control of the system akin to C or C++. This blend of convenience and control is instrumental for the embedded community, enabling developers to write efficient, secure code that can be readily understood and maintained.
Rust for IoT Solutions
The rise of the Internet of Things (IoT) has necessitated a programming language that can efficiently handle the diverse demands of IoT development, ranging from low-level hardware access for edge devices to high-level server processing in the cloud. With its versatility, Rust is proving to be an excellent choice for this purpose.
Notably, Rust can be used to write both the backend and frontend of software applications. This uniformity can be advantageous in the context of IoT, where seamless integration across multiple layers of technology stacks is essential. The use of a single language across an IoT system can streamline the development process and enhance the maintainability of the codebase.
The Expanding Toolset for Rust in IoT
The burgeoning popularity of Rust in IoT development has led to an abundance of tools specifically designed to facilitate the creation of IoT solutions using Rust. Some of these tools include the no_std support for running Rust in bare metal environments without the standard library, and cargo-embed for flashing and debugging microcontroller-based Rust applications.
Libraries like embedded-hal abstract common hardware functionalities, while RTFM (Real-Time For the Masses) provides a framework for building reliable, high-performance real-time systems.
Moreover, the Rust ecosystem is further expanding with the development of WebAssembly (Wasm), enabling Rust applications to run in the browser and opening up new possibilities for frontend IoT development.
Auto-generating Hardware Abstraction Layer from SVD
Another advantage Rust brings to the table in embedded systems development is the ability to auto-generate Hardware Abstraction Layers (HALs) from System View Description (SVD) files. SVD files describe the peripherals of a microcontroller and are typically provided by the silicon vendor. This feature is a significant boon for developers, as it saves a considerable amount of time that would otherwise be spent on manually defining peripherals. Importantly, the use of SVD files has led to Rust supporting a vast variety of microcontrollers. If a particular microcontroller isn’t already supported, the flexible nature of the SVD to Rust translation process means that end users can usually add the required support themselves with relative ease. This broadens the applicability of Rust in the embedded systems domain, accommodating a wide range of hardware requirements and project specifications.
Embassy: An Async Runtime for Bare Metal Systems
When it comes to handling multiple tasks in embedded systems, Rust brings yet another game-changing tool to the field: Embassy. Embassy is a Rust async runtime specially designed for bare metal systems, bringing the power of async programming to embedded development.
Async programming can be a powerful technique for managing complex concurrent behaviors typically found in embedded systems, such as handling multiple sensor inputs or managing communication interfaces. However, it’s often challenging to implement correctly in low-level languages traditionally used for embedded systems, like C or C++.
Historically, in the absence of async programming, embedded systems developers have primarily relied on two approaches to manage multiple tasks.
The first approach is to use a Real-Time Operating System (RTOS) with threads. Each thread would be assigned a task, running independently of the others. While this method allows tasks to be executed concurrently, it requires developers to handle communication between threads manually. This inter-thread communication often involves synchronization primitives such as semaphores, locks, or condition variables, which can introduce complexity and risk of errors such as deadlocks or race conditions. Additionally, this approach adds substantial overhead, as each thread needs its stack, and the system has to manage context switches, further consuming resources.
The second approach involves crafting a main execution queue or a “superloop” by hand. Here, all tasks are managed in a single loop within the main function, with each task checked and serviced sequentially in every loop iteration. While this eliminates the need for complex thread synchronization, it introduces another set of problems. The code can become challenging to manage as the number of tasks increases, and lengthy tasks risk blocking the entire system, as other tasks cannot proceed until the current one is complete. The resulting code is often hard to maintain and reason about, especially in more complex systems.
Embassy leverages Rust’s async/await feature, allowing developers to write non-blocking code that’s easy to read and reason about, offloading the process of manually creating the main loop to the compiler. It provides the embedded systems community with a solution for writing concurrent code in a more straightforward and less error-prone way. This powerful combination of async programming and Rust’s safety features significantly simplifies the process of developing sophisticated, real-time behaviors on embedded systems.
Rust’s rising popularity, its increasing stability, and its embrace by significant industry players mark an exciting era for this relatively young programming language. It demonstrates a significant shift in the software industry towards prioritizing security, performance, and development convenience. Given these advantages, Rust’s footprint in the realm of embedded systems, and particularly within IoT solutions, is likely to expand even further in the years to come.
Feeling inspired? Join us as we navigate the ever-evolving landscape of embedded systems, unlocking new horizons and revealing the extraordinary synergy between Tronel and xBerry. Head to the Tronel blog to discover the gripping “Why C++ Programming Continues to Thrive in Embedded Systems” article, a perfect complement to expand your knowledge and fuel your technological inspiration. Visit the Tronel blog now and embark on a remarkable journey that will leave you craving for more.