Rust on Feather M4

Posted on September 30, 2020
Tags: software, electronics

I’ve been learning Rust recently, and I thought it would be interesting to program a microcontroller in Rust. Luckily, embedded Rust is a thing, and seems to have a fair amount of support. Among others, there is support for ESP32, Longan Nano, Teensy 4, and the Apollo3 chip used in the Artemis module. But there seems to be especially good support for the ATSAMD family of ARM microcontrollers, which are used in a number of Adafruit products.

So, I decided to give embedded Rust a try with a Feather M4 module I had lying around. There is a board support crate for the Feather M4, so the process was relatively straightforward. All I had to do was follow the instructions, but I’ll go ahead and repeat them below.

The first step was to install Rust on my Ubuntu 20.04 machine:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
$ which rustc
/home/ppelleti/.cargo/bin/rustc
$ rustc --version
rustc 1.46.0 (04488afe3 2020-08-24)

Next, following the instructions in the Feather M4 crate, I installed the toolchain needed by Cortex M4 processors, such as the ATSAMD51.

$ rustup target add thumbv7em-none-eabihf

Now, following the instructions for installing cargo-hf2, which will be used to talk to the board’s bootloader to load the program onto the board:

$ sudo apt-get install libudev-dev libusb-1.0-0-dev
$ sudo sh -c 'cat > /etc/udev/rules.d/99-adafruit-boards.rules'
ATTRS{idVendor}=="239a", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idVendor}=="239a", MODE="0666"
SUBSYSTEM=="tty", ATTRS{idVendor}=="239a", MODE="0666"
^D
$ sudo udevadm control --reload-rules
$ sudo udevadm trigger
$ cargo install cargo-hf2

Then, clone the repository to get the Feather M4 examples:

$ git clone https://github.com/atsamd-rs/atsamd
$ cd atsamd/boards/feather_m4/

Finally, connect the Feather M4 board to the computer, and press the feather’s reset button twice.

$ cargo hf2 --release --example blinky_basic

It works! Perhaps the neopixel_rainbow example is a bit more exciting:

$ cargo hf2 --release --example neopixel_rainbow

Don’t forget to press the reset button twice before running cargo hf2.