优化工程目录

This commit is contained in:
2026-02-18 20:08:58 +08:00
parent 118d4bf528
commit ee80965d58
67 changed files with 376 additions and 2210 deletions
+2
View File
@@ -0,0 +1,2 @@
CompileFlags:
Remove: [-f*, -m*]
@@ -0,0 +1,13 @@
ARG DOCKER_TAG=latest
FROM espressif/idf:${DOCKER_TAG}
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN apt-get update -y && apt-get install udev -y
RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc
ENTRYPOINT [ "/opt/esp/entrypoint.sh" ]
CMD ["/bin/bash", "-c"]
@@ -0,0 +1,19 @@
{
"name": "ESP-IDF QEMU",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"idf.gitPath": "/usr/bin/git"
},
"extensions": [
"espressif.esp-idf-extension",
"espressif.esp-idf-web"
]
}
},
"runArgs": ["--privileged"]
}
+78
View File
@@ -0,0 +1,78 @@
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Directory metadata
.directory
# Temporary files
*~
*.swp
*.swo
*.bak
*.tmp
# Log files
*.log
# Build artifacts and directories
**/build/
build/
*.o
*.a
*.out
*.exe # For any host-side utilities compiled on Windows
# ESP-IDF specific build outputs
*.bin
*.elf
*.map
flasher_args.json # Generated in build directory
sdkconfig.old
sdkconfig
# ESP-IDF dependencies
# For older versions or manual component management
/components/.idf/
**/components/.idf/
# For modern ESP-IDF component manager
managed_components/
# If ESP-IDF tools are installed/referenced locally to the project
.espressif/
# CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
# Python environment files
*.pyc
*.pyo
*.pyd
__pycache__/
*.egg-info/
dist/
# Virtual environment folders
venv/
.venv/
env/
# Language Servers
.clangd/
.ccls-cache/
compile_commands.json
# Windows specific
Thumbs.db
ehthumbs.db
Desktop.ini
# User-specific configuration files
*.user
*.workspace # General workspace files, can be from various tools
*.suo # Visual Studio Solution User Options
*.sln.docstates # Visual Studio
+19
View File
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "C:\\Espressif\\tools\\riscv32-esp-elf\\esp-13.2.0_20240530\\riscv32-esp-elf\\bin\\riscv32-esp-elf-gcc.exe",
"compileCommands": "${config:idf.buildPath}/compile_commands.json",
"includePath": [
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}
+10
View File
@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "gdbtarget",
"request": "attach",
"name": "Eclipse CDT GDB Adapter"
}
]
}
+11
View File
@@ -0,0 +1,11 @@
{
"idf.currentSetup": "C:/Espressif/frameworks/esp-idf-v5.3.1/",
"idf.flashType": "UART",
"idf.portWin": "COM3",
"idf.openOcdConfigs": [
"board/esp32c3-builtin.cfg"
],
"idf.customExtraVars": {
"IDF_TARGET": "esp32c3"
}
}
+6
View File
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(demo1)
+53
View File
@@ -0,0 +1,53 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | Linux |
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | ----- |
# Hello World Example
Starts a FreeRTOS task to print "Hello World".
(See the README.md file in the upper level 'examples' directory for more information about examples.)
## How to use example
Follow detailed instructions provided specifically for this example.
Select the instructions depending on Espressif chip installed on your development board:
- [ESP32 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/stable/get-started/index.html)
- [ESP32-S2 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/get-started/index.html)
## Example folder contents
The project **hello_world** contains one source file in C language [hello_world_main.c](main/hello_world_main.c). The file is located in folder [main](main).
ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt` files that provide set of directives and instructions describing the project's source files and targets (executable, library, or both).
Below is short explanation of remaining files in the project folder.
```
├── CMakeLists.txt
├── pytest_hello_world.py Python script used for automated testing
├── main
│ ├── CMakeLists.txt
│ └── hello_world_main.c
└── README.md This is the file you are currently reading
```
For more information on structure and contents of ESP-IDF projects, please refer to Section [Build System](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html) of the ESP-IDF Programming Guide.
## Troubleshooting
* Program upload failure
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
## Technical support and feedback
Please use the following feedback channels:
* For technical queries, go to the [esp32.com](https://esp32.com/) forum
* For a feature request or bug report, create a [GitHub issue](https://github.com/espressif/esp-idf/issues)
We will get back to you as soon as possible.
+6
View File
@@ -0,0 +1,6 @@
# 收集当前目录下所有 .c 文件
file(GLOB_RECURSE SRC_LIST "*.c")
idf_component_register(SRCS ${SRC_LIST}
PRIV_REQUIRES spi_flash esp_driver_spi esp_driver_gpio
INCLUDE_DIRS "")
@@ -0,0 +1,97 @@
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "esp_log.h"
#include "spi.h"
#include "lcd.h"
TimerHandle_t periodic_timer = NULL;
// 定时器回调函数(在定时器服务任务中执行)
void periodic_timer_callback(TimerHandle_t xTimer)
{
//printf("每秒执行一次的函数\n");
// 在这里执行你的周期性任务
// 注意:此回调函数应尽快返回,避免阻塞定时器服务任务
lcd_one_second_task();
}
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
uint32_t flash_size;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
unsigned major_rev = chip_info.revision / 100;
unsigned minor_rev = chip_info.revision % 100;
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK)
{
printf("Get flash size failed");
return;
}
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
printf("sizeof(int) ==%d\n", sizeof(int));
spi_init();
lcd_init();
// 配置一个每隔一秒运行一次的函数 用于计算fps等
// 创建周期性软件定时器
// 参数: 定时器名称, 周期(单位: Tick), 自动重载, 回调参数, 回调函数
periodic_timer = xTimerCreate(
"PeriodicTimer", // 定时器名称
pdMS_TO_TICKS(1000), // 周期:1000毫秒 (转换为Tick)
pdTRUE, // 自动重载 (pdTRUE为周期性,pdFALSE为单次)
(void *)0, // 传递给回调函数的参数
periodic_timer_callback // 回调函数
);
if (periodic_timer != NULL)
{
// 启动定时器 (0 ticks后启动)
xTimerStart(periodic_timer, 0);
}
else
{
printf("创建定时器失败!\n");
}
while (1)
{
lcd_clear_buf(rand());
lcd_send_full_buf();
vTaskDelay(1); // 必须让出CPU
}
}
+263
View File
@@ -0,0 +1,263 @@
#include "lcd.h"
static const char *TAG = "LCD";
// uint16_t dis_buff[LCD_WW][LCD_HH];
lcd_t lcd_main;
// SPI 写数据
esp_err_t lcd_spi_send_data_8(uint8_t data)
{
spi_transaction_t trans = {
.length = 8, // 数据位数
.flags = SPI_TRANS_USE_TXDATA, // 必须加这个标志
};
trans.tx_data[0] = (data);
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
return err;
}
// SPI 写数据
esp_err_t lcd_spi_send_data_16(uint16_t data)
{
spi_transaction_t trans = {
.length = 16, // 数据位数
.flags = SPI_TRANS_USE_TXDATA, // 必须加这个标志
};
trans.tx_data[0] = (data >> 8);
trans.tx_data[1] = (data);
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
return err;
}
esp_err_t lcd_spi_send_data_any(uint8_t *data, uint16_t len)
{
spi_transaction_t trans = {
.length = len * 8,
.tx_buffer = data,
};
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
return err;
}
// SPI 写命令
void lcd_spi_send_cmd(uint8_t data)
{
esp_err_t err;
gpio_set_level(LCD_DS, 0);
err = lcd_spi_send_data_8(data);
gpio_set_level(LCD_DS, 1);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "命令发送失败:%x", err);
}
}
/******************************************************************************
函数说明:设置起始和结束地址
入口数据:x1,x2 设置列的起始和结束地址
y1,y2 设置行的起始和结束地址
返回值: 无
******************************************************************************/
void lcd_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
#if USE_HORIZONTAL == 0
lcd_spi_send_cmd(0x2a); // 列地址设置
lcd_spi_send_data_16(x1);
lcd_spi_send_data_16(x2);
lcd_spi_send_cmd(0x2b); // 行地址设置
lcd_spi_send_data_16(y1);
lcd_spi_send_data_16(y2);
lcd_spi_send_cmd(0x2c); // 储存器写
#elif USE_HORIZONTAL == 1
lcd_spi_send_cmd(0x2a); // 列地址设置
lcd_spi_send_data_16(x1);
lcd_spi_send_data_16(x2);
lcd_spi_send_cmd(0x2b); // 行地址设置
lcd_spi_send_data_16(y1 + 80);
lcd_spi_send_data_16(y2 + 80);
lcd_spi_send_cmd(0x2c); // 储存器写
#elif USE_HORIZONTAL == 2
lcd_spi_send_cmd(0x2a); // 列地址设置
lcd_spi_send_data_16(x1);
lcd_spi_send_data_16(x2);
lcd_spi_send_cmd(0x2b); // 行地址设置
lcd_spi_send_data_16(y1);
lcd_spi_send_data_16(y2);
lcd_spi_send_cmd(0x2c); // 储存器写
#elif USE_HORIZONTAL == 3
lcd_spi_send_cmd(0x2a); // 列地址设置
lcd_spi_send_data_16(x1 + 80);
lcd_spi_send_data_16(x2 + 80);
lcd_spi_send_cmd(0x2b); // 行地址设置
lcd_spi_send_data_16(y1);
lcd_spi_send_data_16(y2);
lcd_spi_send_cmd(0x2c); // 储存器写
#endif
}
void lcd_one_second_task()
{
lcd_main.fps=lcd_main.fps_count;
lcd_main.fps_count=0;
ESP_LOGI(TAG, "FPS:%d",lcd_main.fps);
}
void lcd_send_full_buf()
{
gpio_set_level(LCD_CS, 0);
lcd_set_window(0, 0, LCD_WW - 1, LCD_HH - 1);
// 分包发送 单次9600 分16次发送
spi_transaction_t trans = {
.length = 9600 * 8, // 数据位数
};
uint8_t *d_buf = (uint8_t *)(lcd_main.buf);
for (uint16_t i = 0; i < 16; i++)
{
// trans.length=9600*8;
trans.tx_buffer = d_buf + (i * 9600);
spi_device_transmit(lcd_spi, &trans);
}
gpio_set_level(LCD_CS, 1);
//发送成功 帧计数+1
lcd_main.fps_count+=1;
}
void lcd_clear_buf(uint16_t color)
{
for (uint32_t i = 0; i < (LCD_WW * LCD_HH); i++)
{
lcd_main.buf[i] = color;
}
}
void lcd_init()
{
ESP_LOGI(TAG, "配置GPIO");
// 初始化屏幕IO
// 配置GPIO
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << LCD_DS) | (1ULL << LCD_CS), // LCD_DS LCD_CS
.mode = GPIO_MODE_OUTPUT, // 输出模式
.pull_up_en = GPIO_PULLUP_DISABLE, //
.pull_down_en = GPIO_PULLDOWN_DISABLE, //
.intr_type = GPIO_INTR_DISABLE // 禁用中断
};
// 应用配置
gpio_config(&io_conf);
// 配置 GPIO 驱动能力 (ESP32-C3 支持)
// gpio_set_drive_capability(VSPI_MISO, GPIO_DRIVE_CAP_3); // 最大驱动
gpio_set_drive_capability(VSPI_MOSI, GPIO_DRIVE_CAP_3);
gpio_set_drive_capability(VSPI_SCLK, GPIO_DRIVE_CAP_3);
ESP_LOGI(TAG, "配置GPIO完成");
// 设置初始电平
gpio_set_level(LCD_DS, 0); // 输出低电平
gpio_set_level(LCD_CS, 0);
ESP_LOGI(TAG, "等待初始化LCD");
vTaskDelay(25); //
// uint8_t data_tmp[16];//数据缓存
ESP_LOGI(TAG, "初始化LCD");
lcd_spi_send_cmd(0x36);
if (USE_HORIZONTAL == 0)
lcd_spi_send_data_8(0x00);
else if (USE_HORIZONTAL == 1)
lcd_spi_send_data_8(0xC0);
else if (USE_HORIZONTAL == 2)
lcd_spi_send_data_8(0x70);
else
lcd_spi_send_data_8(0xA0);
lcd_spi_send_cmd(0x3A);
lcd_spi_send_data_8(0x05);
lcd_spi_send_cmd(0xB2);
lcd_spi_send_data_8(0x0C);
lcd_spi_send_data_8(0x0C);
lcd_spi_send_data_8(0x00);
lcd_spi_send_data_8(0x33);
lcd_spi_send_data_8(0x33);
lcd_spi_send_cmd(0xB7);
lcd_spi_send_data_8(0x35);
lcd_spi_send_cmd(0xBB);
lcd_spi_send_data_8(0x19);
lcd_spi_send_cmd(0xC0);
lcd_spi_send_data_8(0x2C);
lcd_spi_send_cmd(0xC2);
lcd_spi_send_data_8(0x01);
lcd_spi_send_cmd(0xC3);
lcd_spi_send_data_8(0x12);
lcd_spi_send_cmd(0xC4);
lcd_spi_send_data_8(0x20);
lcd_spi_send_cmd(0xC6);
lcd_spi_send_data_8(0x0F);
lcd_spi_send_cmd(0xD0);
lcd_spi_send_data_8(0xA4);
lcd_spi_send_data_8(0xA1);
lcd_spi_send_cmd(0xE0);
lcd_spi_send_data_8(0xD0);
lcd_spi_send_data_8(0x04);
lcd_spi_send_data_8(0x0D);
lcd_spi_send_data_8(0x11);
lcd_spi_send_data_8(0x13);
lcd_spi_send_data_8(0x2B);
lcd_spi_send_data_8(0x3F);
lcd_spi_send_data_8(0x54);
lcd_spi_send_data_8(0x4C);
lcd_spi_send_data_8(0x18);
lcd_spi_send_data_8(0x0D);
lcd_spi_send_data_8(0x0B);
lcd_spi_send_data_8(0x1F);
lcd_spi_send_data_8(0x23);
lcd_spi_send_cmd(0xE1);
lcd_spi_send_data_8(0xD0);
lcd_spi_send_data_8(0x04);
lcd_spi_send_data_8(0x0C);
lcd_spi_send_data_8(0x11);
lcd_spi_send_data_8(0x13);
lcd_spi_send_data_8(0x2C);
lcd_spi_send_data_8(0x3F);
lcd_spi_send_data_8(0x44);
lcd_spi_send_data_8(0x51);
lcd_spi_send_data_8(0x2F);
lcd_spi_send_data_8(0x1F);
lcd_spi_send_data_8(0x1F);
lcd_spi_send_data_8(0x20);
lcd_spi_send_data_8(0x23);
lcd_spi_send_cmd(0x21);
lcd_spi_send_cmd(0x11);
vTaskDelay(12);
lcd_spi_send_cmd(0x29);
lcd_clear_buf(0xffff);
gpio_set_level(LCD_CS, 1);
ESP_LOGI(TAG, "初始化LCD完成");
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef LCD_H
#define LCD_H
#include <stdio.h>
#include <inttypes.h>
#include "esp_log.h"
#include "spi.h"
#define LCD_CS 7
#define LCD_DS 6
#define USE_HORIZONTAL 2 //设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
#define LCD_WW 320
#define LCD_HH 240
typedef struct{
uint16_t buf[LCD_WW*LCD_HH];
uint16_t fps_count;
uint16_t fps;
}lcd_t;
extern lcd_t lcd_main;
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
void lcd_init();
void lcd_clear_buf(uint16_t Color);
void lcd_send_full_buf();
void lcd_one_second_task();
#endif
+60
View File
@@ -0,0 +1,60 @@
#include "spi.h"
#define SPI_HOST SPI2_HOST
static const char *TAG = "SPI_2";
spi_device_handle_t lcd_spi;
void spi_init()
{
ESP_LOGI(TAG, "初始化SPI IO总线");
esp_err_t ret;
// 1. SPI 总线配置
spi_bus_config_t buscfg = {
.miso_io_num = VSPI_MISO, // MISO 引脚
.mosi_io_num = VSPI_MOSI, // MOSI 引脚
.sclk_io_num = VSPI_SCLK, // 时钟引脚
.quadwp_io_num = -1, // 不使用 QWP
.quadhd_io_num = -1, // 不使用 QHD
.max_transfer_sz = 9600, // 最大传输大小
// .flags = 0,
// .intr_flags = 0,
};
// 2. 初始化 SPI 总线
ret = spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "IO总线初始化失败");
return;
}
ESP_LOGI(TAG, "SPI IO总线初始化成功");
ESP_LOGI(TAG, "SPI 设备配置");
spi_device_interface_config_t devcfg = {
.command_bits = 0, // 无命令位
.address_bits = 0, // 无地址位
.dummy_bits = 0,
.mode = 0, // SPI 模式 0
.clock_speed_hz = 80000000, //
.spics_io_num = -1,
.queue_size = 7, // 队列深度
.flags = SPI_DEVICE_NO_DUMMY, // 禁用 dummy 周期
.input_delay_ns = 0,
.pre_cb = NULL,
.post_cb = NULL,
};
ret = spi_bus_add_device(SPI_HOST, &devcfg, &lcd_spi);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "SPI 设备配置失败");
return;
}
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef SPI_H
#define SPI_H
#include <stdio.h>
#include <inttypes.h>
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_system.h"
#define VSPI_MISO -1 //接收引脚不使用,仅发送
#define VSPI_MOSI 3
#define VSPI_SCLK 2
void spi_init();
extern spi_device_handle_t lcd_spi;
#endif /* SPI_H */
+53
View File
@@ -0,0 +1,53 @@
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import hashlib
import logging
from typing import Callable
import pytest
from pytest_embedded_idf.dut import IdfDut
from pytest_embedded_qemu.app import QemuApp
from pytest_embedded_qemu.dut import QemuDut
@pytest.mark.supported_targets
@pytest.mark.preview_targets
@pytest.mark.generic
def test_hello_world(
dut: IdfDut, log_minimum_free_heap_size: Callable[..., None]
) -> None:
dut.expect('Hello world!')
log_minimum_free_heap_size()
@pytest.mark.linux
@pytest.mark.host_test
def test_hello_world_linux(dut: IdfDut) -> None:
dut.expect('Hello world!')
def verify_elf_sha256_embedding(app: QemuApp, sha256_reported: str) -> None:
sha256 = hashlib.sha256()
with open(app.elf_file, 'rb') as f:
sha256.update(f.read())
sha256_expected = sha256.hexdigest()
logging.info(f'ELF file SHA256: {sha256_expected}')
logging.info(f'ELF file SHA256 (reported by the app): {sha256_reported}')
# the app reports only the first several hex characters of the SHA256, check that they match
if not sha256_expected.startswith(sha256_reported):
raise ValueError('ELF file SHA256 mismatch')
@pytest.mark.esp32 # we only support qemu on esp32 for now
@pytest.mark.host_test
@pytest.mark.qemu
def test_hello_world_host(app: QemuApp, dut: QemuDut) -> None:
sha256_reported = (
dut.expect(r'ELF file SHA256:\s+([a-f0-9]+)').group(1).decode('utf-8')
)
verify_elf_sha256_embedding(app, sha256_reported)
dut.expect('Hello world!')
View File