ToB企服应用市场:ToB评测及商务社交产业平台

标题: 在vscode的ESP-IDF中利用自界说组件 [打印本页]

作者: 魏晓东    时间: 2024-12-28 20:02
标题: 在vscode的ESP-IDF中利用自界说组件
以hello-world为例,演示步骤和注意事项
1、新建ESP-IDF项目

选择模板

从hello-world模板创建
2、打开项目

3、编译结果没错
  1. 正在执行任务: /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
  2. Total sizes:
  3. Used static DRAM:   10664 bytes ( 170072 remain, 5.9% used)
  4.       .data size:    8472 bytes
  5.       .bss  size:    2192 bytes
  6. Used static IRAM:   48958 bytes (  82114 remain, 37.4% used)
  7.       .text size:   47931 bytes
  8.    .vectors size:    1027 bytes
  9. Used Flash size :  118107 bytes
  10.            .text:   80723 bytes
  11.          .rodata:   37128 bytes
  12. Total image size:  175537 bytes (.bin may be pa
复制代码
4、烧写运行
  1. '/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'
  2. --- esp-idf-monitor 1.5.0 on /dev/ttyUSB0 115200
  3. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
  4. ets Jul 29 2019 12:21:46
  5. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  6. configsip: 0, SPIWP:0xee
  7. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  8. mode:DIO, clock div:2
  9. load:0x3fff0030,len:7128
  10. load:0x40078000,len:15624
  11. load:0x40080400,len:4
  12. --- 0x40080400: _init at ??:?
  13. load:0x40080404,len:3876
  14. entry 0x4008064c
  15. I (29) boot: ESP-IDF v5.1.5-346-g41a885bb2d 2nd stage bootloader
  16. I (29) boot: compile time Dec 24 2024 14:24:47
  17. I (31) boot: Multicore bootloader
  18. I (35) boot: chip revision: v3.0
  19. I (39) boot.esp32: SPI Speed      : 40MHz
  20. I (43) boot.esp32: SPI Mode       : DIO
  21. I (48) boot.esp32: SPI Flash Size : 2MB
  22. I (52) boot: Enabling RNG early entropy source...
  23. I (58) boot: Partition Table:
  24. I (61) boot: ## Label            Usage          Type ST Offset   Length
  25. I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
  26. I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
  27. I (84) boot:  2 factory          factory app      00 00 00010000 00100000
  28. I (91) boot: End of partition table
  29. I (95) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09208h ( 37384) map
  30. I (117) esp_image: segment 1: paddr=00019230 vaddr=3ffb0000 size=02118h (  8472) load
  31. I (121) esp_image: segment 2: paddr=0001b350 vaddr=40080000 size=04cc8h ( 19656) load
  32. I (131) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=13b54h ( 80724) map
  33. I (161) esp_image: segment 4: paddr=00033b7c vaddr=40084cc8 size=07278h ( 29304) load
  34. I (179) boot: Loaded app from partition at offset 0x10000
  35. I (179) boot: Disabling RNG early entropy source...
  36. I (191) cpu_start: Multicore app
  37. I (192) cpu_start: Pro cpu up.
  38. I (192) cpu_start: Starting app cpu, entry point is 0x400810f4
  39. --- 0x400810f4: call_start_cpu1 at /home/azhu/esp/v5.1/esp-idf/components/esp_system/port/cpu_start.c:160
  40. I (0) cpu_start: App cpu up.
  41. I (209) cpu_start: Pro cpu start user code
  42. I (209) cpu_start: cpu freq: 160000000 Hz
  43. I (209) cpu_start: Application information:
  44. I (214) cpu_start: Project name:     use_componet
  45. I (219) cpu_start: App version:      1
  46. I (224) cpu_start: Compile time:     Dec 24 2024 14:24:22
  47. I (230) cpu_start: ELF file SHA256:  7a0e0699229a74e2...
  48. I (236) cpu_start: ESP-IDF:          v5.1.5-346-g41a885bb2d
  49. I (242) cpu_start: Min chip rev:     v0.0
  50. I (247) cpu_start: Max chip rev:     v3.99
  51. I (252) cpu_start: Chip rev:         v3.0
  52. I (257) heap_init: Initializing. RAM available for dynamic allocation:
  53. I (264) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
  54. I (270) heap_init: At 3FFB29A8 len 0002D658 (181 KiB): DRAM
  55. I (276) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
  56. I (282) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
  57. I (289) heap_init: At 4008BF40 len 000140C0 (80 KiB): IRAM
  58. I (296) spi_flash: detected chip: gd
  59. I (299) spi_flash: flash io: dio
  60. 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.
  61. I (317) app_start: Starting scheduler on CPU0
  62. I (321) app_start: Starting scheduler on CPU1
  63. I (321) main_task: Started on CPU0
  64. I (331) main_task: Calling app_main()
  65. Hello world!
  66. This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
  67. Minimum free heap size: 301172 bytes
  68. Restarting in 10 seconds...
复制代码
5、添加组件,功能为再输出一行笔墨
COMMAND->Create ESP-IDF Compontent
最上方输入本身的组件名称然后回车,这里输入 mycomp
ESP-IDF自动创建compontents文件夹,然后在下面有mycomp文件夹,内里有mycomp.c,include/mycomp.h

编写代码,mycomp.c
  1. #include <stdio.h>
  2. #include "mycomp.h"
  3. void func(void)
  4. {
  5.     printf("call by mycomp\n");
  6. }
复制代码
main/hello_world_main.c,只加两行
  1. ......
  2. #include "esp_flash.h"
  3. #include "mycomp.h"        //add
  4. void app_main(void)
  5. {
  6.     ......
  7.     printf("Restarting now.\n");
  8.     func();                //add
  9.     fflush(stdout);
复制代码
6、再次编译,不出意外的话,一定报错:
  1. ............../use_componet/main/hello_world_main.c
  2. /home/azhu/ESP32/test/use_componet/main/hello_world_main.c:14:10: fatal error: mycomp.h: No such file or directory
  3.    14 | #include "mycomp.h"
  4.       |          ^~~~~~~~~~
  5. compilation terminated.
  6. ninja: build stopped: subcommand failed.
复制代码
7、关键要点!
原因是main没有包含本身写的组件,或者说依赖,把main/CMakeLists.txt修改为
  1. idf_component_register(SRCS "hello_world_main.c"
  2.                     INCLUDE_DIRS "")
  3. 改成如下:
  4. idf_component_register(SRCS "main.c"
  5.                     INCLUDE_DIRS ""
  6.                     PRIV_REQUIRES mycomp
  7.                     )
复制代码
再编译,错误变了
  1. /use_componet/main/hello_world_main.c:13:10: fatal error: esp_flash.h: No such file or directory
  2.    13 | #include "esp_flash.h"
  3.       |          ^~~~~~~~~~~~~
  4. compilation terminated.
  5. [902/910] Building C object esp-id...provisioning.dir/src/manager.c.obj
  6. ninja: build stopped: subcommand failed.
复制代码
继续修改main/CMakeLists.txt,右键点#include "esp_flash.h",然后转到界说,可以看到esp_flash.h位于esp_flash模块,以是加上spi_flash组件
  1. idf_component_register(SRCS "hello_world_main.c"
  2.                     INCLUDE_DIRS ""
  3.                     PRIV_REQUIRES spi_flash mycomp
  4.                     )
复制代码
所有的include都有组件支持了,编译无错,运行后可以看到输出了 call by mycomp
  1. I (331) main_task: Calling app_main()
  2. Hello world!
  3. This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
  4. Minimum free heap size: 301172 bytes
  5. Restarting in 10 seconds...
  6. ......
  7. Restarting in 0 seconds...
  8. Restarting now.
  9. call by mycomp
  10. ets Jul 29 2019 12:21:46
  11. rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  12. .....
复制代码
8、如果本身的组件中不止一个源文件怎么办,方法是编辑组件根下的CMakeLists.txt,这里示例为components/mycomp/CMakeLists.txt,有两个方法可以组织源文件和头文件
  1. file(GLOB_RECURSE SOURCES *.c */*.c)
  2. set(include_dirs
  3.     ""
  4.     "include"
  5. )
  6. idf_component_register(SRCS ${SOURCES}
  7.                     INCLUDE_DIRS ${include_dirs})
  8. # idf_component_register(SRCS "mycomp.c" "mycomp2.c"
  9. #                     INCLUDE_DIRS "include")
复制代码
这里简单示例,mycomp2.c和mycomp.c功能差不多,最后在主程序func();背面加了行func2();
运行结果:
  1. I (331) main_task: Calling app_main()
  2. Hello world!
  3. This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.0, 2MB external flash
  4. Minimum free heap size: 301172 bytes
  5. Restarting in 10 seconds...
  6. ....
  7. Restarting in 0 seconds...
  8. Restarting now.
  9. call by mycomp
  10. call by mycomp---2
  11. ets Jul 29 2019 12:21:46
复制代码
9、小结
如果main模块编译出错,需要编辑同级CMakeLists.txt,如果组件编译出错,也要编辑同级CMakeLists.txt
如果需要,执行COMMAND->Full Clean

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4