我已经在 Visual Studio Code 中安装了 Espressif IDF(1.5.0)。
当我在 Espressif IDE 中运行此代码时,我没有任何问题,但当我使用 Visual Studio 代码时,我在 IRAM_ATTR 上收到错误,指出“需要类型说明符”,并且同一行中的第二个错误“需要一个 {”。 我还在 portTICK_PERIOD_MS 上收到一个错误,提示“标识符 portTICK_PERIOD_MS 未定义”
可能出了什么问题?
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include "driver/uart.h"
#include "esp_log.h"
#include "esp_intr_alloc.h"
#include "soc/uart_reg.h"
#include "soc/uart_struct.h"
#include "driver/gpio.h"
#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)
#define UART UART_NUM_2
#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
static intr_handle_t handle_console;
static const char *TAG = "uart_events";
char rxbuf[256];
static void IRAM_ATTR uart_intr_handler(void *arg)
{
uint16_t rx_fifo_len, i=0;
ESP_EARLY_LOGI(TAG,"%s", "Interrupt entered...");
}
void app_main(void)
{
esp_log_level_set(TAG, ESP_LOG_INFO);
/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
ESP_ERROR_CHECK(uart_param_config(UART, &uart_config));
//Set UART log level
esp_log_level_set(TAG, ESP_LOG_INFO);
//Set UART pins (using UART0 default pins ie no changes.)
ESP_ERROR_CHECK(uart_set_pin(UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
//Install UART driver, and get the queue.
ESP_ERROR_CHECK(uart_driver_install(UART, BUF_SIZE * 2, 0, 0, NULL, 0));
// release the pre registered UART handler/subroutine
ESP_ERROR_CHECK(uart_isr_free(UART));
// register new UART subroutine
ESP_ERROR_CHECK(uart_isr_register(UART,uart_intr_handler, NULL, ESP_INTR_FLAG_IRAM, &handle_console));
// enable RX interrupt
ESP_ERROR_CHECK(uart_enable_rx_intr(UART));
while(1)
{
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
缺少包含文件引用。在 .vscode 文件夹中查找
c_cpp_properties.json
。如果没有找到,请创建一个。
更新包含路径等属性,如下所示: https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference
Visual Studio Code 1.70.3、Windows SP1 7x64、esp-idf-v4.4.2、VSCode ESP-IDF 扩展 v1.6.3、C/C++ IntelliSense v1.15.4
项目会有.vscode,它包含需要更改的*.json文件。
问题1: IntelliSense 存在,但如果出现问题,环境可能不会给出任何指示。
解决方案:文件
settings.json
改变
"C_Cpp.intelliSenseEngine": "Tag Parser"
至 "C_Cpp.intelliSenseEngine": "default"
。
最好删除引号内的值,然后按 Ctrl + 空格键并选择“默认”。 保存并关闭。工作室将主动重启环境,同意。
现在,如果 main.c 文件中存在错误,该代码下方会出现一条红色波浪线。 在这里找到。
问题 2: Visual Studio 看不到来自
sdkconfig
的常量
解决方案:文件
c_cpp_properties.json
在“包含路径”和“浏览”部分添加
"${workspaceFolder}/build/config"
完整配置如下
问题 3: Visual Studio 看不到
portTICK_PERIOD_MS
常量。
解决方案:文件
c_cpp_properties.json
更改之前,先构建项目,因为文件“c_cpp_properties.json”将出现在构建文件夹中
在“name”:“ESP-IDF”之后,添加以下行:
"cStandard": "c17",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
了解更多信息
c_cpp_properties.json
更改前:
{
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "C:\\Espressif\\tools\\xtensa-esp32-elf\\esp-2021r2-patch3-8.4.0\\xtensa-esp32-elf\\bin\\xtensa-esp32-elf-gcc.exe",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${config:idf.espIdfPathWin}/components/**",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${config:idf.espIdfPath}/components",
"${config:idf.espIdfPathWin}/components",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": false
},
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
c_cpp_properties.json
更改后:
{
"configurations": [
{
"name": "ESP-IDF",
"cStandard": "c17",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"compilerPath": "C:\\Espressif\\tools\\xtensa-esp32-elf\\esp-2021r2-patch3-8.4.0\\xtensa-esp32-elf\\bin\\xtensa-esp32-elf-gcc.exe",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${config:idf.espIdfPathWin}/components/**",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}/**",
"${workspaceFolder}/build/config"
],
"browse": {
"path": [
"${config:idf.espIdfPath}/components",
"${config:idf.espIdfPathWin}/components",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}",
"${workspaceFolder}/build/config"
],
"limitSymbolsToIncludedHeaders": false
},
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}