Skip to content

Commit

Permalink
🎉 init project
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Feb 4, 2024
0 parents commit 77ad0b3
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[build]
target = "xtensa-esp32-espidf"

[target.xtensa-esp32-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

[unstable]
build-std = ["std", "panic_abort"]

[env]
MCU="esp32"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.2"

40 changes: 40 additions & 0 deletions .github/workflows/rust_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Continuous Integration

on:
push:
paths-ignore:
- "**/README.md"
pull_request:
workflow_dispatch:

env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
rust-checks:
name: Rust Checks
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
action:
- command: build
args: --release
- command: fmt
args: --all -- --check --color always
- command: clippy
args: --all-targets --all-features --workspace -- -D warnings
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Enable caching
uses: Swatinem/rust-cache@v2
- name: Setup Rust
uses: esp-rs/xtensa-toolchain@v1.5
with:
default: true
buildtargets: esp32
ldproxy: true
- name: Run command
run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.vscode
/.embuild
/target
/Cargo.lock
31 changes: 31 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "esp32-rust-example"
version = "0.1.0"
authors = ["Christian M <chriamue@gmail.com>"]
edition = "2021"
resolver = "2"
rust-version = "1.71"

[profile.release]
opt-level = "s"

[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"

[features]
default = ["std", "embassy", "esp-idf-svc/native"]

pio = ["esp-idf-svc/pio"]
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]

[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.48", default-features = false }

[build-dependencies]
embuild = "0.31.3"
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# esp32-rust-example

This is a simple example of how to use Rust to program an ESP32 microcontroller.

This project is based on the [esp-idf-template](https://github.com/esp-rs/esp-idf-template) project.

## Prerequisites

See [Prequisites](https://github.com/esp-rs/esp-idf-template?tab=readme-ov-file#prerequisites)

Add user to dialout group to access serial port:

```bash
sudo usermod -a -G dialout $USER
```

## Board Information

```bash
cargo espflash board-info
```

outputs:

```bash
[2024-02-04T12:12:44Z INFO ] Serial port: '/dev/ttyUSB0'
[2024-02-04T12:12:44Z INFO ] Connecting...
[2024-02-04T12:12:45Z INFO ] Unable to connect, retrying with extra delay...
[2024-02-04T12:12:46Z INFO ] Using flash stub
Chip type: esp32 (revision v1.0)
Crystal frequency: 40MHz
Flash size: 4MB
Features: WiFi, BT, Dual Core, 240MHz, Coding Scheme None
MAC address: 24:6f:28:00:00:00
```

### Troubleshooting

If you get an error like:

```bash
[2024-02-04T12:12:44Z INFO ] Serial port: '/dev/ttyUSB0'
[2024-02-04T12:12:44Z INFO ] Connecting...
[2024-02-04T12:12:45Z INFO ] Unable to connect, retrying with extra delay...
```

Try to press the boot button on the ESP32 board and run the command again.

## Building

```bash
. $HOME/export-esp.sh
cargo build
```

## Flashing

```bash
cargo espflash monitor
```
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
embuild::espidf::sysenv::output();
}
34 changes: 34 additions & 0 deletions diagram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 1,
"editor": "wokwi",
"author": "Christian M <chriamue@gmail.com>",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 0.59,
"left": 0.67,
"attrs": {
"flashSize": "16"
}
}
],
"connections": [
[
"esp:TX",
"$serialMonitor:RX",
"",
[]
],
[
"esp:RX",
"$serialMonitor:TX",
"",
[]
]
],
"serialMonitor": {
"display": "terminal",
"convertEol": true
}
}
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "esp"
10 changes: 10 additions & 0 deletions sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000

# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000

# Workaround for https://github.com/espressif/esp-idf/issues/7631
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();

// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();

log::info!("Hello, world!");
}
5 changes: 5 additions & 0 deletions wokwi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[wokwi]
version = 1
gdbServerPort = 3333
elf = "target/xtensa-esp32-espidf/debug/esp32-rust-example"
firmware = "target/xtensa-esp32-espidf/debug/esp32-rust-example"

0 comments on commit 77ad0b3

Please sign in to comment.