项目场景:
硬件:ESP32-LyraT-Mini V1.2开发板,使用的是ESP32-WROVER-E 模组。
程序:基于soft_i2c示例程序修改协议内容实现与TM1640通信测试
问题描述
编译失败,"full clean"后重新编译依旧失败。没有语法错误,错误提示如下:
[922/924] Linking CXX executable soft_i2c.elf
FAILED: soft_i2c.elf
cmd.exe /C "cd . && E:\.espressif\tools\xtensa-esp-elf\esp-13.2.0_20230928\xtensa-esp-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address -fno-builtin-memcpy -fno-builtin-memset -fno-builtin-bzero -fno-builtin-stpcpy -fno-builtin-strncpy -Wl,--cref -Wl,--defsym=IDF_TARGET_ESP32=0 -Wl,--Map=D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/build/soft_i2c.map -Wl,--no-warn-rwx-segments -fno-rtti -fno-lto -Wl,--gc-sections -Wl,--warn-common -T esp32.peripherals.ld -T esp32.rom.ld -T esp32.rom.api.ld -T esp32.rom.libgcc.ld -T esp32.rom.newlib-data.ld -T esp32.rom.syscalls.ld -T esp32.rom.newlib-funcs.ld -T memory.ld -T sections.ld @CMakeFiles\soft_i2c.elf.rsp -o soft_i2c.elf && cd ."
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj):(.literal.set_din+0x0): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj):(.literal.soft_2wire_master_new+0x14): undefined reference to `dedic_gpio_new_bundle'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj):(.literal.soft_2wire_master_del+0x8): undefined reference to `dedic_gpio_del_bundle'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `set_din':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:180:(.text.set_din+0xa): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `set_sclk':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:174:(.text.emulate_start+0xe): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `set_din':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:180:(.text.emulate_start+0x22): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:180:(.text.emulate_start+0x36): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `set_sclk':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:174:(.text.emulate_write_byte+0x1e): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj):D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:174: more undefined references to `dedic_gpio_bundle_write' follow
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `soft_2wire_master_new':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:83:(.text.soft_2wire_master_new+0xf4): undefined reference to `dedic_gpio_new_bundle'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:86:(.text.soft_2wire_master_new+0x117): undefined reference to `dedic_gpio_bundle_write'
E:/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/soft_2wire_master/libsoft_2wire_master.a(soft_2wire_master.c.obj): in function `soft_2wire_master_del':
D:/WillRush/ESP32_AIToy_Project/Project/soft_i2c/components/soft_2wire_master/soft_2wire_master.c:108:(.text.soft_2wire_master_del+0x2b): undefined reference to `dedic_gpio_del_bundle'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
原因分析:
从错误提示上看应该是"soft_2wire_master.c"中没有包含相关头文件。实际上程序中是有包含头文件。
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_check.h"
#include "driver/dedic_gpio.h"
#include "driver/gpio.h"
#include "soft_2wire_master.h"
是编译时文件没有链接成功么?
该程序是基于示例程序soft_i2c程序生成、修改的。重新基于soft_i2c生成项目,full clean后重新编译存在相同问题。
查了资料可能原因:
1、dedic_gpio_bundle_write等函数为static的类型,无法被外部调用;
"ctrl+左键,单击出问题的函数,发现其并未定义为static类型,该原因排除"
2、未链接到对应的.c文件;
➽ 从报错内容看,错误来源soft_2wire_master.c文件中,对于dedic_gpio.h文件的使用有结构体类型和函数。跟函数相关的调用全部报错,但是并没有报错结构体类型。如果注释掉dedic_gpio.h中的结构体类型定义,编译则直接报错unknown type name。至少说明了.h文件确实是参与编译了的
➽ "组件文件夹下的CMakelists文件中 --PRIV_REQUIRES driver-- 将该语句去掉编译时直接报错,包含的头文件找不到driver,基本验证了至少链接时是链接了driver文件的 "
➽ .h包含了,与.c文件相关的函数调用全部报错,有可能是.c文件没有参与参与编译
➽ 发现问题,项目中build文件夹->esp-idf->driver->gpio文件夹下面只生成了3个.obj文件,并没有包含dedic_gpio的。找到esp-idf文件夹->driver文件夹,打开CMakeLists文件,搜索"dedic"发现有一个相关的宏定义 --CONFIG_SOC_DEDICATED_GPIO_SUPPORTED-- 项目中全局搜索该宏定义,并未发现
➽ 如果直接在build->config->sdkconfig.h中增加#define CONFIG_SOC_DEDICATED_GPIO_SUPPORTED 1或者在sdkconfig.cmake中手动增加,编译后依旧报错,且手动增加的部分会消失,build目录是自动生成的。而main->Kconfig.projbuild内用户可自定义宏定义(配置页面中sample相关配置就是在该处实现的)但CONFIG_SOC开头的是默认规格宏,不应该在此自行定义。
➽ 在sdkconfig.default中自行增加CONFIG_SOC_DEDICATED_GPIO_SUPPORTED=y 可手动修改保存,但编译依旧报相同错误
参考源:https://www.cnblogs.com/lifan3a/articles/18120965
➽ 换个思路,既找不到宏定义,又不能添加宏定义,直接取消宏定义判断,每次都编译"dedic_gpio.c"文件,试试呢。 打开esp-idf文件夹->driver文件夹->CMakeLists,直接在一直编译的源文件中加入"gpio/dedic_gpio.c",编译时报错如下图所示。
E:/esp/esp-idf/components/driver/gpio/dedic_gpio.c:22:10: fatal error: hal/dedic_gpio_cpu_ll.h: No such file or directory
22 | #include "hal/dedic_gpio_cpu_ll.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~
实际查看E:\esp\esp-idf\components\hal\include\hal文件夹中确实没有dedic_gpio_cpu_ll.h。在整个hal文件夹中搜索dedic,发现与芯片型号有关。当前测试使用的ESP32-LyraT-Mini V1.2开发板是ESP32-WROVER-E 模组,芯片是ESP32-D0WD-V3 和 ESP32-D0WDR2-V3,也就是压根不支持该功能。
3、因为宏定义或者其他问题,导致dedic_gpio_bundle_write等函数未编译,导致调用时提示未定义;
在第2点中发现该问题
解决方案:
最终,无解决方案。发现问题:调用的函数当前开发板所用芯片不支持…针对出问题的程序段,用其他方式重新编写。