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
,如下图所示:
TIPS:
ESP-IDF 虽然目前已经更新到 v5.0 版本,但是为了与书本中的版本一致,这里选择了 v4.3.4 版本。
![1676441408983](ESP32-C3 物联网工程开发实战-笔记实录/images/1676441408983.png)
安装过程中,选择的安装目录中不要有空格,这里我们选择的安装路径是:
C:\ProgramFiles\Espressif
安装完成后,会得到两个 ESP_IDF 终端 ,如下图所示:
在这两个终端任选其一,打开终端时会自动添加 ESP-IDF 的环境变量,如下图所示:
![1676441952067](ESP32-C3 物联网工程开发实战-笔记实录/images/1676441952067.png)
,之后就可使用 idf.py 命令进行操作了。
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/blob/HEAD/docs/tutorial/install.md
下面演示安装过程:
在 VS Code 的左侧菜单中选择【扩展(Ctrl + Shift + X)】,在搜索框中输入:
ESP-IDF
在搜索结果中找到 ESP-IDF 插件,如下图所示:
![1676443300021](ESP32-C3 物联网工程开发实战-笔记实录/images/1676443300021.png)
点击【安装】按钮进行安装。
插件安装成功后,如果没有弹出【ESP-IDF 插件】的配置界面,可以安装 F1,在弹出的输入框中输入:
configure esp-idf
如下图所示:
![1676443590555](ESP32-C3 物联网工程开发实战-笔记实录/images/1676443590555.png)
选择【ESP-IDF:配置 ESP-IDF 插件】选项,然后出现配置界面,如下图所示:
![1676443785377](ESP32-C3 物联网工程开发实战-笔记实录/images/1676443785377.png)
因为之前我们已经离线安装了 ESP-IDF 的开发环境,ESP-IDF 插件 已经检测到了,所以这里直接选择第 3 个选择,即:选择已经存在的 ESP-IDF 开发环境,如下图所示:
TIPS
如果在其它选项中通过 ESP-IDF 插件 安装 ESP-IDF 开发环境,有可能因为网络原因而导致安装失败,故推荐的做法是:
先通过 ESP-IDF 离线安装工具进行安装, 在 ESP-IDF 插件 中选择已经存在的ESP-IDF 开发环境。
![1676443953493](ESP32-C3 物联网工程开发实战-笔记实录/images/1676443953493.png)
选择【USE EXISTING SETUP】,然后跳转到配置界面:
![1676444118442](ESP32-C3 物联网工程开发实战-笔记实录/images/1676444118442.png)
接着开始初始化配置:
![1676444059265](ESP32-C3 物联网工程开发实战-笔记实录/images/1676444059265.png)
请耐心等待,过一会,弹出如下界面,表示配置成功:
![1676444149932](ESP32-C3 物联网工程开发实战-笔记实录/images/1676444149932.png)
在 VS Code 的左侧菜单中也会出现一个插件的按钮,如下图所示:
Hello_world 示例
创建项目
打开 VS Code, 按 F1, 在弹框中输入: ESP-IDF,
![1676447151328](ESP32-C3 物联网工程开发实战-笔记实录/images/1676447151328.png)
在弹框中选择【展示项目示例】选项
![1676447201951](ESP32-C3 物联网工程开发实战-笔记实录/images/1676447201951.png)
选择当前的 ESP-IDF 开发环境,最后弹出示例列表,如下图所示:
这里选择【helo_world】示例,使用该示例项目创建新的项目。
在弹框中,选择项目的保存路径,点击确定即可。
特别注意:项目的保存路径中不要出现中文,否则无法项目编译会失败!!!
创建项目成功后,项目的结构如下图所示:
使用 USB 线将 ESP32 开发板连接到电脑,
这里使用的开发板是:ESP32-C3-DevKitM-1
程序入口
程序入口文件,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();
}
串口
然后右键单击【此电脑】图标,选择【管理】菜单项,
![1676448463052](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448463052.png)
看到开发板连接的串口,每个人的串口可能不同,我这里是 COM7,回到 VS Code,
在下方的工具栏中,点击【COM1】按钮 ,如下图所示:
![1676448663654](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448663654.png)
在弹框框中选择【COM7】:
![1676448720747](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448720747.png)
接着选择项目目录:
![1676448758802](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448758802.png)
这时,串口选择变成了【COM7】,如下图所示:
![1676448819760](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448819760.png)
目标设备
下面开始配置开发板,如下图所示进行操作:
![1676448946215](ESP32-C3 物联网工程开发实战-笔记实录/images/1676448946215.png)
在弹出框中选择【esp32c3】
![1676449039971](ESP32-C3 物联网工程开发实战-笔记实录/images/1676449039971.png)
选择【ESP-PROG】:
![1676449086097](ESP32-C3 物联网工程开发实战-笔记实录/images/1676449086097.png)
选择成功后,工具栏的目标设备位置显示:esp32c3,如下图所示:
![1676449145328](ESP32-C3 物联网工程开发实战-笔记实录/images/1676449145328.png)
配置
![1676449266402](ESP32-C3 物联网工程开发实战-笔记实录/images/1676449266402.png)
出错了!
项目目录中有中文,把项目拷贝到新的目录下:
F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04
使用 VS Code 打开文件夹 hell_world, 点击【配置】按钮,
![1676449971790](ESP32-C3 物联网工程开发实战-笔记实录/images/1676449971790.png)
这时,项目目录下会自动生成一个 名为:sdkconfig 的文件和一个 build 文件夹,如下图所示:
![1676450328718](ESP32-C3 物联网工程开发实战-笔记实录/images/1676450328718.png)
注意是设定了,目标设备:
CONFIG_IDF_TARGET="esp32c3"
这里可对特定的开发板进行配置,比如:内置的 LED 灯对应的IO脚进行设置,因为该项目没用使用 GPIO, 故这里不用做任何操作,直接关闭即可,如下图所示:
![1676450440843](ESP32-C3 物联网工程开发实战-笔记实录/images/1676450440843.png)
编译
点击【build】图标按钮进行构建,如下图所示:
然后开始构建:
![1676450656284](ESP32-C3 物联网工程开发实战-笔记实录/images/1676450656284.png)
过程有点漫长,请耐心等待...
出现如下提示,表示构建成功:
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】按钮,将程序上传到开发板,如下图所示:
在弹框中选择【UART】:
然后选择项目目录:
![1676451369405](ESP32-C3 物联网工程开发实战-笔记实录/images/1676451369405.png)
开始烧录:
* 正在执行任务: 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...
监视
点击【监视】按钮,
可以看到输出信息,如下图所示:
完整的输出信息如下:
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.
......
标签:...,esp,ESP32,ESP,boot,详解,IDF,搭建
From: https://www.cnblogs.com/easy5weikai/p/17124123.html