Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: docs: guide for vscode esp-target setup #395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---
Binary file not shown.
178 changes: 178 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,178 @@
---
title: "ESP-IDF VSCode Setup Guide for 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"
---

## Overview
This guide helps you configure VSCode's C/C++ extension for ESP32xx development, addressing common issues like incorrect syntax highlighting, header file navigation, and false error indicators.

## Common Issues
- Incorrect syntax highlighting for code within preprocessor macros
- Incorrect header file resolution (e.g., wrong ll.h file being referenced)
- False positive error indicators in source files
- Navigation issues between different ESP32xx chip variants

## Prerequisites
- VSCode with C/C++ extension installed
- ESP-IDF toolchain installed
- Basic understanding of ESP32 development environment

## Configuration Steps

### 1. Basic Setup
1. Open your ESP-IDF project in VSCode
2. Create or modify `.vscode/c_cpp_properties.json`
3. Configure alias to fix build directory using `alias idf.b="idf.py -B ~/esp/build"`
4. Run `idf.b build` at least once to generate necessary files

### Why Fixed Build Directory?
Using a fixed build directory (ex: `~/esp/build`) provides several benefits:
- Prevents need to update path in `c_cpp_properties.json` for each project
- Makes switching between projects cleaner with centralized build location
- Simplifies configuration for multiple ESP32 variants

### 2. Configuration File Structure
Create separate configurations for each ESP32xx variant in `c_cpp_properties.json`:

**Build-Related Paths**

Two important files are generated during build: `compile_commands.json` and `sdkconfig.h`.

These files are why we need to run `idf.b build` before VSCode can properly resolve includes and macros.

**Key Configuration Paths**
```json

{
"configurations": [
{
// Compiler path: Path to your ESP-IDF GCC compiler for specific chip
"compilerPath": "${env:HOME}/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc",

// Build output: Contains compilation file with include paths and flags
"compileCommands": "${env:HOME}/esp/build/compile_commands.json",

// Project config: Generated header with chip-specific settings
"forcedInclude": [
"${env:HOME}/esp/build/config/sdkconfig.h"
]

}
]
}

```

**Complete Configuration Example**

```json
{
"configurations": [
{
"name": "ESP32",
"cStandard": "c11",
"cppStandard": "c++17",
"compilerPath": "${env:HOME}/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc",
"compileCommands": "${env:HOME}/esp/build/compile_commands.json",
"forcedInclude": [
"${env:HOME}/esp/build/config/sdkconfig.h"
],
"configurationProvider": "ms-vscode.cmake-tools",
"includePath": [
"${workspaceFolder}/**"
]
}
// Additional configurations for S2, S3, and RISC-V variants...
],
"version": 4
}
```

### 3. Chip-Specific Configurations
Four main configurations are needed:
1. ESP32
2. ESP32-S2
3. ESP32-S3
4. ESP32 RISC-V series

Each configuration differs primarily in the `compilerPath` setting.

## Best Practices

### Setup Build Directory Alias

```bash
# Add alias to your shell configuration
alias idf.b="idf.py -B ~/esp/build"
# Now you can use:
idf.b build # Builds project
idf.b fullclean # Cleans build directory
```

### Switching Between Configurations
1. Press F1 or Ctrl+Shift+P
2. Search for "C/C++: Select a Configuration"
3. Choose the appropriate chip variant

### Toolchain Management
```bash
# Script to find latest toolchain
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)
```

### Configuration Generator Script
```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"

```

**Note**: When switching projects or target chips, always run `idf.b fullclean` first to avoid compilation issues.
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": "https://avatars.githubusercontent.com/u/25845066"
}
9 changes: 9 additions & 0 deletions data/authors/igor-udot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Igor Udot",
"bio": "",
"image": "https://avatars.githubusercontent.com/u/47724762",
"social": [
{ "github": "https://github.com/horw" }
]

}