M5Stack Spring Tour 2023 OsakaのLTで発表した、Rustでｽﾀｯｸﾁｬﾝファームウェアを開発する内容です。
M5Stack Japan Tour 2023 Spring Osaka
● 井田 健太
○ まえはXilinx FPGAさわってました
● twitter: @ciniml
Firmware of Stack-chan
● Two types of existing Stack-chan ﬁrmwares
○ Written in Arduino
■ Most famous one is AI stack-chan ﬁrmware authored by @robo8080 san
○ Written in TypeScript for Moddable
■ Authored by ししかわ san (also known as the author of Stack-chan itself)
● There doesn't seem to be any ﬁrmware written in MicroPython/UI Flow.
○ If you have any information, please let me know.
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 ﬁrmware implements reﬁt version of M5Stack-Avatar
● Controls servo motors forj panning and tilt.
○ ServoEasing Arduino library.
○ Moddable ﬁrmware also implements easing? (I don’t know about it)
● Wireless communications, etc…
Platforms/Languages for ESP32 (1/2)
● Oﬃcial 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++)
○ Python interpreter optimized for embedded systems.
○ Based on Python 3.6 language spec + some extensions.
Platforms/Languages for ESP32 (2/2)
○ ECMA Script compiler for embedded systems.
○ User can use TypeScript compiler to generate ECMA Script run on Moddable.
○ 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.
The Rust Language
● “A language empowering everyone to build reliable and eﬃcient
○ 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
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 ﬁrmware and less compilation time.
○ You CANNOT use rich ESP-IDF functionality at all.
○ WiFi and BLE communications are currently experimental.
Writing Stack-chan firmware in Rust
● I've started writing Stach-chan’s ﬁrmware in Rust.
● There is a lot of functions to implement.
○ Showing “kawaii” face.
○ Controlling servo motors.
○ M5Stack Core2 platform support ← !!!!
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.
■ Deﬁning shapes, then draw to the display.
○ Around 3 days to port the minimal functionality.
Controlling servo motors
● Implementing easing function by Rust.
○ Ported from Wio Terminal-chan’s ﬁrmware. (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.
M5Stack Core2 platform support (1/2)
● Power management IC (AXP192) driver
○ Core2 uses AXP192 to control its power source.
○ In order to conﬁgure 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 oﬃcial driver for Core2 to Rust.
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.
Make it all together!
● Rusty Stack-chan!
● Drawing LCD is too slow.
○ around 2 frames/s
○ LCD driver for ILI9341 is not optimized well.
● LovyanGFX is great.
● M5Stack Coreシリーズは
○ LCD, ボタン, センサデバイス
● Interface 2023年5月号は組込みRust特集
○ なんかページ数おかしい… (65ページ)