# Embedded rust current situation?

**URL:** <https://users.rust-lang.org/t/embedded-rust-current-situation/57264>\
**Category:** embedded\
**Created:** [March 21, 2021, 5:08pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264 "2021-03-21T17:08:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iot-crazy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/iot-crazy/32/18793_2.png) [@iot-crazy](https://users.rust-lang.org/u/iot-crazy)\
**Post date:** [March 21, 2021, 5:08pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/1 "2021-03-21T17:08:30Z")

</div>

I've seen various threads on this but I'm struggling to gather the most up to date picture.  
What is the current situation regarding using Rust on devices such as the 8266, ESP32, Teensy 4.0 and pi pico?

---

<div class="post-metadata">

**Author:** ![trembel](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@trembel](https://users.rust-lang.org/u/trembel)\
**Post date:** [March 21, 2021, 6:41pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/2 "2021-03-21T18:41:04Z")

</div>

For ARM processors (Teensy, Pi Pico) it is possible to compile, although the best support have ARM processors from ST and Nordic. For the esp8266/32 the support is just really bad, as the underlying Xtensa architecture is not really supported by Rust.

Take a look here: [https://github.com/rust-embedded/awesome-embedded-rust](https://github.com/rust-embedded/awesome-embedded-rust)

---

<div class="post-metadata">

**Author:** ![iot-crazy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/iot-crazy/32/18793_2.png) [@iot-crazy](https://users.rust-lang.org/u/iot-crazy)\
**Post date:** [March 21, 2021, 8:26pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/3 "2021-03-21T20:26:58Z")

</div>

That's great, thank you, I'll read that material.  
I'm new to this, (web developer for 23 years), does this mean that the features of the boards are also supported, for example I2C, SPI, PWM through existing libraries?

---

<div class="post-metadata">

**Author:** ![hannobraun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hannobraun/32/17381_2.png) [@hannobraun](https://users.rust-lang.org/u/hannobraun)\
**Post date:** [March 22, 2021, 12:43pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/4 "2021-03-22T12:43:34Z")

</div>

I'm not familiar with the Teensy or Pi Pico, but in general, there are two types of libraries for hardware support in Embedded Rust (at least for ARM Cortex-M microcontrollers):

1. Peripheral Access Crates (PACs): Those are automatically generated from a vendor-provided file (the SVD file) and the resulting PAC is as good or complete as the SVD file (which typically means, mostly complete but possibly rather buggy). It's also usually a pain to use. Very low-level, and can't be understood without a good understanding of the hardware itself.
2. Hardware Abstraction Layer (HAL): Those are hand-written libraries built on top of a PAC, with an API that is typically more convenient and harder to get wrong. Writing those is a lot of work, so their completeness and quality varies a lot. Often they are started by a single person for a single project. For the more popular hardware, they are then built upon by others and can become quite good.

What you want to use depends heavily on the requirements of your project, and thus what hardware you want to use. I'll give you three pointers:

1. [stm32-rs](https://github.com/stm32-rs): GitHub org for STM32 microcontrollers. Definitely the most active sub-community within embedded Rust, but quality, completeness and active maintenance vary between the different HALs.
2. [nrf-hal](https://github.com/nrf-rs/nrf-hal): HALs for Nordic microcontrollers. I haven't used it in a while, but I think it covers the basics (and probably more) very well.
3. [LPC8xx HAL](https://github.com/lpc-rs/lpc8xx-hal): Last but not leased, a HAL I've been working on for a few years. Works great with the [LPC845-BRK](https://www.nxp.com/products/processors-and-microcontrollers/arm-microcontrollers/general-purpose-mcus/lpc800-cortex-m0-plus-/lpc845-breakout-board-for-lpc84x-family-mcus:LPC845-BRK), which is quite cheap and easily available, but not very powerful. Covers the basics you mentioned (I2C, SPI, PWM) quite well, and has some more advanced features too. And in my biased opinion, it's also more thought out and better documented that most other HALs.

Other than that, @trembel already posted a link to Awesome Embedded Rust. Searching for the hardware you're interested in on [crates.io](https://crates.io/) might also be useful.

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derekdreery/32/4788_2.png) [@derekdreery](https://users.rust-lang.org/u/derekdreery)\
**Post date:** [April 3, 2021, 9:48am UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/5 "2021-04-03T09:48:52Z")

</div>

This post ended up being quite long, but it is full of information I wish I had known when starting in embedded Rust. Would have saved me a lot of learning.

So very helpfully, when you buy ARM IP to put on your chip, they mandate that you must provide an [SVD file](https://www.keil.com/pack/doc/CMSIS/SVD/html/index.html) that describes your memory mapped peripherals (and possibly other things: I'm not an expert). The Rust embedded folks have built a tool called [`svd2rust`](https://docs.rs/svd2rust/0.17.0/svd2rust/) that takes this SVD file (which is actually an XML file with a particular schema), and generates zero-cost rust code that allows you to manipulate registers without having to write the addresses by hand. (Chip providers also generally provide C headers that have the memory addresses defined, which is why C developers don't need the SVD files so often).

Crates generated from `svd2rust` are called 'peripheral access crates' and generally have a name like `<board serial>-pac`, e.g. [`nrf52832-pac`](https://crates.io/crates/nrf52832-pac), (but not always, e.g. [`stm32f1`](https://crates.io/crates/stm32f1)). You can usually find the crate by searching for '\<board serial\> rust' or variations thereof.

The rust embedded folks have been working on building another layer on top of the PACs called HAL (or hardware abstraction layer). The idea is that say you have a peripheral that talks to the chip over the [SPI](https://en.wikipedia.org/wiki/Serial_Peripheral_Interface) and you want to make a crate that other people with the same peripheral can use (e.g. driver crate). If you write it for your specific register memory addresses (from the PAC crate) then it would only work with your particular configuration, and even then only if you used the same pins/SPI peripheral. HAL defines traits for generic commands, which in the case of SPI include 'send/receive data', for example. By writing your driver using these traits, you allow the end-user to bring their own particular setup by implementing those traits and forwarding the commands to the correct registers. Most PACs also have a corresponding HAL crate that in contrast to the `svd2rust`-generated PAC is hand written, and implements the HAL traits (in the [`embedded-hal`](https://crates.io/crates/embedded-hal) crate). Board HAL crates often also expose the PAC crate as a submodule (e.g. `nrf52832_hal::pac` in the `nrf52832-hal` crate).

The idea of board HAL and driver crates works really well when you are running a series of tasks sequentially, but some of the abstractions don't work so well when you want to perform multiple tasks concurrently, or sleep when there is nothing to do. For this, the basic recipe is (slightly simplified)

1. Set up an interrupt handler for an event that marks the end of some peripheral activity (for example copying data from RAM to an SPI peripheral).
2. Start the thing off doing what you want it to do.
3. Go do something else or sleep.
4. When the task is complete, the processor will stop what it is doing and run your interrupt handler. In the handler, you can either go back to 1 and set of another task, or maybe send some data to the main process and wake it up.

Software that does some of this stuff for you is called a 'Real-Time Operating System' and the Rust ecosystem has a really good one of these, called [RTIC](https://rtic.rs/0.5/book/en/) (formerly RTFM). This crate will help you set up all your interrupts and prioritise them, so that more important stuff runs first, which could be very important in, for example, a medical device.

RTIC does force you to think about interrupts however, and also it is best suited for hard-realtime requirements when you have to _prove_ that things will happen quickly enough (see medical devices again). In other projects, for example a smartwatch, your primary focus might be to minimize power usage by powering down all peripherals and the processor where possible, while still running tasks concurrently. This space is currently being explored by projects like [`embassy`](https://github.com/embassy-rs/embassy) using Rust's `async` machinery. It turns out that scheduling tasks concurrently is basically what a Rust async executor does. What we want to be able to do is things like

```rust
// Turning on a made-up LCD screen peripheral

// SPI commands take a while to run, because we have to copy data out of memory
spi.send_data(Command::POWER_UP).await;

// data sheet says we must wait at least 150ms before sending more commands
sleep(Duration::from_millis(150)).await;

spi.send_data(Command::TURN_ON_BACKLIGHT).await;
spi.send_data(Command::CONFIGURE_SCREEN).await;
spi.send_data(screen_configuration).await;

// we have to wait another 10ms before sending pixel data
sleep(Duration::from_millis(10)).await;

spi.send_data(pixel_data_for_first_frame).await;

```

and let the async executor handle things like getting on with another task while we write out the pixel data. ( **Aside** In the above example we might have wanted to block the processor instead of `await`ing for some of the commands since the overhead of the executor would actually take longer than just spinning the processor until the SPI operation has finished.)

This is where we are up to. One very current problem is that it is difficult to design an executor in Rust utilising interrupts that is safe with respect to memory leakage. This means that in practice you can get UB without unsafe code by using `mem::forget` or the other methods for leaking futures. Maybe this problem will be solved, or there will always be a caveat that you mustn't leak any futures in async embedded Rust (which is not hard to avoid, but does mean that safe code is actually unsafe).

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [April 3, 2021, 10:29am UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/6 "2021-04-03T10:29:58Z")

</div>

Seems people have been making some headway with Rust for the Pi Pico:

> **[Raspberry Pi Pico Gets supports for Rust, RT-Thread OS and FreeRTOS - CNX...](https://www.cnx-software.com/2021/02/05/raspberry-pi-pico-gets-supports-for-rust-rt-thread-os-and-freertos/)**
>
> Raspberry Pi Pico has flexible software support compatible with RP2040 microcontroller, including Rust code, RT-Thread OS and FreeRTOS.

---

<div class="post-metadata">

**Author:** ![jschievink](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jschievink/32/7453_2.png) [@jschievink](https://users.rust-lang.org/u/jschievink)\
**Post date:** [April 3, 2021, 5:03pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/7 "2021-04-03T17:03:56Z")

</div>

> [@derekdreery](#):
>
> they mandate that you must provide an [SVD file](https://www.keil.com/pack/doc/CMSIS/SVD/html/index.html)

Do they? I've seen vendors that don't provide one.

(and unfortunately the vast majority of SVD files is also riddled with bugs)

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derekdreery/32/4788_2.png) [@derekdreery](https://users.rust-lang.org/u/derekdreery)\
**Post date:** [April 9, 2021, 2:46pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/8 "2021-04-09T14:46:31Z")

</div>

I'm sure someone once told me that they were mandatory when licensing ARM designs, but I can't find any written evidence to back up the claim. Shame. They are very useful.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 8, 2021, 2:46pm UTC](https://users.rust-lang.org/t/embedded-rust-current-situation/57264/9 "2021-07-08T14:46:48Z")

</div>

Closed on July 8, 2021, 2:46pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
