|
| 1 | +--- |
| 2 | +title: Debugging with Visual Studio Code |
| 3 | +permalink: /docs/tutorials/debugging-vscode/ |
| 4 | +author: Ingo Lütkebohle |
| 5 | +--- |
| 6 | + |
| 7 | +This is a follow-up to [Debugging with gdb and openocd](/docs/tutorials/debugging-gdb-openocd/), because the set up done in that tutorial is a pre-requisite to debugging with Visual Studio Code. |
| 8 | + |
| 9 | +## Motivation |
| 10 | + |
| 11 | +Visual Studio Code is a modern IDE that is very easy to extend and popular with both the Web/Cloud and IoT communities. It is also one of the easiest IDEs to get working with embedded systems. That said, it is *not* the most powerful or featureful IDEs for this purpose, but it is easy and will do. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | + * All the prerequisites of [Debugging with gdb and openocd](/docs/tutorials/debugging-gdb-openocd/) |
| 16 | + * Cortex-M hardware (all of our standard boards are ARM based) |
| 17 | + * [Visual Studio Code](https://code.visualstudio.com/) |
| 18 | + |
| 19 | + |
| 20 | +## Installing Cortex-Debug |
| 21 | + |
| 22 | +In the extensions marketplace, enter "cortex", then install "Cortex-Debug". Depending on your version of Visual Studio Code, you may need to restart after installing the extension. |
| 23 | + |
| 24 | +## Set up your project for debugging |
| 25 | + |
| 26 | +Open your project folder in Visual Studio Code -- this is usually the `NuttX` folder, or a subdirectory of `apps`. |
| 27 | + |
| 28 | +### Create a Visual Studio Code launch configuration for NuttX |
| 29 | + |
| 30 | +From the `Debug` menu, select `Open Configurations`. This will open a `launch.json' file. See [Cortex-Debug Launch configurations](https://marcelball.ca/projects/cortex-debug/cortex-debug-launch-configurations/) for documentation. |
| 31 | + |
| 32 | +To get started, I have prepared a working launch configuration for using our STM32-E407 board with the ARM-USB-OCD-H jtag probe. If you use a different board or probe, you only need to replace the `configFiles` section. Each entry in the section is an argument that you would normally pass as a `-f` option to OpenOCD. |
| 33 | +```json |
| 34 | +{ |
| 35 | + "version": "0.2.0", |
| 36 | + "configurations": [ |
| 37 | + { |
| 38 | + "name": "Debug (OpenOCD/NuttX)", |
| 39 | + "cwd": "${workspaceRoot}", |
| 40 | + "executable": "nuttx", |
| 41 | + "request": "launch", |
| 42 | + "type": "cortex-debug", |
| 43 | + "servertype": "openocd", |
| 44 | + "device": "stm32f4x", |
| 45 | + "configFiles": [ |
| 46 | + "interface/ftdi/olimex-arm-usb-ocd-h.cfg", |
| 47 | + "target/stm32f4x.cfg" |
| 48 | + ] |
| 49 | + } |
| 50 | + ] |
| 51 | +} |
| 52 | +``` |
| 53 | +The `name` is what will appear in the status bar for running it. |
| 54 | + |
| 55 | +### Running the debugger |
| 56 | + |
| 57 | +Either press `F5` or select `Debug/Start Debugging` from the menu to get started. This will take a moment, and then you should get a red status bar and the debug window, like in the following image: |
| 58 | + |
| 59 | + |
| 60 | +As in the gdb tutorial, initially you won't see much because the program is stopped during OS initialization. Press `F5` again or click the "play" button from the debug menu at the top of the window, wait a few seconds, then press `F6` or click the pause button. The window should change to give you thread, variable, and register information, like in the following. Note that "Call Stack" window displays multiple threads. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Adding an SVD File |
| 65 | + |
| 66 | +You may have noticed that on the left-hand side, there is a sub-window called "Cortex Peripherals" which simply states "No SVD File loaded". SVD means "System View Description" and is a standard format which microcontroller vendors use to describe the available features of their MCUs. |
| 67 | + |
| 68 | +For example, in the case of our STM32-E407 board, which features an STM32F407ZGT6 MCU, we can download the SVD description from [STM's web-page for the STM32F407ZG series](https://www.st.com/en/microcontrollers-microprocessors/stm32f407zg.html). In the "HW Model, CAD Libraries & SVD", you will find a link to the [STM32F4 series SVD](https://www.st.com/resource/en/svd/stm32f4_svd.zip). |
| 69 | + |
| 70 | +Extract the SVD and then add an `svdFile` attribute to the launch configuration. The full configuration will look like this: |
| 71 | + |
| 72 | +```json |
| 73 | +{ |
| 74 | + "version": "0.2.0", |
| 75 | + "configurations": [ |
| 76 | + { |
| 77 | + "name": "Debug (OpenOCD/SVD)", |
| 78 | + "cwd": "${workspaceRoot}", |
| 79 | + "executable": "nuttx", |
| 80 | + "request": "launch", |
| 81 | + "type": "cortex-debug", |
| 82 | + "servertype": "openocd", |
| 83 | + "device": "stm32f4x", |
| 84 | + "svdFile": "STM32F407.svd", |
| 85 | + "configFiles": [ |
| 86 | + "interface/ftdi/olimex-arm-usb-ocd-h.cfg", |
| 87 | + "target/stm32f4x.cfg" |
| 88 | + ] |
| 89 | + } |
| 90 | + ] |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Run the debugger again, and your window should look as follows: |
| 95 | + |
| 96 | + |
| 97 | +Voilà! The `Cortex Peripherals` is populated with everything that the STM32F407 MCU has to offer. Please note that not all of these peripherals might actually be connected on the board. However, those that are, and that are used in your application, can easily be investigated like this. |
0 commit comments