[GUIDE] Setting up vscode for debugging OpenWare

This is a brief explanation how to get debugging working for OpenWare. It’s assumed that you’ve connected ST-Link programmer and have a working openocd config in Hardware/openocd.cfg. You should be able to run “make upload” in MidiBoot without errors before you can start a debugging session.

There are multiple other options to do it, this is just one of them (that works fairy well)

  1. Install Visual Studio Code - https://code.visualstudio.com/

  2. Install at least 2 extensions in VSCode extensions page: “Cortex-Debug” and “Cortex-Debug: Device Support Pack - STM32F4”.

  3. Create this directory/file for debugging config in OpenWare/.vscode/launch.json

{  
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",  
            "cwd": "${workspaceRoot}",
            "executable": "Magus/Build/Magus.elf",
            "name": "Debug Magus - no bootloader",
            "device": "STM32F427ZI",
            "configFiles": [    
                "Hardware/openocd.cfg"
            ],
            "postLaunchCommands": [
                "monitor arm semihosting enable"
            ]        
        },
        { 
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",  
            "cwd": "${workspaceRoot}",
            "executable": "MidiBoot/Build/MidiBoot.elf",
            "name": "Debug MidiBoot",
            "device": "STM32F427ZI", 
            "configFiles": [
                "Hardware/openocd.cfg"
            ],
            "postLaunchCommands": [
                "monitor arm semihosting enable"
            ]
        },
        { 
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",  
            "cwd": "${workspaceRoot}",
            "executable": "MidiBoot/Build/MidiBoot.elf",
            "name": "Debug MidiBoot + Magus",
            "device": "STM32F427ZI",
            "configFiles": [
                "Hardware/openocd.cfg"
            ],
            "postLaunchCommands": [
                "monitor arm semihosting enable",
                "add-symbol-file Magus/Build/Magus.elf 0x080101b0" // Last number is address for start of .text section, may change if you do something funny with linker script or firmware structure. Check Magus.elf contents with readelf tool if necessary.
            ]
        } 
    ]
}  

This would create 3 configurations in VSCode “Run” page.

First one should be used if you intend to debug only bootloader (and would overwrite it when you start debugging session).

Second would erase bootloader section and write only firmware to flash. This is the easiest way to debug firmware, but you must restore bootloader later.

Third config would overwrite bootloader, but then it would run currently stored firmware. This allows you to debug firmware without re-flashing it. This has some limitations, specifically that some symbols from firmware may be shadowed by identical symbols from bootloader. This can make debugging harder (yet not impossible).

Clicking “Run” > “Debug …” should flash previously built firmware and start debugging session

This allows you to:

  • execute code step by step
  • see MCU peripheral and registers
  • watch current variable states
  • see memory contents
  • disassemble functions

, etc. There are more detailed tutorial links at cortex-debug wiki

3 Likes