Custom Debugger

Custom Debugger

Introduction

Every firmware or hardware developer has a few different debuggers in their arsenal. I mostly use ST-Links and a J-Link to program my boards. Since I started using the TC2050 cable and its footprint, I basically have a standard programming and debugging interface on all my boards. A while ago I also built an adapter board that connects a debugger and a UART converter to the target board at the same time. The annoying thing about this setup is that it needs two USB cables: one for the debugger and one for the UART converter. Add the cable to the board itself, and my desk quickly turns into spaghetti.

After stumbling over this video I decided to build my own custom debugger to cut down the cable clutter. The idea is to let one MCU handle both the programming and the UART interface to the target MCU, while only a single USB-C port connects to the PC.

So what does the new thing actually do? When you plug it into your PC, the debugger shows up as two devices over one USB-C cable:

  • a CMSIS-DAP probe that works with OpenOCD, so you can flash and debug the target over SWD (west flash, west debug, GDB, ...)
  • a virtual COM port (USB CDC-ACM) that forwards the target's UART, so you can read the logs in your favourite serial terminal

Everything is powered from USB, the firmware runs on Zephyr RTOS, and the target connects through my usual TC2050 cable. One cable to the PC, one cable to the target. That's it!
Let's dive into it!

Standard Interface TC2050

As already mentioned, I switched to this cable and its footprint a long time ago. The TC2050 is a 10-pin "plug-of-nails" cable: the board only has a footprint with exposed pads, and the cable connects through spring-loaded pogo pins and is held in place by retention clips. The main benefit is that you don't need a pin header on the board. For small runs this doesn't really matter, but in higher volumes it saves cost, and it saves board space every time.

TC2050 pogo pins

My pinout standard

For the pinout I settled on the mapping below (keep in mind that the names are from the MCU/target side). It combines the power pins, the debug pins and a UART connection, which covers everything I usually need.

Pin Name Pin Number Pin Number Pin Name
VDD 1 2 GND
UART_RXD 3 4 UART_TXD
JTMS_SWDIO 5 6 JTCK_SWCLK
JTDO_SWO 7 8 JTDI
TBD/Free 9 10 JTRST

This means the UART_RXD of the target MCU goes to pin 3, which connects to the TX of the programmer. Pin 9 is currently unused and can be assigned later if the need arises.

Fun Fact: because I use this pinout on all my recent boards (Stepper Controller V1.0, the Motor Controller, the Sensor Node, ...), the new debugger works with all of them right away.

Design Goals and Considerations

The requirements were pretty simple:

  • SWD programming/debugging plus UART to the target
  • USB-C connector
  • Zephyr based firmware
  • Low cost
  • Powered only from USB 5 V, no external supply

The tricky part was how to get two functions through one USB port. I looked at three options:

Option Idea Verdict
USB hub (e.g. USB2422) Hub splits USB into one port for a debugger MCU and one for a separate UART bridge More parts, more cost, more complexity
Dedicated bridge IC (e.g. FT2232H) One IC does SWD/JTAG and UART Cheap and proven, but no Zephyr. Not what I wanted to learn
MCU as USB composite device One MCU presents CMSIS-DAP and CDC-ACM on the same USB connection Winner: one chip, all in Zephyr

Zephyr already has a CMSIS-DAP subsystem and a CDC-ACM UART class, so in theory I just have to glue the two together. In theory...

Development Testing

Before designing any hardware, I wanted to prove that the composite device actually works on a dev board.

Blue Pill: the counterfeit chapter

My first choice was the good old Blue Pill (STM32F103C8T6), since I had a whole bag of them lying around. A first test build already showed that this would be tight from a flash standpoint:

Memory region         Used Size  Region Size  %age Used
           FLASH:       59924 B        64 KB     91.44%
             RAM:       19688 B        20 KB     96.13%

Over 90 % of Flash and RAM before I had written a single line of my own code. Not great. But it got worse: when I tried to flash one of them, OpenOCD greeted me with this:

Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477

The ID code doesn't match a genuine STM32F103, so it's a counterfeit chip. I bought these boards during the COVID chip shortage and was sure at least one of them would have a real chip on it. Well... I was wrong.

Black Pill: the working prototype

So I moved on to a Black Pill (STM32F411CE), which has 512 KB Flash and 128 KB RAM. Plenty of headroom:

west build -b blackpill_f411ce
Memory region         Used Size  Region Size  %age Used
           FLASH:       58180 B       512 KB     11.10%
             RAM:       17280 B       128 KB     13.18%

The debugger itself is flashed over the STM32's built-in USB DFU bootloader: hold BOOT0 while plugging it in, release it, and run west flash --runner dfu-util. The UART bridge worked straight away. Flashing a target took a bit more effort, but in the end I was able to flash my Stepper Controller with the Black Pill as the debugger:

Info : Using CMSIS-DAPv2 interface with VID:PID=0x2fe3:0x0204
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: FW Version = 2.1.0
Info : SWD DPIDR 0x2ba01477
Info : [stm32f4x.cpu] Cortex-M4 r0p1 processor detected
Info : starting gdb server for stm32f4x.cpu on 3333

Most of my connection problems turned out to be caused by the jumper wires. To be on the safe side I limited the adapter speed to 500 kHz during testing. With a proper PCB and a short cable this shouldn't be a problem anymore. With the concept proven, it was time for the real hardware!

Board Hardware

For the final board I chose the STM32G431CBT6. It's a modern Cortex-M4 with 128 KB Flash and 32 KB RAM, which is a nice step up from the F103. It also has an internal 48 MHz oscillator (HSI48) for USB, so I don't need a crystal, which keeps the Bill of Materials (BOM) short and cheap.
all the hardware files can be found in this git repository: https://github.com/Nightshadow1258/acc-dap-uart-debugger-stm32g431cbtx-hw.
The main interfaces are listed in the table below

Function Pins
Target UART (USART2) PA2 (TX), PA3 (RX)
Target SWD (bit-banged GPIO) PA5 (SWCLK), PA4 (SWDIO)
Target reset PB0
USB USB-C, bus-powered
Target connector TC2050 (10-pin)

Apart from the MCU, I only need the connectors, a buck converter for the 3.3V power rail and some push buttons. This should allow a compact and cost efficient design.

Schematic & Pinout

After some messing around with the different possible pin outs I landed on this pin configuration for my setup.

MCU Pinout

After implementing this in the Schematic design in KICad, it was time for the PCB layout.

PCB Design

PCB layout
3D rendering of the board
Delivered PCBs

I ordered the PCBs and had two of them fully assembled. The rest came as bare boards, in case I need more later (or break one).

Firmware Adaption

On the firmware side I created a custom Zephyr board definition based on the nucleo_g431kb. The SWD interface is a simple GPIO node:

dp0 {
    compatible = "zephyr,swdp-gpio";
    status = "okay";
    clk-gpios   = <&gpioa 5 GPIO_ACTIVE_HIGH>;
    dio-gpios   = <&gpioa 4 GPIO_PULL_UP>;
    reset-gpios = <&gpiob 0 GPIO_ACTIVE_HIGH>;
    port-write-cycles = <2>;
};

Of course the first boot didn't go smoothly:

<err> udc_stm32: Could not select USB domain clock
<err> udc_stm32: Error enabling clock(s)
<err> usbd_core: Failed to init device driver

Classic. I forgot to enable the HSI48 clock for USB, the one feature I picked this MCU for. One overlay line fixed it:

&clk_hsi48 {
    status = "okay";
};

The second problem was the UART. The Nucleo base config enables LPUART1 on PA2/PA3, which are exactly the pins I use for USART2. After disabling LPUART1 and enabling USART2, the UART bridge worked as well:

&usart2 {
    status = "okay";
    current-speed = <115200>;
    pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
    pinctrl-names = "default";
};

The firmware can be found on GitHub as well.

Using It in a Zephyr Project

To use the debugger in another Zephyr project, I only need to add one line to the target's board.cmake:

board_runner_args(openocd --config interface/cmsis-dap.cfg --config target/stm32f4x.cfg)

After that, a plain west flash --runner openocd flashes the target. So far I have tested it with:

Case Design

It can get pretty hectic on the desk during debugging and probing, so I also designed a simple case. It protects the board and keeps me from accidentally shorting something on the bottom side. I exported the PCB from KiCad as a STEP file, imported it into FreeCAD and built the case around it. It's a two-part design: the screws go in from the bottom and sit in recessed mounting points, and the top part holds the M2 threaded inserts and has a cut-out for the TC2050 connector. Nothing fancy, but it does its job.

Case design in FreeCAD
PCB inside the 3D printed Case

With this all I set out to achieve with this project was done. All in all, I'm really happy with how it turned out. One cable to the PC, one cable to the board, and my desk is a little less chaotic now. Happy debugging!