alfin K

I plan to use this place to document my work, keep short notes for easy reference and write about things I might forget.

Learning about USB protocol with a RISC-V board

RISC-V toolchain and rv003usb program

Published
written by Alfin · category embedded ·

I stumbled upon this project called rv003usb[1] by cnlohr. It features a bit-banged USB controller for the RISC-V CH32V003 MCU. I was always interested in learning more about the protocol and since I have a lot of free time due to sem breaks I will try to play around with this software. This article will document interesting things I find in the process for future reference.

[1] https://github.com/cnlohr/rv003usb

Hardware#

I got a CH32V006 dev board and its programmer WCH.linkE from WeActStudios on aliexpress. They have schematics uploaded on their github page, which shows some interesting connections related to this rv003usb project. Maybe the board supports programming directly using the on-board USB-C by first flashing the bootloader USB bit-banged code. I will have to verify it later.

Software Stack#

So cnlohr has a nice repo with a lot of built in functionality [2]. This project is an attempt to decouple essential functionality from the official IDE, so that a lightweight toolchain is available for these boards. However I still need the RISC-V GCC compiler in my system. I use Gentoo, here is how I set it up using crossdev:

Gentoo riscv toolchain installation#

To start development we need the riscv 32bit bare metal toolchain which has the assembler, compiler, linker and a glibc replacement called newlib. We probably won't need to use newlib since ch32fun provides a lot of functionality out of the box. We need to first emerge sys-devel/crossdev if not already installed. To configure crossdev refer the wiki as it gives multiple options.

Note: crossdev is a collection of scripts which can generate e-builds for portage to build cross-development toolchains. (Handles all the symlinking to your system GCC and all that stuff)

Now that crossdev is configured we can compile our toolchain using a single command:

crossdev --target riscv32-unknown-none-newlib
// this next command is equivalent but your binary name will be different and 
// shorter for example: your gcc will now be riscv32-unknown-elf-gcc instead 
// of riscv32-unknown-none-newlib-gcc
crossdev --target riscv32-unknown-elf

// command to uninstall in case of errors; read gentoo wiki for more details
crossdev --clean --target <target>