Live data from Hacker News

Embassy: Modern embedded framework, using Rust and async

github.com

41–50 of 167 posts

Re: Embassy: Modern embedded framework, using Rust and async

#41

Earlier quoted context omitted.

In my experience, most of embassy's HALs support blocking variants as well. I don't quite understand the opposition to async in this context though. Embassy's executor is quite nice. You get to write much more straightforward linear code, and it's more battery efficient because the CPU core goes to sleep at await points. The various hardware interrupts then wake up the core and notify the executor to continue making…

My general 2c on this, in context with my observations in rust embedded: I think you are overestimating the difficulty of doing these tasks without Async. I point out again, that the Async vs blocking meme, while widespread, is not accurate. There is nothing about Async that makes it more battery efficient than non Async code. Hardware interrupts, sleep, or non-blocking operations are neither unique to Async, nor dif…

Is it _that_ hard? No. Is writing assembly _that_ hard? Also no. It's simple, but not ergonomic and takes time.

In C or "regular" embedded Rust, if I want to compose several tasks together, while sleeping the CPU core while waiting on interrupts, I need to scatter global variables over the code, and write custom state machines for all the "yield" points in my code. Oh and then requirements come in later and I need to add some timeouts to various operations. That gets messy quickly. Yes it's "not that hard" but Embassy is right there and it works. I get the state machines for free, I get CPU sleeps for free, the code is easier for others to jump in and work with, and with async combinators it's significantly easier to rearrange logic when new requirements get added.

Just for a concrete example from a (somewhat esoteric) project I'm working on:

https://gist.github.com/bschwind/3905ecf8acd3046d35bf750283f...

This code is receiving uncompressed video frames over USB High Speed and forwarding them to an OLED display. In this case I have the luxury of having enough SRAM to hold two framebuffers in memory, so it's a classic double-buffering strategy of displaying one buffer while the other is being filled. Using a simple combinator, `join()`, I can kick off two DMA transfers with one filling the back buffer, and the other transmitting the front buffer to the display. I can have timeouts on these operations, the code flows pretty linearly, and no external globals or custom interrupt handlers are needed (obviously these exist, but they're in the Embassy code layer). And while these transfers are happening the core is automatically sleeping, assuming I don't have other async tasks running.

To me, this is beautiful for embedded code, and brings a major ergonomic gain over the equivalent in C or even regular old embedded Rust. Obviously you don't have to use it, but I see a bright future for embedded Rust if Embassy and others (like RTIC) can keep up the momentum.

Re: Embassy: Modern embedded framework, using Rust and async

#43

I am a big fan of the embassy project and it’s a great example of why async Rust is so great: Because this is possible. It works without a heap, is a really low cost abstraction and you can do stuff concurrently on a single core chip (where you can’t just spawn a new “thread”) and you don’t have the complexity of an RTOS. I believe there is a great future for embassy ahead and it’s so great how far the team has come.…

If you had to pick a MCU to try this out on, do you have a preference for a devkit to test this with?

Re: Embassy: Modern embedded framework, using Rust and async

#44
I never learned any C based languages, so it's been a challenge, but I've enjoyed learning the basics of bare-metal no_std rust on the esp32c3, with esp-rs and its support for embassy to help me get started!

My learning project -- using mqtt for HomeAssistant integration: https://github.com/n8henrie/esp32c3-rust-mqtt>

Re: Embassy: Modern embedded framework, using Rust and async

#45

Probably off topic, but what's the best way to get started with embedded development? I've been a web developer for over a decade, but I'd really love to try something much lower level, and I'm currently making my way through the Rust book. I've got a Raspberry Pi on the way, but I assume that's not truly embedded development.

Buy a dev board with an RP2040 in it, an electronics starter kit (resistors, capacitors, LEDs, diodes, breadboard, breadboard cables, etc.) and start writing some code!

If we're talking Rust, rp-hal is great for starting, and of course Embassy is great too, though maybe Embassy is better for later when you start running into the types of problems that it aims to solve.

rp-hal: https://github.com/rp-rs/rp-hal

Re: Embassy: Modern embedded framework, using Rust and async

#46
post #25

This channel contains videos of journey from setting up environment and busy wait embedded LED blinking, to basically re-inventing and then using Embassy. 4 oldest videos. https://www.youtube.com/@therustybits/videos

Wow, this dude is good! Thanks for sharing.

Re: Embassy: Modern embedded framework, using Rust and async

#47
post #23

This is at the center of a friction point in embedded rust: most of the OSS ecosystem has shifted to this framework, and as a result, is incompatible with, or is high friction if you don't want to make your firmware and control flow Async. This is notable because Rust embedded is nascent and small, so I think splitting the ecosystem along with Async is not ideal. It's also confused some people new to embedded: I regu…

How async works with embassy is also interesting. In effect it works like a work queue, when something is waiting on a waker (interrupt) and is woken, the Future (task) is enqueued to be polled (run) by the executor. There’s good and bad things about this. It’s clever for sure but there can be variable latency between when the hardware event occurs and when the next step in the task starts. This is a lot like zephyr/…

Here's a good technical writeup on latency and jitter (latency standard deviation) for interrupts when it comes to Embassy, FreeRTOS, and RTIC:

https://tweedegolf.nl/en/blog/65/async-rust-vs-rtos-showdown

Obviously if you're working on something truly hard real-time you probably wouldn't be reaching for these tools to begin with, but for the average embedded project it seems you will enjoy quite good latency and jitter characteristics by default.

Re: Embassy: Modern embedded framework, using Rust and async

#48
post #43

I am a big fan of the embassy project and it’s a great example of why async Rust is so great: Because this is possible. It works without a heap, is a really low cost abstraction and you can do stuff concurrently on a single core chip (where you can’t just spawn a new “thread”) and you don’t have the complexity of an RTOS. I believe there is a great future for embassy ahead and it’s so great how far the team has come.…

If you had to pick a MCU to try this out on, do you have a preference for a devkit to test this with?

RP2040 is really great experience. You can get a debug probe (either buy or make yourself with another RP2040)

I cant tell you how awesome it is with minimal setup to get

- Full print logging

- Option to attach a debugger

- cargo r will just flash your code and reset the RP2040

Re: Embassy: Modern embedded framework, using Rust and async

#49

I never learned any C based languages, so it's been a challenge, but I've enjoyed learning the basics of bare-metal no_std rust on the esp32c3, with esp-rs and its support for embassy to help me get started! My learning project -- using mqtt for HomeAssistant integration: https://github.com/n8henrie/esp32c3-rust-mqtt >

I know bare metal programming is a learning exercise but i can say for 99% of use cases esphome is awesome. I just finished making a cat feeder a esphome smart device
Post reply on HN