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.

sched slides

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.