Slide 1

Slide 1 text

Rusty Stack-chanのすすめ
 2023-05-01 M5Stack Japan Tour 2023 Spring Osaka

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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…

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

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 ← !!!!

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

宣伝
 ● Interface 2023年5月号は組込みRust特集 ● M5StampC3を使った記事あり ○ なんかページ数おかしい… (65ページ) 中林さん書きすぎw ● 組込みRust本もよろしくね! https://interface.cqpub.c o.jp/magazine/202305/ https://www.c-r.com/boo k/detail/1403