thai maker

Developing Rust applications for the ESP32-S3

Developing Rust applications for the ESP32-S3 requires a specialized toolchain because the ESP32-S3 uses dual Xtensa LX7 cores, which rely on a forked version of LLVM and Rust maintained by the Espressif Rust Ecosystem rather than standard upstream targets


Getting Started and Toolchain Setup
To write Rust for the ESP32-S3, you need specific helper tools to manage the Xtensa toolchain, flashing, and project generation: [1, 2]
  • espup: Installs and updates the required Xtensa Rust compiler fork and LLVM components. [1, 2]
  • espflash: Serial flashing utility derived from esptool to push binaries to your board. [1]
  • esp-generate: Project scaffolding tool to quickly bootstrap a new no-std or std project. [1]
  • Refer to The Rust on ESP Book for official installation instructions. [1]


Choosing Your Development Paradigm
  1. no_std (Bare-Metal with esp-hal):
    • Best for ultra-low power, minimal resource overhead, and maximum performance.
    • Uses esp-hal, the official hardware abstraction layer supporting peripherals like GPIO, SPI, I2C, and DMA.
    • Can be paired with async runtimes like Embassy for clean asynchronous peripheral handling. [1, 2, 3]
  2. std (Using esp-idf):
    • Best if you need standard library features, Wi-Fi/BLE stacks via higher-level abstractions, or integration with the Espressif IoT Development Framework.
    • Targets xtensa-esp32s3-espidf. [1, 2]   


การเริ่มต้นใช้งาน ESP32-S3 ด้วย Rust ในโหมด no_std (Bare-Metal) จะช่วยให้โค้ดของคุณทำงานได้เร็ว กินพลังงานต่ำสุด และควบคุมฮาร์ดแวร์ได้โดยตรง โดยมีขั้นตอนการติดตั้งเครื่องมือและการสร้างโปรเจกต์แรกดังนี้ครับ
1. ติดตั้งเครื่องมือและ Toolchain
เนื่องจาก ESP32-S3 ใช้สถาปัตยกรรมแบบ Xtensa จึงต้องติดตั้งคอมไพเลอร์ Rust เวอร์ชันพิเศษที่ Espressif ดูแลอยู่ โดยเราจะใช้เครื่องมือชื่อ espup ในการจัดการทั้งหมด
เปิด Terminal แล้วรันคำสั่งตามลำดับ:
bash
# 1. ติดตั้ง espup สำหรับจัดการ Rust toolchain ของ ESP
cargo install espup

# 2. ติดตั้งคอมไพเลอร์ Xtensa Rust (ขั้นตอนนี้อาจใช้เวลาสักครู่)
espup install

# 3. ติดตั้งเครื่องมือสร้างโปรเจกต์และโปรแกรมแฟลชบอร์ด
cargo install cargo-generate
cargo install espflash cargo-espflash
2. สร้างโปรเจกต์ใหม่ด้วย esp-generate
ปัจจุบันชุมชนผู้พัฒนาแนะนำให้ใช้ esp-generate ในการเริ่มโปรเจกต์ no_std เวอร์ชั่นใหม่ ๆ ครับ ให้เปิด Terminal ในโฟลเดอร์ที่คุณต้องการทำงานแล้วรัน:
bash
# ติดตั้งตัวช่วยเจนโปรเจกต์รุ่นใหม่
cargo install esp-generate

# สร้างโปรเจกต์ใหม่
esp-generate -o esp32s3 -o unstable-hal -o esp-backtrace --headless esp32s3hello
(ระหว่างนี้จะมีเมนูให้คุณเลือกฟีเจอร์เพิ่มเติม เช่น การเปิดใช้งาน Allocator, การใช้ Async ร่วมกับ Embassy หรือการเปิดใช้งาน Wi-Fi ให้เลือกตามความต้องการพื้นฐานก่อนได้เลย)
เมื่อเสร็จแล้วให้เข้าไปที่โฟลเดอร์โปรเจกต์:
bash
cd esp32s3hello


3. โครงสร้างโค้ดเริ่มต้น (src/main.rs)
ภายในโปรเจกต์ที่สร้างขึ้น โค้ด no_std พื้นฐานสำหรับ ESP32-S3 จะใช้คอร์หลักจากไลบรารี esp-hal (Hardware Abstraction Layer) หน้าตาจะประมาณนี้:



4. คอมไพล์และแฟลชโค้ดลงบอร์ด
ต่อสาย USB จากคอมพิวเตอร์เข้ากับพอร์ต USB-to-UART หรือพอร์ต Native USB ของบอร์ด ESP32-S3 จากนั้นรันคำสั่งเพื่อคอมไพล์และอัปโหลดโค้ดทันที:
bash
cargo run --release


ความคิดเห็น

ไม่มีความคิดเห็น:

แสดงความคิดเห็น