This tutorial explains how to debug the nRF52840-MDK board using Visual Studio Code.

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. With the addition of the C/C++ extension and Cortex-Debug extension to Visual Studio Code, you might have what is needed in such a small, cross-platform editor.

Hardware Requirements

  • nRF52840-MDK development board
  • A USB-C cable
  • A Windows/macOS/Linux PC

Installing Visual Studio Code

You need to install Visual Studio Code with the necessary extensions to begin.

  1. Install Visual Studio Code.
  2. Open Visual Studio Code, and click on the Extensions button.
  3. Search for the C/C++ extension and Cortex-Debug extension, then install them.
Cortex-Debug extension

Setting up debug toolchain

nRF52840-MDK has a CMSIS-DAP interface chip on board, which offers debugging capabilities for stack trace analysis, register dumps and inspection of program execution (breakpoints, watchpoints and so on).

Make sure you have installed the GNU Arm Embedded Toolchain and added it to your PATH. To verify that you have the correct version installed, open a terminal and run:

$ arm-none-eabi-gdb --version
GNU gdb (GNU Tools for ARM Embedded Processors 6-2017-q2-update) 7.12.1.20170417-git
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.

Then, install pyOCD. The latest stable version of pyOCD may be installed via pip as follows:

$ pip install -U pyocd

Configuring pyOCD GDB Server

Cortex-Debug is an extension to enhance the debug capabilities of Visual Studio Code when debugging embedded software running on ARM® Cortex-M microcontrollers.

To configure the pyOCD GDB Server for your project:

  1. Open the folder in Visual Studio Code.
  2. Click Debug -> Open Configurations to open the .vscode/launch.json file. Here is an example configuration for the pyOCD GDB Server:
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "pyocd",
            "cwd": "${workspaceRoot}",
            "executable": "./armgcc/_build/nrf52840_xxaa.out",
            "name": "Debug (pyOCD)",
            "device": "nrf52840_xxaa",
            "targetId": "nrf52",
            "boardId": ""
        }
    ]
}

Debugging your project

Open View -> Terminal . In the Terminal window, change to armgcc directory and make the project.

Make blinky example

Connect the board to your PC, click Debug -> Start Debugging , and debugging starts. Click on the Debug Console tab to see the debug output:

start debugging

Now you can explore the debugging capabilities for Variables, Breakpoints, Registers dumps and so on.

Conclusion

Visual Studio Code is a powerful and modern editor for Embedded Development. With Cortex-Debug extension, it is very convenient to debug your applications as you did in other IDEs(Keil or IAR). Above all, it is FREE and Open source!

Reference