esp_err.h ESP_ERROR_CHECK() problem

GerardoBB
Posts: 2
Joined: Wed Jun 28, 2023 4:08 am

esp_err.h ESP_ERROR_CHECK() problem

Postby GerardoBB » Wed Jun 28, 2023 4:25 am

I'm just starting with ESP-IDF, and I'm trying to build a simple UART communication script. This is my code:

Code: Select all

#include <stdio.h>
#include <esp_err.h>
#include <driver/uart.h>
#include <driver/gpio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

const uart_port_t uart_num = UART_NUM_0;
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,
    .rx_flow_ctrl_thresh = 122,
};

ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));

ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));

const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;

ESP_ERROR_CHECK(uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));

void app_main(void)
{
    gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);

    while(true)
    {   
        gpio_set_level(GPIO_NUM_2, 1);
        vTaskDelay(100);
        gpio_set_level(GPIO_NUM_2, 0);
        vTaskDelay(100); 
    }

}
I'm getting the following build error:

Code: Select all

In file included from C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:2:
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:116:28: error: expected identifier or '(' before 'do'  
  116 | #define ESP_ERROR_CHECK(x) do {                                         \
      |                            ^~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:19:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   19 | ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
      | ^~~~~~~~~~~~~~~
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:122:7: error: expected identifier or '(' before 'while'  122 |     } while(0)
      |       ^~~~~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:19:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   19 | ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
      | ^~~~~~~~~~~~~~~
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:116:28: error: expected identifier or '(' before 'do'  
  116 | #define ESP_ERROR_CHECK(x) do {                                         \
      |                            ^~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:21:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   21 | ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
      | ^~~~~~~~~~~~~~~
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:122:7: error: expected identifier or '(' before 'while'  122 |     } while(0)
      |       ^~~~~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:21:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   21 | ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
      | ^~~~~~~~~~~~~~~
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:116:28: error: expected identifier or '(' before 'do'  
  116 | #define ESP_ERROR_CHECK(x) do {                                         \
      |                            ^~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:26:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   26 | ESP_ERROR_CHECK(uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
      | ^~~~~~~~~~~~~~~
C:/Users/angel/esp/esp-idf/components/esp_common/include/esp_err.h:122:7: error: expected identifier or '(' before 'while'  122 |     } while(0)
      |       ^~~~~
C:/Users/angel/OneDrive/Escritorio/esp32Test/main/TestESP32.c:26:1: note: in expansion of macro 'ESP_ERROR_CHECK'
   26 | ESP_ERROR_CHECK(uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
      | ^~~~~~~~~~~~~~~
[573/578] Linking C static library esp-idf\fatfs\libfatfs.a
ninja: build stopped: subcommand failed.

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command ninja " terminated with exit code: 1. 
It looks like the error is in this line in the esp_err.h file:
recorteidfforum.png
Capture from esp_err.h showing error
recorteidfforum.png (22.83 KiB) Viewed 1976 times

Code: Select all

#else
#define ESP_ERROR_CHECK(x) do {                                         \
        esp_err_t err_rc_ = (x);                                        \
        if (unlikely(err_rc_ != ESP_OK)) {                              \
            _esp_error_check_failed(err_rc_, __FILE__, __LINE__,        \
                                    __ASSERT_FUNC, #x);                 \
        }                                                               \
    } while(0)
#endif
I'm using the xtensa-esp32-elf-gcc.exe compiler, this is my c_cpp_properties,json:

Code: Select all

{
    "configurations": [
        {
            "name": "Win32",
            "compilerPath": "C:/Users/angel/.espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc.exe",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            "includePath": [
                "${workspaceFolder}/**",
                "${config:idf.espIdfPath}/components/**",
                "${config:idf.espIdfPathWin}/components/**",
                "C:/Users/angel/esp/esp-idf/components/**",
                "C:/Users/angel/esp/esp-idf/components/driver/include/driver/",
                "C:/Users/angel/esp/esp-idf/components/driver/include/",
                "${config:idf.espIdfPath}/**",
                "C:/Users/angel/esp/**",
                "C:/Users/angel/esp/esp-idf/components/newlib/platform_include"
            ],
            "browse": {
                "path": [
                    "${config:idf.espIdfPath}/components",
                    "${config:idf.espIdfPathWin}/components",
                    "${workspaceFolder}",
                    "${config:idf.espIdfPath}/**",
                    "C:/Users/angel/esp/**",
                    "${workspaceFolder}/**",
                    "${config:idf.espIdfPath}/components/**",
                    "${config:idf.espIdfPathWin}/components/**",
                    "C:/Users/angel/esp/esp-idf/components/**",
                    "C:/Users/angel/esp/esp-idf/components/driver/include/driver/",
                    "C:/Users/angel/esp/esp-idf/components/driver/include/",
                    "${config:idf.espIdfPath}/**",
                    "C:/Users/angel/esp/**",
                    "C:/Users/angel/esp/esp-idf/components/newlib/platform_include"
                ],
                "limitSymbolsToIncludedHeaders": false
            },
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
and my tasks.json:

Code: Select all

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\mingw\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
Any help would be greatly apreciated.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_err.h ESP_ERROR_CHECK() problem

Postby ESP_Sprite » Wed Jun 28, 2023 6:05 am

All the things wrapped in ESP_ERROR_CHECK are functions; they need to be called within a function, e.g. your app_main. You can't just plunk them anywhere in your C file.

GerardoBB
Posts: 2
Joined: Wed Jun 28, 2023 4:08 am

Re: esp_err.h ESP_ERROR_CHECK() problem

Postby GerardoBB » Wed Jun 28, 2023 11:07 pm

ESP_Sprite wrote:
Wed Jun 28, 2023 6:05 am
All the things wrapped in ESP_ERROR_CHECK are functions; they need to be called within a function, e.g. your app_main. You can't just plunk them anywhere in your C file.
Got it, thank you very much.

Who is online

Users browsing this forum: Baidu [Spider] and 97 guests