Skip to content

Commit

Permalink
[vscode] Support tasks and intelliSense
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Oct 27, 2021
1 parent 0722111 commit 8d0ffa5
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 35 deletions.
29 changes: 29 additions & 0 deletions tools/ide/vscode/c_cpp_properties.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"configurations": [
%% for profile, config in profiles.items()
{
"name": "{{ platform }}GCC ({{ profile | capitalize }})",
"includePath": [
%% for path in include_paths | sort
"${workspaceFolder}/{{ path | modm.windowsify(escape_level=1) }}"{% if not loop.last%},{% endif %}
%% endfor
],
"defines": [
%% for define in config.cppdefines | sort
"{{ define }}"{% if not loop.last%},{% endif %}
%% endfor
],
"compilerPath": "{{ compiler_path }}",
"compilerArgs": [
%% for flag in config.archflags | sort
"{{ flag }}"{% if not loop.last%},{% endif %}
%% endfor
],
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-arm"
}{% if not loop.last%},{% endif %}
%% endfor
],
"version": 4
}
37 changes: 29 additions & 8 deletions tools/ide/vscode/launch.json.in
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
{
"version": "0.2.0",
"configurations": [
%% for profile in profiles
%% for config in configs
{
"name": "modm debug configuration ({{ profile.name }}) for {{ partname }}",
"cwd": "${workspaceRoot}",
"executable": "{{ profile.executable }}",
"name": "Debug ({{ config.profile | capitalize }}, {{ config.tool | capitalize }}, {{ partname }})",
"cwd": "${workspaceFolder}",
"executable": "{{ config.executable }}",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"device": "{{ partname }}",
"runToMain": true,
%% if with_freertos
"rtos": "FreeRTOS",
%% endif
"configFiles": [
%% for openocd_config_file in openocd_config_files
"{{ openocd_config_file }}"{% if not loop.last %},{% endif %}
%% for cfg in openocd_cfg
"{{ cfg }}"{% if not loop.last %},{% endif %}
%% endfor
]
}{% if not loop.last %},{% endif %}
],
%% if with_rtt
"rttConfig": {
"enabled": true,
"address": "0x{{"%0x" % rtt_ram.start}}",
"searchSize": {{ rtt_ram.size }},
"searchId": "modm.rtt.modm",
"polling_interval": 1,
"decoders": [
%% for id in rtt_channels
{
"port": {{id}},
"type": "console"
},
%% endfor
]
},
%% endif
},
%% endfor
]
}
91 changes: 72 additions & 19 deletions tools/ide/vscode/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Raphael Lehmann
# Copyright (c) 2021, Niklas Hauser
#
# This file is part of the modm project.
#
Expand All @@ -10,38 +11,90 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

import subprocess
import platform

def init(module):
module.name = ":ide:vscode"
module.description = FileReader("module.md")


def prepare(module, options):
module.depends(":build")
return options[":target"].has_driver("core:cortex-m*")
return True


def build(env):
debug_profiles = list()
pass


def post_build(env):
env.outbasepath = env.relcwdoutpath(".vscode/")

configs = []
build_path = env.relative_outpath(env[":build:build.path"])
for buildtool in ["scons", "make", "cmake"]:
for buildtool in ["scons", "make"]:
for profile in ["release", "debug"]:
if env.has_module(":build:" + buildtool):
debug_profiles.append({
"name": buildtool + " profile=" + profile,
configs.append({
"tool": buildtool,
"profile": profile,
"executable": "".join([build_path, "/", buildtool, "-", profile, "/", env[":build:project.name"], ".elf"]),
})

openocd_config_files = [
"modm/openocd.cfg",
]
if env[":build:openocd.cfg"]:
openocd_config_files.append(env[":build:openocd.cfg"])

env.substitutions.update({
"profiles": debug_profiles,
"partname": env[":target"].partname,
"openocd_config_files": openocd_config_files,
})
# these files are placed into the CWD
env.outbasepath = env.relcwdoutpath(".vscode/")
env.template("launch.json.in")
compiler = ""
core = ""
if env[":target"].has_driver("core:cortex-m*"):
compiler += "arm-none-eabi-"
core = "Arm "
elif env[":target"].has_driver("core:avr*"):
compiler += "avr-"
core = "AVR "
compiler += "g++"

# Find out the toolchain location
if "Windows" in platform.platform():
# FIXME: how do I query this on Windows?
compiler_path = compiler + ".exe"
else:
compiler_path = subprocess.run("which " + compiler, shell=True, stdout=subprocess.PIPE)
compiler_path = compiler_path.stdout.decode("ascii").strip()

flags = env.query("::collect_flags")(env)[None]
profiles = {
p: {f: (flags[f][""] + flags[f][p]) for f in ["cppdefines", "archflags"]}
for p in ["release", "debug"]
}

env.substitutions = {
"configs": configs,
"partname": env[":target"].partname.upper(),
"with_freertos": env.has_module(":freertos"),
"platform": core,
"profiles": profiles,
"include_paths": env.collector_values("::path.include"),
"compiler_path": compiler_path,
# FIXME: RTT block is searched for too early.
# See https://github.com/Marus/cortex-debug/wiki/SEGGER-RTT-support#known-issues
"with_rtt": False, # env.has_module(":platform:rtt"),
}
# IntelliSense config
env.template("c_cpp_properties.json.in")

# Only generate the tasks file for non-CMake build systems!
if configs:
env.template("tasks.json.in")

# Debugging support for Cortex-M only
if env[":target"].has_driver("core:cortex-m*"):
# rtt_ram = env.query(":platform:cortex-m:linkerscript", {})
# rtt_ram = rtt_ram.get("cont_ram_regions", [{"start": 0x20000000, "size": 4096}])[0]
# env.substitutions["rtt_ram"] = rtt_ram
# env.substitutions["rtt_channels"] = range(len(env.get(":platform:rtt:buffer.tx", [])))

openocd_cfg = ["modm/openocd.cfg"]
if env.get(":build:openocd.cfg", ""):
openocd_cfg.append(env[":build:openocd.cfg"])
env.substitutions["openocd_cfg"] = openocd_cfg

env.template("launch.json.in")
30 changes: 22 additions & 8 deletions tools/ide/vscode/module.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Visual Studio Code (VSCode)

[Visual Studio Code (VSCode)][VSCode] is a popular IDE with an integrated debugger UI.
Together with the [Cortex-Debug][] extension it works great for Cortex-M targets.
[Visual Studio Code (VSCode)][VSCode] is a popular IDE with an integrated
debugger UI.

The blog [mcuoneclipse.com has a nice post][mcuoneclipse-vscode-debug] on how to setup
and use Visual Studio Code for C/C++ with ARM Cortex-M properly.
This module provides configuration files to integrate building, debugging,
uploading and code completion into the IDE:

This module generates a `.vscode/` folder with the following files:
- `.vscode/tasks.json`: If the `modm:build:scons` and `modm:build:make` module
are included, this file wraps the building and uploading commands by profile.
You can invoke them by Ctl+Shift+B (macOS: Cmd+Shift+B).
- `.vscode/c_cpp_properties.json`: Configures IntelliSense for better code
completion and for using the correct system headers.
- `.vscode/launch.json`: Configures the [Cortex-Debug][] extension to start the
debugger with a single click from the IDE.

- `launch.json`: configuration file to launch the debugger with a single click
(or keyboard shortcut) from the IDE.
Note that not all build system features are provided in the IDE, only the most
common. You can call all build system tools from the command line.
In addition, the `modm:build:cmake` module is supported natively by the
[CMake Tools][] extension and is therefore not wrapped.

We recommend adding this module from the command line so that you don't
accidentally overwrite your modified files later: `lbuild build -m ::vscode`.

Note that some configuration options may be specific to your environment (such
as the compiler path) and may not work in other environments.


[VSCode]: https://code.visualstudio.com/
[CMake Tools]: https://github.com/microsoft/vscode-cmake-tools
[Cortex-Debug]: https://github.com/Marus/cortex-debug#readme
[mcuoneclipse-vscode-debug]: https://mcuoneclipse.com/2021/10/23/visual-studio-code-for-c-c-with-arm-cortex-m-part-10-assembly-stepping/
33 changes: 33 additions & 0 deletions tools/ide/vscode/tasks.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

{
// See https://code.visualstudio.com/docs/editor/tasks
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
%% for config in configs
{
"type": "shell",
"label": "Build ({{ config.profile | capitalize }})",
"command": "{{ config.tool }} build profile={{ config.profile }}",
"group": "build",
"presentation": {
"reveal": "silent",
"showReuseMessage": false
},
"problemMatcher": "$gcc"
},
%% endfor
%% for config in configs
{
"type": "shell",
"label": "Upload ({{ config.profile | capitalize }})",
"command": "{{ config.tool }} program profile={{ config.profile }}",
"group": "build",
"presentation": {
"reveal": "silent",
"showReuseMessage": false
}
},
%% endfor
]
}

0 comments on commit 8d0ffa5

Please sign in to comment.