Close

Hack Chat Transcript, Part 1

A event log for Embedded Rust Hack Chat

Rust never sleeps

dan-maloneyDan Maloney 05/12/2021 at 20:110 Comments

So let's get started! Like I said, lots of new names, so welcome to the Hack Chat. I'm Dan, I'll be moderating today along with Dusan as we welcome James Munns to talk about Rust.

James Munns12:01 PM
Hey all!

James, are you online yet?

Great timing...

James Munns12:01 PM
Yup :)

regs.normal joined the room.12:01 PM

Welcome aboard. Care to kick us off with a little about yourself?

john joined the room.12:02 PM

James Munns12:02 PM
Sure, I'm James, I'm one of the founders of Ferrous Systems, where we do a lot of stuff with Rust, and lots of embedded Rust!

Daniel Söderholm joined the room.12:02 PM

skyfire360 joined the room.12:02 PM

james0d0a joined the room.12:02 PM

Dominik joined the room.12:03 PM

James Munns12:03 PM
I used to write for hackaday (a long time ago), and I've done a lot of embedded systems stuff for safety critical + connected devices areas

Dominik12:03 PM
Hi all

James Munns12:04 PM
I also just started learning how to do PCB design, and started playing round with some FM synth stuff, hopefully to make little embedded synths. Happy to answer any questions!

A timely aspect of programming, given the attention that the Colonial Pipeline attack has attracted

Chris Sattler joined the room.12:04 PM

James Munns12:05 PM
Ah yeah! I've seen that as well lately. Was that ransomware for that one?

andypugh12:05 PM
Yes

loman.daniel12:05 PM
whats the best way to get started with rust on embedded?

Trevor R.H. Clarke joined the room.12:05 PM

James Munns12:05 PM
So, there's a lot of really great material out there, let me pull some links

Yes, but I'm thinking about all the embedded devices in the SCADA system used to control these pipelines.

James Munns12:06 PM
https://docs.rust-embedded.org/ is a good place to find a couple of books/guides

john12:06 PM
I'm a newbie with rust, so I was wondering, is the ESP32 a good platform to learn rust?

andypugh12:06 PM
I have been assuming the pipeline thing is a Shodan / SCADA attack.

loman.daniel12:07 PM
do you use a HAL? or are you building rust functions to interact with your peripherals?

James Munns12:07 PM
The "Discovery Book" is meant for folks that are learning embedded, when they know rust, and the Book is meant for learning the embedded specific parts and libraries

Sorry, didn't mean to derail the Rust chat, just thought the embedded aspect of both was interesting.

Inne12:07 PM
Does Rust give embedded devices super powers not to be had with C or python. (I heard something about simultaneous LED control).

James Munns12:07 PM
Ferrous also has an open source project called Knurling sessions, which is good if you like learning by doing hardware projects :)

https://knurling.ferrous-systems.com/sessions/

andypugh12:08 PM
I think my first question about embedded Rust would be "why?" I can think of some likely reasons, but could you elucidate?

Audi McAvoy12:08 PM
Why choose Rust over <insert your favorite language here>?

Brad Hanson12:08 PM
Actually, I think there's an element there that might be interesting - do you see a future wherein Rust requires a similar restriction like MISRA C in order to comply with safety critical systems, or do you feel that the language is enough right now to prevent most safety-related issues?

James Munns12:08 PM
> is the ESP32 a good platform to learn rust?

It's not great, Rust is built on top of LLVM (like Clang and Swift), and there isn't official LLVM support for that arch

Alex M12:09 PM
Hi James, thanks for your time.

Is it possible to link existing c/c++ libs (arduino libs) And would you recommend it? =)

skyfire36012:09 PM
Hey James! Aside from the various handbooks/'nomicons, do you have any pointers to solid resources/git repos that demonstrate Rust patterns, practices, and general tips for embedded Rust?

James Munns12:09 PM
https://github.com/MabezDev/xtensa-rust-quickstart is the work in progress for experimental support, but cortex-m is the most supported platform

Trevor R.H. Clarke12:09 PM
I use a bluepill/blackpill for playing with rust and it works well

James Munns12:10 PM
> Hey James! Aside from the various handbooks/'nomicons, do you have any pointers to solid resources/git repos that demonstrate Rust patterns, practices, and general tips for embedded Rust?

there is also the http://showcase.rust-embedded.org/, and https://github.com/rust-embedded/awesome-embedded-rust!

deʃhipu12:11 PM
so it's not like micropython/circuitpython, where they are simpler than the non-embedded versions, and it's actually easier to start with them?

James Munns12:11 PM
> Actually, I think there's an element there that might be interesting - do you see a future wherein Rust requires a similar restriction like MISRA C in order to comply with safety critical systems, or do you feel that the language is enough right now to prevent most safety-related issues?

So my company is actually working on this! Our goal is to get the Rust language usable in safety critical areas, likely starting with automotive!

https://ferrous-systems.com/ferrocene/ has some more details. I definitely would love to have Rust in safety critical, and can hope that "Safe Rust" can be an out-of-box equivalent of MISRA-C!

Brad Hanson12:12 PM
Considering the ISO 26262 requirement of MISRA, are there regulatory hurdles you see in your way?

Inne12:12 PM
@James Munns What are some rust hardware projects that you found really cool/novel.

James Munns12:12 PM
> so it's not like micropython/circuitpython, where they are simpler than the non-embedded versions, and it's actually easier to start with them?

Embedded Rust is just Rust! Like C/C++. The only difference is you don't have the parts of the standard library that does threads/files/heap stuff, but you can add them back in or use other libraries that don't require it!

john12:12 PM
state machines in rust, are they cleaner (more readable) than in C?

andypugh12:12 PM
I work in automotive control systems, I think that we are too far down the road, with millions of lines of C, to switch now.

Brad Hanson12:13 PM
Linux said that too

Neal12:13 PM
@James Munns what do you think are the best parts of learning Rust? What are the worst parts?

benjamin joined the room.12:13 PM

James Munns12:13 PM
> @James Munns What are some rust hardware projects that you found really cool/novel.

This is an STM32 w/ ethernet that is doing real time control of quantum computer systems!

https://github.com/quartiq/stabilizer

andypugh12:14 PM
@Brad Hanson Good point, I guess that _parts_ could be written in Rust. Heck, half our C is auto-generated from Simulink models.

James Munns12:14 PM
> I work in automotive control systems, I think that we are too far down the road, with millions of lines of C, to switch now.

A lot of automakers would disagree with this! From what I have found they are starting to hit the limit of diminishing returns of sticking with C, and are already evaluating rust, often in pre-production/R&D testing!

richard12:15 PM
is Rust any better at minimizing heap fragmentation when working with low memory devices and doing a lot of dynamic allocations?

andypugh12:15 PM
Yes, I just changed my mind, realising how much is auto-generated from Simulink / Grafcet input.

James Munns12:15 PM
state machines in rust, are they cleaner (more readable) than in C?

Yeah definitely! Rust has match statements/pattern matching, which is like a superpowered version of switch statements! Here is a state machine where I parse a bit-banged I2C analyzer:

https://github.com/jamesmunns/pigpio-i2c-rs/blob/master/i2c_parser/src/lib.rs#L111-L170

Brad Hanson12:16 PM
I agree there's going to be inertia, but that's just the auto industry liking to use known-good parts

andypugh12:16 PM
@richard Could it be worse :-)

hpux73512:16 PM
@James Munns , Do you have any advice for how to connect with the community in a collaborative sense? I've done the discovery series, and I'm comfortable writing and debugging Rust on stm32. But, I tried to make my own map for DroneOS for the STM32WLE5 LoRa chip, and kinda hit a brick wall with getting the MR reviewed, etc... https://github.com/drone-os/drone-stm32-map/pull/22

James Munns12:17 PM
> Do you have any advice for how to connect with the community in a collaborative sense?

I'd definitely recommend checking out matrix! We have a rust-embedded channel, and a ton of other Rust related groups hang out there and in more specific channels!

richard12:17 PM
@andypugh I hope not

James Munns12:17 PM
They are also super helpful!

James Munns12:17 PM
Link to the Matrix room: https://matrix.to/#/#rust-embedded:matrix.org

morgan joined the room.12:17 PM

James Munns12:18 PM
> @James Munns what do you think are the best parts of learning Rust? What are the worst parts?

I think the tooling is the best in Rust, even if the language wasn't any easier to use, the build system and integrated tooling makes it super easy to do stuff like cross compiling, package management, unit testing, etc.

hpux73512:19 PM
> I'd definitely recommend checking out matrix! We have a rust-embedded channel, and a ton of other Rust related groups hang out there and in more specific channels!

James Munns12:19 PM
And since it's all PC side, you can use all the other Rust CLI tooling to help make testing/analysis easier

Marek Miettinen joined the room.12:19 PM

hpux73512:19 PM
arg, didn't mean to send. Ok, thanks :D

anfractuosity12:20 PM
How does debugging the firmware work with embedded rust out of interest? is there a specific tool for that

James Munns12:20 PM
I got a "why rust? [vs X other lang]", I'll soapbox for a minute:

Rust has a ton of developer convenience stuff, that makes it easier to re-use code or make it portable, and gets to leverage all the compile-time safety checks, which makes it way easier to optimize and reason about.

hpux73512:20 PM
OpenOCD does the protocol conversion, and it opens up a GDB server. Then you just use GDB. It's super slick

john12:20 PM
@James Munns, I've tried rust on the desktop, and binaries tend to be a bit on the big side. I tried several recommendations to trim things, but without too much success. How do you shrink code to fit in a microcontroller? how much flash would a blinking LED need?

James Munns12:21 PM
I actually don't try too hard to convince people, but if you aren't happy with your tools, there are definitely other good options

Marek Miettinen12:21 PM
I've been doing some embedded Rust projects and it seems to me that it's not really possible to use any of the wireless networking capabilities that various MCUs offer. Can you talk a bit about landscape? What are the reasons, and is there any hope we'll be able to do some wireless networking before the chip manufacturers start having official Rust support?

Joseph12:21 PM
Do you know of any aerospace engineering companies that are evaluating Rust for their safety-critical systems?

James Munns12:21 PM
> I've tried rust on the desktop, and binaries tend to be a bit on the big side. I tried several recommendations to trim things, but without too much success. How do you shrink code to fit in a microcontroller? how much flash would a blinking LED need?

Rust code statically links more aggressively, and binaries also contain debug info. Most of the time if you use `strip` on it, it gets a ton smaller!

richard12:22 PM
Are there any efforts to port RTOS to Rust?

hpux73512:22 PM
There are several Rust RTOSs available

James Munns12:22 PM
> how much flash would a blinking LED need?

About 420 bytes of flash, including the vector table!

https://interrupt.memfault.com/blog/zero-to-main-rust-1

WooDWorkeR12:22 PM
hehe 420

James Munns12:22 PM
(it was nice)

hpux73512:22 PM

https://www.drone-os.com

Drone

Drone

cargo takes development comfort to the next level, and Drone extends its philosophy to the embedded world. If your target chip and debug probe are supported by Drone, you can flash your first program and get a feedback right away! Each interrupt is an executor for async tasks.

Read this on Drone

Trevor R.H. Clarke12:23 PM
there's tik

Trevor R.H. Clarke12:23 PM
and I think there's a port of FreeRTOS

richard12:23 PM
@hpux735 any links handy otherwise I'll go google

James Munns12:23 PM
Yeah! The biggest "traditional" RTOS is tock-os, but we also have toolkits like RTIC, which is like a build-your-own toolbox of RTOS components:

https://rtic.rs/0.5/book/en/

Trevor R.H. Clarke12:23 PM

https://awesomeopensource.com/project/rust-embedded/awesome-embedded-rust

hpux73512:23 PM
There's one up a bit :point up: 👆 called Drone-OS

RichardCollins12:23 PM
Can you tell me how much memory Rust uses for run time on a processor? And what the minimum development system requires? The "community" is chaotic on the Internet. But if it were to mature, I am sure that can be fixed. If there were a "rust" code that could be added onto any processor die, that would help. You keep talking about current things, but think long term. Ten years from now.

James Munns12:24 PM
Also the folks at Oxide Computer: https://oxide.computer/ are writing their own OS for server motherboard management

dkhayes117 joined the room.12:24 PM

john12:24 PM
@James Munns , how easy is to mix C and Rust? What about C++ and Rust? sometimes C and C++ is hard enough

James Munns12:25 PM
> Can you tell me how much memory Rust uses for run time on a processor? And what the minimum development system requires?

Rust doesn't have a (dynamic) runtime, like C or C++, so there really is no lower limit. It depends on what you do. My firmware is usually in the 1-10's of KiBs, 50-100KiB would be a really huge project.

morgan12:25 PM
also interested in hearing about Rust binding into C code, crates?

James Munns12:26 PM
> Do you know of any aerospace engineering companies that are evaluating Rust for their safety-critical systems?

Yes! We've talked to some as part of our https://ferrous-systems.com/ferrocene/ project.

Trevor R.H. Clarke12:26 PM
FFI is how you call C code...I find it similar to cython if you've used that....define what functions you want to share (externs) and optionally some glue code...then you call it in an unsafe {} block

James Munns12:27 PM
Rust works really well for FFI, as it can speak the C ABI. There are tools like "bindgen" and "cbindgen" that automatically produce headers/definitions for each language

dkhayes11712:27 PM

https://github.com/rust-lang/rust-bindgen

GitHub rust-lang

rust-lang/rust-bindgen

bindgen automatically generates Rust FFI bindings to C (and some C++) libraries. For example, given the C header doggo.h: typedef struct Doggo { int many; char wow; } Doggo; void eleven_out_of_ten_majestic_af(Doggo* pupper); bindgen produces Rust FFI code allowing you to call into the doggo library's functions and use its types: The minimum supported Rust version is 1.40.

Read this on GitHub

Trevor R.H. Clarke12:27 PM
which indicates that rust's checks (memory, type, etc.) are not used in that block

James Munns12:27 PM
https://doc.rust-lang.org/nomicon/ffi.html is a good guide!

morgan12:28 PM
cool!

James Munns12:28 PM
How does debugging the firmware work with embedded rust out of interest? is there a specific tool for that

GDB (including over a j-link or other SWD adapter) supports Rust already!

We also have lots of tools for debugging and logging super performantly: https://knurling.ferrous-systems.com/tools/

James Munns12:29 PM
And we did a guide on how to do GDB debugging and logging over a single debugger interface, for native-looking debugging in vscode!

https://ferrous-systems.com/blog/gdb-and-defmt/

john12:29 PM
@James Munns , what about hard real time, is it doable in rust?

James Munns12:29 PM
> @James Munns , what about hard real time, is it doable in rust?

Yes!

James Munns12:29 PM
There is no garbage collection or interpreter, so you can make hard-realtime execution guarantees.

James Munns12:30 PM
RE: tooling, I did a demo with some non-embedded folks a bit back, where we worked on some fun LED examples, including a local dashboard for the device LED control:

john12:31 PM
@James Munns , so rust seems like the future... if something would stop it from taking over as a systems language, what would that be?

Trevor R.H. Clarke12:31 PM
does anyone know of any LIN BUS tools for rust? (code generators, etc.)

Marek Miettinen12:31 PM
Do you know of any microcontroller/board that has Rust wireless networking support? (WiFi, Bluetooth, Zigbee, ..)?

James Munns12:32 PM
> I've been doing some embedded Rust projects and it seems to me that it's not really possible to use any of the wireless networking capabilities that various MCUs offer. Can you talk a bit about landscape? What are the reasons, and is there any hope we'll be able to do some wireless networking before the chip manufacturers start having official Rust support?

Depends what you mean! There are folks using the SoftDevice on the nrf52 family: https://github.com/embassy-rs/nrf-softdevice, as well as AT serial/SPI based wifi and other radios

James Munns12:32 PM
> so rust seems like the future... if something would stop it from taking over as a systems language, what would that be?

Something even better coming along first!

Trevor R.H. Clarke12:32 PM
you should be able to FFI to C based network stacks as well

john12:33 PM
maybe we could write a crate around lwip ;)

James Munns12:33 PM
Yup!

Trevor R.H. Clarke12:33 PM
I believe there is a port of lwip in rust avaialble

Trevor R.H. Clarke12:34 PM

https://github.com/cyrex562/netloom_rs

GitHub cyrex562

cyrex562/netloom_rs

Netloom RS is a pivot from the original Netloom project. Netloom is a port and partial re-write of the LWiP framework. todo: screenshot? Running the program Configuration File Josh M.

Read this on GitHub

James Munns12:34 PM
> maybe we could write a crate around lwip ;)

Rust has a library called smoltcp you might want to check out: https://github.com/smoltcp-rs/smoltcp

Matt12:34 PM
Are the Knurling debug tools only good for small microcontrollers like Cortex M4 or can they be used with bigger processors (Cortex A53 with Linux)?

morgan12:34 PM
ESP32 seems to have at least some support but I cannot speak to the state of it, only that its where my interest is (when I get aroudn to it)

Eric Forbes joined the room.12:34 PM

morgan12:34 PM
Has anyone here used Rust on the ESP32?

Federico Roux joined the room.12:34 PM

James Munns12:35 PM
> Are the Knurling debug tools only good for small microcontrollers like Cortex M4 or can they be used with bigger processors (Cortex A53 with Linux)?

Most of them are intended to support all architectures, and we actually created knurling from some tools we developed for devloping bare-metal Cortex-A systems

john12:35 PM
the ESP32-C3 is a RISC-V, it should be easier to go RUST

Marek Miettinen12:35 PM
I'll look into the nrf-softdevice stuff, thanks! I've written an AT serial connection using an ESP8266, but that seems a bit complicated. I was wondering if there are solutions where I don't need a separate wifi chip.

James Munns12:35 PM
I think we have the most support for Cortex-M, but if you are interested in getting it working on another platform, it should be straightforward, probably best to ping me if you run into any troubles!

Trevor R.H. Clarke12:35 PM
> the ESP32-C3 is a RISC-V, it should be easier to go RUST

has anyone used rust with existing RISC-V yet?

James Munns12:36 PM
> has anyone used rust with existing RISC-V yet?

Yup! Lots! There are links on the Awesome Embedded Rust, and Bunnie is writing the RISC-V firmware in Rust for his new betrusted device.

Trevor R.H. Clarke12:36 PM
<3

James Munns12:36 PM

https://betrusted.io/

betrusted.io

betrusted.io

Betrusted is a protected place for your private matters. It's built from the ground up to be checked by anyone, but sealed only by you. Betrusted is more than just a secure CPU - it is a system complete with screen and keyboard, because privacy begins and ends with the user.

Read this on betrusted.io

dkhayes11712:36 PM
>has anyone used rust with existing RISC-V yet?

I have, The HiFive1 Rev B with E310 SOC

James Munns12:37 PM
RISC-V is probably the second/third most well supported platform after cortex-m, maybe tied with cortex-a

James Munns12:37 PM
(for embedded rust + bare metal dev)

James Munns12:37 PM
It's also really well supported on linux environments for the more MPU type processors

Trevor R.H. Clarke12:37 PM
it would be interesting to see if rust would work pretty much out of the box on the RISC-V esps...should be fairly easy to wrap some of the xtensa libraries for more robust wifi support, etc.

salec12:38 PM
Any rust for rusty old 8-bitters ?

James Munns12:38 PM
It really does! There are also some folks building libraries for the bl602, and some other similar boards

dkhayes11712:38 PM
Still waiting on the official SVDs to create a better PAC for the ESP32-C3 @Trevor R.H. Clarke

James Munns12:38 PM
There is a "still a little rough around the edges" work to mainstream AVR support, and MSP430 (okay it's 16-bit) is also workable!

hpux73512:40 PM
I had a really hard time getting the MSP430 stuff to work a year ago

James Munns12:40 PM
I'm not an msp430 kinda person, but cr1901 is the main rust embedded person working on it for his AT2XT project

Trevor R.H. Clarke12:40 PM
anyone working on an easy-to-use arduino-like environment for rust? something to get the less programming oriented artist/musician types, etc. interested?

dkhayes11712:40 PM
RISC-V also has the GD32Vs

Patrick Van Oosterwijck12:40 PM
I've been looking at using higher level but not interpreted languages on microcontrollers, and another contender seems to be Nim. Do you have any insight in why Rust or Nim would be the way to go?

john12:41 PM
@James Munns , if C is easy to learn, hard to master, and C++ is hard to learn and impossible to master it (for reasons different than C) , where would you place Rust?

James Munns12:41 PM
> anyone working on an easy-to-use arduino-like environment for rust? something to get the less programming oriented artist/musician types, etc. interested?

Hm, we are trying to make things as easy as possible for all folks! Rather than limit the environment, we'd love to make it easier for people to learn!

James Munns12:42 PM
> C is easy to learn, hard to master, and C++ is hard to learn and impossible to master it (for reasons different than C) , where would you place Rust?

I would say "Rust is pleasantly consistent". There are some concepts to learn, but in many cases they are similar to other languages (like Python, Ruby, JS, or C++), but even though it has features from all over, they are well engineered, and work together in the ways you expect them to

dkhayes11712:42 PM
I want to see a probe-rs plug-in for IntelliJ Idea

hpux73512:42 PM
> anyone working on an easy-to-use arduino-like environment for rust? something to get the less programming oriented artist/musician types, etc. interested?

To be honest VSCode is much easier to use than the arduino IDE, in my opinion. But, it could be that I'm used to real IDEs.

Discussions