Making ch58x rust ecosystem great again
Table of Contents
This article is being 100% rawdogged by a human being. I will not waste anyone’s time on reading some slop without warning.
Why I’m doing this⌗
I’m a great fan of cheap MCUs. I enjoy making custom PCBs for projects that could easily be made from a protoboard and an arduino. And I love how cheap these devices can get.
For Cebulacamp 2025 I tried to make a badge based on this chip, produced 50 of them, with a fatal mistake in speaker design.
The goal of the badge was to have working sleep mode, and RTC timer, so at 21:37 every badge would play a chiptune song, and everyone could sing along. This more or less worked, but required annoying PCB patching. Low power operation was achieved. But how did i verify it?
See https://vcc.earth/posts/ad2-torment-nexus/ - I used this setup to verify that entering low power mode worked. I don’t have the original measurements saved, but I’ve done a new batch of measurements, when preparing code for 40C3 unannounced secret project :)
How to make rust work on new hardware⌗
The manufacturer never intended for rust to work on this chip. The programmers have to work with public sources available to them:
MCU datasheet⌗
A random public source I’ve found, the datasheet is not available on the chipmaker’s website.
https://static.chipdip.ru/lib/393/DOC047393310.pdf
SVD descriptors⌗
SVD (System View Description) is a source-of-truth for memory mapping registers, with additional metadata, eg. if a register is readable, writable, or maybe works differently depending when read from, or written to.
It works great with rust, as automated tools allow creating code wrappers that (in theory) would always forbid the user from performing illegal operations.
What’s interesting, SVDs extracted from WCH’s MounRiver IDE have mistakes in them, so in this repository, they’re patched, before being used for code generation.
https://github.com/ch32-rs/ch32-rs/tree/main/svd
Patched SVDs are then used to create PACs (Peripheral Access Crates) - these crates are generated by a script, and contain thousands of lines of code. Each peripheral gets its own wrapper, with human-readable names for registers. Thanks to that, no “magic values” (write 0b0001000 to register 0x0800002) or “header defines” (#define RANDOM_REG_VAL 0x4F) are needed in code. When reading the code, developer can simply either follow the code accessing device register, or reference it by the name in the datasheet.
Manufacturer’s SDK⌗
https://github.com/openwch/ch583
This is useful especially in LLM era - If some functionality in rust ecosystem isn’t working as expected, agent can simply lookup how it’s done in the barbaric C language, and apply any patches needed.
CH58x ecosystem and it’s issues⌗
Another benefit of using rust instead of C is portability - embassy is a
multiplatform framework, utilized across various microcontrollers. For example, if you
implement SPI trait that came from embedded-hal, you can reuse crates that communicate with SPI chips without any porting effort.
Sadly, having a lot of shared code requires effort from maintainers. Rust on CH58x is not something many people use, so upstream changes made the ecosystem a bit broken.
ch32-rs crate⌗
https://github.com/ch32-rs/ch32-rs/tree/main
This repo contains the patched SVDs and PAC generators. Generated PAC crates (for many MCUs) are then uploaded to cargo.
PAC crate for our MCU is named ch58x - and as I’m writing the article, the
newest version is 0.3.0
ch58x-hal⌗
https://github.com/ch32-rs/ch58x-hal
The HAL utilizes PAC crate for ch58x. It also integrates embassy framework, and provides some usage examples.
It also contains some vendored BLOBs (copy-pasted binary libraries) required for eg. bluetooth to work.
The issue is, mainbranch currently requires ch58x 0.4.0 - it’s not been
published on cargo, so the build is broken. The only way to make it work is to
patch the Cargo.toml, so the buildsystem directly references a package we build
maually from locally cloned ch32-rs.
The workaround I’ve made was to temporarily “vendor” the output of the ch32-rs
in my ch58x-hal fork. (https://github.com/cr1tbit/ch58x-hal/tree/main/vendor/ch58x)
The actual code⌗
https://github.com/cr1tbit/ch58x-rs-playground
In this repository, I keep examples non-specific to any board. It’s also used for
verifying that further changes I make to my ch58x-hal fork don’t break any builds.
My efforts at fixing the ecosystem⌗
At the beginning, my goal was to integrate bluetooth into my Cyberlamp project. It’s an overegineered tetrachromatic battery lamp. It has ch582 onboard, my goal was to be able to control a couple of these lamps simultaneously, by broadcasting advertisement packets.
I also wanted to fix some old issues, and make sure the ecosystem is good enough for creating a new device based on this MCU.
During work, i started with old repositories cleanup, and move to newer HAL version. This is when I discovered that things kind of don’t work at all.
The most important issue was fixing debug builds not working properly. After this was fixed, agents could autonomously work on fixing other issues.
The result⌗
The MCU can now work in very low power mode, with bluetooth still working. In theory it can sit a couple of months on a CR2032, while constantly being connectable.
This was achieved by fixing the embassy’s sleep mode - The wakeups now only happen when there’s an event to handle.
So i went from this:
Baseline measurement - no MCU sleep
To this:
Sleep between events
Sleep current of the badge between advertising events