Skip to content

Commit

Permalink
docs: guide for vscode esp-target setup
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed Feb 6, 2025
1 parent 19fc0a1 commit abc0118
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
3 changes: 3 additions & 0 deletions content/authors/chen-ji-chang/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Chen Ji Chang
---
3 changes: 3 additions & 0 deletions content/authors/igor-udot/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Igor Udot
---
176 changes: 176 additions & 0 deletions content/blog/vscode-esp-idf-setup-targets/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
title: "ESP-IDF VS Code Setup Target Development"
date: 2025-01-21T16:07:09+08:00
showAuthor: true
featureAsset: ""
tags: ["ESP-IDF extension", "VSCode", "Visual Studio Code"]
authors:
- "igor-udot"
- "chen-ji-chang"
- "brian-ignacio"
---

# How to Configure VSCode C/C++ for Correct Navigation

## Problem Description
1. When modifying code for ESP32XX, some code within #if macro definitions lacks syntax highlighting, making it unclear whether the code will be compiled
2. When trying to view ESP32's ll.h file, it incorrectly jumps to ESP32C6's ll.h file
3. Source files are full of red wavy underlines, even though they don't affect normal compilation and flashing

## Root Cause
The VSCode C/C++ extension is not configured correctly, resulting in inability to find the correct include paths for header files.

## Solution

Configure the C/C++ extension's include paths by adding the following content to c_cpp_properties.json:
```
{
"name": "ESP32",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "/home/username/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc",
"compileCommands": "/home/username/esp/build/compile_commands.json",
"forcedInclude": [
"/home/username/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
}
```

- `compilerPath` is the path to the gcc compiler, typically found in your .espressif directory
- `compileCommands` is the path to the compilation commands json file, typically found in your ./build directory. To avoid having to modify the `build` command for each project folder, you can use idf.py with -B param like `idf.py -B` -B setup path to your build folder, in this way you can put it in fixed location (it's recommended to use an alias to simplify this command) like `idf.py -B `~/.esp-build
- `includePath` includes all files in the current workspace to allow navigation in uncompiled projects (note that navigation in uncompiled projects may not be entirely accurate, and red underlines cannot be completely avoided)
- Since different chips use different gcc toolchains, you need to choose different configurations based on the target chip (there are currently 4 types: **32**, **S2**, **S3**, and **RISC-V** series)

### Important Notes

1. When switching between compilation projects or target chips, use `idf.py -B` your_build_path `fullclean` to clean the project, otherwise compilation will fail
2. When switching target chips, you need to change the C/C++ extension configuration by using F1(Command palir) to search for `C/C++: Select a Configuration` and select the corresponding configuration
3. Different IDF versions may have different toolchain versions. It's recommended to use the latest toolchain as it generally maintains backward compatibility
4. Included drivers can be different from project to project, so you need to set the target and build in the project you are working on, otherwise some references will not work correctly.
5. For any other unclear issues, refer to the [c_cpp_properties.json reference](https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference)

## Example

Modify c_cpp_properties.json to the following content, and set compilerPath and compileCommands according to your own idf path and build path:

```
{
"configurations": [
{
"name": "ESP32",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "/home/username/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc",
"compileCommands": "/home/username/esp/build/compile_commands.json",
"forcedInclude": [
"/home/username/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
},
{
"name": "ESP32-S2",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "/home/username/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32s2-elf-gcc",
"compileCommands": "/home/username/esp/build/compile_commands.json",
"forcedInclude": [
"/home/username/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
},
{
"name": "ESP32-S3",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "/home/username/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32s3-elf-gcc",
"compileCommands": "/home/username/esp/build/compile_commands.json",
"forcedInclude": [
"/home/username/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
},
{
"name": "ESP32-RSICV",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "/home/username/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20240530/riscv32-esp-elf/bin/riscv32-esp-elf-gcc",
"compileCommands": "/home/username/esp/build/compile_commands.json",
"forcedInclude": [
"/home/username/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
}
],
"version": 4
}
```

## Script example

Alias example
```
alias idf.b=idf.py -B ~/esp/build
```

```bash

# Find latest toolchain directories
LATEST_XTENSA_DIR=$(ls -d ~/.espressif/tools/xtensa-esp-elf/esp-*/ | sort | tail -n 1)
LATEST_RISCV_DIR=$(ls -d ~/.espressif/tools/riscv32-esp-elf/esp-*/ | sort | tail -n 1)

# Function to echo a configuration block
echo_config() {
local name=$1
local compiler_path=$2

cat << EOF
---------------------------
Configuration for $name
---------------------------
{
"name": "$name",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "$compiler_path",
"compileCommands": "$HOME/esp/build/compile_commands.json",
"forcedInclude": [
"$HOME/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"\${workspaceFolder}/**"
]
}
EOF
}

# Echo ESP32 configuration
echo_config "ESP32" "${LATEST_XTENSA_DIR}xtensa-esp-elf/bin/xtensa-esp32-elf-gcc"
# Echo ESP32-S2 configuration
echo_config "ESP32-S2" "${LATEST_XTENSA_DIR}xtensa-esp-elf/bin/xtensa-esp32s2-elf-gcc"
# Echo ESP32-S3 configuration
echo_config "ESP32-S3" "${LATEST_XTENSA_DIR}xtensa-esp-elf/bin/xtensa-esp32s3-elf-gcc"
# Echo ESP32-RISC-V configuration
echo_config "ESP32-RISC-V" "${LATEST_RISCV_DIR}riscv32-esp-elf/bin/riscv32-esp-elf-gcc"

```
5 changes: 5 additions & 0 deletions data/authors/chen-ji-chang.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Chen Ji Chang",
"bio": "",
"image": ""
}
5 changes: 5 additions & 0 deletions data/authors/igor-udot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Igor Udot",
"bio": "",
"image": ""
}

0 comments on commit abc0118

Please sign in to comment.