Student Jonas Stirnemann has decided to take a Raspberry Pi single-board computer back to its most basic layers, doing away with an operating system in favor of programming the device directly in Rust and running it on the bare metal — and has provided a guide so you can do the same.
The idea of "bare metal" programming means getting down-and-dirty with the hardware. Traditionally, the programmer is several layers of abstraction away from the underlying device — the biggest layer being the operating system, which loads up before you can even think about running your own code. Bare metal programming, by contrast, does away with the operating system entirely — running your program directly on the hardware, providing the most pared-down experience possible.
Stirnemann's project was simple enough: Blink an LED connected to a general-purpose input/output (GPIO) pin on a Raspberry Pi 3 Model B single-board computer (SBC). Traditionally, that's something you'd do on top of a Linux-based operating system like the official Raspberry Pi OS — either by interfacing with the hardware directly through the Linux kernel or by using a library in a language like Python or C.
For Stirnemann, though, the operating system was superfluous. Instead, he decided to program the device directly — creating a program in Rust that, when compiled, can be booted directly on the Raspberry Pi with no intervening operating system.
"We want to compile code for a specific architecture, this mean we’ll need to cross-compile the code because the architecture of our PC is different from the one on the [Raspberry] Pi," Stirnemann explains of the process. "We also want to use it bare metal which means we don't want it to run on an existing operating system so we can't use existing OS libraries."
What follows is the setup of a build environment that allows an x86-based PC to compile code for the Raspberry Pi's Arm-based processor using Cargo, the creation of a Rust program which includes panic handling, and a linker script which puts the program where the Raspberry Pi bootloader needs it to be. Once a simple proof-of-concept bootable binary is built, Stirnemann then walks through adding the LED blink functionality — making reference to the Broadcom BCM2835 datasheet to figure out how to address the GPIO pin to which the LED is connected.
The full tutorial is available, with source code and bootable pre-compiled binary for loading onto a microSD card, is available on Stirnemann's website.