First steps with Embedded Rust: Selecting a board
Selecting a board
There are a bewildering array of microcontrollers out there and an equally bewildering array of exciting projects going on in the embedded Rust world. So, how do I know where to start?
I have tried to focus on things I think are most important to someone with very little embedded experience. That may not fit you exactly. I will try to point out adjacent options as I go so please don’t stop reading just yet.
If you just want to be told what to buy you can skip straight to the end.
What do we want in a development board?
So, what do we actually want in a development board? I’ve tried to order the criteria in a way that allows us to whittle down our options as we go.
- Good architecture support
- Good chip support
- An active community
- Built in debugger
What architecture should I get?
The architecture with the most complete libraries, most thorough guides and the largest community around it is ARM Cortex-M. ARM Cortex-M are low power, low cost processors aimed at microcontroller applications. Looking at downloads on crates.io is not a perfect metric but it gives an idea of the difference in scale. In the last 90 days the cortex-m crate had over 250k downloads. The largest architecture crates for RISC-V, AVR, or Xtensa had at most 3k downloads and the cortex-a crate had about 18k downloads. ARM Cortex-M is in a league of its own.
Before we zoom off down ARM Cortex-M boulevard let's take a quick look sideways at the architectures we're leaving behind.
AVR
AVR is a family of 8-bit microcontrollers mainly used in embedded systems. Thanks to Arduino there are a lot of AVR development boards out there and lots of support for them outside of Rust. Within the Rust ecosystem they are not as well supported. Until recently you needed to use a fork of rustc to build for AVR. There are a couple of different options now and awesome-avr-rust is a good place to start.
ARM Cortex-A
These are bigger, beefier multi-core ARM processors designed for running bigger things. You would usually run a full operating system on them. For example; this is the architecture used in most smartphones and handheld games consoles. Check out the cortex-a crate for more info.
RISC-V
This seems to be the new hotness when it comes to machine architectures. It is a free and open Instruction Set Architecture (ISA). It has also been designed from outset to be modular. This means chip designers can create a wide variety of specialised chips. Although at the moment the range of development boards is small. There is an active Rust RISC-V community so it's worth keeping an eye on. If you’re interested in learning more, SiFive or www.riscv.org are both great places to start. In terms of Rust check out the riscv crate.
Xtensa
The most popular group of boards here are the ESP32 family of chips from Espressif. They are small, cheap, WiFi enabled boards. They are great. It should be noted that not all ESP32 boards use Xtensa chips, the new ESP32-C3 is RISC-V based. Probably the biggest hurdle to using Rust on Xtensa chips is that it’s not supported by llvm so you need to build the esp-rs fork of rust.
What chip should I get?
So, we’re going to use an ARM Cortex-M. That narrows down our search but there are still a lot of options. If we go through the cortex-m crate’s dependants we’ll see that there are two groups of chips used more than any others; the STM32 series of chips and the nRF5 series, this is where we’re going to focus the search.
STM32
The STM32 series of chips are probably the most widely used ARM Cortex-M chips for embedded rust. There are a lot of chips in the stm32 family and they have long and incomprehensible names like STM32F401CCU6. If you’re interested there is a guide to understanding the naming convention.
The STM32 Peripheral Access Crates (PAC) are defined in the stm32-rs/stm32-rs repo and you can find various Hardware Abstraction Layer (HAL) and Board Support (BSP) crates in the stm32-rs GitHub organisation.
There is a chat room for discussion on all STM32 chips at #stm32-rs:matrix.org.
Two of the most popular STM32 boards are the Blue Pill and Black Pill. There are a bunch of blogs about using them with rust. The main downside is that they do not come with an on board debugger. And for that reason alone I think they make a great second board.
If you want an STM32 based board with a debugger then getting one of the official STMicroelectronics discovery kits is a great option (an STM32F3 or STM32F4 are good bets). The original version of the Rust Embedded Discovery book is written targeting the STM32F3 discovery board so there is really high quality beginner focused documentation. If you go for a discovery board I would definitely start there.
nRF5
The second most widely used family of ARM Cortex-M chips for embedded rust is the nRF5 series from Nordic Semiconductor. The PACs are defined in the nrf-rs/nrf-pacs repo, the HALs are defined in the nrf-rs/nrf-hal and there are various BSPs in the nrf-rs organisation. There is a chat room for discussion on all nRF chips at #nrf-rs:matrix.org.
The official development kits (DK) are great first boards. The knurling-rs sessions from Ferrous Systems use the nRF52840 Development Kit. The knurling sessions are really high quality, hands on guides that teach you embedded rust with interesting, fun projects. I think that right now, they are the best entry point to embedded development with rust. The one challenge I had was the space required. I live in an apartment with my partner and a toddler. I do not have the space to safely leave a project with lots of wires and small components lying around, and I only have free time for a couple of hours in the evening. I found myself constantly packing things up and setting them up again to make a tiny amount of progress.
Another great nRF based development board is the BBC micro:bit. It comes with an on board debugger and a bunch of fun on board peripherals like an LED display, buttons and sensors all on the board. The BBC micro:bit is designed as an educational platform so the hardware is documented in a really beginner friendly way on their developer community and there are loads of project ideas around the internet (albeit in languages other than Rust). Check out the examples in the micro:bit BSP for some idea of what you can do.
RP2040
The last chip we’re going to look at is a bit of a curveball. The RP2040, released at the end of 2020, is the Raspberry Pi Foundation’s first foray into designing their own silicon. Being so new, Rust support for it is still very much in development. You can find the PAC, the HAL and a bunch of supporting tools in the rp-rs organisation on GitHub. There is a chat room dedicated to the RP2040 at #rp-rs:matrix.org.
Like the BBC micro:bit the RP2040 is geared towards being an educational platform so the hardware documentation is first class and there are loads of beginner friendly code examples and libraries in other programming languages. That said there isn’t much beginner friendly documentation for Embedded Rust on the RP2040. This is a really exciting platform and there is a huge amount of activity around it in the Embedded Rust community so definitely keep an eye on it, but it probably isn’t ideal as your first board.
What’s the fuss about an on board debugger?
When you run a program on your host machine you can easily run it in your shell and see printed output. This is more difficult on an embedded target. You can make an LED flash but there is no terminal. A debugger fills this gap. As well as allowing you to do step through, breakpoint debugging, it allows you to load programs onto your device and easily see output. A debugger is a really useful tool for embedded development.
There is a catch though. It is usually a separate device that you connect to your host and then wire up to your target device. When you’re first starting out this is a not inconsiderable expense and yet another thing that you will have to set up correctly. Luckily some devices come with a built-in debugger. With these devices you should be able to plug them straight into your host and probe-run your code in a flash. (There is usually a bit of setup needed on your host to get the debugger working, ferrous have a good setup guide).
What board should I get?
All these boards have great HAL and BSP crates, active friendly communities, and an on board debugger.
- A BBC micro:bit (about £13); it is the board used in the new version of the Rust Embedded Discovery book.
- An nRF52840 Development Kit (about £35); it is the board used in the Knurling Sessions and training by Ferrous Systems.
- An STM32F3 Discovery Kit (about £14); it is the board used in the first version of the Rust Embedded Discovery book.
And keep an eye on
- Raspberry Pi Pico (about £6 with pre-soldered pins); ARM Cortex-M but no built in debugger and the HAL is still a work in progress. On the other hand, there is a lot of activity on it at the moment and things are moving fast.
- HiFive1 Rev B (about £50); RISC-V is the new hotness. There seems to be a lot of activity around it in Rust however it does not yet have the support that ARM Cortex-M has right now. Other boards to keep an eye on here are the Logan Nano and the ESP32-C3.