ESP32 開發環境的搭建與詳解

ESP32 開發環境的搭建與詳解

ESP-IDF

ESP-IDF (Espressif IoT Development Framework) 是樂鑫科技提供的一站式物聯網開發框架,它以C/C++ 爲主要的開發語言。

Github:https://github.com/espressif/esp-idf

安裝

windows 系統

windows 系統安裝 ESP-IDF 開發環境,可以通過 https://dl.espressif.cn/dl/esp-idf 下載在線或者離線安裝工具。

TIPS:

離線安裝工具本身文件大(500MB 至1.xG),但相較於只有 4MB 的在線安裝工具,安裝成功機率大,在線安裝可能會因爲網絡問題導致安裝失敗,故這裏推薦使用離線安裝包進行安裝。

安裝工具本身也是開源,可通過訪問 https://github.com/espressif/idf-installer 查看源碼。

在瀏覽器中打開下載地址:https://dl.espressif.cn/dl/esp-idf ,下載離線安裝包 esp-idf-tools-setup-offline-4.3.4,如下圖所示:

安裝過程中,選擇的安裝目錄中不要有空格,這裏我們選擇的安裝路徑是:

C:\ProgramFiles\Espressif

安裝完成後,會得到兩個 ESP_IDF 終端 ,如下圖所示:

1676444772254

在這兩個終端任選其一,打開終端時會自動添加 ESP-IDF 的環境變量,之後就可使用 idf.py 命令進行操作了。如下圖所示:

idf.py

使用 idf.py 工具進行開發,參見:

https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/get-started/windows-setup.html#get-started-windows-first-steps

VS Code 代碼編輯工具

ESP-IDF SDK默認不附帶代碼編輯工具(最新的Windows版安裝工具可選擇安裝ESP-IDF Eclipse),讀者可使用任何文本編輯工具進行代碼的編輯,代碼編輯完成後可在終端控制檯使用命令進行代碼的編譯。

VS Code 下載地址 :https://code.visualstudio.com/

vscode-esp-idf-extension

要在 VS Code 中開發 ESP,還得安裝插件 vscode-esp-idf-extension,

TIPS:

插件地址:https://github.com/espressif/vscode-esp-idf-extension

如何安裝插件,可以查看其安裝文檔:https://github.com/espressif/vscode-esp-idf-extension/blob/HEAD/docs/tutorial/install.md

下面演示安裝過程:

在 VS Code 的左側菜單中選擇【擴展(Ctrl + Shift + X)】,在搜索框中輸入:

ESP-IDF

在搜索結果中找到 ESP-IDF 插件,如下圖所示:

點擊【安裝】按鈕進行安裝。

插件安裝成功後,如果沒有彈出【ESP-IDF 插件】的配置界面,可以安裝 F1,在彈出的輸入框中輸入:

configure esp-idf

如下圖所示:

選擇【ESP-IDF:配置 ESP-IDF 插件】選項,然後出現配置界面,如下圖所示:

因爲之前我們已經離線安裝了 ESP-IDF 的開發環境,ESP-IDF 插件 已經檢測到了,所以這裏直接選擇第 3 個選擇,即:選擇已經存在的 ESP-IDF 開發環境,如下圖所示:

TIPS

如果在其它選項中通過 ESP-IDF 插件 安裝 ESP-IDF 開發環境,有可能因爲網絡原因而導致安裝失敗,故推薦的做法是:

先通過 ESP-IDF 離線安裝工具進行安裝, 在 ESP-IDF 插件 中選擇已經存在的ESP-IDF 開發環境。

選擇【USE EXISTING SETUP】,然後跳轉到配置界面:

接着開始初始化配置:

請耐心等待,過一會,彈出如下界面,表示配置成功:

VS Code 的左側菜單中也會出現一個插件的按鈕,如下圖所示:

1676444772254

Hello_world 示例

創建項目

打開 VS Code, 按 F1, 在彈框中輸入: ESP-IDF,

在彈框中選擇【展示項目示例】選項

選擇當前的 ESP-IDF 開發環境,最後彈出示例列表,如下圖所示:
1676451345824

這裏選擇【helo_world】示例,使用該示例項目創建新的項目。

在彈框中,選擇項目的保存路徑,點擊確定即可。

特別注意:項目的保存路徑中不要出現中文,否則無法項目編譯會失敗!!!

創建項目成功後,項目的結構如下圖所示:

1676447625834

程序入口

程序入口文件,hello_world_main.c

代碼清單:main/hello_world_main.c

/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

void app_main(void)
{
    printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
            CONFIG_IDF_TARGET,
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

ESP32 開發板連接 PC

使用 USB 線將 ESP32 開發板連接到電腦,如下圖所示:

1676447625834

這裏使用的開發板是:ESP32-C3-DevKitM-1

選擇串口

ESP32 開發板連接 PC 後,在 PC 桌面右鍵單擊【此電腦】圖標,選擇【管理】菜單項,

看到開發板連接的串口,每個人的串口可能不同,我這裏是 COM7, 請記住串口名,您會在後續步驟中使用。

回到 VS Code,在下方的工具欄中,點擊【COM1】按鈕 ,如下圖所示:

在彈框框中選擇【COM7】:

接着選擇項目目錄:

這時,串口選擇變成了【COM7】,如下圖所示:

目標設備

下面開始配置開發板,如下圖所示進行操作:

在彈出框中選擇【esp32c3】

選擇【ESP-PROG】:

選擇成功後,工具欄的目標設備位置顯示:esp32c3,如下圖所示:

配置工程

點擊【配置】按鈕,如下所示:

出錯了!因爲項目目錄中有中文,把項目拷貝到新的目錄下:

F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04

使用 VS Code 打開文件夾 hell_world, 點擊【配置】按鈕,

這時,項目目錄下會自動生成一個 名爲:sdkconfig 的文件和一個 build 文件夾,如下圖所示:

其中,通過如下代碼設置目標設備:

CONFIG_IDF_TARGET="esp32c3"

這裏可對特定的開發板進行配置,比如:內置的 LED 燈對應的IO腳進行設置,因爲該項目沒用使用 GPIO, 故這裏不用做任何操作,直接關閉即可,如下圖所示:

編譯工程

點擊【build】圖標按鈕進行構建,如下圖所示:

1676450525199

然後開始構建:

過程有點漫長,請耐心等待...

出現如下提示,表示構建成功:

Total sizes:
Used stat D/IRAM:   51602 bytes ( 276078 remain, 15.7% used)
      .data size:    7024 bytes
      .bss  size:    3688 bytes
      .text size:   40890 bytes
Used Flash size :   98344 bytes
      .text     :   71984 bytes
      .rodata   :   26104 bytes
Total image size:  146258 bytes (.bin may be padded larger) 

燒錄到設備

點擊【flash】按鈕,將程序上傳到開發板(俗稱:燒錄),如下圖所示:

1676451236876

在彈框中選擇【UART】:

1676451345824

然後選擇項目目錄:

1676451345824

開始燒錄:

 *  正在執行任務: C:/ProgramFiles/Espressif/python_env/idf4.3_py3.8_env/Scripts/python.exe C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\esptool_py\esptool\esptool.py -p COM7 -b 460800 --before default_reset --after hard_reset --chip esp32c3 write_flash --flash_mode dio --flash_freq 80m --flash_size detect 0x8000 partition_table/partition-table.bin 0x0 bootloader/bootloader.bin 0x10000 hello-world.bin 

esptool.py v3.3.2-dev
Serial port COM7
Connecting....
Chip is ESP32-C3 (revision 3)
Features: Wi-Fi 
Crystal is 40MHz
MAC: f4:12:fa:03:30:0c
Uploading stub...     
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x00000000 to 0x00004fff...
Flash will be erased from 0x00010000 to 0x00033fff...
Compressed 3072 bytes to 103...
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 391.1 kbit/s)...
Hash of data verified.
Flash params set to 0x022f
Compressed 20432 bytes to 12167...
Wrote 20432 bytes (12167 compressed) at 0x00000000 in 0.6 seconds (effective 268.2 kbit/s)...
Hash of data verified.
Compressed 146384 bytes to 78299...
Wrote 146384 bytes (78299 compressed) at 0x00010000 in 2.7 seconds (effective 433.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

當出現如下提示,表示燒錄成功。

Leaving...
Hard resetting via RTS pin...

監視輸出

點擊【監視】按鈕,

可以看到輸出信息,如下圖所示:

1676451745048

完整的輸出信息如下:

F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world>set IDF_PATH=C:/ProgramFiles/Espressif/frameworks/esp-idf-v4.3.4/

F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world>C:/ProgramFiles/Espressif/python_env/idf4.3_py3.8_env/Scripts/python.exe C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\tools\idf_monitor.py -p COM7 -b 115200 --toolchain-prefix riscv32-esp-elf- --target esp32c3 f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world\build\hello-world.elf
--- WARNING: GDB cannot open serial ports accessed as COMx
--- Using \\.\COM7 instead...
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\tools\idf_monitor.py:518: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(serial.VERSION) < StrictVersion('3.3.0'):
--- idf_monitor on \\.\COM7 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x191c
load:0x403ce000,len:0x8d4
load:0x403d0000,len:0x2d80
SHA-256 comparison failed:
Calculated: cad2e99d2d13fe2764d0287f1986bf5ae3b6c778c7ebb696474c299799b04393
Expected: c4e0839fb749861d811badbc02c51d14ebb0a5e10442504381d759d0270a49c4
Attempting to boot anyway...
entry 0x403ce000
I (48) boot: ESP-IDF v4.3.4 2nd stage bootloader
I (49) boot: compile time 16:45:17
I (49) boot: chip revision: 3
I (50) boot.esp32c3: SPI Speed      : 80MHz
I (55) boot.esp32c3: SPI Mode       : DIO
I (60) boot.esp32c3: SPI Flash Size : 4MB
I (64) boot: Enabling RNG early entropy source...
I (70) boot: Partition Table:
I (73) boot: ## Label            Usage          Type ST Offset   Length
I (81) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (88) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (96) boot:  2 factory          factory app      00 00 00010000 00100000
I (103) boot: End of partition table
I (107) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=066f8h ( 26360) map
I (120) esp_image: segment 1: paddr=00016720 vaddr=3fc8a000 size=01b70h (  7024) load
I (126) esp_image: segment 2: paddr=00018298 vaddr=40380000 size=07d80h ( 32128) load
I (139) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=11930h ( 71984) map
I (153) esp_image: segment 4: paddr=00031958 vaddr=40387d80 size=0223ch (  8764) load
I (155) esp_image: segment 5: paddr=00033b9c vaddr=50000010 size=00010h (    16) load
I (162) boot: Loaded app from partition at offset 0x10000
I (165) boot: Disabling RNG early entropy source...
I (181) cpu_start: Pro cpu up.
I (190) cpu_start: Pro cpu start user code
I (190) cpu_start: cpu freq: 160000000
I (190) cpu_start: Application information:
I (193) cpu_start: Project name:     hello-world
I (198) cpu_start: App version:      1
I (202) cpu_start: Compile time:     Feb 15 2023 16:43:03
I (208) cpu_start: ELF file SHA256:  24d1a3f78a5c5519...
I (214) cpu_start: ESP-IDF:          v4.3.4
I (219) heap_init: Initializing. RAM available for dynamic allocation:
I (227) heap_init: At 3FC8C9E0 len 00033620 (205 KiB): DRAM
I (233) heap_init: At 3FCC0000 len 0001F060 (124 KiB): STACK/DRAM
I (240) heap_init: At 50000020 len 00001FE0 (7 KiB): RTCRAM
I (246) spi_flash: detected chip: generic
I (251) spi_flash: flash io: dio
I (255) sleep: Configure to isolate all GPIO pins in sleep state
I (261) sleep: Enable automatic switching of GPIO sleep configuration
I (269) cpu_start: Starting scheduler.
Hello world!
This is esp32c3 chip with 1 CPU core(s), WiFi/BLE, silicon revision 3, 4MB external flash
Minimum free heap size: 326768 bytes
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x4038050c
0x4038050c: esp_restart_noos_dig at C:/ProgramFiles/Espressif/frameworks/esp-idf-v4.3.4/components/esp_system/system_api.c:62 (discriminator 1)

SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x191c
load:0x403ce000,len:0x8d4
load:0x403d0000,len:0x2d80
SHA-256 comparison failed:
Calculated: cad2e99d2d13fe2764d0287f1986bf5ae3b6c778c7ebb696474c299799b04393
Expected: c4e0839fb749861d811badbc02c51d14ebb0a5e10442504381d759d0270a49c4
Attempting to boot anyway...
entry 0x403ce000
I (53) boot: ESP-IDF v4.3.4 2nd stage bootloader
I (53) boot: compile time 16:45:17
I (54) boot: chip revision: 3
I (55) boot.esp32c3: SPI Speed      : 80MHz
I (60) boot.esp32c3: SPI Mode       : DIO
I (65) boot.esp32c3: SPI Flash Size : 4MB
I (69) boot: Enabling RNG early entropy source...
I (75) boot: Partition Table:
I (78) boot: ## Label            Usage          Type ST Offset   Length
I (86) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (93) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (101) boot:  2 factory          factory app      00 00 00010000 00100000
I (108) boot: End of partition table
I (112) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=066f8h ( 26360) map
I (125) esp_image: segment 1: paddr=00016720 vaddr=3fc8a000 size=01b70h (  7024) load
I (131) esp_image: segment 2: paddr=00018298 vaddr=40380000 size=07d80h ( 32128) load
I (144) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=11930h ( 71984) map
I (158) esp_image: segment 4: paddr=00031958 vaddr=40387d80 size=0223ch (  8764) load
I (160) esp_image: segment 5: paddr=00033b9c vaddr=50000010 size=00010h (    16) load
I (167) boot: Loaded app from partition at offset 0x10000
I (170) boot: Disabling RNG early entropy source...
I (186) cpu_start: Pro cpu up.
I (195) cpu_start: Pro cpu start user code
I (195) cpu_start: cpu freq: 160000000
I (195) cpu_start: Application information:
I (198) cpu_start: Project name:     hello-world
I (203) cpu_start: App version:      1
I (207) cpu_start: Compile time:     Feb 15 2023 16:43:03
I (213) cpu_start: ELF file SHA256:  24d1a3f78a5c5519...
I (219) cpu_start: ESP-IDF:          v4.3.4
I (224) heap_init: Initializing. RAM available for dynamic allocation:
I (232) heap_init: At 3FC8C9E0 len 00033620 (205 KiB): DRAM
I (238) heap_init: At 3FCC0000 len 0001F060 (124 KiB): STACK/DRAM
I (245) heap_init: At 50000020 len 00001FE0 (7 KiB): RTCRAM
I (251) spi_flash: detected chip: generic
I (256) spi_flash: flash io: dio
I (260) sleep: Configure to isolate all GPIO pins in sleep state
I (266) sleep: Enable automatic switching of GPIO sleep configuration
I (273) cpu_start: Starting scheduler.

......

使用 idf.py 工具

使用 idf.py 工具進行開發,參見:

https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/get-started/windows-setup.html#get-started-windows-first-steps

其實上面在 VS Code 中的所有的操作,底層調用的就是 idf.py 命令,在安裝 ESP_IDF 成功後,得到兩個 ESP_IDF 終端 ,如下圖所示:

1676444772254

在這兩個終端任選其一,也可以在 VS code 中打開,如下圖所示:

1676458609816

然後就可以使用 idf.py 進行操作了。

這裏打開 ESP-IDE PowerShell

Using Python in C:\ProgramFiles\Espressif\python_env\idf4.3_py3.8_env\Scripts
Python 3.8.7
Using Git in C:\ProgramFiles\Espressif\tools\idf-git\2.34.2\cmd
git version 2.34.1.windows.1
Setting IDF_PATH: C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4
Adding ESP-IDF tools to PATH...

Name                           Value
----                           -----
OPENOCD_SCRIPTS                C:\ProgramFiles\Espressif\tools\openocd-esp32\v0.11.0-esp32-20220706\openocd-esp32\sh...
IDF_CCACHE_ENABLE              1
IDF_PYTHON_ENV_PATH            C:\ProgramFiles\Espressif\python_env\idf4.3_py3.8_env

Added to PATH
-------------
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\esptool_py\esptool
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\app_update
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\espcoredump
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\partition_table
C:\ProgramFiles\Espressif\tools\xtensa-esp32-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32-elf\bin
C:\ProgramFiles\Espressif\tools\xtensa-esp32s2-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32s2-elf\bin
C:\ProgramFiles\Espressif\tools\xtensa-esp32s3-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32s3-elf\bin
C:\ProgramFiles\Espressif\tools\riscv32-esp-elf\esp-2021r2-patch3-8.4.0\riscv32-esp-elf\bin
C:\ProgramFiles\Espressif\tools\esp32ulp-elf\2.28.51-esp-20191205\esp32ulp-elf-binutils\bin
C:\ProgramFiles\Espressif\tools\esp32s2ulp-elf\2.28.51-esp-20191205\esp32s2ulp-elf-binutils\bin
C:\ProgramFiles\Espressif\tools\cmake\3.16.4\bin
C:\ProgramFiles\Espressif\tools\openocd-esp32\v0.11.0-esp32-20220706\openocd-esp32\bin
C:\ProgramFiles\Espressif\tools\ninja\1.10.2\
C:\ProgramFiles\Espressif\tools\idf-exe\1.0.1\
C:\ProgramFiles\Espressif\tools\ccache\3.7\
C:\ProgramFiles\Espressif\tools\dfu-util\0.9\dfu-util-0.9-win64
C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\tools
Checking if Python packages are up to date...
Python requirements from C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\requirements.txt are satisfied.

Done! You can now compile ESP-IDF projects.
Go to the project directory and run:
    idf.py build

打開終端時會自動添加 ESP-IDF 的環境變量。

然後切換到項目的根目錄下:

PS C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4> cd F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world

接着就開始進行命令行操作。

選擇串口

ESP32 開發板連接 PC 後,在 PC 桌面右鍵單擊【此電腦】圖標,選擇【管理】菜單項,

看到開發板連接的串口,每個人的串口可能不同,我這裏是 COM7, 請記住串口名,您會在後續步驟中使用。

選擇設備

idf.py set-target esp32c3

其中,目標設置爲:esp32c3

注意,此操作將清除並初始化項目之前的編譯和配置(如有) ,

如果執行失敗,嘗試手動刪除在使用 VS Code 編譯時產生的 build 文件夾,執行結果如下:

> idf.py set-target esp32c3

Adding "set-target"'s dependency "fullclean" to list of commands with default set of options.
Executing action: fullclean
Build directory 'f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world\build' not found. Nothing to clean.
Executing action: set-target
Set Target to: esp32c3, new sdkconfig created. Existing sdkconfig renamed to sdkconfig.old.
Running cmake in directory f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world\build
Executing "cmake -G Ninja -DPYTHON_DEPS_CHECKED=1 -DESP_PLATFORM=1 -DIDF_TARGET=esp32c3 -DCCACHE_ENABLE=1 f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world"...
-- Found Git: C:/ProgramFiles/Espressif/tools/idf-git/2.34.2/cmd/git.exe (found version "2.34.1.windows.1")
-- ccache will be used for faster recompilation
......
-- Component paths: C:/ProgramFiles/Espressif/frameworks/esp-idf-v4.3.4/components/app_trace C:/ProgramFiles/Espressif/frameworks/esp-idf-v4.3.4/components/app_update ..
......
C:/ProgramFiles/Espressif/frameworks/esp-idf-v4.3.4/components/wpa_supplicant
-- Configuring done
-- Generating done
-- Build files have been written to: F:/05-workspace/dev/01-lab/esp32/book-esp32-c3-guide/src/ch04/hello_world/build

配置工程

使用如下命令進行配置:

idf.py menuconfig

正確操作上述步驟後,系統將顯示以下菜單

工程配置 — 主窗口

你可以通過此菜單設置項目的具體變量,包括 Wi-Fi 網絡名稱、密碼和處理器速度等。hello_world 示例項目會以默認配置運行,因此在這一項目中,可以跳過使用 menuconfig 進行項目配置這一步驟

編譯工程

請使用以下命令,編譯工程:

idf.py build

運行以上命令可以編譯應用程序和所有 ESP-IDF 組件,接着生成引導加載程序、分區表和應用程序二進制文件。

> idf.py build
Executing action: all (aliases: build)
Running ninja in directory f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world\build
Executing "ninja all"...
[0/1] Re-running CMake...
-- ccache will be used for faster recompilation
......
*******************************************************************************
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs,data,nvs,0x9000,24K,
phy_init,data,phy,0xf000,4K,
factory,app,factory,0x10000,1M,
*******************************************************************************
[27/966] Building C object esp-idf/mbedtls/mbedtls/library/CMakeFiles/mbedcrypto.dir/ecjpake.c.obj
.....

編譯需要一段漫長的時間,請耐心等待....

當出現如下提示,表示編譯成功

esptool.py v3.3.2-dev
Creating esp32c3 image...
Merged 1 ELF section
Successfully created esp32c3 image.
Generated F:/05-workspace/dev/01-lab/esp32/book-esp32-c3-guide/src/ch04/hello_world/build/bootloader/bootloader.bin
[966/966] Generating binary image from built executable
esptool.py v3.3.2-dev
Creating esp32c3 image...
Merged 1 ELF section
Successfully created esp32c3 image.
Generated F:/05-workspace/dev/01-lab/esp32/book-esp32-c3-guide/src/ch04/hello_world/build/hello-world.bin

Project build complete. To flash, run this command:
C:\ProgramFiles\Espressif\python_env\idf4.3_py3.8_env\Scripts\python.exe C:\ProgramFiles\Espressif\frameworks\esp-idf-v4.3.4\components\esptool_py\esptool\esptool.py -p (PORT) -b 460800 --before default_reset --after hard_reset --chip esp32c3  write_flash --flash_mode dio --flash_size detect --flash_freq 80m 0x0 build\bootloader\bootloader.bin 0x8000 build\partition_table\partition-table.bin 0x10000 build\hello-world.bin
or run 'idf.py -p (PORT) flash'

燒錄到設備

請運行以下命令,將剛剛生成的二進制文件燒錄至您的 ESP32-C3 開發板:

idf.py -p PORT flash

請將 PORT 替換爲 ESP32-C3 開發板的串口名稱。如果 PORT 未經定義,idf.py 將嘗試使用可用的串口自動連接。

更多有關 idf.py 參數的詳情,請見 idf.py

若在燒錄過程中遇到問題,請前往 燒錄故障排除與 ESP32 創建串口連接 獲取更多詳細信息。

在燒錄過程中,您會看到類似如下的輸出日誌 :

> idf.py -p COM7 flash

Executing action: flash
Running ninja in directory f:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\hello_world\build
Executing "ninja flash"...
[1/4] Performing build step for 'bootloader'
ninja: no work to do.
[1/2] cmd.exe /C "cd /D C:\ProgramFiles\Espressif\framewor...sp-idf-v4.3.4/components/esptool_py/run_serial_tool.cmake"
esptool.py esp32c3 -p COM7 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 80m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x0 bootloader/bootloader.bin 0x10000 hello-world.bin
esptool.py v3.3.2-dev
Serial port COM7
Connecting....
Chip is ESP32-C3 (revision 3)
Features: Wi-Fi
Crystal is 40MHz
MAC: f4:12:fa:03:30:0c
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x00000000 to 0x00004fff...
Flash will be erased from 0x00010000 to 0x00033fff...
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 391.1 kbit/s)...
Hash of data verified.
Compressed 20432 bytes to 12168...
Writing at 0x00000000... (100 %)
Wrote 20432 bytes (12168 compressed) at 0x00000000 in 0.6 seconds (effective 266.0 kbit/s)...
Hash of data verified.
Compressed 146384 bytes to 78299...
Writing at 0x00010000... (20 %)
Writing at 0x00019c1a... (40 %)
Writing at 0x000203de... (60 %)
Writing at 0x00027729... (80 %)
Writing at 0x0002d8f3... (100 %)
Wrote 146384 bytes (78299 compressed) at 0x00010000 in 2.7 seconds (effective 434.6 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Done

如果一切順利,燒錄完成後,開發板將會復位,應用程序 “hello_world” 開始運行。

監視輸出

您可以使用 命令,監視 “hello_world” 工程的運行情況:

idf.py -p PORT monitor

注意,不要忘記將 PORT 替換爲您的串口名稱。

運行該命令後,IDF 監視器 應用程序將啓動

> idf.py -p COM7 monitor
...
Hello world!
This is esp32c3 chip with 1 CPU core(s), WiFi/BLE, silicon revision 3, 2MB external flash
Minimum free heap size: 326768 bytes
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...

此時,您就可以在啓動日誌和診斷日誌之後,看到打印的 “Hello world!” 了 .

您可使用快捷鍵 Ctrl+],退出 IDF 監視器。

TIPS

您也可以運行以下命令,一次性執行構建、燒錄和監視過程:

idf.py -p PORT flash monitor

總結

​ 以上所用到的idf.py彙總如下:

idf.py set-target esp32c3 # 選擇設備(這裏的設備是:esp32c3開發板)
idf.py menuconfig         # 配置工程
idf.py build              # 編譯工程
idf.py -p COM7 flash      # 燒錄到設備
idf.py -p COM7 monitor    # 監視輸出
idf.py -p COM7 flash monitor # 燒錄到設備並監視輸出
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章