1. 环境搭建与Hello World
任务目标
在ESP32-S3开发板上完成ESP-IDF开发环境搭建,编译并烧录一个Hello World程序,通过串口输出验证工具链与开发板工作正常。
环境依赖
- 硬件:ESP32-S3-DevKitC-1(或兼容开发板)、USB数据线
- 主机系统:Ubuntu 20.04/22.04、macOS或Windows(以下以Ubuntu为例)
- 依赖包:
git、cmake、ninja-build、python3、python3-venv
环境搭建步骤
- 安装依赖:
sudo apt install git cmake ninja-build python3 python3-venv - 获取ESP-IDF(v5.2版本):
mkdir -p ~/esp && cd ~/esp && git clone -b v5.2 --recursive https://github.com/espressif/esp-idf.git - 运行安装脚本:
cd ~/esp/esp-idf && ./install.sh esp32s3 - 设置环境变量(每次新终端需执行):
. ~/esp/esp-idf/export.sh
创建Hello World工程
使用IDF自带示例创建工程,避免手动编写CMake与组件配置:
cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world hello_world_s3
cd hello_world_s3
idf.py set-target esp32s3
关键完整代码
示例工程中main/hello_world_main.c的内容如下(这是本案例的核心验证代码):
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
void app_main(void)
{
// 打印Hello World
printf("Hello World!\n");
// 获取并打印芯片基本信息,验证芯片访问正常
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is %s chip with %d core(s), WiFi%s%s\n",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "/BLE" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
// 获取flash容量
unsigned flash_size;
esp_flash_get_size(NULL, &flash_size);
printf("Flash size: %lu MB\n", flash_size / (1024 * 1024));
// 延时后重启打印,便于观察
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
编译与烧录
idf.py build
连接开发板后查看串口设备(通常为/dev/ttyUSB0或/dev/ttyACM0),执行烧录并打开串口监视器:
idf.py -p /dev/ttyUSB0 flash monitor
退出监视器按Ctrl+]。
验证方法与判定标准
- 编译无错误,生成
build/hello_world_s3.bin文件。 - 烧录后串口监视器每约1秒输出一次
Hello World!,同时打印芯片型号、核心数与Flash容量。 - 若输出
Hello World!且芯片信息与ESP32-S3规格一致(2核、WiFi/BLE、Flash容量与板载相符),则判定环境搭建成功。
常见问题
- 串口权限不足:执行
sudo usermod -aG dialout $USER后重新登录。 - 烧录失败:按住开发板
BOOT键再复位,或检查USB线是否为数据线。 - 环境变量丢失:新终端需重新执行
. ~/esp/esp-idf/export.sh。