Using USB on the CH32V003
Ever wanted a 10 cent USB-capable processor? Well, look no further. RV003USB allows you to, in firmware connect a 10 cent RISC-V to a computer with USB. It is fundamentally just a pin-change interrupt routine that runs in assembly, and some C code to handle higher level functionality.
It's small!
The bootloader version of the tool can create a HID device, enumerate and allow execution of code on-device in 1,920 bytes of code. Basic HID setups are approximately 2kB, like the Joystick demo.
It's simple!
The core assembly code is very basic, having both an interrupt and code to send. And only around 250 lines of C code to facilitate the rest of the USB protocol.
It's adaptable!
The core assembly code is standardized, and there is also a c file code, to handle different functionality with hid feature requests, control setup events, interrupt endpoints (send and receive) are all done in.
It shows both how to be a normal USB device, as well as how to write programs to run on your PC that can talk to your USB device.
It's got demos!
Here are the following demos and their statuses...
Example | Description | β | π§ |
---|---|---|---|
demo_gamepad | Extremely simple example, 2-axis gamepad + 8 buttons | β
|
β
|
demo_composite_hid | Mouse and Keyboard in one device | β
|
β
|
demo_hidapi | Write code to directly talk to your project with fully-formed messages. (Works on Android) | β
|
β
|
bootloader | CH32V003 Self-bootloader, able to flash itself with minichlink | β
|
β
|
And the following largely incomplete, but proof-of-concept projects:
Example | Description | β | π§ |
---|---|---|---|
cdc_exp | Enumerate as a USB Serial port and send and receive Data (incomplete, very simple) | β οΈ |
β
|
demo_midi | MIDI-IN and MIDI-OUT | β
|
β
|
test_ethernet | RNDIS Device | β |
β
|
Note: CDC In windows likely CAN work, but I can't figure out how to do it. Linux explicitly blacklists all low-speed USB Ethernet that I could find. The MIDI example only demonstrates MIDI-OUT.
It's built on ch32v003fun
ch32v003fun is a minimal development SDK of sorts for the CH32V003, allowing for maximum flexability without needing lots of code surrounding a HAL.
General developer notes
Care surrounding interrupts and critical sections.
You are allowed to use interrupts and have critical sections, however, you should keep critical sections to approximately 40 or fewer cycles if possible. Additionally if you are going to be using interrupts that will take longer than about 40 cycles to execute, you must enable preemption on that interrup. For an example of how that works you can check the ws2812b SPI DMA driver in ch32v003fun. The external pin-chane-interrupt must be the highest priority. And it must never be preempted. While it's OK to have a short delay before it is fired, interrupting the USB reception code mid-transfer is prohibited.
It's still in beta.
This project is not ready for prime time, though it is sort of in a beta phase. Proof of concept works, lots of demos work. Maybe you can help out!
β
Able to go on-bus and receive USB frames
β
Compute CRC in-line while receiving data
β
Bit Stuffing (Works, tested)
β
Sending USB Frames
β
High Level USB Stack in C
β
Make USB timing more precise.
β
Use SE0 1ms ticks to tune HSItrim
β
Rework sending code to send tokens/data separately.
β
Fix CRC Code
β
Make minichlink able to use bootloader.
β
Optimize high-level stack.
β
Fit in bootloader (NOTE: Need to tighten more)
β
Do basic retiming, at least in preamble to improve timing.
β
Use HID custom messages.
β
Improve sync sled. I.e. coarse and fine sledding.
β
Abort on non-8-bit-aligned-frames.
π³
Make more demos
π³
API For self-flashing + printf from bootloader
π³
Improve timing on send, for CRC bits. Currently we are off by about 6 cycles total.
β
Further optimize Send/Receive PHY code. (Please help)
β οΈ
Enable improved retiming (Requires a few more cycles) (Please help!)
β οΈ
Arduino support (someone else will have to take this on)