Visual Studio Code

From KONNEKTING Wiki
Jump to: navigation, search

Setup

  1. Download Visual Studio Code for your system: https://code.visualstudio.com/
  2. Install VS Code on your system (depends on your platform)
  3. Run VS Code an click on the "Extensions" icon on the vertical icon bar the rectangular icon below the "bug") Vscode started extensions.png
  4. Search for "Arduino", install the one that is provided by Microsoft, then reload VS Code to active the extension Vscode arduino by microsoft.png
  5. Go to the "Folders" (icon bar, top most icon with the two paper-sheets) and open your Arduino Sketch Folder that you want to use in VS Code

User-Settings

  1. Press F1 and enter "settings". You will get list of search results. Select "Preferences: Open User Settings (JSON)"
  2. Below you will see the default user settings on the left and the user settings on th right.
  3. Copy and paste the following to "User settings" on the right and adapt the paths to your system:
{
    "arduino.path": "/home/achristian/arduino-1.8.5", // <-- Your Arduino IDE folder
    "arduino.logLevel": "verbose",
    "C_Cpp.intelliSenseEngine": "Tag Parser",
    "C_Cpp.clang_format_fallbackStyle": "LLVM", // <-- For using the correct code formatter when doing auto-format
    "arduino.additionalUrls": [
         // Arduino 3rd party core download URLs you use
        "http://digistump.com/package_digistump_index.json",
        "http://downloads.arduino.cc/Hourly/samd/package_samd-hourly-build_index.json",
        "https://github.com/esp8266/Arduino/releases/download/2.4.0-rc2/package_esp8266com_index.json"
    ],
    "C_Cpp.default.browse.path": [
        "/media/bigdisk/Programming/Arduino/Sketchbook/libraries",         // <-- Path to the Arduino Libraries Folder (typically in Sketchbook folder)
        "/home/achristian/arduino-1.8.5/hardware/arduino",                 // <-- Path to base Arduino hardware folder for common Arduino API
        "/home/achristian/.arduino15/packages",                            // <-- Path to the downloaded Arduino Cores like ESP8266, Arduino Zero, ...
        "${workspaceRoot}"                                                 // <-- Variable pointing to your current project workspace root
    ],
    "editor.wordWrap": "on"    
}
  1. Close and save the settings.
  2. Restart VS Code and reopen project

Additional recommended Extensions

  • vscode-icons: brings you nice and fancy icons to your files and folders
  • GitLens: more control over git-controlled code/projects

Hotkeys

  • F1 - InputConsole
  • F5 - Start Debugging
  • CTRL+ALT+R - Verify Arduino Project
  • CTRL+ALT+U - Upload Arduino Project
  • CTRL-Shift+O - Current outline: Shows list of variables and methods of current opened file

Debugging

Visual Studio Code is storing the debug configuration in ${workspaceRoot}/.vscode/launch.json.

{
    // Verwendet IntelliSense zum Ermitteln möglicher Attribute.
    // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
    // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Arduino - STLink v2",
            "type": "arduino",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "MIMode": "gdb",
            "targetArchitecture": "arm",
            "miDebuggerPath": "/home/achristian/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gdb",
            "debugServerPath": "/home/achristian/.arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/bin/openocd",
            "debugServerArgs": " -f /home/achristian/.arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts/interface/stlink-v2.cfg -f /media/bigdisk/Programming/Arduino/at91samdXXZero.cfg -s /home/achristian/.arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts",

            "customLaunchSetupCommands": [
                {
                    "text": "target remote localhost:3333"
                },
                {
                    "text": "file \"${file}\""
                },
                {
                    "text": "load"
                },
                {
                    "text": "monitor reset halt"
                },
                {
                    "text": "monitor reset init"
                }
            ],
            "stopAtEntry": true,
            "serverStarted": "Info\\ :\\ [\\w\\d\\.]*:\\ hardware",
            "launchCompleteCommand": "exec-continue",
            "filterStderr": true,
            "args": []
        },
        {
            "name": "Arduino - Atmel ICE - SAMD",
            "type": "arduino",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "MIMode": "gdb",
            "targetArchitecture": "arm",
            "miDebuggerPath": "/home/achristian/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gdb",
            "debugServerPath": "/home/achristian/.arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/bin/openocd",
            "debugServerArgs": " -d2 -s /home/achristian/.arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts/ -f /home/achristian/.arduino15/packages/arduino/hardware/samd/1.6.19/variants/arduino_zero/openocd_scripts/arduino_zero.cfg",
            "customLaunchSetupCommands": [
                {
                    "text": "target remote localhost:3333"
                },
                {
                    "text": "file \"${file}\""
                },
                {
                    "text": "load"
                },
                {
                    "text": "monitor reset halt"
                },
                {
                    "text": "monitor reset init"
                }
            ],
            "stopAtEntry": true,
            "serverStarted": "Info\\ :\\ [\\w\\d\\.]*:\\ hardware",
            "launchCompleteCommand": "exec-continue",
            "filterStderr": true,
            "args": [],
            "logging": { "trace": true, "traceResponse": true, "engineLogging": true }
        }
    ]
}

Copy & paste the content shown here to you file and adapt the paths contained in the following parameters to match your system/installation:

  • miDebuggerPath
  • debugServerPath
  • debugServerArgs

ST-Link v2

For ST-Link v2 there's an extra file that you need to download to your harddrive and set the path accordingly:

https://raw.githubusercontent.com/DeqingSun/Debug-Arduino-Zero-with-ST-Link-V2/master/at91samdXXZero.cfg

For more information about VSC with STLinkv2, see: https://github.com/DeqingSun/Debug-Arduino-Zero-with-ST-Link-V2

Global Debug Config

It is possible to have the debug config global, for all projects. See: https://github.com/Microsoft/vscode/issues/18401#issuecomment-272400316