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.
Hey all!
James, are you online yet?
Great timing...
Yup :)
Welcome aboard. Care to kick us off with a little about yourself?
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!
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
Hi all
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
Ah yeah! I've seen that as well lately. Was that ransomware for that one?
Yes
whats the best way to get started with rust on embedded?
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.
https://docs.rust-embedded.org/ is a good place to find a couple of books/guides
I'm a newbie with rust, so I was wondering, is the ESP32 a good platform to learn rust?
I have been assuming the pipeline thing is a Shodan / SCADA attack.
do you use a HAL? or are you building rust functions to interact with your peripherals?
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.
Does Rust give embedded devices super powers not to be had with C or python. (I heard something about simultaneous LED control).
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/
I think my first question about embedded Rust would be "why?" I can think of some likely reasons, but could you elucidate?
Why choose Rust over <insert your favorite language here>?
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?
> 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
Hi James, thanks for your time.
Is it possible to link existing c/c++ libs (arduino libs) And would you recommend it? =)
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?
https://github.com/MabezDev/xtensa-rust-quickstart is the work in progress for experimental support, but cortex-m is the most supported platform
I use a bluepill/blackpill for playing with rust and it works well
> 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!
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?
> 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!
Considering the ISO 26262 requirement of MISRA, are there regulatory hurdles you see in your way?
@James Munns What are some rust hardware projects that you found really cool/novel.
> 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!
state machines in rust, are they cleaner (more readable) than in C?
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.
Linux said that too
@James Munns what do you think are the best parts of learning Rust? What are the worst parts?
> @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
@Brad Hanson Good point, I guess that _parts_ could be written in Rust. Heck, half our C is auto-generated from Simulink models.
> 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!
is Rust any better at minimizing heap fragmentation when working with low memory devices and doing a lot of dynamic allocations?
Yes, I just changed my mind, realising how much is auto-generated from Simulink / Grafcet input.
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
I agree there's going to be inertia, but that's just the auto industry liking to use known-good parts
@richard Could it be worse :-)
@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
> 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!
@andypugh I hope not
They are also super helpful!
Link to the Matrix room: https://matrix.to/#/#rust-embedded:matrix.org
> @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.
> 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!
And since it's all PC side, you can use all the other Rust CLI tooling to help make testing/analysis easier
arg, didn't mean to send. Ok, thanks :D
How does debugging the firmware work with embedded rust out of interest? is there a specific tool for that
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.
OpenOCD does the protocol conversion, and it opens up a GDB server. Then you just use GDB. It's super slick
@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?
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
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?
Do you know of any aerospace engineering companies that are evaluating Rust for their safety-critical systems?
> 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!
Are there any efforts to port RTOS to Rust?
There are several Rust RTOSs available
> 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
hehe 420
(it was nice)
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.
there's tik
and I think there's a port of FreeRTOS
@hpux735 any links handy otherwise I'll go google
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://awesomeopensource.com/project/rust-embedded/awesome-embedded-rust
There's one up a bit :point up: 👆 called Drone-OS
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.
Also the folks at Oxide Computer: https://oxide.computer/ are writing their own OS for server motherboard management
@James Munns , how easy is to mix C and Rust? What about C++ and Rust? sometimes C and C++ is hard enough
> 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.
also interested in hearing about Rust binding into C code, crates?
> 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.
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
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
https://github.com/rust-lang/rust-bindgen
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.
which indicates that rust's checks (memory, type, etc.) are not used in that block
https://doc.rust-lang.org/nomicon/ffi.html is a good guide!
cool!
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/
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/
@James Munns , what about hard real time, is it doable in rust?
> @James Munns , what about hard real time, is it doable in rust?
Yes!
There is no garbage collection or interpreter, so you can make hard-realtime execution guarantees.
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:
@James Munns , so rust seems like the future... if something would stop it from taking over as a systems language, what would that be?
does anyone know of any LIN BUS tools for rust? (code generators, etc.)
Do you know of any microcontroller/board that has Rust wireless networking support? (WiFi, Bluetooth, Zigbee, ..)?
> 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
> 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!
you should be able to FFI to C based network stacks as well
maybe we could write a crate around lwip ;)
Yup!
I believe there is a port of lwip in rust avaialble
https://github.com/cyrex562/netloom_rs
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.
> 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
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)?
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)
Has anyone here used Rust on the ESP32?
> 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
the ESP32-C3 is a RISC-V, it should be easier to go RUST
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.
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!
> the ESP32-C3 is a RISC-V, it should be easier to go RUST
has anyone used rust with existing RISC-V yet?
> 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.
<3
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.
>has anyone used rust with existing RISC-V yet?
I have, The HiFive1 Rev B with E310 SOC
RISC-V is probably the second/third most well supported platform after cortex-m, maybe tied with cortex-a
(for embedded rust + bare metal dev)
It's also really well supported on linux environments for the more MPU type processors
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.
Any rust for rusty old 8-bitters ?
It really does! There are also some folks building libraries for the bl602, and some other similar boards
Still waiting on the official SVDs to create a better PAC for the ESP32-C3 @Trevor R.H. Clarke
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!
I had a really hard time getting the MSP430 stuff to work a year ago
I'm not an msp430 kinda person, but cr1901 is the main rust embedded person working on it for his AT2XT project
anyone working on an easy-to-use arduino-like environment for rust? something to get the less programming oriented artist/musician types, etc. interested?
RISC-V also has the GD32Vs
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?
@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?
> 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!
> 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
I want to see a probe-rs plug-in for IntelliJ Idea
> 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
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.