Debugging @ 5.33 Mbit/sec (5333333 Baud) on CH32V003 (RISC-V) Microcontrollers

Back in 2018 I wrote about my favorite way to debug small microcontrollers: bit-banging serial debug output as fast as the CPU can toggle a pin, and letting a modern USB-serial chip like a PL2303 and Linux handle the weird baud rate and buffering.

That post ended up on Hackaday, and has been one the most red articles on my BLOG, later I ported the same trick to the cheap 8-bit STM8S. Below are links to the original articles:

Now there is a new contender for the ultimate ten-cent microcontroller: the WCH CH32V003 a 48 MHz 32-bit RISC-V (RV32EC) chip with 16 KB Flash and 2 KB SRAM for pennies.

ch32v003f4 serial dprint @ 5M3 baud

Naturally, the first thing I want on a new CPU is my out-of-band dprint stream so I can keep the single hardware UART (PD5) free for my RS-485 / 1-wire field bus and still print from anywhere—including inside interrupt routines—with almost zero timing impact, having out-of-band printf proved to be of great value while I was working on porting my nq485-system  to the RISC-V architecture.

Because 48 MHz / 9 cycles = 5,333,333 baud, we can hit the exact same 5.33 Mbit/sec baud rate as I used in the 16 MHz AVR version, so our existing picocom -b 5333333 muscle memory works without changing a thing. — Yes I am getting old and do not remember how to change my habbits.

The CH32V003 Makes Bit-Banging Even Cleaner

Compared to AVR and STM8S, the CH32V003 has three hardware features that make deterministic bit-banging remarkably clean:

  • Atomic Bit Set/Reset Register (BSHR):  On AVR, you had to read the whole 8-bit PORTx register into a temporary register, toggle the bit with bld , and write the whole port back—keeping interrupts disabled so no ISR would destroy the timing.
    On CH32V003, GPIOx->BSHR lets you set a pin by writing (1 << bit) or clear it by writing (1 << (bit + 16)) in a single atomic sw instruction without touching any other pin on the port.
  • Zero-Wait-State Flash at 48 MHz: Unlike some Cortex-M parts that insert Flash wait states above 24 MHz, the CH32V003 executes straight-line code from Flash with zero wait states at 48 MHz, giving rock-solid cycle counts.

Hardware Auto-Disable: Ground the Pin to Silence Debug Output

Just like my AVR and STM8S versions, I want to leave dprint compiled into production firmware permanently, so if problem shoul arise I am not in the blind.  To turn off debugprint just a pulldowb resister to kine that would otherwise send the dprints to theUSB-serial adapter.  This is simply accomplished with nqprint_init() briefly enables the CH32V003’s built-in ~40 kΩ pull-up resistor on the debug pin (PD4 by default) and reads the pin state:

  • Pin left open (or connected to a USB-serial RX input): Internal pull-up pulls the pin HIGH (1 ), keeping debug_level active, and we will have dprints to investigate,
  • Pin pulled LOW through a 2.2 kΩ –4.7 kΩ resistor to GND: Pin reads LOW (0 ), setting debug_level = 0 so every dprint() becomes a fast no-op.
#include <nqprint.h>
#include <nq_ch32v.h>

# (C) storepeter.dk 2026 license beerware
#ifndef DPRINT_TX
# define DPRINT_TX D,4
#endif

NQ_INIT nqprint_init(void)
{
	funGpioInitAll();

	// Enable internal ~40 kΩ pull-up to check for an external pull-down resistor
	GPIO_SET(DPRINT_TX);
	GPIO_INPUT_PULLUP(DPRINT_TX);
	Delay_Us(10);

	// If pulled LOW externally (e.g. 2.2 kΩ to GND), silence all debug prints
	if (!GPIO_READ(DPRINT_TX)) {
		dprint_init(0);
		GPIO_INPUT(DPRINT_TX);
		return;
	}

	// Pre-load HIGH (UART idle state) before switching to 50 MHz push-pull output
	GPIO_SET(DPRINT_TX);
	GPIO_OUTPUT(DPRINT_TX);
}

Sending a Byte in 9 Cycles per Bit (5,333,333 Baud)

At 48 MHz, one bit at 5,333,333 baud is 9 clock cycles (187.5 ns per bit, or ~1.9 µs for a full 10-bit start + 8 data + stop frame).

To avoid branch prediction or pipeline-flush jitter between 0 bits and 1 bits, we can select the BSHR set mask vs. clear mask branchlessly using a single right-shift:

  • mask_lo has bit pin + 16 set (1 << (pin + 16) ), which clears the pin when written to BSHR .
  • mask_hi has bit pin set (1 << pin ), which sets the pin when written to BSHR .
  • Shifting mask_lo right by 0 leaves mask_lo (0 bit); shifting mask_lo right by 16 shifts bit pin + 16 down into bit pin , producing mask_hi (1 bit)!
/*
 * Bit-bang debug UART on CH32V003 @ 48 MHz
 * 9 clock cycles per bit -> 48,000,000 / 9 = 5,333,333 baud (5.33 Mbit/s)
void nqprintc(char ch)
{
        uint32_t mstatus;
        uint32_t mask_hi = (1U << GPIO_BIT(DPRINT_TX));
        uint32_t mask_lo = (1U << (GPIO_BIT(DPRINT_TX) + 16));
        volatile uint32_t *bshr = &GPIO_PORT(DPRINT_TX)->BSHR;

        __asm__ volatile (
                "csrrci %[ms], mstatus, 0x8\n\t"   // Disable interrupts (save mstatus)
                ".balign 4\n\t"
                // --- Start bit (0) ---
                "sw     %[lo], 0(%[bshr])\n\t"     // 4 cycles: drive LOW
                "c.nop\n\t"                        // 1 cycle
                "c.nop\n\t"                        // 1 cycle

                // --- Macro for bits 0..7 (9 cycles/bit -> 5,333,333 baud @ 48 MHz) ---
                ".irp bit, 0, 1, 2, 3, 4, 5, 6, 7\n\t"
                "andi   t0, %[ch], 1\n\t"          // 1 cycle: isolate LSB
                "srli   %[ch], %[ch], 1\n\t"       // 1 cycle: shift next bit
                "slli   t0, t0, 4\n\t"             // 1 cycle: 0 -> 0 (clear), 1 -> 16 (set)
                "srl    t0, %[lo], t0\n\t"         // 1 cycle: t0 = mask_lo (0) or mask_hi (1)!
                "c.nop\n\t"                        // 1 cycle
                "sw     t0, 0(%[bshr])\n\t"        // 4 cycles: output bit on BSHR!
                ".endr\n\t"

                // --- Stop bit (1) ---
                "c.nop\n\t"                        // 1 cycle
                "c.nop\n\t"                        // 1 cycle
                "c.nop\n\t"                        // 1 cycle
                "c.nop\n\t"                        // 1 cycle
                "c.nop\n\t"                        // 1 cycle
                "sw %[hi], 0(%[bshr])\n\t"         // 4 cycles Drive HIGH (stop bit)           "csrw   mstatus, %[ms]\n\t"        // Restore interrupts
                : [ms] "=&r" (mstatus), [ch] "+r" (ch)
                : [bshr] "r" (bshr), [hi] "r" (mask_hi), [lo] "r" (mask_lo)
                : "t0", "memory"
        );
}

Viewing the Stream on Linux

Connect PD4 and GND from the CH32V003 to a fast USB-serial adapter (such as a PL2303HX) and open picocom :

	picocom -l /dev/ttyUSB0 -b 5333000

Happy hacking — StorePeter


											
This entry was posted in Arduino, Embedded, on Hackaday. Bookmark the permalink.

Leave a Reply