You’ve probably heard that Rust is a systems programming language that has quite the following growing. It purports to be fast like C, but has features like guaranteed memory and thread safety, generics, and it prevents segmentation faults. Sounds like just the thing for an embedded system, right? [Jorge Aparicio] was frustrated because his CPU of choice, an STM32 ARM Cortex-M didn’t have native support for Rust.
Apparently, you can easily bind C functions into a Rust program but that wasn’t what he was after. So he set out to build pure Rust programs that could access the device’s hardware and he documented the effort.
Not only does the post show you the tools you need and the software versions, but using OpenOCD, [Jorge] even managed to do some debugging. The technique seems to pretty generally applicable, too, as he says he’s done the same trick on six different controllers from three different vendors with no problem. You do have to configure the project by changing some values in a template.
Although it isn’t a Rust tutorial, following along with [Jorge’s] code and his explanations will give you a pretty good idea of what Rust looks like. He also shows off a neat tool, gdb-dashboard. To build the API to the ARM’s special memory regions, [Jorge] uses a tool called svd2rust to process the vendor’s SVD files. These are usually used for JTAG programming and testing, so we thought that was a novel way to automatically build support for the processor.
A lot of languages that offer safety features tend to compile fat code. [Jorge] shows a blinking LED example and disassembles it and it looks quite compact about 127 bytes. He then abstracted away the timer registers and the code is almost the exact same size when compiled.