$30 off During Our Annual Pro Sale. View Details »

Rusty Stack-chanのすすめ

Rusty Stack-chanのすすめ

M5Stack Spring Tour 2023 OsakaのLTで発表した、Rustでスタックチャンファームウェアを開発する内容です。

Kenta IDA

May 02, 2023

More Decks by Kenta IDA

Other Decks in Programming


  1. Rusty Stack-chanのすすめ

    M5Stack Japan Tour 2023 Spring Osaka

    View Slide

  2. 自己紹介

    ● 井田 健太
    ● おしごと:ESP32のファームウェアを書く
    ○ まえはXilinx FPGAさわってました
    ● twitter: @ciniml

    View Slide

  3. Firmware of Stack-chan

    ● Two types of existing Stack-chan firmwares
    ○ Written in Arduino
    ■ Most famous one is AI stack-chan firmware authored by @robo8080 san
    ■ https://github.com/robo8080/M5Unified_StackChan_ChatGPT
    ○ Written in TypeScript for Moddable
    ■ Authored by ししかわ san (also known as the author of Stack-chan itself)
    ● There doesn't seem to be any firmware written in MicroPython/UI Flow.
    ○ If you have any information, please let me know.

    View Slide

  4. Main function of Stack-chan firmware

    ● Shows “kawaii” face on the display of M5Stack.
    ○ M5Stack-Avatar (by ししかわさん) is mainly used to implement this.
    ○ Some variants of M5Stack-Avatar are also available.
    ○ Moddable firmware implements refit version of M5Stack-Avatar
    ● Controls servo motors forj panning and tilt.
    ○ ServoEasing Arduino library.
    ○ Moddable firmware also implements easing? (I don’t know about it)
    ● Wireless communications, etc…

    View Slide

  5. Platforms/Languages for ESP32 (1/2)

    ● Official platform from Espressif is ESP-IDF
    ○ Mainly written in C. some part (e.g. NVS driver) is written in C++.
    ○ User code can be written in both C, C++.
    ● Arduino core for the ESP32
    ○ Arduino core for ESP32 series.
    ○ Based heavily on ESP-IDF
    ○ User code: Arduino language (C++)
    ● MicroPython
    ○ Python interpreter optimized for embedded systems.
    ○ Based on Python 3.6 language spec + some extensions.

    View Slide

  6. Platforms/Languages for ESP32 (2/2)

    ● Moddable
    ○ ECMA Script compiler for embedded systems.
    ○ User can use TypeScript compiler to generate ECMA Script run on Moddable.
    ● TinyGo
    ○ Go language compiler for embedded systems.
    ○ You can use powerful Go language multithreading feature like goroutines and channels
    on embedded systems.
    ● Rust ← New!
    ○ Zero cost, memory safe language, used mainly at desktop and server.
    ○ Can generate binaries for embedded systems.

    View Slide

  7. The Rust Language

    ● “A language empowering everyone to build reliable and efficient
    software.” (https://www.rust-lang.org/)
    ○ Zero cost abstractions
    ○ Memory safety without GC
    ○ Powerful package manager and build system (cargo)
    ● Rust applications for embedded systems are increasing.
    ○ Linux kernel module
    ○ Microsoft also uses Rust for writing kernel of their OS.
    ● Espressif is also actively working on creating a development environment
    for Rust.

    View Slide

  8. Rust Platforms for ESP32

    ● There are 2 options available.
    ● Using esp-idf-sys and write application for ESP-IDF in Rust.
    ○ On this platform, you can use ESP-IDF functionality from Rust.
    ○ You can also use FreeRTOS functionality via Rust threading library.
    ● Using esp-hal and write bare-metal application in Rust. ← I used this.
    ○ No ESP-IDF dependency.
    ○ You can get very compact firmware and less compilation time.
    ○ You CANNOT use rich ESP-IDF functionality at all.
    ○ WiFi and BLE communications are currently experimental.

    View Slide

  9. Writing Stack-chan firmware in Rust

    ● I've started writing Stach-chan’s firmware in Rust.
    ● There is a lot of functions to implement.
    ○ Showing “kawaii” face.
    ○ Controlling servo motors.
    ○ M5Stack Core2 platform support ← !!!!

    View Slide

  10. Showing “kawaii” face with Rust

    ● I’ve ported M5Stack-Avatar for Rust graphics library.
    ○ M5Stack-Avatar uses FreeRTOS functions, which does not exist in baremetal ESP32.
    ○ The structure of M5Stack-Avatar works well with embedded-graphics.
    ■ Defining shapes, then draw to the display.
    ○ Around 3 days to port the minimal functionality.

    View Slide

  11. Controlling servo motors

    ● Implementing easing function by Rust.
    ○ Ported from Wio Terminal-chan’s firmware. (which is also written in Rust)
    ● Implementing PWM output driver.
    ○ esp-hal crate has MCPWM peripheral driver. so all I need to do is just call it.

    View Slide

  12. M5Stack Core2 platform support (1/2)

    ● Power management IC (AXP192) driver
    ○ Core2 uses AXP192 to control its power source.
    ○ In order to configure powers for peripherals, AXP192 must be initialized correctly.
    ■ Peripherals, LCD backlights, etc…
    ○ I have experience implementing M5StickC AXP192 driver for ESP-IDF in C++.
    ○ I’ve ported this driver and M5Stack official driver for Core2 to Rust.

    View Slide

  13. M5Stack Core2 platform support (2/2)

    ● LCD (ILI9342) driver
    ○ Core2 uses ILI9342 LCD controller.
    ○ There is existing library, “ili9341” crate in crates.io, but it is a bit old and do not
    compatible with current embedded-hal crate.
    ○ I’ve ported my ILI9341 library I wrote a few years ago to latest environment.

    View Slide

  14. Make it all together!

    ● Rusty Stack-chan!
    ● 全部がっちゃんこ
    ● とりあえず動いた!
    ● デモ展示あり

    View Slide

  15. Known Issues

    ● Drawing LCD is too slow.
    ○ around 2 frames/s
    ○ LCD driver for ILI9341 is not optimized well.
    ● LovyanGFX is great.

    View Slide

  16. まとめ

    ● スタックチャンの基本機能は実装できた.
    ● ESP32のRust環境がかなり整ってきた
    ○ Espressifが開発リソースをかなり突っ込んでいる
    ● M5Stack Coreシリーズは
    ○ LCD, ボタン, センサデバイス
    ● スタックチャンはやはりスーパーカワイイ
    ● Rustはいいぞ

    View Slide

  17. 宣伝

    ● Interface 2023年5月号は組込みRust特集
    ● M5StampC3を使った記事あり
    ○ なんかページ数おかしい… (65ページ)
    ● 組込みRust本もよろしくね!

    View Slide