This guide shows you how to set up Visual Studio Code to develop and debug Mynewt applications. Visual Studio Code is supported on Mac OS, Linux, and Windows. This guide shows you how to:
Prerequisites:
Have Internet connectivity to fetch remote Mynewt components.
Have a computer to build a Mynewt application.
Perform native installation for the Mynewt tools and toolchains.
Note: For Windows platforms, ensure that the MinGW bash you install is added to your Windows Path. In addition, if you are using Windows 10 WSL, you must have the MinGW bash before the Windows 10 WSL bash in your Windows Path.
Read the Mynewt OS Concepts section.
Create a project space (directory structure) and populate it with the core code repository (apache-mynewt-core) or know how to as explained in Creating Your First Project.
Complete one of the Blinky Tutorials.
Notes:
Download and install Visual Studio Code from https://code.visualstudio.com/.
You need to install two extensions:
The C/C++ extension from Microsoft. This extension provides language support such as symbol searching, signatuare help, go to definition, and go to declaration.
The Native Debug extension from webfreak. This extension provides GDB support.
Ctrl-P
to open the search box.ext install cpptools
in the search box and press Enter. You should see the extension at the top of the list.Install
to install the extension. To install the Native Debugger:
Ctrl-P
to open the search box.ext install webfreak.debug
in the search box and press Enter. You should see the Native Debug extension at the top of the list.Install
to install the extension. Two main concepts in Visual Studio Code are workspaces and tasks. A workspace represents a folder that is open. You can open multiple workspaces and switch between workspaces.
Tasks allow you to integrate the external tools and operations that are used to build or test your project into Visual Studio Code. Tasks are run from and the task results can be analyzed in Visual Studio Code. Tasks are defined within the scope of a workspace. This means that the tasks you define for a workspace only apply to the given workspace.
Select File > Open Folder, and select the myproj
folder from the Select Folder
dialog box to open the folder.
You define Visual Studio Code tasks to build and debug your Mynewt targets in Visual Studio Code. We use the Blinky application for the Arduino Zero board from the Blinky On Arduino Zero Tutorial to illustrate how to define the tasks to build and debug the Arduino blinky bootloader and application targets.
Perform the following steps to create the tasks to build and debug the Arduino blinky bootloader and appliction targets:
Step 1: Press Ctrl-Shift-P
, type task
, and select Tasks:Configure Task Runner from the search results.
Step 2: Select Others (scroll down to the bottom of the list) to create a task runner for external commands.
The sample tasks.json
file defines a simple task that runs the echo command with “Hello World” as the argument.
Step 3: Delete the content from the tasks.json
file, add the following definitions, and press Ctrl-S
to save the file.
{ "version": "0.1.0", "command": "newt", "echoCommand": true, "isShellCommand": true, "tasks":[ { "taskName": "build_arduino_boot", "args": ["build", "arduino_boot"], "suppressTaskName": true }, { "taskName": "build_arduino_blinky", "args": ["build", "arduino_blinky"], "isBuildCommand": true, "suppressTaskName": true }, { "taskName": "create_arduino_blinky", "args": ["create-image", "arduino_blinky", "1.0.0"], "suppressTaskName":true }, { "taskName": "debug_arduino_blinky", "args": ["debug", "arduino_blinky", "-n"], "suppressTaskName": true } ] }
The following tasks are defined in this example:
build_arduino_boot: Runs the newt build arduino_boot
command to build the arduino_boot target.
build_arduino_blinky: Runs the newt build arduino_blinky
command to build the arduino_blinky target.
Note: This task sets the isBuildCommand
property to true
. This is an optional property that, when set to true, allows you to run the Tasks: Run Build Task(Ctrl-Shift-B
) command to start the task.
create_arduino_blinky: Runs the newt create-image arduino_blinky
command to create the image file.
debug_arduino_blinky: Runs the newt debug arduino_blinky -n
command to debug the arduino_blinky target. The -n
flag is specified to start only the GDB server and not the GDB client. We will launch the GDB client from Visual Studio Code.
For more information on tasks and all supported properties, see the Visual Studio Code Task documentation.
To run a task, press Ctrl-Shift-P
, type task
on the search box, and select Tasks: Run Task. The tasks that you define in the tasks.json
file are listed. Select the task to run.
The following is an example of running the build_arduino_boot
task:
Note:To run the build_arduino_blinky
task, you can use the keyboard shortcut Ctrl-Shift-B
because the task has the property isBuildCommand
set to true.
Other newt commands, such as the newt load
command, do not need to run from within Visual Studio Code. You can define a task for each command as a convenience and run the command as a task, or you can run the newt command on the command line from the Visual Studio Code integrated terminal or an external terminal.
To create the tasks for the newt load arduino_boot
and newt load arduino_blinky
commands, add the following definitions to the tasks.json
file:
{ "taskName": "load_arduino_boot", "args": ["load", "arduino_boot"], "suppressTaskName":true }, { "taskName": "load_arduino_blinky", "args": ["load", "arduino_blinky"], "suppressTaskName":true },
Step 1: Select Debug > Open Configuration, and select the GDB environment.
You should see a default launch.json
file created in the .vscode
folder.
{ "version": "0.2.0", "configurations": [ { "name": "gdb_arduino_blinky", "type": "gdb", "request": "attach", "executable": "${workspaceRoot}\\bin\\targets\\arduino_blinky\\app\\apps\\blinky\\blinky.elf", "target": ":3333", "cwd": "${workspaceRoot}", "gdbpath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\4.9 2015q2\\bin\\arm-none-eabi-gdb.exe", "remote": true } ] }
blinky.elf
file for the executable.Step 1: Run the debug_arduino_blinky task to start the GDB server. Perform the following:
Ctrl-Shift-P
and type task
in the search box.Ctrl-Shift-U
to open the Output Panel and see the OpenOCD GDB Server output. Step 2: Start the GDB session. Perform the following:
Ctrl-Shift-Y
to view the Debug Console.gdb_arduino_blinky
from the DEBUG drop down menu.As mentioned previously, each mynewt project corresponds to a Visual Studio Code workspace. If you have multiple Mynewt application targets defined in same project, you will need to define build and debug tasks for each target in the tasks.json
file and debugger configurations for the targets in the launch.json
file for the workspace. If you have a different Mynewt project for each mynewt application, you will need to define build and debug tasks in the tasks.json
file and the debugger configuration in the launch.json
file for each workspace.