ch32-rs / ch58x-hal

HAL for the CH583/CH582/CH581 family of microcontrollers. BLE 5.3, RISC-V Qingke V4.
Apache License 2.0
31 stars 7 forks source link

Serial over USB? #7

Open rmortes opened 5 months ago

rmortes commented 5 months ago

FIrst of all, I'm extremely new to MCU programming so sorry if I'm asking something stupid.

Is it possible to get the debug output over USB? Via serial, uart, I don't know. I have a CH582M in one of the dev boards with the type-c, and I've been trying to get the debug output, but it hasn't been working for me. May be a drivers thing (I'm using Windows 10), but the computer can't recognize the device when it's actually running the code, so the "serial" and "uart-echo" examples don't do much for me.

Thanks!

rgoulter commented 1 month ago

The WCH EVT examples tend to use UART1 TX (typically PA9) for writing 'debug' output.

I see this codebase has some examples which do the same thing: setup UART1 (using PA9 for TX), which is then used with writeln!. https://github.com/ch32-rs/ch58x-hal/blob/20eac4fd7d65b2eaa8b4eae40f882724a5710646/examples/serial.rs

To read these over USB, you'd connect the TX from the CH58x to RX to something that you can read from your PC.

e.g. If it's cheap/easy for you to obtain a FTDI FT232RL USB Adapter, that would work.

Otherwise, using a spare Pro Micro or Raspberry Pi Pico could be used.

e.g. here's CircuitPython code to read bytes from a UART RX, and output to USB CDC serial console.

"""Relay UART to CDC console."""
import board
import busio
import usb_cdc

uart = busio.UART(None, board.GP1, baudrate=115200)

while True:
    data = uart.read(32)

    if data is not None:
        usb_cdc.console.write(data)
        usb_cdc.console.flush()

You'd flash CircuitPython to a Pico, update the code.py with that program, connect the CH58x's UART1 TX (PA9) to GPIO1 of the Pico.

And you can connect to the USB serial console (e.g. the 232RL adapter, or CircuitPython on the Pico) using Putty https://wiki.archlinux.org/title/Working_with_the_serial_console#Making_Connections


For USB serial connection from the CH58x directly, I think you'd want to study/port this example. https://github.com/openwch/ch583/blob/main/EVT/EXAM/USB/Device/COM/src/Main.c