PlatformIO Community

How to debug esp32 with OpenOCD on remote raspberrypi zero

Hi , I have installed openOCD on a raspberrypi zero and connected the jtag pins to a esp32 dev board . Here is a link with the instructions: ESP32 JTAG Debugging using Raspberry Pi . How do I get this to work in platformio , what do I need to add in platformio.ini ?

PIO Remote, in my understanding does not support remote debugging like this, with your main PC connecting to a remote PIO agent and forwarding debug server traffic. You can remotely upload the firmware (via the JTAG connection) using pio remote run and remotely view the serial output though (pio remote device monitor). If you can SSH into the Pi, you can also use pio debug locally (or in the VSCode GUI) to debug the program.

One could also probably make it work ‘by hand’ by emptying out the debug_server (so that OpenOCD is not started locally), then using a e.g. SSH port tunnel you can set it up so that if you connect to localhost port 4444, it gets forwarded through the tunnel program to the raspberry pi’s localhost 4444 (which is the OpenOCD debug server that must be started by hand). If the Pi is locally in your network, it gets easier – just empty out debug_server and repoint debug_port = ... to the IP:port combination that OpenOCD is listening on (that you must still manually start).

The best way to go forward would be to open an issue in the core. Issues · platformio/platformio-core · GitHub

Thanks , I am starting the OpenOCD on the raspberry manually now and it is working .
My platformio.ini looks like this :

[env:esp32dev]

platform = espressif32

board = esp32dev

framework = arduino

debug_init_break = tbreak setup

;sudo OPENOCD_SCRIPTS=$PWD/tcl src/openocd -f interface/raspberrypi2-native.cfg -f target/esp32.cfg -c “adapter_khz 1000” -c “bindto 0.0.0.0”

debug_tool = custom

debug_port = raspberrypi.local:3333

debug_server = pyocd-gdbserver

I only need a way to automate the line that is commented , to start OpenOCD .

Hey, could you explain a bit more how you’re making it work?
I’ve followed the same guide to install openOCD till where they say to make the ssh tunnel

I’ve then copied the part from your platformio.ini
Yet I see nothing when I press debug?

There is one difference between our configs,
namely that I use a different board ( Xiuxin ESP32, seller mentioned to use the NodeMCU-32S profile)

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s

I see the following error in the debug console:

ndefinedError: Traceback (most recent call last):
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\__main__.py", line 121, in main
    cli()  # pylint: disable=no-value-for-parameter
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\commands\__init__.py", line 44, in invoke
    return super(PlatformioCLI, self).invoke(ctx)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\commands\debug.py", line 171, in cli
    loop.run_until_complete(coro)
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\debug\process\gdb.py", line 37, in run
    await super(GDBClientProcess, self).run()
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\debug\process\client.py", line 55, in run
    self.debug_config.port = await self._server_process.run()
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\debug\process\server.py", line 103, in run
    await self.spawn(
  File "C:\Users\Massiveatoms\.platformio\penv\lib\site-packages\platformio\debug\process\base.py", line 77, in spawn
    await loop.subprocess_exec(
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\base_events.py", line 1661, in subprocess_exec
    transport = await self._make_subprocess_transport(
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\windows_events.py", line 394, in _make_subprocess_transport
    transp = _WindowsSubprocessTransport(self, protocol, args, shell,
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\base_subprocess.py", line 36, in __init__
    self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\windows_events.py", line 890, in _start
    self._proc = windows_utils.Popen(
  File "C:\Users\Massiveatoms\.platformio\python3\lib\asyncio\windows_utils.py", line 153, in __init__
    super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd,
  File "C:\Users\Massiveatoms\.platformio\python3\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Massiveatoms\.platformio\python3\lib\subprocess.py", line 1421, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 is not a valid Win32 application

============================================================


I think it’s trying to call into the debug tool here

(if you’ve copied that bit from above). Do you have pyOCD globally installed? The invocation may have also changed for recent versions.

1 Like

Yes, I do have it globally installed.
I can run pyocd-gdbserver from my terminal, although it does complain that it is deprecated:
0000968:WARNING:gdb_server:pyocd-gdbserver is deprecated; please use the new combined pyocd tool. Waiting for a debug probe to be connected...

What happens when you set

debug_server = 

i.e., empty?

1 Like

I get the following error box:

Failed to launch GDB: .pioinit:13 : Error in sourced command file:
192.168.1.16::333: The requested adress is not valid in its context. (from interpreter-exec console "source .pioinit")

With the option to open launch.json and cancel.

Debug console has the following message:

Reading symbols from e:\Documents\PlatformIO\Projects\TEMP-PROBER\.pio\build\nodemcu-32s\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = custom
PlatformIO: Initializing remote target...
.pioinit:13: Error in sourced command file:
192.168.1.16::3333: The requested address is not valid in its context.

EDIT:
I think I have found my error, I see iv’e added a :: instead of a single : before the port

Did you set that as debug_port with those ::? There should only be 1.

Yeah, I spotted that.

In the interest of making sure there are no other errors in my config, this is the config now

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 9600
lib_deps =
	paulstoffregen/OneWire@^2.3.6
	milesburton/DallasTemperature@^3.9.1

debug_init_break = tbreak setup

debug_tool = custom
debug_port = 192.168.1.16:3333
; debug_server = pyocd-gdbserver
debug_server =

I’ve tried running it with both debug_server specified, and empty.

With it empty, I get the following result:

with this in my debug console:

Reading symbols from e:\Documents\PlatformIO\Projects\TEMP-PROBER\.pio\build\nodemcu-32s\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = custom
PlatformIO: Initializing remote target...
.pioinit:13: Error in sourced command file:
192.168.1.16:3333: The system tried to join a drive to a directory on a joined drive.

With debug_server = pyocd-gdbserver specified I get the same error as mentioned in

Never mind, after reloading vscode, it works now with

debug_server =

, empty, as you suggested.

Thanks for the help, and sorry for wasting your time with what was mostly a typo!

You wrote debug_server = empty or with what exact setting is it working? You already set it to an empty value in the config above.

Sorry, could’ve been clearer.
With the value left out, as you suggested earlier.

Hi , I am not using this method anymore , I have a ft2232 based board now. I tried using openOCD installed on a raspberry pi as a test. In my testing I used ssh to start the openOCD debug server. (It is not started automatically.)

Hi @frankdunn37. Did you modify your launch.json ? I’m also using the same HW setup and my platformio.ini is same as yours. I’m getting this error once I hit “run → start debugging”:
Reading symbols from/Users/pakiki/Documents/PlatformIO/Projects/Gate_Driver/.pio/build/esp32dev/firmware.elf…
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = custom
PlatformIO: Initializing remote target…
.pioinit:13: Error in sourced command file:
192.168.0.89:3333: Operation timed out.

Hi @pakiki . I have not used this kind of setup in some time . Please check on the raspberry that it can accept connections from your debugging pc . make sure when you start pyocd on the raspberry it should look similar to this :

sudo OPENOCD_SCRIPTS=$PWD/tcl src/openocd -f interface/raspberrypi2-native.cfg -f target/esp32.cfg -c “adapter_khz 1000” -c “bindto 0.0.0.0”

Please make sure you have “bindto 0.0.0.0” in the command .

Hi @frankdunn37, thank you for your reply. Communication between RPi and mac seem to be fine, I figured out the “bindto 0.0.0.0” tag and that jump started debugging on VS Code on the mac - I can see the ssh tunnel updating along with the debug console, so far so good. Still though I am having a tough time because of this error the system runs into that I don’t understand:

Reading symbols from /Users/pakiki/Documents/PlatformIO/Projects/Gate_Driver/.pio/build/esp32dev/firmware.elf…
undefined0000990:WARNING:gdb_server:pyocd-gdbserver is deprecated; please use the new combined pyocd tool.
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = custom
PlatformIO: Initializing remote target…
0x40144163 in pm_update_next_tbtt ()
JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
[esp32.cpu0] requesting target halt and executing a soft reset
[esp32.cpu0] Debug controller was reset.
[esp32.cpu0] Core was reset.
[esp32.cpu0] Target halted, PC=0x500000CF, debug_reason=00000000
[esp32.cpu1] requesting target halt and executing a soft reset
[esp32.cpu0] Core was reset.
[esp32.cpu0] Target halted, PC=0x40000400, debug_reason=00000000
[esp32.cpu1] Debug controller was reset.
[esp32.cpu1] Core was reset.
[esp32.cpu1] Target halted, PC=0x40000400, debug_reason=00000000
[esp32.cpu0] Target halted, PC=0x40092612, debug_reason=00000001
Flash mapping 0: 0x10020 → 0x3f400020, 92 KB
Flash mapping 1: 0x30020 → 0x400d0020, 476 KB
[esp32.cpu0] Target halted, PC=0x40092612, debug_reason=00000001
Auto-detected flash bank ‘esp32.cpu1.flash’ size 4096 KB
Using flash bank ‘esp32.cpu1.flash’ size 4096 KB
** Programming Started **
[esp32.cpu0] Target halted, PC=0x40092612, debug_reason=00000001
couldn’t open /Users/pakiki/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/bin/bootloader_dio_40m.bin
** Programming Failed **
.pioinit:15: Error in sourced command file:
Protocol error with Rcmd
Loop <_UnixSelectorEventLoop running=False closed=True debug=False> that handles pid 4176 is closed

Any idea what is going on especially why supposedly bootloader_dio_40m.bin cannot be opened?

Hi , I have not used this method recently but I remember that I had to download before I could debug .
I am not sure why this worked .