以hello-world为例,演示步骤和注意事项
1、新建ESP-IDF项目
选择模板
从hello-world模板创建
2、打开项目
3、编译结果没错
正在执行任务: /home/azhu/.espressif/python_env/idf5.1_py3.10_env/bin/python /home/azhu/esp/v5.1/esp-idf/tools/idf_size.py /home/azhu/ESP32/test/use_componet/build/use_componet.map Total sizes:
Used static DRAM: 10664 bytes ( 170072 remain, 5.9% used).data size: 8472 bytes.bss size: 2192 bytes
Used static IRAM: 48958 bytes ( 82114 remain, 37.4% used).text size: 47931 bytes.vectors size: 1027 bytes
Used Flash size : 118107 bytes.text: 80723 bytes.rodata: 37128 bytes
Total image size: 175537 bytes (.bin may be pa
4、烧写运行
'/home/azhu/.espressif/python_env/idf5.1_py3.10_env/bin/python' '/home/azhu/esp/v5.1/esp-idf/tools/idf_monitor.py' -p /dev/ttyUSB0 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 '/home/azhu/ESP32/test/use_componet/build/use_componet.elf'
--- esp-idf-monitor 1.5.0 on /dev/ttyUSB0 115200
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jul 29 2019 12:21:46rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7128
load:0x40078000,len:15624
load:0x40080400,len:4
--- 0x40080400: _init at ??:?load:0x40080404,len:3876
entry 0x4008064c
I (29) boot: ESP-IDF v5.1.5-346-g41a885bb2d 2nd stage bootloader
I (29) boot: compile time Dec 24 2024 14:24:47
I (31) boot: Multicore bootloader
I (35) boot: chip revision: v3.0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 2MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09208h ( 37384) map
I (117) esp_image: segment 1: paddr=00019230 vaddr=3ffb0000 size=02118h ( 8472) load
I (121) esp_image: segment 2: paddr=0001b350 vaddr=40080000 size=04cc8h ( 19656) load
I (131) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=13b54h ( 80724) map
I (161) esp_image: segment 4: paddr=00033b7c vaddr=40084cc8 size=07278h ( 29304) load
I (179) boot: Loaded app from partition at offset 0x10000
I (179) boot: Disabling RNG early entropy source...
I (191) cpu_start: Multicore app
I (192) cpu_start: Pro cpu up.
I (192) cpu_start: Starting app cpu, entry point is 0x400810f4
--- 0x400810f4: call_start_cpu1 at /home/azhu/esp/v5.1/esp-idf/components/esp_system/port/cpu_start.c:160I (0) cpu_start: App cpu up.
I (209) cpu_start: Pro cpu start user code
I (209) cpu_start: cpu freq: 160000000 Hz
I (209) cpu_start: Application information:
I (214) cpu_start: Project name: use_componet
I (219) cpu_start: App version: 1
I (224) cpu_start: Compile time: Dec 24 2024 14:24:22
I (230) cpu_start: ELF file SHA256: 7a0e0699229a74e2...
I (236) cpu_start: ESP-IDF: v5.1.5-346-g41a885bb2d
I (242) cpu_start: Min chip rev: v0.0
I (247) cpu_start: Max chip rev: v3.99
I (252) cpu_start: Chip rev: v3.0
I (257) heap_init: Initializing. RAM available for dynamic allocation:
I (264) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (270) heap_init: At 3FFB29A8 len 0002D658 (181 KiB): DRAM
I (276) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (282) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (289) heap_init: At 4008BF40 len 000140C0 (80 KiB): IRAM
I (296) spi_flash: detected chip: gd
I (299) spi_flash: flash io: dio
W (303) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (317) app_start: Starting scheduler on CPU0
I (321) app_start: Starting scheduler on CPU1
I (321) main_task: Started on CPU0
I (331) main_task: Calling app_main()
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
Minimum free heap size: 301172 bytes
Restarting in 10 seconds...
5、添加组件,功能为再输出一行文字
COMMAND->Create ESP-IDF Compontent
最上方输入自己的组件名称然后回车,这里输入 mycomp
ESP-IDF自动创建compontents文件夹,然后在下面有mycomp文件夹,里面有mycomp.c,include/mycomp.h
编写代码,mycomp.c
#include <stdio.h>
#include "mycomp.h"void func(void)
{printf("call by mycomp\n");
}
main/hello_world_main.c,只加两行
......
#include "esp_flash.h"
#include "mycomp.h" //addvoid app_main(void)
{......printf("Restarting now.\n");func(); //addfflush(stdout);
6、再次编译,不出意外的话,一定报错:
............../use_componet/main/hello_world_main.c
/home/azhu/ESP32/test/use_componet/main/hello_world_main.c:14:10: fatal error: mycomp.h: No such file or directory14 | #include "mycomp.h"| ^~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
7、关键要点!
原因是main没有包含自己写的组件,或者说依赖,把main/CMakeLists.txt修改为
idf_component_register(SRCS "hello_world_main.c"INCLUDE_DIRS "")
改成如下:idf_component_register(SRCS "main.c"INCLUDE_DIRS ""PRIV_REQUIRES mycomp)
再编译,错误变了
/use_componet/main/hello_world_main.c:13:10: fatal error: esp_flash.h: No such file or directory13 | #include "esp_flash.h"| ^~~~~~~~~~~~~
compilation terminated.
[902/910] Building C object esp-id...provisioning.dir/src/manager.c.obj
ninja: build stopped: subcommand failed.
继续修改main/CMakeLists.txt,右键点#include "esp_flash.h",然后转到定义,可以看到esp_flash.h位于esp_flash模块,所以加上spi_flash组件
idf_component_register(SRCS "hello_world_main.c"INCLUDE_DIRS ""PRIV_REQUIRES spi_flash mycomp)
所有的include都有组件支持了,编译无错,运行后可以看到输出了 call by mycomp
I (331) main_task: Calling app_main()
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
Minimum free heap size: 301172 bytes
Restarting in 10 seconds...
......
Restarting in 0 seconds...
Restarting now.
call by mycomp
ets Jul 29 2019 12:21:46rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
.....
8、如果自己的组件中不止一个源文件怎么办,方法是编辑组件根下的CMakeLists.txt,这里示例为components/mycomp/CMakeLists.txt,有两个方法可以组织源文件和头文件
file(GLOB_RECURSE SOURCES *.c */*.c)set(include_dirs """include"
)
idf_component_register(SRCS ${SOURCES}INCLUDE_DIRS ${include_dirs})# idf_component_register(SRCS "mycomp.c" "mycomp2.c"
# INCLUDE_DIRS "include")
这里简单示例,mycomp2.c和mycomp.c功能差不多,最后在主程序func();后面加了行func2();
运行结果:
I (331) main_task: Calling app_main()
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
Minimum free heap size: 301172 bytes
Restarting in 10 seconds...
....
Restarting in 0 seconds...
Restarting now.
call by mycomp
call by mycomp---2
ets Jul 29 2019 12:21:46
9、小结
如果main模块编译出错,需要编辑同级CMakeLists.txt,如果组件编译出错,也要编辑同级CMakeLists.txt
如果需要,执行COMMAND->Full Clean