Debugging Guide

Being able to debug your code is pretty much an essential skill for developing embedded firmware. While printf statements are a perfectly valid way of debugging, modern debugging tools exist to supercharge your bug tracking. Some features of modern debuggers include setting breakpoints, stepping through code, watching variables, and much more.

Any modern debugging tool that supports ARM Cortex-M microcontrollers will probably work for debugging STM32 chips, but GDB is the officially supported debugging engine for mJackets. GDB, the GNU Project debugger, allows you to see what is going on ‘inside’ another program while it executes – or what another program was doing at the moment it crashed. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

  1. Start your program, specifying anything that might affect its behavior.

  2. Make your program stop on specified conditions.

  3. Examine what has happened, when your program has stopped.

  4. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

The following sections will detail getting GBD set up with your development environment.

Install Required Tools

Several tools are required to debug STM32 microcontrollers:

VSCode / VSCodium Setup

Cortex-Debug

The Cortex-Debug extension is the glue that pulls in all of the capabilites of GDB into VSCode. You will need to enable the extension from the extension marketplace.

In your VSCode .vscode/settings.json file, add the following line to let Cortex-Debug know where your JLink GDB server executable lives:

“cortex-debug.JLinkGDBServerPath”: “/opt/SEGGER/JLink/JLinkGDBServerCLExe”

You then need to add a debug configuration to your .vscode/launch.json. For the reccommended setup, use the below configuration, editing the device, interface, and executable parameters to match your project.


{

“version”: “0.2.0”, “configurations”: [

{

“name”: “Debug”, “cwd”: “${workspaceRoot}”, “executable”: “./build/debug/myApp.elf”, “request”: “launch”, “type”: “cortex-debug”, “servertype”: “jlink”, “interface”: “jtag”, “device”: “STM32F769NI”, “runToMain”: true, “preRestartCommands”: [

“load”, “enable breakpoint”, “monitor reset”

]

}

]

}