Skip to content

Instantly share code, notes, and snippets.

@japaric
Created June 2, 2017 19:27
Show Gist options
  • Save japaric/7302257be4d330343052f94e3b4f93aa to your computer and use it in GitHub Desktop.
Save japaric/7302257be4d330343052f94e3b4f93aa to your computer and use it in GitHub Desktop.
Content for the areweembeddedyet.com

The embedded space is vast and Rust support varies depending on the application space and platform. However, we can roughly split the ecosystem in two: std land and no-std land, where the standard library is not used or can't be used.

In std land we mainly have Single Board Computers like the Raspberry Pi and routers running OpenWRT or similar. Development for these platforms is very similar to native development, modulo the need for cross compilation, so the tooling is in good shape. The ecosystem has crates for doing embedded I/O through the Linux subsystem and crates for interfacing some sensors.

In no-std land we mainly have microcontrollers either running some OS or operating bare metal (without an OS). Development is pretty different from native development but the tooling is getting in good shape and the development workflows are documented. There are crates in the ecosystem for doing I/O at the lowest level (with registers), one framework for doing bare metal concurrency and one embedded OS but robotics and IoT-ish applications have been demoed. We are currently working towards higher level abstractions and always growing platform support.

Resources

Getting started

  • If you haven't worked with microcontrollers before the Discovery book will walk you through the basics.
  • If you know the basics this quickstart blog post is the fastest way to get something working on pretty much any ARM Cortex-M microcontroller.

Blogs

As we are still figuring things out people are writing down their experiences with Rust and sharing information about how to develop embedded applications. Keep an eye on these blogs:

  • Talking Tock. A blog about the development of the Tock OS.

Chat

The embedded Rust community gathers in the #rust-embedded IRC channel on Mozilla's network (irc.mozilla.org).

Supported platforms

ARM Cortex-M

Tock is a safe, multitasking operating system for low-power, low-memory microcontrollers.

Tock supports the following development boards:

  • Hail
  • Imix
  • nRF51-DK

Check their hardware page.

For kernel development check the tock repository.

For userland applications check their WIP libtock repository.

Real Time For the Masses

The Real Time For the Masses (RTFM) framework is a framework (not an OS) for building efficient concurrent applications.

The cortex-m-rtfm crate is an implementation of the framework for ARM Cortex-M microcontrollers. The crate fully supports Cortex M3, M4 and M7 microcontrollers and partially supports Cortex M0 and M0+ microcontrollers. The RTFM framework only provides scheduling and synchronization primitives, and it's totally decoupled from the I/O layer so you'll have to build your own I/O abstractions or use one of the existing ones on crates.io. The only requirement of the framework is a device crate generated using the svd2rust tool.

The f3 crate provides support for this development board. Version 0.3.x provides a blocking API for LEDs, Serial, timers, the accelerometer, the magnetometer, the gyroscope, etc. Version 0.4.x provides a non-blocking API compatible with the RTFM framework but it's not yet on parity with version 0.3.x.

The Discovery book targets this development board and makes use of the version 0.3.x.

You can use the 0.4.x version with the cortex-m-quickstart template.

The blue-pill crate provides support for this development board. The crate provides a non blocking API and it's compatible with the RTFM framework.

You can use this crate with the cortex-m-quickstart template.

The vl crate provides support for this development board. The crate provides a non blocking API and it's compatible with the RTFM framework.

You can use this crate with the cortex-m-quickstart template.

The photon-quickstart Cargo project is a template for building Photon applications. The photon-hal crate provides a safe Rustic API on top of the Photon HAL which is written in C. That HAL doesn't expose the whole Photon API but already provides WiFi functionality for integration with the Particle cloud API.

The teensy3 crate provides a safe and rustic API on top of the Teensyduino HAL which is written in C. The teensy3-rs-demo repository contains a Cargo project template for using the teensy3 crate. The teensy-clock repository contains an example of building a wall clock using the teensy3 crate.

The lm4f120 crate provides an API to use LM4F120 microcontrollers. The stellaris-launchpad crate is a crate for working with the Stellaris launchpad and at the same time it's a Cargo project template.

nRF51822 (WIP)

A Rust layer is being built on top of the SoftDevice firmware. The WIP ble400 crate provides support for the BLE400 development board which has a nRF51822 microcontroller, and makes use of the version S130 of the SoftDevice firmware.

Other devices

You can easily bootstrap support for almost any ARM Cortex-M microntroller using svd2rust and following the quickstart guide. Pretty much the only requirement is having a SVD file for your device. You can probably find such file in this database or on your microcontroller vendor's website.

There are quite a few device crates that have been generated with svd2rust floating around:

MSP430 (WIP)

Support for the MSP430 architecture is being worked on. The corresponding LLVM backend has been enabled in the rustc compiler, but there are no target definition built into the compiler. However you can build applications starting from this template.

Progress is being tracked in this issue.

AVR (WIP)

Support for the AVR architecture is being worked on out of tree. The corresponding LLVM backend is not enabled in the rustc compiler so you'll need to build a custom compiler to target this architecture. The backend has not been enabled in rustc because it still has bugs and you can't compile the core crate, which is required for every single Rust program out there, for the AVR architecture.

If you are willing to build a custom compiler and use a fork of the core crate that woks with the LLVM AVR backend in its current state you can follow this blog post.

ARM Linux

Raspberry Pi & co

The rust-embedded organization have been working on Rust support for Single Board Computers like the Raspberry Pi. The organization maintains several crates that are published on crates.io for doing embedded I/O on these boards. Among those crates we have:

  • gpio-utils. Command-line utilities for interacting with GPIOs under Linux.
  • i2cdev. Provides API for safe access to Linux i2c device interface.
  • rust-sysfs-gpio. Provides access to the Linux sysfs interface to GPIOs.
  • spidev. Provides access to the Linux spidev interface.
  • sysfs-pwm. Provides access to the Linux sysfs interfaces to PWMs.

General purpose crates

Tools

  • Xargo is a drop-in Cargo replacement that builds and manages sysroots. Xargo is required for no-std development where there are no binary releases of the core crate.
  • Cross is a drop-in Cargo replacement that facilitates cross compilation from x86_64 Linux to ARM / Aarch64 / MIPS / PowerPC Linux, *BSD, Windows, etc.
  • svd2rust is a tool for converting the System View Description (SVD) file of a microcontroller into a crate that exposes an API to use that microcontroller hardware.
  • dslite2svd is a tool for converting TI's version of SVD files into standard SVD files that can be used with svd2rust.
  • itmdump is a tool for decoding ITM frames and printing their payloads to stdout.
  • bindgen a tool for automatically generating Rust FFI bindings to C and C++ libraries.

Formatting

  • fast_fmt. Faster and leaner alternative to the core::fmt formatting machinery.
  • numtoa. A crate for formatting numbers into byte arrays ([u8])

Memory management

  • alloc-cortex-m. A dynamic memory allocator for Cortex-M microcontrollers.
  • alloc_buddy_simple. Simple, drop-in replacement allocator for Rust running on bare metal (no_std)
  • linked_list_allocator. Simple allocator usable for no_std systems. It builds a linked list from the freed blocks and thus needs no additional data structures.

Data structures

  • aligned. Statically allocated arrays with guaranteed memory alignments
  • fixedvec. A heapless version of the Rust vector type.
  • heapless. Data structures that can be placed in static variables.

Miscellaneous

  • cty. Type aliases to C types like c_int for use with bindgen
  • lazy_static. A macro for declaring lazily evaluated statics in Rust.
  • m. A C free / pure Rust mathematical library ("libm") for no_std code

Even more

For more crates check the following categories on crates.io:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment