Tokio on ESP32
- Brent Lewis
- Feb 11, 2024
- 2 min read
There's an example of using Tokio on ESP32 platforms here. However, it took me a long time to discover the same thing in the context of trying to build WebRTC, and this example demonstrates that more Tokio features are usable.
Directory Structure
.
├── build.rs
├── .cargo
│ └── config.toml
├── Cargo.toml
├── rust-toolchain.toml
├── sdkconfig.defaults
└── src
└── main.rs
2 directories, 6 files
Contents of: build.rs
fn main() {
embuild::espidf::sysenv::output();
}
Contents of: .cargo/config.toml
[build]
target = "xtensa-esp32s3-espidf"
[target.xtensa-esp32s3-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
"--cfg", "mio_unsupported_force_poll_poll" # MIO support for ESP IDF https://github.com/tokio-rs/mio/issues/1703#issuecomment-1923797872
]
[unstable]
build-std = ["std", "panic_abort"]
[env]
MCU="esp32s3"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.2"
Contents of: Cargo.toml
[package]
name = "esp32s3-tokio-sandbox"
version = "0.1.0"
authors = ["coder0xff <coder0xff@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 }
tokio = { version = "1.36.0", features = ["rt", "io-util", "macros", "net", "time"] }
[build-dependencies]
embuild = "0.31.3"
Contents of: rust-toolchain.toml
[toolchain]
channel = "esp"
Contents of: sdkconfig.defaults
# 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
Contents of: src/main.rs
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!");
}
Comentarios