Blake Smith

create. code. learn.

»

Rust on a ATSAMD51

I recently tried my hand at running Rust on an ATSAMD51 ARM microcontroller. Rust is well setup for embedded work, and this experiment proved this to be true. I designed a prototype PCB, with the goal of figuring out a Rust based toolchain and workflow. The main components from the custom board I spun were:

  1. Microcontroller: ATSAMD51G19A.
  2. Flash memory: MT25QL512 512M SPI Flash.
  3. 2xAPA102 DotStar RGB LEDs.

Along with a tactile switch, this was enough to get familiar with the ATSAMD microcontroller and start playing around. I very well could have ordered a prefabbed development board, but I usually like to get familiar with the schematic layout of a chip I’m using, since it helps me learn how to wire up the components for a later project.

Here’s the PCB I put together in KiCAD:

KiCAD PCB

And the 3D view:

KiCAD PCB 3D

Putting the board together:

Initial Board assembly

Final board:

Final board.

For software, I was able to use a few great existing Rust libraries (besides the core cortex-m ones):

  1. atsamd-rs. ATSAMD target library. Provides nices safe wrappers around unsafe register access to underlying peripherals and register mappings.
  2. smart-leds. Provides a nice abstraction over many different smart RGB leds.
  3. apa102-spi-rs. Provides a smart-leds implementation for APA102 specific LEDs. They even took one of my patches, since my pixels were needed a different color ordering!.
  4. usb-device. Provides nice “user-land” USB protocol bindings. I like that the core USB protocol interfaces are separated from board specific implementations (register interactions, etc.). The atsamd-rs library provides a USB peripheral implementation for this interface.

Putting all this together, I implemented a single button KeyboardHID USB device, that also blinks the RGB LEDs when the button is pushed, and sends a Play/Pause media key over USB. I also managed to have the device talk to the SPI flash from Rust, just as a bonus experiment.

It feels really great writing Rust in embedded land. I definitely had to resort to use of unsafe a few times, but this seems more par for the coarse when there aren’t as many layers of abstraction between you and the silicon.

I’ve taken this knowledge, and begun laying out a larger macro pad / media keyboard for controlling my music at my desk:

Media Paid 3D

It’s going to have really chunky arcade buttons, so I can control my music Street Fighter style. I’ll share more about that project soon!

In the meantime, you can check out the firmware, bill of materials, and KiCAD schematics on my Github.


about the author

Blake Smith is a Principal Software Engineer at Sprout Social.