VSCode, Define custom variables to use in tasks, lauch and cpp properties

2.1k Views Asked by At

I am setting up VSCode for Embedded development with Nordic NRF52. I have noticed that in the nrf_sdk there are examples for several boards and several soft devices. I have seen that it is possible to reference env variables that have been previously defined both in tasks, launch and extension properties, such as: c_cpp, stm32-for-vscode. But what about having a section in the workspace file defined as my_vars so the workspace file looks like this?

{
"my_vars": {
    "BOARD_NAME": "NRF52840_MDK_USB_DONGLE",
    "MCU_NAME": "NRF52840",
    "SOFT_DEVICE": "S140",
    "BOARD_VARIANT_PATH": "/pca10059/mbr"

},
"launch": {
"version": "0.2.0",
"configurations": [
    {
    "name": "Cortex Debug",
    "cwd": "${workspaceRoot}",
    "executable": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc/_build/${my_vars:MCU_NAME}_xxaa.out",
    "request": "launch",
    "type": "cortex-debug",
    "servertype": "jlink",
    "device": "nrf52",
    "interface": "swd",
    "ipAddress": null,
    "serialNumber": null,
    "armToolchainPath": "${env:VSARM}/armcc/bin/"
    },
]

},
"folders": [
    {
        "path": "."
    }
],
"settings":{
    // "C_Cpp.name": "nRF52840 DK",
    "C_Cpp.default.includePath": [
        "${workspaceFolder}/**",
        "${env:GNU_GCC}/arm-none-eabi/include",
        "${env:NRF_SDK}/modules/**",
        "${env:NRF_SDK}/components/**"
    ],
    "C_Cpp.default.defines":  [
        "${my_vars:BOARD_NAME}",
        "CONFIG_GPIO_AS_PINRESET",
        "INITIALIZE_USER_SECTIONS",
        "FLOAT_ABI_HARD",
        "NRF52",
        "${my_vars:MCU_NAME}_XXAA",
        "NRF_SD_BLE_API_VERSION=6",
        "${my_vars:SOFT_DEVICE}",
        "SOFTDEVICE_PRESENT",
        "SWI_DISABLE0"
    ],
    "C_Cpp.default.compilerPath": "${env:GNU_GCC}/bin/arm-none-eabi-gcc",
    "C_Cpp.default.cStandard": "c11",
    "C_Cpp.default.cppStandard": "c++17",
    "C_Cpp.default.intelliSenseMode": "clang-x64"
},
"tasks": {
    "version": "2.0.0",
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "tasks": [
        {
            "label": "nRF52 build",
            "type": "shell",
            "command": "make",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
            },
            {
            "label": "nRF52 build clean",
            "type": "shell",
            "command": "make clean",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
                }
            },
            {
            "label": "nRF52 flash",
            "type": "shell",
            "command": "make flash",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
            },
            {
            "label": "nRF52 dfu upload",
            "type": "shell",
            "command": "make bootload",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
            },
            {
            "label": "nRF52 flash_softdevice",
            "type": "shell",
            "command": "make flash_softdevice",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            },
            {
            "label": "nRF52 sdk_config",
            "type": "shell",
            "command": "make sdk_config",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "problemMatcher": []
            },
            {
            "label": "nRF52 erase",
            "type": "shell",
            "command": "make erase",
            "options": {
                "cwd": "${workspaceFolder}${my_vars:BOARD_VARIANT_PATH}/armgcc"
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            }
        ]
}
}

I tried having a task which is set_env, which in my case, since I am in Windows, run a batch script that does:

set BOARD_NAME=NRF52840_MDK_USB_DONGLE
set MCU_NAME=NRF52840
set SOFT_DEVICE=S140
set BOARD_VARIANT_PATH=/pca10059/mbr

This will ease a lot playing around with the SDK examples and could be potential work for a NRF52 VSCode extension, as https://marketplace.visualstudio.com/items?itemName=bmd.stm32-for-vscode does.

2

There are 2 best solutions below

3
On

Have a look at the extension Command Variable.

It can use the content of a file as key-value pairs.

0
On

You've undoubtedly solved this problem by now, but hopefully I can save someone else some searching.

There is a top-level section called env that holds global variables that you can reference in your configs.

Here's an example from MSDN.

{
  "env": {
    "myDefaultIncludePath": ["${workspaceFolder}", "${workspaceFolder}/include"],
    "myCompilerPath": "/usr/local/bin/gcc-7"
  },
  "configurations": [
    {
      "name": "Mac",
      "intelliSenseMode": "clang-x64",
      "includePath": ["${myDefaultIncludePath}", "/another/path"],
      "macFrameworkPath": ["/System/Library/Frameworks"],
      "defines": ["FOO", "BAR=100"],
      "forcedInclude": ["${workspaceFolder}/include/config.h"],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "compileCommands": "/path/to/compile_commands.json",
      "browse": {
        "path": ["${workspaceFolder}"],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}