Rust on Microcontrollers, or Embedded Rust

One of the more interesting aspects of Rust is that it runs everywhere: on the server, the desktop, the web and even on microcontrollers. Rust is a great language for embedded development because at its core it is a systems programming language that provides a lot of control over the hardware, while still being safe and expressive.

Over the years, the support for embedded development in Rust has grown significantly. There are now a number of libraries and frameworks that make it easier to develop embedded applications in Rust. In this chapter, we'll be looking at some basics of embedded development in Rust, and we'll be building a few projects to get a feel for how Rust can be used in embedded systems.

Embedded Rust

We'll be using the Embassy framework for this chapter. Embassy is a lightweight async/await runtime for embedded systems. It has many features that backend developers are familiar with, like tasks, timers, and channels. It's a great way to get started with embedded development in Rust.

Microcontrollers

A microcontroller is a small computer on a single integrated circuit. It contains a processor core, memory, and peripheral devices. Microcontrollers are used in embedded systems, which are systems that are designed to perform a specific task. They are found in a wide range of devices, from microwave ovens to cars to industrial machinery.

We'll be using three different microcontrollers in this chapter:

  • Raspberry Pi Pico,
  • STM32F103 (Blue Pill),
  • STM32F401 (Black Pill).

The projects we'll be building are straightforward, but they should give you a good idea of how to get started with embedded development in Rust. You should be able to combine the knowledge you gain from these projects with the documentation of the microcontroller you are using to build more complex projects.

Raspberry Pi Pico

The Raspberry Pi Pico is a microcontroller board based on the RP2040 microcontroller chip. It has a dual-core ARM Cortex-M0+ processor, 264KB of SRAM, and 2MB of flash memory. The Pico is a great board for beginners because it is inexpensive and easy to use.

STM32F103 (Blue Pill)

The STM32F103 (Blue Pill) is a development board based on the STM32F103C8T6 microcontroller. It has an ARM Cortex-M3 processor, 64KB of flash memory, and 20KB of SRAM. The Blue Pill is a popular board for hobbyists because it is cheap and powerful.

STM32F401 (Black Pill)

The STM32F401 (Black Pill) is a development board based on the STM32F401CCU6 microcontroller. It has an ARM Cortex-M4 processor, 256KB of flash memory, and 64KB of SRAM. The Black Pill is a more powerful board than the Blue Pill, and it is suitable for more complex projects.