Skip to content
  1. 基于 FreeRTOS 的 ESP-IDF 开发 —— 0.Windows 下 esp idf 的环境搭建

https://blog.csdn.net/qq_53381910/article/details/129629866

clion 配置 esp-idf

https://blog.csdn.net/weixin_48991062/article/details/131525614

https://blog.csdn.net/zwLoneranger/article/details/138975612

https://www.bilibili.com/video/BV1Q7epebEbb/

最新版 ESP32 IDF 环境搭建教程:基于 CLION 安装 ESP32 开发环境 IDF(含同时安装多个 IDF 教程)

https://blog.csdn.net/wcc243588569/article/details/132805558

IDF 指令

使用之前需要进入 esp-idf 的环境

shell
C:\Espressif\idf_cmd_init.bat

构建指令

shell
idf.py build
shell
E:\esp32s3_idf>idf.py
Usage: idf.py [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...

  ESP-IDF CLI build management tool. For commands that are not known to idf.py an attempt to execute it as a build
  system target will be made. Selected target: None

Options:
  --version                       Show IDF version and exit.
  --list-targets                  Print list of supported targets and exit.
  -C, --project-dir PATH          Project directory.
  -B, --build-dir PATH            Build directory.
  -w, --cmake-warn-uninitialized / -n, --no-warnings
                                  Enable CMake uninitialized variable warnings for CMake files inside the project
                                  directory. (--no-warnings is now the default, and doesn't need to be specified.) The
                                  default value can be set with the IDF_CMAKE_WARN_UNINITIALIZED environment variable.
  -v, --verbose                   Verbose build output.
  --preview                       Enable IDF features that are still in preview.
  --ccache / --no-ccache          Use ccache in build. Disabled by default. The default value can be set with the
                                  IDF_CCACHE_ENABLE environment variable.
  -G, --generator [Ninja]         CMake generator.
  --no-hints                      Disable hints on how to resolve errors and logging.
  -D, --define-cache-entry TEXT   Create a cmake cache entry. This option can be used at most once either globally, or
                                  for one subcommand.
  -p, --port PATH                 Serial port. The default value can be set with the ESPPORT environment variable.
                                  This option can be used at most once either globally, or for one subcommand.
  -b, --baud INTEGER              Baud rate for flashing. It can imply monitor baud rate as well if it hasn't been
                                  defined locally. The default value can be set with the ESPBAUD environment variable.
                                  This option can be used at most once either globally, or for one subcommand.
  --help                          Show this message and exit.

Commands:
  add-dependency               Add dependency to the manifest file.
  all                          Aliases: build. Build the project.
  app                          Build only the app.
  app-flash                    Flash the app only.
  bootloader                   Build only bootloader.
  bootloader-flash             Flash bootloader only.
  build-system-targets         Print list of build system targets.
  clang-check                  run clang-tidy check under current folder, write the output into "warnings.txt"
  clang-html-report            generate html report to "html_report" folder by reading "warnings.txt" (may take a few
                               minutes). This feature requires extra dependency "codereport". Please install this by
                               running "pip install codereport"
  clean                        Delete build output files from the build directory.
  confserver                   Run JSON configuration server.
  coredump-debug               Create core dump ELF file and run GDB debug session with this file.
  coredump-info                Print crashed task’s registers, callstack, list of available tasks in the system,
                               memory regions and contents of memory stored in core dump (TCBs and stacks)
  create-component             Create a new component.
  create-manifest              Create manifest for specified component.
  create-project               Create a new project.
  create-project-from-example  Create a project from an example in the ESP Component Registry.
  docs                         Open web browser with documentation for ESP-IDF
  efuse-common-table           Generate C-source for IDF's eFuse fields.
  efuse-custom-table           Generate C-source for user's eFuse fields.
  encrypted-app-flash          Flash the encrypted app only.
  encrypted-flash              Flash the encrypted project.
  erase-flash                  Erase entire flash chip.
  erase-otadata                Erase otadata partition.
  flash                        Flash the project.
  fullclean                    Delete the entire build directory contents.
  gdb                          Run the GDB.
  gdbgui                       GDB UI in default browser.
  gdbtui                       GDB TUI mode.
  menuconfig                   Run "menuconfig" project configuration tool.
  merge-bin
  monitor                      Display serial output.
  openocd                      Run openocd from current path
  partition-table              Build only partition table.
  partition-table-flash        Flash partition table only.
  post-debug                   Utility target to read the output of async debug action and stop them.
  python-clean                 Delete generated Python byte code from the IDF directory
  qemu                         Run QEMU.
  read-otadata                 Read otadata partition.
  reconfigure                  Re-run CMake.
  save-defconfig               Generate a sdkconfig.defaults with options different from the default ones
  set-target                   Set the chip target to build.
  show-efuse-table             Print eFuse table.
  size                         Print basic size information about the app.
  size-components              Print per-component size information.
  size-files                   Print per-source-file size information.
  uf2                          Generate the UF2 binary with all the binaries included
  uf2-app                      Generate an UF2 binary for the application only
  update-dependencies          Update dependencies of the project

环境搭建好了之后,先尝试一下 hello world 程序

创建新项目

输入命令创建新工程,格式是:

shell
idf.py create-project filename

filename 是工程名

然后进行创建,稍等一下就创建好了

shell
E:\esp32s3_idf>idf.py create-project s3_01_helloworld
Executing action: create-project
The project was created in E:\esp32s3_idf\s3_01_helloworld

现在工程已经建立好了,那么我们就可以来编写我们的 hello world 程序了,我们在 Windows 下进入工程目录 s3_01_helloworld,我们可以看到里面有一个,它的作用是告诉我们的编译器编译的目录在哪些地方可以找到。 项目创建好了之后,目录结构如下:

shell
E:\esp32s3_idf\s3_01_helloworld>tree /f
E:.
  CMakeLists.txt

└─main
        CMakeLists.txt
        s3_01_helloworld.c
  • CMakeLists.txt:告诉编译器编译的目录在哪些地方可以找到
  • main:项目代码文件夹
  • s3_01_helloworld.c:项目代码文件

编写程序

使用任意工具修改 s3_01_helloworld.c,将内部代码修改如下

c
#include <stdio.h>

void app_main(void)
{
	// 注意,此处一定要加 \n,否则无法看到输出
	printf("hello world !\n");
}

编译工程

ESP-IDF 默认板子的型号是 esp32,如果你的板子与我一样是 esp32s3 则需要运行以下指令修改

shell
idf.py set-target esp32s3

(在 ESP-IDF 的虚拟环境中,) 输入以下命令构建项目。

shell
idf.py build

使用以下指令进行烧录并且查看运行情况

shell
idf.py flash monitor

不出意外,运行之后最后可以看到输出的 hello world !

点亮一颗灯

点亮 WS2812

依赖地址:https://components.espressif.com/components/espressif/led_strip/versions/2.5.5

先添加依赖

shell
idf.py add-dependency "espressif/led_strip^2.5.5"

然后清理一下缓存之后重新编译一下项目

shell
idf.py clean
idf.py build
c
#include <stdio.h>
#include "led_strip.h"
#include "esp_log.h"
#include "esp_err.h"

// GPIO assignment
#define LED_STRIP_BLINK_GPIO  GPIO_NUM_48
// Numbers of the LED in the strip
#define LED_STRIP_LED_NUMBERS 24
static const char *TAG = "example";

led_strip_handle_t configure_led(void) {
    // LED strip general initialization, according to your led board design
    led_strip_config_t strip_config = {
            .strip_gpio_num = LED_STRIP_BLINK_GPIO,   // The GPIO that connected to the LED strip's data line
            .max_leds = LED_STRIP_LED_NUMBERS,        // The number of LEDs in the strip,
            .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip
            .led_model = LED_MODEL_WS2812,            // LED strip model
            .flags.invert_out = false,                // whether to invert the output signal
    };

    // LED strip backend configuration: SPI
    led_strip_spi_config_t spi_config = {
            .clk_src = SPI_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
            .flags.with_dma = true,         // Using DMA can improve performance and help drive more LEDs
            .spi_bus = SPI2_HOST,           // SPI bus ID
    };

    // LED Strip object handle
    led_strip_handle_t led_strip;
    ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
    ESP_LOGI(TAG, "Created LED strip object with SPI backend");
    return led_strip;
}

void app_main(void) {

    led_strip_handle_t led_strip = configure_led();
    bool led_on_off = false;

    ESP_LOGI(TAG, "Start blinking LED strip");
    while (1) {
        if (led_on_off) {
            /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
            for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) {
                ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, 255, 0, 0));
            }
            /* Refresh the strip to send data */
            ESP_ERROR_CHECK(led_strip_refresh(led_strip));
            ESP_LOGI(TAG, "LED ON!");
        } else {
            /* Set all LED off to clear all pixels */
            ESP_ERROR_CHECK(led_strip_clear(led_strip));
            ESP_LOGI(TAG, "LED OFF!");
        }

        led_on_off = !led_on_off;
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}