第03章 工程搭建
约 658 字大约 2 分钟
2026-03-23
1. 使用模板创建项目
工程名: AI_Xiaozhi
创建过程参考《尚硅谷嵌入式项目之智能门锁》项目
2. 几个必备配置
| 作用 | 配置名 | 配置值 |
|---|---|---|
| 滴答频率配置为1000Hz | FREERTOS_HZ | 1000 |
| Flash类型 | ESPTOOLPY_FLASHMODE | DOUT |
| Flash大小 | ESPTOOLPY_FLASHSIZE | 8MB |
| Flash速度 | ESPTOOLPY_FLASHFREQ | 80MHz |
| SPI_FLASH的高性能模式 | SPI_FLASH_HPM | Enable |
| 使能外置PSRAM | SPIRAM | ESP PSRAM打勾 |
| 外置RAM的速度 | SPIRAM_SPEED | 80MHz |
| Cpu主频 | ESP_DEFAULT_CPU_FREQ_MHZ | 240MHz |

| 选项 | 全称(概念) | 数据线 | 特点 | 适用场景 |
|---|---|---|---|---|
| DIO | Dual I/O | 2 根 (SD0, SD1) | 同时传输数据和地址,速度较快,兼容性好。 | 最常用的默认模式,绝大多数 ESP32 开发板都支持。 |
| QIO | Quad I/O | 4 根 (SD0-SD3) | 同时传输 4 路数据,速度最快,但对 Flash 芯片要求高。 | 追求极致速度,且 Flash 支持四线模式时使用。 |
| DOUT | Dual Output | 2 根 | 数据并行输出,但地址/指令串行,速度较慢。 | 较老的或特定的 Flash 芯片。 |
| QOUT | Quad Output | 4 根 | 数据并行输出,但地址/指令串行,速度中等偏快。 | 较少见,用于特定的高速单向传输场景。 |
3. 项目结构
在 main 目录下新建 app、com、dir、int 四个目录,作为4个层级,com 目录下新建 com_debug.h、com_debug.c 作为调试模块
main 目录中的CMakeLists.txt 配置如下:
idf_component_register(
SRCS
"main.c"
"./com/com_debug.c"
INCLUDE_DIRS
"."
"./com"
"./dri"
"./int"
"./app"
)4. 调试模块
esp-idf提供的sdk已经有了比较完善的日志系统, 但是在输出日志的时候不能自动添加文件名和行号,我们对已有日志系统做扩展,能够自动添加文件名和行号, 方便日志的查看
com_debug.h
#ifndef __COM_DEBUG_H__
#define __COM_DEBUG_H__
#include <string.h>
#include "esp_task.h"
#include "esp_log.h"
#define DEBUG 1
extern char tag[100];
#if (DEBUG == 1)
#define MY_LOGE(format, ...) \
do \
{ \
sprintf(tag, "[%10s:%4d]", __FILENAME__, __LINE__); \
ESP_LOGE(tag, format, ##__VA_ARGS__); \
} while (0)
#define MY_LOGW(format, ...) \
do \
{ \
sprintf(tag, "[%10s:%4d]", __FILENAME__, __LINE__); \
ESP_LOGW(tag, format, ##__VA_ARGS__); \
} while (0)
#define MY_LOGI(format, ...) \
do \
{ \
sprintf(tag, "[%10s:%4d]", __FILENAME__, __LINE__); \
ESP_LOGI(tag, format, ##__VA_ARGS__); \
} while (0)
#define MY_LOGD(format, ...) \
do \
{ \
sprintf(tag, "[%10s:%4d]", __FILENAME__, __LINE__); \
ESP_LOGD(tag, format, ##__VA_ARGS__); \
} while (0)
#define MY_LOGV(format, ...) \
do \
{ \
sprintf(tag, "[%10s:%4d]", __FILENAME__, __LINE__); \
ESP_LOGV(tag, format, ##__VA_ARGS__); \
} while (0)
#else
#define MY_LOGE(format, ...)
#define MY_LOGW(format, ...)
#define MY_LOGI(format, ...)
#define MY_LOGD(format, ...)
#define MY_LOGV(format, ...)
#endif
#endif /* __COM_DEBUG_H__ */com_debug.c
#include "com_debug.h"
char tag[100];main.c 验证
#include <stdio.h>
#include "com_debug.h"
void app_main(void)
{
// 将字符串给串口
MY_LOGE("This is an error message.");
MY_LOGW("This is a warning message.");
MY_LOGI("This is an info message.");
// 不会给串口
MY_LOGD("This is a debug message.");
MY_LOGV("This is a verbose message.");
while (1)
{
vTaskDelay(1000);
}
}