0% found this document useful (0 votes)
43 views

Rust Esp32 Iot

This document discusses using the Rust programming language to build IoT applications that run on the ESP32 microcontroller. It begins by introducing Rust and its goals of safety and reliability. It then discusses some of Rust's key features like compile-time checking that eliminates classes of bugs. The document outlines how Rust has grown in popularity and taken first place in Stack Overflow surveys. It also discusses challenges of using Rust for embedded systems that don't support the standard library and shows how features like no_std address this. Finally, it presents an end-to-end example of using Rust on the ESP32 via a containerized toolchain to compile for the Xtensa architecture.

Uploaded by

V.s Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Rust Esp32 Iot

This document discusses using the Rust programming language to build IoT applications that run on the ESP32 microcontroller. It begins by introducing Rust and its goals of safety and reliability. It then discusses some of Rust's key features like compile-time checking that eliminates classes of bugs. The document outlines how Rust has grown in popularity and taken first place in Stack Overflow surveys. It also discusses challenges of using Rust for embedded systems that don't support the standard library and shows how features like no_std address this. Finally, it presents an end-to-end example of using Rust on the ESP32 via a containerized toolchain to compile for the Xtensa architecture.

Uploaded by

V.s Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

A tale of Rust,

the ESP32 and IoT


It can‘t be that hard…
Who am I?

Jens Reimann @ctron


• Principal Software Engineer https://dentrassi.de
• Red Hat
• Middleware, Messaging, IoT
• Programming languages
• 90s: Basic, Pascal, C
• 00s: C, C++, Java
• 10s: Java, Go, Rust
Telemetry data for Eclipse Hono
Goal of this talk?
Get you excited about Rust!
Why Rust?
Do we really need another programming language?
Rust

"A language empowering everyone to


build reliable and efficient software."

https://www.rust-lang.org/
Rust
“Rust is a language for systems programming.”
Jim Blandy & Jason Orendorff, Programming Rust

“Systems programming is for:



• Code that runs in very cheap devices, or devices that must be
extremely reliable


Rust is …
“A safe, concurrent language with the performance of C
and C++”
Jim Blandy & Jason Orendorff, Programming Rust

“…eliminate many classes of bugs at compile-time.”


https://www.rust-lang.org/
History of languages

http://rigaux.org/language-study/diagram.html
History of Rust
• Started 2006 by Mozilla employee Graydon Hoare
• Announced 2010 by the Mozilla Foundation
• Self-compiled 2011
• Getting things right, before moving on
• A community to grow the language, not only use it
“Rust was the third-most-loved
programming language in the 2015
Stack Overflow annual survey, and took
first place in 2016, 2017, 2018, and
2019.” – Wikipedia
Fixing stuff at compile time
• Have a compiler which understands your code
• Have language rules which prevent bugs
• Eliminate “undefined behavior”
• Reduce “unexpected behavior”
What is undefined behavior?
Undefined behavior: behavior, upon use of a nonportable or
erroneous program construct or of erroneous data, for which this
International Standard imposes no requirements
§3.4.3, Cx11

Undefined behavior: Renders the entire program meaningless if


certain rules of the language are violated.
cppreference.com
Undefined / unexpected behavior

Go FAQ Java „CME“ - HashSet


Q: „Why are map operations not “Fail-fast iterators throw
defined to be atomic? “ ConcurrentModificationException on
A: „…This was not an easy decision, a best-effort basis. Therefore, it
however, since it means uncontrolled would be wrong to write a program
map access can crash the that depended on this exception for
program.…“ its correctness: the fail-fast behavior
of iterators should be used only to
detect bugs.”
A helping compiler…

Code Output
fn main() { Compiling playground v0.0.1 (/playground)
error[E0597]: `s2` does not live long enough
--> src/main.rs:8:13
let mut s1 = "Foo"; |
8 | x = &mut s2;
let mut x = &mut s1; | ^^^^^^^ borrowed value does not live long enough
assert_eq!(*x, "Foo"); 9 | }
| - `s2` dropped here while still borrowed
10 | assert_eq!(*x, "Bar");
{ | ---------------------- borrow later used here
|
let mut s2 = "Bar"; = note: this error originates in a macro outside of the current crate (in
x = &mut s2; Nightly builds, run with -Z external-macro-backtrace for more info)
} error: aborting due to previous error

assert_eq!(*x, "Bar"); For more information about this error, try `rustc --explain E0597`.
error: Could not compile `playground`.

} To learn more, run the command again with --verbose.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cc5f9c6bbb709ac1c0e292b3042a4b40
The cost of a bug over time…
Bugs
100

90

80

70

60

50

40

30

20

10

0
Compile Test Release
Bugs
…with Rust
Bugs
100

90

80

70

60

50

40

30

20

10

0
Compile Test Release
Bugs
Dependencies
• “Crates” are the “JAR files of
Rust” … but contain code, not
binaries!
• Then “crates.io” is “Maven My App
Central for Rust”
• “cargo” manages
dependencies, and foo bar baz
orchestrates the build
and test libbaz.so
std
baz.dll
The problem with „std“
• „std“ provides all kinds of functionality
• Files, Streams, …
• Network, Sockets, …
• …
• But also requires a POSIX-like operating system
• So, what about embedded systems? Like the ESP32?
#![no_std], “core” & “alloc”
• You can disable the usage of “std” and switch to “core” instead
• If you can provide an allocator, you can also use “alloc” for
dynamic memory allocations (like String, Vec, …)
• Some crates support this by using “features”, which
enable/disable features of the crate at compile time
• e.g. “serde” with “serde-json-core”

https://crates.io/keywords/nostd
Unsafe Superpowers
• How?
• “unsafe {}” block
• “unsafe” keyword
• Why?
• Call other „unsafe“ methods
• Dereference raw pointers
• …
ESP-IDF & Rust

My App

serde / JSON
Rust
core alloc „timer“ „http“

malloc TLS HTTP


C – ESP IDF

… FreeRTOS TCP/IP WiFi


End-to-end example

https://github.com/ctron/rust-esp32-hono
End-to-end example
IDE Integration
Eclipse Corrosion
IntelliJ + Rust
One more thing…
How to compile for the Xtensa
architecture?
Forked LLVM, forked rustc and a bunch of
scripts
• Execute ~50 different commands
• Hope you have the right OS, in the right version, with the right
packages
• Wait ~3½ hours
• Enjoy … or try again
Containerized
• docker run quay.io/ctron/rust-esp
• –ti –v $PWD:/home/project
• Runs on Windows, Linux, (and should on Mac OS)
What lies ahead?
• Rust Runtime for AWS Lambda
• https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-
lambda/
• Rust Embedded Book
• https://rust-embedded.github.io/book/
• Linux kernel experiments with Rust
• https://lwn.net/Articles/797828/
• Microsoft
• https://msrc-blog.microsoft.com/2019/07/16/a-proactive-approach-
to-more-secure-code/
• „~70% of the vulnerabilities Microsoft assigns a CVE each
year continue to be memory safety issues”
Questions?
… and answers!
A few links
• Rust • Rust for ESP32
• https://www.rust-lang.org/ • https://github.com/ctron/rust-
esp-container/
• Rust Embedded Book
• https://rust- • Rust, ESP32, ESP-IDF, Hono
embedded.github.io/book/ • https://github.com/ctron/rust-
esp32-hono
• Programming Rust
• O'Reilly Media • LLVM for Xtensa
• https://github.com/espressif/llv
• Eclipse Corrosion m-xtensa
• https://marketplace.eclipse.or
g/content/corrosion-rust- • Rust fork for Xtensa
edition-eclipse-ide • https://github.com/MabezDev/r
ust-xtensa
Thank you!

You might also like