[Debug][Keil][VSCode] How to debug Keil's STM32 project in VSCode?

Assumptions:
  1. STM32CubeIDE needs to be installed. At the time of writing this guide, STM32CubeIDE version is 1.6.1 Build: 9958_20210326_1446 (UTC).
    OpenOCD and GDB server are already preinstalled in STM32CubeIDE, so no need to install separately;
  2. Keil's .uvprojx project needs to be built to .elf file (generated from .axf file);
    Used command:
    Code:
    fromelf.exe example_in.axf --elf --output example_out.elf"
  3. VSCode has plugins: “C/C++” and “Cortex-Debug”.
Steps:
Generate the STM32CubeIDE project for your MCU to get Debug Configuration File for OpenOCD.
To do that, follow [Debug][Keil][STM32CubeIDE] How to debug Keil's STM32 project in STM32CubeIDE? (Part 1)

Open Keil's Project location by “Open with Code” integrated menu in Windows - the place where you have /Src, /Inc, etc.


NOTE: If you are missing "Open with Code" option in Context Menu, then reinstall VSCode and make sure that you have checked these options:


Create “launch.json” file by hitting F1 in VSCode (which corresponds to “Menu->View->Command Palette”) and start typing “launch”.
Select “Debug: Open launch.json” and then select “Cortex Debug”.



Copy-paste the following template to the newly created “launch.json”:
JSON:
{
  "version": "0.2.0",
  "trace": true,
  "configurations": [
    {
      "name": "Debug stm32 template",
      "type": "cortex-debug",
      "request": "launch",
      "servertype": "openocd",
      "device": "not required to specify here",
      "executable": "${workspaceFolder}/relative/path/to/elf/file.elf",
      "gdbPath": "absolute/path/to/arm-none-eabi-gdb.exe",            // it is required, only if not in PATH
      "serverpath": "absolute/path/to/openocd.exe",                   // it is required, only if not in PATH
      "cwd": "${workspaceRoot}/relative/path/to/dir/with/uvprojx/or/uvproj/project/file/dir",
      "configFiles": [
        "${workspaceFolder}/relative/path/to/generated/in/STM32CubeIDE/debug/configuration/file_Debug.cfg"
      ],
      "searchDir": [
        "absolute/path/to/openocd/st_scripts",
        "or/absolute/path/to/openocd/scripts/with/board/interface/and/target/inside"
      ]
    }
  ]
}

Mine “launch.json” looks as follows:
JSON:
{
  "version": "0.2.0",
  "trace": true,
  "configurations": [
    {
      "name": "Debug stm32",
      "type": "cortex-debug",
      "request": "launch",
      "servertype": "openocd",
      "device": "not required to specify here",
      "executable": "${workspaceFolder}/ci_tmp/[DBG]_UC_PRODUCT_APP/[DBG]_UC_PRODUCT_APP.elf",
      "gdbPath": "C:/ST/STM32CubeIDE_1.6.1/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924/tools/bin/arm-none-eabi-gdb.exe",
      "serverpath": "C:/ST/STM32CubeIDE_1.6.1/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.openocd.win32_1.6.0.202101291314/tools/bin/openocd.exe",
      "cwd": "${workspaceRoot}/Projects/MDK-ARM",
      "configFiles": [
        "${workspaceFolder}/Application/project_CubeMX/CubeMX_UC_PRODUCT_APP/CubeMX_UC_PRODUCT_APP_Debug.cfg"
      ],
      "searchDir": [
        "C:/ST/STM32CubeIDE_1.6.1/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.debug.openocd_1.6.0.202102111023/resources/openocd/st_scripts"
      ]
    }
  ]
}

Hit F5 in VSCode (which corresponds to “Menu->Run->Start Debugging”) and start debugging in VSCode!





Hope it was helpful for someone there.

------------------------------------------------------
Is this blog entry helpful or does it need an improvement?
Please leave the comment below.

Comments

There are no comments to display.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…