Building OpenOCD for Windows and Exploring its Internal Logic

This tutorial shows how to diagnose problems internal to OpenOCD using VisualGDB and the Sysprogs OpenOCD fork that includes convenient CMake build rules. Before you begin, install VisualGDB 5.4 or later.

We will show how to build OpenOCD from our Github repo, configure it to run with meaningful arguments and analyze its internal logic responsible for erasing FLASH memory.

  1. Download our MinGW toolchain with libusb library from the releases section of our OpenOCD repo. Unpack it to c:\MinGW or another folder:01-extract
  2. Clone our OpenOCD repo (including submodules) into a folder on your computer:

  3. Ensure you have VisualGDB 5.4 or later installed. Then locate and open the OpenOCD.sln file inside the repository. VisualGDB will ask for the location of the MinGW toolchain:tc
  4. Select the location where you installed the MinGW toolchain and click “OK”. Then build OpenOCD by pressing Ctrl-Shift-B:oocd
  5. Open an embedded project that reproduces the issue you would like to diagnose, go to VisualGDB Project Properties -> Debug Settings and click “Test”:
  6. The “Testing settings” will show the command line used to launch OpenOCD. Copy it to clipboard:07-cmdlineA typical command line for STM32F4 devices is shown below:
  7. Switch back to OpenOCD.sln and open VS Properties for the openocd.exe target. Remove the “gdb_port ” and “telnet_port” commands (and the corresponding -“c” flags) from the command line and copy it to the “Command line arguments” field:08-copied
  8. Locate the original OpenOCD installation under %LOCALAPPDATA%\VisualGDB\EmbeddedDebugPackages\com.sysprogs.arm.openocd\bin and copy all .dll files from its directory to the VisualGDB\Debug\src subdirectory of the OpenOCD project:09-libs
  9. Press F5 to start debugging. Confirm that OpenOCD starts listening on port 3333 for gdb connections:10-openocd
  10. Now we will show how to step through the logic responsible for FLASH erasing. Ensure your embedded project is built and open its binary in gdb using the following command line:

    The ELF file is typically located in the VisualGDB\Debug directory and has no extension. Then enter “target remote :3333″ in the gdb command prompt in order to connect to OpenOCD:
  11. Locate the FLASH driver for your device family (e.g. stm32f2x.c for STM32F4) and set a breakpoint in the erase function. Then switch to gdb and run the “load” command in order to program the memory manually. The breakpoint inside OpenOCD will hit:
    12-eraseYou can now step through the code to double-check the OpenOCD logic responsible for various parts of the erase process and fine-tune parts of it.
  12. If you want to use a modified version of OpenOCD instead of the one shipped with VisualGDB, simply replace the %LOCALAPPDATA%\VisualGDB\EmbeddedDebugPackages\com.sysprogs.arm.openocd\bin\openocd.exe executable with the one you built and VisualGDB will use it instead.