Example to run Rust code on the MCH2022 badge.

Overview

Rust on the MCH2022 badge

This repo contains instructions and code to run Rust on the MCH2022 badge. There are two approaches regarding environment:

When using devcontainers, it supports Wokwi simulation: Rusto onMCH2022

It also contains instructions and recommendations on how to start a new project.

Installing the toolchains

Install the Rust toolchain for ESP, see full instructions here

  1. Install the prerequisites
    • Linux
    • MacOs: No prerequisites are needed
  2. Clone the repository or download the installer
    • If downloading the installer, make it executable: chmod a+x install-rust-toolchain.sh
  3. Run the following command:
    • Linux/MacOs:
      ./install-rust-toolchain.sh \
      --extra-crates "cargo-espflash ldproxy" \
      --clear-cache "YES" --export-file ~/export-esp.sh \
      --esp-idf-version "release/v4.4" \
      --minified-esp-idf "YES" \
      --build-target "esp32"
  4. Update the environment variables as told at the end of the installation script.

Configure Wifi

Copy the file './src/wifi_creds_example.rs' to './src/wifi_creds.rs' Set the Wifi credentials in the file './src/wifi_creds.rs' to the proper SSID and password.

Build

cargo build

Flash

We are setting cargo espflash --monitor as custom runner in .cargo/config.toml, so we can use:

cargo run [OPTIONS] [SERIAL] [SUBCOMMAND]

And it will flash the target in the SERIAL port and open a serial monitor after flashing. We can also use cargo-espflash directly:

cargo espflash [OPTIONS] [SERIAL] [SUBCOMMAND]

See Usage section of cargo-espflash for information on arguments.

Devcontainers

The repository supports:

Note

When using VS Code Dev Containers or GitHub Codespaces, we can pull the image from Dockerhub instead of builing it from the Dockerfile. In order to do so, please, uncomment the image property of .devcontainer/devcontainer.json and comment the build property.

Warning

When using GitHub Codespaces, we need to make the ports public, see instructions, in order to flash and run Wokwi simulations.

Build

  • UI approach:
    • From UI: Press Build on the left side of the Status Bar.
    • From the Command Palette (Ctrl-Shift-P or Cmd-Shift-P) run the Tasks: Run Build Task command.
    • Terminal-> Run Build Task in the menu.
    • With Ctrl-Shift-B or Cmd-Shift-B.
    • From the Command Palette (Ctrl-Shift-P or Cmd-Shift-P) run the Tasks: Run Task command and select Build.
  • Terminal approach:
    ./build.sh  [debug | release]
    

    If no argument is passed, release will be used as default

Flash

  • UI approach:
    • From UI: Press Build & Flash on the left side of the Status Bar.
    • From the Command Palette (Ctrl-Shift-P or Cmd-Shift-P) run the Tasks: Run Task command and select Build & Flash.
  • Terminal approach:
    • Manual approach :

      cargo build --release
      cd target/xtensa-esp32-espidf/release/
      

      (After each cargo build) Convert the elf image to binary:

      esptool.py --chip esp32 elf2image rus4mch
      

      Mount an uSD card and copy the .bin file to the card. Install via the development tools -> File browser (SD card) Run via the installed apps.

    • Using flash.sh script: (Not supported at this moment)

      ./flash.sh [debug | release]
      

      If no argument is passed, release will be used as default

  • Any alternative flashing method from host machine.

Wokwi Simulation

  • UI approach:

    The default test task is already set to build the project, and it can be used in VsCode and Gitpod:

    • From UI: Press Build & Run Wokwi on the left side of the Status Bar.
    • From the Command Palette (Ctrl-Shift-P or Cmd-Shift-P) run the Tasks: Run Test Task command
    • With Ctrl-Shift-, or Cmd-Shift-,

      Note

      This Shortcut is not available in Gitpod by default.

    • From the Command Palette (Ctrl-Shift-P or Cmd-Shift-P) run the Tasks: Run Task command and select Build & Run Wokwi.
  • Terminal approach:

    ./run-wokwi.sh [debug | release]
    

    If no argument is passed, release will be used as default

Warning

The simulation will pause if the browser tab is in the background. This may affect the execution, especially when debugging.

Debugging with Wokwi

Wokwi offers debugging with GDB.

  • UI approach:

    1. Run the Wokwi Simulation in debug profile
    2. Go to Run and Debug section of the IDE (Ctrl-Shift-D or Cmd-Shift-D)
    3. Start Debugging by pressing the Play Button or pressing F5
    4. Choose the proper user:
      • esp when using VsCode or GitHub Codespaces
      • gitpod when using Gitpod
  • Terminal approach:

    $HOME/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gdb target/xtensa-esp32-espidf/debug/rust4mch -ex "target remote localhost:9333"
    

    Warning

    Be sure to build the project in debug mode

    Wokwi Blog: List of common GDB commands for debugging.

Inspiration

Creating Your own project

Using cargo-generate is recomeneded. In order to install it:

  • Append it to the --extra-crates: --extra-crates "cargo-espflash ldproxy cargo-generate"
  • Install it: cargo install cargo-generate

Std

Use esp-idf-template as starting point:

cargo generate  https://github.com/esp-rs/esp-idf-template

cargo-generate will as you a few questions, after those, you will have a "Hello, world!" Rust binary crate for the ESP-IDF framework.

No-Std

Use esp-template as starting point:

cargo generate  https://github.com/esp-rs/esp-template

cargo-generate will as you a few questions, after those, you will have a bare-metal minimalist project!

Hello world example

This project added a minimal hello world example in the examples directory. You can run it as follows:

cargo build --example hello_world
cd target/xtensa-esp32-espidf/debug/examples/
esptool.py --chip esp32 elf2image hello_world
../../../../tools/webusb_push.py hello_world hello_world.bin

Reset the badge. Connect a serial program to /dev/ttyACM0 at 115k2 baud. After that you can navigate to the apps and run the app.

Rust on ESP32 STD Demo App

The best example on useing std Rust on the ESP32 can be found here. It uses a lot of peripheral and protocols.

https://github.com/ivmarkov/rust-esp32-std-demo.git

git clone https://github.com/ivmarkov/rust-esp32-std-demo.git
cd rust-esp32-std-demo/
You might also like...
A simplified example in Rust of training a neural network and then using it based on the Candle Framework by Hugging Face.

candle-simplified-example A simplified example in Rust of training a neural network and then using it based on the Candle Framework by Hugging Face. H

🤖 just is a handy way to save and run project-specific commands.

just just is a handy way to save and run project-specific commands. (非官方中文文档,这里,快看过来!) Commands, called recipes, are stored in a file called justfile

1 library and 2 binary crates to run SSH/SCP commands on a
1 library and 2 binary crates to run SSH/SCP commands on a "mass" of hosts in parallel

massh 1 library and 2 binary crates to run SSH/SCP commands on a "mass" of hosts in parallel. The binary crates are CLI and GUI "frontends" for the li

koyo is a cli tool that lets you run commands as another user. It is similar to doas or sudo.

koyo is a cli tool that lets you run commands as another user. It is similar to doas or sudo.

Helps cargo build and run apps for iOS

cargo-xcodebuild Helps cargo build and run apps for iOS. 📦 ⚙️ 🍏 Setup You need to install Xcode (NOT just Command Line Tools!), xcodegen, cargo-xcod

A simple, modern fuzzy finder tool to run examples in a Cargo project.

cargo-rx cargo-rx is a simple, modern Runner for Examples in a Cargo project. This crate provides a single executable: rx. Basically anywhere you woul

CLI application to run clang-format on a set of files specified using globs in a JSON configuration file.
CLI application to run clang-format on a set of files specified using globs in a JSON configuration file.

run_clang_format CLI application for running clang-format for an existing .clang-format file on a set of files, specified using globs in a .json confi

A run-codes cli front end with some extra features

run-cli Run-cli A run-codes cli front end with some extra features Report Bug · Request Feature Table of Contents About The Project Built With Getting

CLI application to run clang-tidy on a set of files specified using globs in a JSON configuration file.
CLI application to run clang-tidy on a set of files specified using globs in a JSON configuration file.

run-clang-tidy CLI application for running clang-tidy for an existing .clang-tidy file on a set of files, specified using globs in a .json configurati

Comments
  • Binary generated doesn't run on MCH2022 badge

    Binary generated doesn't run on MCH2022 badge

    Actions: Run latest firmware from the MCH2022 firmware repository (2022-06-15) Build an ELF binary with cargo run Generate stripped binary file via esptool --chip esp32 elf2image rust4ch Copy it to uSD card and install it as an app. Run the app.

    Expected. View the message ""Hello MCH2022 from Rust!" and info on serial console.

    Observed: The bootloader starts and then freezes a moment after encountering the application. Then the bootloader restart and goes to the main menu.

    Extra: See logfile rust4mch_log.txt

    bug 
    opened by jhaand 14
  • Devcontainer support, including Wokwi simulation, and readme update

    Devcontainer support, including Wokwi simulation, and readme update

    Hello! I saw your tweet a few days ago and wanted to help, so this PR:

    • Adds devcontainer support to bootstrap the environment (we can even develop ESP boards in Rust in a browser app without installing anything :exploding_head: )
      • Devcontainers in VsCode
      • Gitpod
      • GigHub Codespaces
    • Wokwi simulation: Since the board was already in Wokwi I was able to use it and simulate Rust code on it, using wokwi-server.
      • Wokwi also supports GDB debugging, so I included launch.json with the necessary configuration to debug the project from devcontainers
    • When using devcontainers, we cant flash with cargo-espflash so I added web-flash to flash real devices from devcontainers!
    • Add some task to build, flash, and run Wokwi simualtions from devcontainers
    • Updated the code so now it prints a message in the display
      • Please, test it on the real HW as I only tried it on Wokwi :), beaware that you may need to change this
    • Updated the readme:
      • Updated installation commands
      • Devcontainers documentation
      • Updated the recommended way to create new projects

        Both templates repositories will soon have support for devcontainers/wokwi-server/web-flash (I am currently finishing the changes and will make a PR to the esp-rs repos)

    • Added default runner in .cargo/config.toml to use cargo-espflash with cargo run
    • Added rust-toolchain.toml to override the rustup toolchain without having to set esp toolchain as default

    Thanks for creating this great repo and playing with Rust on the MCH2022 badge! Any feedback or suggestion is more than welcome!

    opened by SergioGasquez 6
Owner
Jelle Haandrikman
General purpose hacker of hardware and software. Advocate of Freedom Software. Member of hackerspace TDvenlo.
Jelle Haandrikman
🕺 Run React code snippets/components from your command-line without config

Run React code snippets/components from your command-line without config.

Eliaz Bobadilla 11 Dec 30, 2022
Run code from many programming languages!

Langbot Creating an image Install podman Build an image: podman build -t langbot . Running a language Run podman run --rm -i langbot ./scripts/run.sh

Martin Dørum 14 Jun 9, 2022
ClangQL is a tool that allow you to run SQL-like query on C/C++ Code instead of database files using the GitQL SDK

ClangQL - Clang AST Query Language ClangQL is a tool that allow you to run SQL-like query on C/C++ Code instead of database files using the GitQL SDK.

Amr Hesham 490 Oct 23, 2024
Run your Rust CLI programs as state machines with persistence and recovery abilities

step-machine Run your CLI programs as state machines with persistence and recovery abilities. When such a program breaks you'll have opportunity to ch

Imbolc 31 Nov 23, 2022
A simple, opinionated way to run containers for tests in your Rust project.

rustainers rustainers is a simple, opinionated way to run containers for tests. TLDR More information about this crate can be found in the crate docum

wefox 4 Nov 23, 2023
ChatGPT-Code-Review is a Rust application that uses the OpenAI GPT-3.5 language model to review code

ChatGPT-Code-Review is a Rust application that uses the OpenAI GPT-3.5 language model to review code. It accepts a local path to a folder containing code, and generates a review for each file in the folder and its subdirectories.

Greg P. 15 Apr 22, 2023
Little example projects for learning Rust and building awesome cli tools! ⭐️

rust-cli-examples Examples of clean and well-tested command line utilities, written in Rust. ?? ?? What is this repo? This repo is a collection of man

Jim Lynch 5 Mar 31, 2023
This is an example Nostr rust project to enable '402 Payment Required' responses for requests to paid content.

Nostr Paywall Example This is an example Nostr rust project to enable 402 Payment Required responses for requests to paid content. To prove payment, a

Blake Jakopovic 6 May 6, 2023
TinyTodo is a Cedar Agent example, with a server in Rust and client in python

TinyTodo - OPAL and Cedar Agent Demo TinyTodo is a simple application for managing task lists. It uses OPAL and Cedar Agent to control who has access

Permit.io 4 Aug 9, 2023
A working, tested example for how to use Rust with warp and JWT

rust-jwt-example Example of JWT authentication and authorization in Rust using Warp Login curl http://localhost:8000/login -d '{"email": "user@userlan

Akhil Sharma 3 Sep 18, 2023