Faster SPI bitbang for Zephyr on the ch32v003

As part of porting Zephyr to the CH32V003, I’ve been working on my own spin of wagiminator@’s CH32V003 based mini game console.

Once change was swapping the monochrome OLED dispaly for a 160x80 colour LCD with a ST7735R controller. Unfortunately, the SOIC-20 version of the CH32V003 doesn’t expose the hardware SPI interface, so I need to emulate it in software.

Zephyr includes a generic spi_bitbang driver but it’s quite slow, topping out at 160 kHz on this processor. I tried optimising it but the changes were too invasive.

The display is write-only, so I implemented a new driver and got it up to 1.5 MHz. The driver uses a few ideas such as precomputing the MOSI and CLK bits based on CPOL and CHPA, packing up to 4 byes into a single word, and using the hardware multi-bit set/reset support. The latter is fairly common across SoCs, so the driver should still be fairly portable.

Here’s the update rate before:

and after:

And, for bonus points, here’s a kite bouncing around the screen:

Avatar
Michael Hope
Software Engineer