Attaching to STM32 with OpenOCD during sleep

January 10, 2020


mbed-os uses sleep to save power. This is nice except when the debugger won't attach. So here's how to force it.


The error is shown when running openocd below:


~$ openocd -f interface/stlink.cfg -f board/stm32h7x3i_eval.cfg
Open On-Chip Debugger 0.10.0+dev-00985-g2dc88e14 (2019-12-27-11:14)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Warn : Interface already configured, ignoring
Error: already specified hl_layout stlink
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1800 kHz
Info : STLINK V3J2M1 (API v3) VID:PID 0483:374E
Info : Target voltage: 3.290837
Info : stm32h7x.cpu0: hardware has 8 breakpoints, 4 watchpoints
Error: mem2array: Read @ 0x5c001004, w=4, cnt=1, failed
Error executing event examine-end on target stm32h7x.cpu0:
/usr/local/bin/../share/openocd/scripts/target/stm32h7x.cfg:199: Error: 
in procedure 'stm32h7x_dbgmcu_mmw' called at file "/usr/local/bin/../share/openocd/scripts/target/stm32h7x.cfg", line 149
in procedure 'stm32h7x_mmw' called at file "/usr/local/bin/../share/openocd/scripts/target/stm32h7x.cfg", line 224
in procedure 'stm32h7x_mrw' called at file "/usr/local/bin/../share/openocd/scripts/target/stm32h7x.cfg", line 205
at file "/usr/local/bin/../share/openocd/scripts/target/stm32h7x.cfg", line 199
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Info : Halt timed out, wake up GDB.
Error: timed out while waiting for target halted
Error executing event gdb-attach on target stm32h7x.cpu0:

Warn : Cannot identify target as a STM32H7xx family.
Error: auto_probe failed
Error: Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.
Error: attempted 'gdb' connection rejected
Info : accepting 'gdb' connection on tcp/3333
Info : Halt timed out, wake up GDB.
Error: timed out while waiting for target halted
Error executing event gdb-attach on target stm32h7x.cpu0:


and from the gdb side


(gdb) target remote localhost:3333
Remote debugging using localhost:3333
Remote communication error.  Target disconnected.: Connection reset by peer.


This is the part of interest from OpenOCD's log:


Error: Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.
Error: attempted 'gdb' connection rejected


I'm not sure if this is a problem with OpenOCD or what but it just doesn't work unless you time it right. Thankfully it suggests how to fix it.


First close openocd and then rerun it with the -c "gdb_memory_map disable" argument added on


openocd -f interface/stlink.cfg -f board/stm32h7x3i_eval.cfg -c "gdb_memory_map disable"


Then open gdb and attach again and gdb will output something like


(gdb) target remote localhost:3333
Remote debugging using localhost:3333
0x00000000 in ?? ()


openocd will cough out a few warnings then halt.


Warn : negative reply, retrying
Warn : negative reply, retrying


Now you can halt in gdb, then restart openocd without the gdb_memory_map disable option.



Then reconnect gdb and you can load the new firmware and carry on as usual.


(gdb) load
Loading section .text, size 0x841c lma 0x8000000
Loading section .ARM.exidx, size 0x6e0 lma 0x800841c
Loading section .data, size 0x10 lma 0x8008afc
Loading section .crash_data_ram, size 0x100 lma 0x20000298
Start address 0x8000362, load size 35852
Transfer rate: 80 KB/sec, 5975 bytes/write.
(gdb) monitor reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08006c24 msp: 0x24080000



Hope this helps someone!