EOSS2023 – Fearless Embedded Rust – Martin Mosler, Zuehlke Engineering AG

EOSS2023 – Fearless Embedded Rust (Martin Mosler – Zuehlke Engineering AG)

 

Martin is Rust advocate within his company, and is allowed to do some community things on company time like organise meetups. Zühlke believes that Rust is very useful, especially in embedded.

He created a demo on ESP32-C3 devkit. It’s well-supported, RISC-V and has Wifi.

(22) Arnout Vandecappelle | LinkedIn , one of our sr. embedded consultants has written a report on his presentation.

arnout vandecapelle, mind (essensium division), embedded software consultant
Arnout Vandecapelle
27/06/2023

There is already a esp-idf-template project for the ESP32C3 (provided by Espressif), so it’s just a matter of cargo generate to start working. The template pulls in to initialise the board and have a FreeRTOS based OS on it. It has a logger interface, so you can log “Hello world!” to it. cargo run is set up to build and flash directly to the board connected with USB. For this to work, a udev rule must be added to make the USB serial port accessible to the user. cargo run opens this serial port after flashing so you can see the log messages. This IDF even provides the standard library with all features.

Martin set up a temperature sensor demo, with just the sensor, batteries and a voltage divider to monitor the battery as peripherals. The measurements are uploaded to the cloud over wifi.

The rust ownership model directly maps to reserving peripherals. I.e. when you ask for a certain peripheral, no other code will be able to access it and this is enforced by the compiler itself. However, the function to reserve it may still be called twice, so it returns an Optional value. This has to be unwrapped, which panics in case the value is invalid.

Most functions may return errors in addition to a valid value. Rust enforces you to catch all the cases with pattern matching. The typical use case of “continue if OK, return if error” can be abbreviated by putting ? after the expression.

To upload data to the cloud, it has to be serialized. This is really easy in Rust by declaring the structure to be derived from Serialize.

Unfortunately, the EPS32 project has two timers: one for more than 10ms and one for less than 10ms. If you use the wrong one, it will just return without waiting.

To save power, the CPU should go into deep sleep between measurements. Instead of a loop in the main function, the main function does just a single iteration and then calls esp_deep_sleep. This function is a C function, so it has to be put in an unsafe block.

To use wifi, there is a simple call to start wifi. After that, a socket can be used directly, without requiring anything more related to wifi. For the demo, he uses a bare UDP socket that is captured with netcat and written to a file on the server.

The compiler checks a lot of things so you’ll probably get many error messages. They can be overwhelming at first, but they almost always do point to errors in the code.

Alternative to FreeRTOS is Embassy, with is an async programming library for embedded.

If the full standard lib is used, the heap is used a lot. Does a tool exist to evaluate heap usage? There is a tool called bloat to evaluate flash usage.

More info on this topic:

https://static.sched.com/hosted_files/eoss2023/d4/Fearless_embedded_rust.pdf

https://eoss2023.sched.com/event/1LaQP/fearless-embedded-rust-martin-mosler-zuehlke-engineering-ag   (slides)

Presentations

Drop the docs and embrace the model with Gaphor Fosdem '24 - Frank Van Bever 20 March, 2024 Read more
How to update your Yocto layer for embedded systems? ER '23 -Charles-Antoine Couret 28 September, 2023 Read more
Tracking vulnerabilities with Buildroot & Yocto EOSS23 conference - Arnout Vandecapelle 12 July, 2023 Read more
Lua for the lazy C developer Fosdem '23 - Frank Van Bever 5 February, 2023 Read more
Exploring a Swedish smart home hub Fosdem '23 - Hannah Kiekens 4 February, 2023 Read more
prplMesh An Open-source Implementation of the Wi-Fi Alliance® Multi-AP (Arnout Vandecappelle) 25 October, 2018 Read more

 

News