🪲 Debugging Zephyr RTOS in VS Code
Setting Up Zephyr RTOS Debugging in VSCode for STM32 Development
Debugging embedded systems can be challenging, but with Visual Studio Code and the right configuration, you can create a seamless debugging experience for Zephyr RTOS applications. This guide walks through setting up a debugging environment for STM32F3 Discovery board using VSCode, OpenOCD, and the Cortex-Debug extension.
Prerequisites
Before starting, ensure you have:
- Zephyr RTOS SDK installed
- Visual Studio Code with Cortex-Debug extension
- OpenOCD installed
Environment Setup
First, we need to set up our environment variables in PowerShell. These variables help VSCode locate the necessary tools and source files:
setx ZEPHYR_BASE "C:\Users\walid\zephyrproject\zephyr"
setx ZEPHYR_SDK_INSTALL_DIR "C:\Users\walid\zephyr-sdk-0.16.8"
These commands create persistent environment variables pointing to:
- The Zephyr RTOS base directory
- The Zephyr SDK installation directory
VSCode Debug Configuration
Create or update your .vscode/launch.json
file with the following configuration:
{
"version": "0.0.1",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${env:ZEPHYR_BASE}",
"executable": "${env:ZEPHYR_BASE}/build/zephyr/zephyr.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"runToEntryPoint": "main",
"device": "STM32F303VC",
"configFiles": [
"board/stm32f3discovery.cfg"
],
"gdbPath": "${env:ZEPHYR_SDK_INSTALL_DIR}/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb.exe",
"armToolchainPath": "${env:ZEPHYR_SDK_INSTALL_DIR}/arm-zephyr-eabi/bin"
}
]
}
Let’s break down the key configuration elements:
- Working Directory: Set to
${env:ZEPHYR_BASE}
to ensure proper file resolution - Executable: Points to the compiled Zephyr ELF file
- Debug Server: Uses OpenOCD for communication with the target
- Entry Point: Automatically runs to
main()
function - Target Device: Specified as STM32F303VC
- OpenOCD Config: Uses the STM32F3 Discovery board configuration
- GDB Path: Points to the Zephyr SDK’s GDB executable
- Toolchain Path: References the ARM toolchain directory
Usage
With this configuration in place, you can start debugging using VSCode’s debug view (F5)
Conclusion
This setup provides a professional debugging environment for Zephyr RTOS development on STM32 platforms. The integration between VSCode, OpenOCD, and Cortex-Debug creates a powerful toolchain for embedded development.