Rust on the Hologram Dash
The Rust language, with its focus on safety and concurrency, is a natural conceptual fit for bare-metal programming. Spending most of my professional time writing code for microcontrollers, I have a deeply personal relationship with C. Will there every be a true replacement for C on the microcontroller? Could Rust be that successor? The short answer is, not yet. But let’s see how far we can get. I work on the Hologram Dash, so that is going to be the target board. The user microcontroller is an ARM Cortex-M4F NXP (formerly Freescale) MK22FN1m0.
The zinc project is an attempt to get Rust to run on multiple ARM Cortex-M microcontrollers. Quite an ambitious undertaking! Unfortunately zinc uses experimental features in Rust only available in the nightly builds. Even worse, because these nightly features change, well, nightly, zinc is broken often. If we roll back to the last working nightly, then we can at least build zinc.
Included in zinc is a HAL for a K20. Since the Dash MCU is similar, we can make some small tweaks and get something running. The blink_k20 example looks simple enough. Blink an LED. All that’s needed is to change the LED pin from B16 to B19. It builds!
But to load the code, I want to use the USB upload tool for the Dash. That means I need to keep the bootloader on the Dash and move the start location of Rust code. Easy enough, just modify the layout.ld linker file to point to 0x8000 instead of 0x000. That will tell the linker to leave room for the bootcode. Upload and it blinks! Do you get irrationally excited about an LED blinking? Then you might be a firmware engineer.
Not content with just the LED, we need a Hello, World! program to really fell accomplished. The UART isn’t fully implemented for the K20, so we’ll need to add to it. First, enable the UART clock (otherwise the dreaded hard-fault!). Then setup the pins for the UART and enable the UART module. Hook up an FT232 breakout board to UART2 (Dash pins L6 and L8), open the terminal, and Hello, World! Our clock rate isn’t right (56000 instead of 115200), but we have output. Put fix clocks on the TODO list.
With just some small tweaks to zinc we have Rust code running on the Dash. I hope development continues on zinc and that one day it will be stable enough to support the hundreds of Cortex-M chips out there. I hope to learn enough about Rust that I can contribute also.
If you have a Dash and want to run Rust on it, I wrote some very detailed instructions for installing the toolchains and building the project. All the modifications I described are on Github in the forked zinc project.
Rust isn’t ready to be a C-replacement on ARM microcontrollers just yet, but maybe someday it will.