A cheap usb2jtag chip CH347 for esp32 debugging

FT2232HL is the chip used by ESP-Prog for debugging, but it’s too expensive. There is a cheap usb2jtag chip, CH347, which the price is only one fifth of FT2232HL even buy only one, it can be used for ESP32 debugging and uart.

I have test the “DTR, RTS, TXD, RXD” of CH347, as the picture illustrated, it works very well, it’s easy to upload the program for esp32 by platformio, just one clicking, nothing added in platformio.ini.
But when debugging esp32, namely, using “TMS, TCK, TDO, TDI” of CH347, platformio cannot recognize this chip. Then I contact WCH, the technical staff provide me two files: “ch347.cfg and openocd.exe”, the openocd.exe is ported by WCH.
The technical staff told me that replace the openocd.exe in “.platformio\packages\tool-openocd-esp32\bin” with “ch347.cfg and openocd.exe” provided.
But after doing all the steps above, platformio still cannot debug esp32 using ch347.

Does anyone knows how to do with it?
Best regards.

P.S. The “ch347.cfg and openocd.exe” can be downloaded from:
https://github.com/wanggaoteng/C-

Well that’s not entirely accurate, PlatformIO must also be expanded to make use of this adapter.

You should try this:

  • place the openocd.exe from the repo in C:\Users\<user>\.platformio\packages\tool-openocd-esp32\bin
  • place the ch347.cfg in C:\Users\<user>\.platformio\packages\tool-openocd-esp32\scripts\interface\.
  • open the file C:\Users\<user>\.platformio\platforms\espressif32\platform.py

Expand the array

with the entry "ch347".

Expand the list

by "ch347".

Try and use the platformio.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
upload_protocol = ch347
debug_tool = ch347

(both uploading and debugging should be possible via JTAG) together with a standard Blink code

Then, execute the project task “Build”. Then, use the project task “Advanced → Verbose Upload” and share the output.

Edit: Fixed debug_protocoldebug_tool.

Thank you.
Does the quoted means:

            if link in ("jlink", "cmsis-dap", "ch347"):
                openocd_interface = link

?
Best regards.

Correct. A string value must but added to these arrays/lists, just like you’ve shown.

Thanks a lot.
After clicking the “Verbose Upload”, the output is:

Executing task: C:\Users\wgt04\.platformio\penv\Scripts\platformio.exe run --verbose --target upload 

Processing firebeetle32 (platform: espressif32; board: firebeetle32; framework: 
arduino; upload_protocol: ch347; debug_tool: ch347)
--------------------------------------------------------------------------------CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/firebeetle32.html
PLATFORM: Espressif 32 (5.0.0) > FireBeetle-ESP32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (ch347) External (ch347, cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.20003.220626 (2.0.3)
 - tool-esptoolpy @ 1.30300.0 (3.3.0)
 - tool-mkfatfs @ 2.0.1
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - tool-openocd-esp32 @ 2.1100.20220411 (11.0)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
<lambda>(["checkprogsize"], [".pio\build\firebeetle32\firmware.elf"])
MethodWrapper(["checkprogsize"], [".pio\build\firebeetle32\firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.9% (used 16148 bytes from 327680 bytes)
Flash: [==        ]  16.2% (used 212701 bytes from 1310720 bytes)
.pio\build\firebeetle32\firmware.elf  :

section
       size         addr

.rtc.text
          0   1074528256

.rtc.dummy
          0   1073217536

.rtc.force_fast
          0   1073217536

.rtc.data
         16   1342177792

.rtc_noinit
          0   1342177808

.rtc.force_slow
          0   1342177808

.iram0.vectors
       1027   1074266112

.iram0.text
      50859   1074267140

.dram0.data
      13644   1073470304

.ext_ram_noinit
          0   1065353216

.noinit
          0   1073483948

.ext_ram.bss
          0   1065353216

.dram0.bss
       2504   1073483952

.flash.appdesc
        256   1061158944

.flash.rodata
      41336   1061159200

.flash.rodata_noload
          0   1061200536

.flash.text
     105835   1074593824

.iram0.text_end
          1   1074317999

.iram0.data
          0   1074318000

.iram0.bss
          0   1074318000

.dram0.heap_start
          0   1073486456

.xtensa.info
         56            0

.comment
        166            0

.xt.lit._ZN3nvs4Lock4initEv
          0            0

.xt.prop._ZN3nvs4Lock4initEv
         72            0

.xt.lit._ZN14intrusive_listIN3nvs7Storage14NamespaceEntryEE17clearAndFreeNodesEv          0            0

.xt.lit._ZN19CompressedEnumTableIbLj1ELj256EE3setEjb
          8            0

.xt.prop._ZN14intrusive_listIN3nvs7Storage14NamespaceEntryEE17clearAndFreeNodesEv       108            0

.xt.prop._ZN19CompressedEnumTableIbLj1ELj256EE3setEjb
         60            0

.xt.prop._ZN14intrusive_listIN3nvs7Storage14NamespaceEntryEE9push_backEPS2_     
         60            0

.xt.lit._ZN3nvs12NVSPartitionD5Ev
          0            0

.xt.prop._ZN3nvs12NVSPartitionD5Ev
          0            0

.xt.prop._ZN3nvs12NVSPartitionD2Ev
         36            0

.xt.prop._ZN3nvs12NVSPartitionD0Ev
         36            0

.xt.prop._ZTVN3nvs12NVSPartitionE
         12            0

.xt.lit._ZN3nvs19NVSPartitionManagerD5Ev
          0            0

.xt.prop._ZN3nvs19NVSPartitionManagerD5Ev
          0            0

.xt.prop._ZN3nvs19NVSPartitionManagerD2Ev
         36            0

.xt.prop._ZN3nvs19NVSPartitionManagerD0Ev
         36            0

.xt.prop._ZN14intrusive_listIN3nvs12NVSPartitionEE5eraseENS2_8iteratorE
         84            0

.xt.prop._ZTVN3nvs19NVSPartitionManagerE
         12            0

.xt.prop._ZN14intrusive_listIN3nvs8HashList13HashListBlockEE5eraseENS3_8iteratorE        84            0

.xt.lit._ZN3nvs4ItemC5EhNS_8ItemTypeEhPKch
          0            0

.xt.lit._ZNK19CompressedEnumTableIN3nvs4Page10EntryStateELj2ELj126EE3getEj      
          8            0

.xt.lit._ZN19CompressedEnumTableIN3nvs4Page10EntryStateELj2ELj126EE3setEjS2_    
          8            0

.xt.prop._ZN3nvs4ItemC5EhNS_8ItemTypeEhPKch
          0            0

.xt.prop._ZNK19CompressedEnumTableIN3nvs4Page10EntryStateELj2ELj126EE3getEj     
         60            0

.xt.prop._ZN19CompressedEnumTableIN3nvs4Page10EntryStateELj2ELj126EE3setEjS2_   
         60            0

.xt.prop._ZN3nvs20isVariableLengthTypeENS_8ItemTypeE
         48            0

.xt.prop._ZN3nvs4ItemC2EhNS_8ItemTypeEhPKch
         60            0

.xt.lit._ZN14intrusive_listIN3nvs4PageEE5clearEv
          0            0

.xt.prop._ZN14intrusive_listIN3nvs4PageEE5clearEv
         60            0

.xt.prop._ZN14intrusive_listIN3nvs4PageEE9push_backEPS1_
         60            0

.xt.prop._ZN14intrusive_listIN3nvs4PageEE5eraseENS2_8iteratorE
         84            0

.xt.prop._ZTISt9exception
         12            0

.xt.prop._ZTISt9bad_alloc
         12            0

.xt.prop._ZTVN10__cxxabiv117__class_type_infoE
         12            0

.xt.prop._ZTVN10__cxxabiv120__si_class_type_infoE
         12            0

.debug_frame
      53576            0

.debug_info
    2574536            0

.debug_abbrev
     227268            0

.debug_aranges
      21664            0

.debug_ranges
      53904            0

.debug_line
    1023332            0

.debug_str
     228001            0

.debug_loc
     399241            0

Total
    4798362
<lambda>(["upload"], [".pio\build\firebeetle32\firmware.bin"])
AVAILABLE: ch347, cmsis-dap, esp-bridge, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = ch347
openocd -d2 -s C:/Users/wgt04/.platformio/packages/tool-openocd-esp32/share/openocd/scripts -f interface/ch347.cfg -f board/esp-wroom-32.cfg -c "adapter_khz 5000" -c "program_esp {{.pio\build\firebeetle32\firmware.bin}} 0x10000 verify" -c "program_esp {{C:/Users/wgt04/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/bin/bootloader_dio_40m.bin}} 0x1000 verify" -c "program_esp {{C:/Users/wgt04/Documents/PlatformIO/Projects/testE/.pio/build/firebeetle32/partitions.bin}} 0x8000 verify" -c "program_esp {{C:/Users/wgt04/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin}} 0xe000 verify" -c "reset run; shutdown"
Open On-Chip Debugger 0.11.0+dev-00706-g822097a35-dirty (2022-06-08-19:08)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 2

Info : only one transport option; autoselect 'jtag'
WARNING: boards/esp-wroom-32.cfg is deprecated, and may be removed in a future release.
Warn : Transport "jtag" was already selected
C:/Users/wgt04/.platformio/packages/tool-openocd-esp32/share/openocd/scripts/target/esp32.cfg:50: Error: Unknown target type esp32, try one of arm7tdmi, arm9tdmi, arm920t, arm720t, arm966e, arm946e, arm926ejs, fa526, feroceon, dragonite, xscale, cortex_m, cortex_a, cortex_r4, arm11, ls1_sap, mips_m4k, avr, dsp563xx, dsp5680xx, testee, avr32_ap7k, hla_target, nds32_v2, nds32_v3, nds32_v3m, esp32s2, or1k, quark_x10xx, quark_d20xx, stm8, riscv, mem_ap, esirisc, arcv2, aarch64,  
or mips_mips64
in procedure 'script' 
at file "embedded:startup.tcl", line 26
at file "C:/Users/wgt04/.platformio/packages/tool-openocd-esp32/share/openocd/scripts/board/esp-wroom-32.cfg", line 3
at file "C:/Users/wgt04/.platformio/packages/tool-openocd-esp32/share/openocd/scripts/target/esp32.cfg", line 50
*** [upload] Error 1
========================== [FAILED] Took 4.54 seconds ==========================
 *  The terminal process "C:\Users\wgt04\.platformio\penv\Scripts\platformio.exe 'run', '--verbose', '--target', 'upload'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Best regards.

That is uh, very bad. I think they gave a wrong openocd.exe that was not compiled from GitHub - espressif/openocd-esp32: OpenOCD branch with ESP32 JTAG support and thus has no ESP32 support.

Can you revert to the openocd.exe file from the latest one in https://dl.registry.platformio.org/download/platformio/tool/tool-openocd-esp32/2.1100.20220706/tool-openocd-esp32-windows-2.1100.20220706.tar.gz ?

By the way, my board is firebeetle-esp32, which based on ESP32-WROOM-32E, so I set

board = firebeetle32

I download the .tar.gz file and unzip it, and get openocd.exe:
image
Then copy “openocd.exe” above to “C:\Users\wgt04.platformio\packages\tool-openocd-esp32\bin”.
After clicking upload, some messages appeared:

Processing firebeetle32 (platform: espressif32; board: firebeetle32; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/firebeetle32.html
PLATFORM: Espressif 32 (5.0.0) > FireBeetle-ESP32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (ch347) External (ch347, cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.20003.220626 (2.0.3)
 - tool-esptoolpy @ 1.30300.0 (3.3.0)
 - tool-mkfatfs @ 2.0.1
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - tool-openocd-esp32 @ 2.1100.20220411 (11.0)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Retrieving maximum program size .pio\build\firebeetle32\firmware.elf
Checking size .pio\build\firebeetle32\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.9% (used 16148 bytes from 327680 bytes)
Flash: [==        ]  16.2% (used 212701 bytes from 1310720 bytes)
Configuring upload protocol...
AVAILABLE: ch347, cmsis-dap, esp-bridge, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = ch347
Uploading .pio\build\firebeetle32\firmware.bin
Open On-Chip Debugger  v0.11.0-esp32-20220706 (2022-07-06-15:48)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

Error: The specified debug interface was not found (ch347)
The following debug adapters are available:
1: ftdi
2: usb_blaster
3: esp_usb_jtag
4: ft232r
5: usbprog
6: jlink
7: vsllink
8: rlink
9: ulink
10: arm-jtag-ew
11: hla
12: osbdm
13: opendous
14: cmsis-dap
15: xds110
16: st-link
17: jtag_esp_remote
*** [upload] Error 1
=================================================================== [FAILED] Took 3.55 seconds ===================================================================
 *  The terminal process "C:\Users\wgt04\.platformio\penv\Scripts\platformio.exe 'run', '--target', 'upload'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Yeah, as I thought. This is a dead end.

The openocd.exe provided in the repo does not have OpenOCD support – maybe they forked it from an older mainline OpenOCD version and added CH347 supoprt there, and didn’t use GitHub - espressif/openocd-esp32: OpenOCD branch with ESP32 JTAG support for the ESP32.

Since I don’t see the source code for the CH347 adapter in OpenOCD open-source, the only thing you can do is ask them to provide an openocd.exe version with ESP32 support (and that still has their CH347 adapter code in it).

PlatformIO calls into the openocd correctly with

, this problem is on the binary side now.

Thank you very much.
Now I know how to do with it. I will ask the technical staff of WCH for a better support.

Hi, @maxgerhardt
After I mailed the CH437 board and the ESP32 board to the technical support of WCH, they tried some times, but have not successfully debugged the ESP32 with CH347. Technical support said that the problem could not be solved in the short term, now, the board is sent back to me.
By the way, the new powerful chip esp32-s3, which is stable supported by arduino-esp32:


has a debugger inside:
image
So for the new chip, it’s no need to buy/diy a debugger, just need a usb cable.