We are absurdly spoiled these days by our microcontrollers. Take the CH32V00X family– they’ve been immortalized by meme as “the ten cent micro” but with a clock speed of 48MHz and 32-bit registers to work with, they’re astoundingly capable machines even by the standards of home computers of yore. That’s what motivated [Tim] to see if he could use one to play MOD files, with only minimal extra parts– and quite specifically no DAC.
Well, that’s part of what motivated him. The other part was seeing Hackaday feature someone use a CH32V003 making chiptune-like beeps. [Tim] apparently saw that post as a gauntlet thrown down, and he picked it up with an even smaller chip: the CH32V002, which he proceeded to turn into a MOD player. For those of you who slept through 80s and early 90s (or for those precocious infants reading this who hadn’t then yet been born), MOD files are an electronic music format, pioneered on the Amiga home computers. Like MIDI, the file specifies when to play specific voices rather than encoding the sound directly. Unlike MIDI, MOD files are self-contained, with the samples/voices used being stored inside the file. The original version targeted four-channel sound, and that’s what [Tim] is using here.
As you can see from the demo video, it sounds great. He pulled it off by using the chip’s built-in PWM timer. Since the timer’s duty cycle is determined by a variable that can be changed by DMA, the CPU doesn’t end up with very much to do here. In the worst case, with everything in flash memory instead of SRAM, the CPU is only taxed at 24%, so there’s plenty of power to say, add graphics for a proper demo. Using the existing MODPlay Library, [Tim]’s player fits into 4kB of memory, leaving a perfectly-usable 12kB for the MOD file. As far as external components needed, it’s just an RC filter to get rid of PWM noise.
[Tim] has put his code up on GitHub for anyone interested, and has perhaps inadvertently cast down another gauntlet for anyone who wants to use these little RISC V microprocessors for musical tasks. If you can do better, please do, let us know.
 
            
 
 
    									 
    									 
    									 
    									 
			 
			 
			 
			 
			 
			 
			 
			 
			 
			
Sounds like an interesting microchip to play with, can it be used with Arduino IDE or similar C based IDE that is free to use, free of ad, and free of restriction? I need to move away from PAL chip when making a custom Atari 2600 that requires bankswitching and some ATTiny isn’t quite fast enough (8MHz max internal)
It’s risc-v based, so it uses a gcc based toolchain. [cnlohr]+community has a library for this series of chips (https://github.com/cnlohr/ch32fun) that has examples for many of the peripherals. It’s actually part of what this project was based on.
Arduino? Yes – https://hackaday.com/2024/01/04/ch32-risc-v-mcus-get-official-arduino-support/
I wish these had the 1 uA low power moder of the ATtiny AVRs.
But how do you program them?
You have to buy an external programmer sold by WCH, or use an Arduino 328p (Arduigler) supported by Minichlink. For the Arduino port, I filed an issue long ago to update the Minichlink shipped with it:
https://github.com/AlexanderMandera/arduino-wch32v003/issues/15
https://github.com/AlexanderMandera/arduino-wch32v003/issues/27
I also made static builds of minichlink for Linux and Windows, but they are somewhere on a branch here:
https://github.com/zoobab/ch32v003fun/tree/minichlink-static
Arduigler:
https://gitlab.com/BlueSyncLine/arduino-ch32v003-swio
I am working on a .ino fork of BlueSyncLine version to support many other microcontrollers:
https://github.com/zoobab/zooswio
There are tons of open-source programmers based on your particular microcontroller of choice as well, so you can just use whatever you’ve got lying around.
For grins, Charles and I ported his programmer to micropython https://github.com/Hack-a-Day/2024-Supercon-8-Add-On-Badge/tree/main/i2c_proto_petal_tutorial on the badge last year because we made some CH32v003 based add ons. We needed micropython b/c it was supposed to program them on the fly, but there is no shortage of similar projects.
Or you pay a couple bucks. The programmers are dirt cheap too, and highly recommended.
There is also apparently a way to flash the chip via it’s uart via
wchisprust tool:https://ch32-rs.github.io/wchisp/
Testing it with ‘–serial’ option with a usb-uart dongle is still on my todo.
This is insane. Seeing a microcontroller play audio files that would have required a beefy mid-90s PC is mind-boggling.
hahah no way – i had the exact opposite thought.
On seeing the first article (with the mere beeps), i didn’t respect it because modplay (MP.COM) could passably decode 4-channel tracker files using 1-bit DAC (PC speaker) on a 286-12. So i think i understand exactly how Tim felt. In the end, i bet using the PWM peripheral almost felt like cheating…modplay was a ridiculous hack because it had to interleave mod decoding/resampling/mixing with doing that PWM in software for each ‘sample’.
Really, the CH32V003 is much more capable than my old 286. Faster clock, wider word size, more registers, and (i haven’t looked but probably) much fewer clocks per instruction. Nevermind the advantage we get by a great set of compilers targeting CH32V003. Just a question of how to live within the tiny built-in RAM/ROM.
Can you use Arduino’s tone() function with this chip?
“and quite specifically no DAC”
To be pedantic, this setup is known as a 1-bit DAC.
Due to the digital high rate driving of the filter you can change the shape of the output waveform, but the RC lowpass removes the high frequency edges of the pwm.
https://en.wikipedia.org/wiki/1-bit_DAC
In what way is the CH32V002 smaller? It’s basically a pin compatible V003 with more SRAM.
I continue to be impressed with what can be done with the CH32Vxxx. It’s a shame they’ve so little RAM – I’m having to investigate the PY32 instead.
if you care about the amount of ram may i direct you to the rp2040 pico board :)