Home Wireless
Published in

Home Wireless

VSCode Building and Debugging Zephyr on the Lora-E5 (Part 3)

Building and debugging the code you create with Visual Studio Code is not a hard task, thanks to existing extensions, but it does take some attention.

Add the VSCode Extensions

If you haven’t done this, install the C++ extension as well as cortex-debug and the cortex-debug device support pack for stm32wl. This can be done in VSCode in the extensions tab on the left.

Creating the Build Tasks

Create some build tasks. These are simple shell calls that call the west application as if you were typing to do a Build, Clean, Full Build. These are defined in tasks.json in the .vscode folder of the project.

  1. Click the F1 function key to pull down the command menu.
  2. Select Tasks: Configure Task
  3. Select any task to have VSCode create the tasks.json file and fill it with ‘stuff’ which we’ll completely change : )

Once this is done, you will now have a .vscode folder with tasks.json in it. If the folder is otherwise empty add a blank settings.json file.

Now to edit the files, delete the tasks and add four new tasks as below.

  1. Edit settings.json and add some configuration settings for the workspace as below:
"workspaceFolder": {
"path": ".",
"zephyrproject": "%HOMEPATH%/zephyrproject",
"AppUnderDev": "${workspaceRoot}"
},
"bsp": {
"board": "lora_e5_dev_board",
},
"app": {
"name": "${config:workspaceFolder.AppUnderDev}",
"build_dir": "${config:workspaceFolder.AppUnderDev}\\build",
},

2. Edit tasks.json and add the four tasks we want. These are simple shell calls from powershell except the clean — which deletes the build folder.

{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"group": "build",
"command": "west",
"args": [
"build",
"-p",
"auto",
"-d",
"${config:app.build_dir}",
"-b",
"${config:bsp.board}",
"${config:app.name}",
],
"problemMatcher": [ "$gcc" ]
},
{
"label": "Full Build",
"type": "shell",
"group": "build",
"command": "west",
"args": [
"build",
"-p",
"always",
"-d",
"${config:app.build_dir}",
"-b",
"${config:bsp.board}",
"${config:app.name}",
],
//"dependsOn": ["Clean Build"],
"problemMatcher": [ "$gcc" ]
},
{
"label": "Clean Build",
"type": "shell",
"group": "build",
"command": "Remove-Item",
"args": [
"-Path",
"${config:app.build_dir}",
"-Force",
"-Recurse"
],
"dependsOn": [],
"problemMatcher": []
},
{
"label": "Debug Build",
"type": "shell",
"group": "build",
"command": "west",
"args": [
"build",
"-p",
"always",
"-d",
"${config:app.build_dir}",
"-b",
"${config:bsp.board}",
"${config:app.name}",
"--",
"-DCONF_FILE='prj.conf;debug.conf;./boards/${config:bsp.board}.conf'"
],
//"dependsOn": ["Clean Build"],
"problemMatcher": [ "$gcc" ]
},
{
"label": "Flash",
"type": "shell",
"group": "build",
"command": "west",
"args": [
"flash",
"--build-dir",
"${config:app.build_dir}"
],
"dependsOn": [],
"problemMatcher": []
},
]
}

The debug build uses a debug.conf file that enables most debugging stuff in zephyr. Place this with the prj.conf file. You may not have a local board/my_board.conf.

# -- disable code optimization so we can debug
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_EXCEPTION_STACK_TRACE=y
CONFIG_DEBUG_INFO=y
CONFIG_ASSERT=y
# -- system debug options
CONFIG_SYS_HEAP_VALIDATE=y
CONFIG_STACK_SENTINEL=y

Create the Debug Launcher

The debug launcher lets you run/debug the application. I have an stlink v3 and with that hardware the best setting for servertype was stlink. It does not seem to require openocd.

  1. Create a launch.json file in the .vscode folder
  2. Add this text to it. Note the stlink servertype here.
  3. Instead of launch you can use attach as the request below and it won’t try to upload/start the binary app.
{
"version": "0.2.0",
"configurations": [
{
"name": "zephyr STM32wl",
"type": "cortex-debug",
"request": "launch",
"servertype": "stlink",
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/build/zephyr/zephyr.elf",
"device": "STM32WLE5JC",
"interface": "swd",
},
]
}

Now you should be able to:

  1. Build your application. Use the F1 (command) and select Tasks: Run Task and find Build.
  2. Flash the application: Similarly there is a Flash task (which may work)
  3. Debug the application by (a) load a source .c file (b) switch to the debug tab on the left and (c) click the run icon (or just click F5).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark Zachmann

Entrepreneur, software architect, electrical engineer. Ex-academic.