Rust Everywhere
Nvim & Rust

Rust Everywhere

Over the December break I decided to start looking at learning Rust, the main reason really is that I have been using "C" for all my embedded controller code and stumbled on some articles mentioning that Rust works great on a lot of micro-controllers and saw that for the controllers I use (Atmega640, ESP32 and Arm Cortex M series) it has good support.

The first step was to look at some online tutorials (https://doc.rust-lang.org/book/) is a good starting place. Having some experience in "C" and "Go" , I thought that after a week or two I should be up to speed, converting a lot of the Go micro-services I have build to Rust and then once that was out the way start seriously on the embedded projects. I also looked at a pile of Youtube videos and tutorials on Rust.

Almost 3 months later and I'm still at beginner level, still learning, still pulling my hair out, Rust is not for the fainthearted, I found an article that states the benefits of Rust "The main purpose of using Rust is enhanced safety, speed, and concurrency, In simple words, Rust is used for three essential purposes in programming; performance, safety, and memory management." Rust does not have a garbage collector, all variables are immutable by default, it has the concept of ownership, the compiler is extremely helpful with its borrow checker, it won't allow your code to compile if you have memory issues, it's frustrating, yet when you see the benefits of "fearless concurrency" and the guarantee that when your code compiles it is memory safe, no race conditions, it does give you a great sense of confidence.

So back to my embedded journey I created an ESP32S3 (16MB of flash memory, 32 bit micro controller running at 40Mhz), using Kicad (an amazing opensource electronics cad program that runs on Linux). I sent the board artifacts to OSHPark for manufacturing and then bought the components from a local electronics distributor (mouser.it). It took me about an hour to solder all the components and do a simple power-on test.


No alt text provided for this image
The assembled ESP32S3 board

The next step took me a while to get working, I built the rust toolchain for esp from scratch (thanks to Sylvain Kerkour https://kerkour.com/compile-rust-for-esp32-xtensa-on-raspberry-pi-aarch64)

mkdir esp32
$ cd esp32
$ git clone https://github.com/esp-rs/rust.git
$ cd rust        

You need to get the LLVM project as esp32 is not upstream yet

$ git submodule update --init --recursive --remote src/llvm-project        

Then, you can finally build Rust

$ ./configure --experimental-targets=Xtensa
$ python3 x.py dist --stage 2
# still in esp32/rust
$ rustup toolchain link esp `pwd`/build/x86_64-unknown-linux-gnu/stage2        

Build LLVM - both took a while to compile , so if you are this adventurous, be patient :)

$ cd src/llvm-project
$ mkdir build
$ cd build
$ cmake -G Ninja -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release ../llvm
$ cmake --build .
$ export PATH="$HOME/esp32/rust/src/llvm-project/build/bin:$PATH"        

Once I had my rust environment set, I started on getting the tools to compile and flash my specific ESP32S3 target.

$ cargo install -f ldproxy espflash espmonitor
$ rustup default esp        

I created a simple webserver from the project https://github.com/ivmarkov/rust-esp32-std-demo. Compiled it for the target (set in the .cargo/config.toml file). Then spent about an hour to figure out why the espflash was not working. I found that in my design I needed to pull the GPIO0 (Boot) pin for the ESP32 to ground and press the reset button on the board.

Finally got the espflash to upload and flash the code

No alt text provided for this image
No alt text provided for this image

I then went into the serial console to verify the messages and boot sequence

No alt text provided for this image

Finally executed a curl command (in a loop) to verify it was all working

No alt text provided for this image

This was one the hardest "learning objectives", I have tackled, Rust is different, you need lots of time to understand it, practice it and grasp its uniqueness with regards to memory safety.

For me now its Rust everywhere, I'm going to finish off my Arm Cortes M series designs, build and embed Rust and connect them to the ESP32 interface, this will make for a fast robust generic IoT device. I'm looking at WebAssembly next, using a WASM runtime on Kubernetes (the project is called Krustlet), all written in Rust, I honestly think that the future is Rust, my prediction is (like in the Linux kernel now) all high performance and highly scalable enterprise applications will be written in Rust. Its truly going to be "Rust Everywhere"

Marco Troisi

CTO @ Trilo ? Cloud Consultant ? Writer @ The Serverless Mindset ? AWS Community Builder

2 å¹´

You can rely on Luigi?to be a great judge of what the future of technology looks like. I’ve seen him getting it right time and again over the years. It’s time to learn Rust!

Anton Whalley

Open to conversations on Edge systems and software delivery

2 å¹´

And inspired me to spend the evening with an old Amica board Conall Laverty gave to me a few years ago Thanks again

  • 该图片无替代文字
Chirag Kyal

Software Engineer at Red Hat || Prev : IBM Cloud ?? || OpenSource ?? || Kubernetes || Golang

2 å¹´

Very nice article Luigi. May be writing a K8s Operator in rust would be fun ??

Anton Whalley

Open to conversations on Edge systems and software delivery

2 å¹´

Fantastic article I went WASM first and I'm now starting to look at the embedded pieces All roads lead to Rome ?? FYI I've built a wasm deployer for k8s here that also works on microshift 4.12 Might be of interest in your research (Built with Rust ?? of course) https://github.com/knawd/deployer

要查看或添加评论,请登录

Luigi Zuccarelli的更多文章

  • Predicting 'NEWS' using a simple neural network in Rust

    Predicting 'NEWS' using a simple neural network in Rust

    Introduction I while back I wrote this article using the NEWS (National Early Warning Score) and a ML models with Scala…

  • Podman Executing Unikernels ?

    Podman Executing Unikernels ?

    I'm really big fan of Podman, as a team we are responsible for a component (oc-mirror) that does bulk copying of…

    8 条评论
  • Unikernel Platform As A Service

    Unikernel Platform As A Service

    We are extremely fortunate at Red Hat. We have the opportunity to work on technology that could benefit the open-source…

    3 条评论
  • Rust is fast (super fast)

    Rust is fast (super fast)

    In a previous article, I posted on LinkedIn (see here), I wrote about a highly available and scalable IoT solution…

    2 条评论
  • High Availability and Scalable IOT Edge Service using a Raspberry Pi Cluster

    High Availability and Scalable IOT Edge Service using a Raspberry Pi Cluster

    I built a cluster of Raspberry PI's (6 pi3s and 1 pi4). They are fairly old now (except for the pi4), I used them for…

    1 条评论
  • You gotta love Red Hat Universal Base Images (ubi)

    You gotta love Red Hat Universal Base Images (ubi)

    When working with Linux Containers, I create all images using centos8 for development (dev) and user acceptance testing…

    2 条评论
  • My disastrous technical interview

    My disastrous technical interview

    I was a bit reluctant to write this article because I felt I would be exposing myself. I have over 25 years of…

    14 条评论
  • Custom CICD pipeline with less than 1 minute end to end builds

    Custom CICD pipeline with less than 1 minute end to end builds

    I am a big fan of Tekton and was recently on a team that completed a proof of concept over a period of 2 months using…

  • Event based on demand IOT measurements

    Event based on demand IOT measurements

    In my last article I wrote about a gRPC microservice event service (without a message broker) based on a design…

  • Taking some design principles from kubernetes

    Taking some design principles from kubernetes

    I watched a video a while ago about a deep dive into kubernetes architecture. It covered a few principles but the one…

社区洞察

其他会员也浏览了