ESP32 ESP-IDF 项目文件结构

概述

使用ESP-IDF开发时,项目特指一个目录,其中包含了构建可执行应用程序所需的全部文件和配置,以及其他支持型文件,例如分区表、数据/文件系统分区和引导程序。
ESP-IDF 并不是项目的一部分,它独立于项目,通过 IDF_PATH 环境变量(保存 esp-idf 目录的路径)链接到项目,从而将 IDF 框架与项目分离。

项目结构

- myProject/
    - CMakeLists.txt
    - sdkconfig
    - components/
        - component1/
            - CMakeLists.txt
            - Kconfig
            - src1.c
        - component2/
            - CMakeLists.txt
            - Kconfig
            - src1.c
            - include/
                - component2.h
    - main/
        - src1.c
        - src2.c
    - build/
  • 顶层项目 CMakeLists.txt 文件,这是 CMake 用于学习如何构建项目的主要文件,可以在这个文件中设置项目全局的 CMake 变量。顶层项目 CMakeLists.txt 文件会导入 esp-idf/tools/cmake/project.cmake 文件,由它负责实现构建系统的其余部分。该文件最后会设置项目的名称,并定义该项目

  • sdkconfig 项目配置文件,执行 idf.py menuconfig 时会创建或更新此文件,文件中保存了项目中所有组件(包括 ESP-IDF 本身)的配置信息。 sdkconfig 文件可能会也可能不会被添加到项目的源码管理系统中。

  • 可选的 component 目录中包含了项目的部分自定义组件,并不是每个项目都需要这种自定义组件,但它组件有助于构建可复用的代码或者导入第三方(不属于 ESP-IDF)的组件。

  • main 目录是一个特殊的 伪组件,包含项目本身的源代码。main 是默认名称,CMake 变量 COMPONENT_DIRS 默认包含此组件,但您可以修改此变量。或者,您也可以在顶层 CMakeLists.txt 中设置 EXTRA_COMPONENT_DIRS 变量以查找其他指定位置处的组件。

  • build 目录是存放构建输出的地方,如果没有此目录,idf.py 会自动创建。CMake 会配置项目,并在此目录下生成临时的构建文件。随后,在主构建进程的运行期间,该目录还会保存临时目标文件、库文件以及最终输出的二进制文件。此目录通常不会添加到项目的源码管理系统中,也不会随项目源码一同发布。

参考

hello-world的项目结构分析

- hello_world/
    - main/
        - CMakeLists.txt
        - component.mk
        - hello_world_main.c
    - .gdbinit.ci
    - CMakeLists.txt
    - loadable_elf_example_test.py
    - Makefile
    - README.md
    - sdkconfig
    - sdkconfig.ci

main 目录

CMakeLists.txt 文件

CMakeLists.txt内容

idf_component_register(SRCS "hello_world_main.c"
                    INCLUDE_DIRS "")

idf_component_register函数中SRC中包含所有的源文件,INCLUDE_DIRS中包含所有的头文件目录如果main中的文件结构这样

- main/
    - include/
        - hello_world.h
        - light_control.h
    - CMakeLists.txt
    - component.mk
    - hello_world_main.c
    - light_control.c

CMakeLists.txt中的内容应该修改成

idf_component_register(SRCS "hello_world_main.c"
                            "light_control.c"
                    INCLUDE_DIRS "include")

component.mk 文件

GUN Make中使用的文件,通过CMake构建时可以在CMakeLists.txt设置 COMPONENT_ADD_INCLUDEDIRS 和 COMPONENT_SRCDIRS 等变量将组件添加到编译过程中。

hello_world_main.c 文件

开发的源文件

.gdbinit.ci 文件

gdb初始化配置文件

CMakeLists.txt

CMakeLists.txt 内容

# 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.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello-world)
  • cmake_minimum_required(VERSION 3.5) 必须放在 CMakeLists.txt 文件的第一行,它会告诉 CMake 构建该项目所需要的最小版本号。ESP-IDF 支持 CMake 3.5 或更高的版本。

  • include($ENV{IDF_PATH}/tools/cmake/project.cmake) 会导入 CMake 的其余功能来完成配置项目、检索组件等任务。

  • project(hello-world) 会创建项目本身,并指定项目名称。该名称会作为最终输出的二进制文件的名字,即 hello-world.elf 和 hello-world.bin。每个 CMakeLists 文件只能定义一个项目。

Makefile

项目顶层 Makefile,该 Makefile 设置了 PROJECT_NAME 变量,还可以定义作用于整个项目的其它 make 变量(可选)。顶层 Makefile 会导入核心 Makefile 文件 $(IDF_PATH)/make/project.mk ,由它负责实现 ESP-IDF 构建系统的剩余部分。在CMake编译过中是不需要的。

README.md 文件

项目说明文件,markdown文本。

sdkconfig

它的功能和使用Make的KConfig文件类似,使用CMake编译时,从 sdkconfig 文件中加载项目配置信息,生成 sdkconfig.cmake 和 sdkconfig.h 文件,分别用在 CMake 和 C/C++ 中定义配置项。如果项目配置发生了更改,CMake 会自动重新运行,重新生成上述两个文件,接着重新配置项目。通过idf.py menuconfig可以修改配置项。

idf_component_register函数

idf_component_register函数在/tools/cmake/component.cmake中被定义。

# idf_component_register
#
# @brief Register a component to the build, creating component library targets etc.
#
# @param[in, optional] SRCS (multivalue) list of source files for the component
# @param[in, optional] SRC_DIRS (multivalue) list of source directories to look for source files
#                       in (.c, .cpp. .S); ignored when SRCS is specified.
# @param[in, optional] EXCLUDE_SRCS (multivalue) used to exclude source files for the specified
#                       SRC_DIRS
# @param[in, optional] INCLUDE_DIRS (multivalue) public include directories for the created component library
# @param[in, optional] PRIV_INCLUDE_DIRS (multivalue) private include directories for the created component library
# @param[in, optional] LDFRAGMENTS (multivalue) linker script fragments for the component
# @param[in, optional] REQUIRES (multivalue) publicly required components in terms of usage requirements
# @param[in, optional] PRIV_REQUIRES (multivalue) privately required components in terms of usage requirements
#                      or components only needed for functions/values defined in its project_include.cmake
# @param[in, optional] REQUIRED_IDF_TARGETS (multivalue) the list of IDF build targets that the component only supports
# @param[in, optional] EMBED_FILES (multivalue) list of binary files to embed with the component
# @param[in, optional] EMBED_TXTFILES (multivalue) list of text files to embed with the component
# @param[in, optional] KCONFIG (single value) override the default Kconfig
# @param[in, optional] KCONFIG_PROJBUILD (single value) override the default Kconfig
function(idf_component_register)
    set(options)
    set(single_value KCONFIG KCONFIG_PROJBUILD)
    set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
                    INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
                    PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
    cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})

    if(NOT __idf_component_context)
        message(FATAL_ERROR "Called idf_component_register from a non-component directory.")
    endif()

    __component_check_target()
    __component_add_sources(sources)

    # Add component manifest to the list of dependencies
    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${COMPONENT_DIR}/idf_component.yml")

    # Create the final target for the component. This target is the target that is
    # visible outside the build system.
    __component_get_target(component_target ${COMPONENT_ALIAS})
    __component_get_property(component_lib ${component_target} COMPONENT_LIB)

    # Use generator expression so that users can append/override flags even after call to
    # idf_build_process
    idf_build_get_property(include_directories INCLUDE_DIRECTORIES GENERATOR_EXPRESSION)
    idf_build_get_property(compile_options COMPILE_OPTIONS GENERATOR_EXPRESSION)
    idf_build_get_property(c_compile_options C_COMPILE_OPTIONS GENERATOR_EXPRESSION)
    idf_build_get_property(cxx_compile_options CXX_COMPILE_OPTIONS GENERATOR_EXPRESSION)
    idf_build_get_property(common_reqs ___COMPONENT_REQUIRES_COMMON)

    include_directories("${include_directories}")
    add_compile_options("${compile_options}")
    add_c_compile_options("${c_compile_options}")
    add_cxx_compile_options("${cxx_compile_options}")

    # Unfortunately add_definitions() does not support generator expressions. A new command
    # add_compile_definition() does but is only available on CMake 3.12 or newer. This uses
    # add_compile_options(), which can add any option as the workaround.
    #
    # TODO: Use add_compile_definitions() once minimum supported version is 3.12 or newer.
    idf_build_get_property(compile_definitions COMPILE_DEFINITIONS GENERATOR_EXPRESSION)
    add_compile_options("${compile_definitions}")

    if(common_reqs) # check whether common_reqs exists, this may be the case in minimalistic host unit test builds
        list(REMOVE_ITEM common_reqs ${component_lib})
    endif()
    link_libraries(${common_reqs})

    idf_build_get_property(config_dir CONFIG_DIR)

    # The contents of 'sources' is from the __component_add_sources call
    if(sources OR __EMBED_FILES OR __EMBED_TXTFILES)
        add_library(${component_lib} STATIC ${sources})
        __component_set_property(${component_target} COMPONENT_TYPE LIBRARY)
        __component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" PUBLIC)
        __component_add_include_dirs(${component_lib} "${__PRIV_INCLUDE_DIRS}" PRIVATE)
        __component_add_include_dirs(${component_lib} "${config_dir}" PUBLIC)
        set_target_properties(${component_lib} PROPERTIES OUTPUT_NAME ${COMPONENT_NAME} LINKER_LANGUAGE C)
        __ldgen_add_component(${component_lib})
    else()
        add_library(${component_lib} INTERFACE)
        __component_set_property(${component_target} COMPONENT_TYPE CONFIG_ONLY)
        __component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" INTERFACE)
        __component_add_include_dirs(${component_lib} "${config_dir}" INTERFACE)
    endif()

    # Alias the static/interface library created for linking to external targets.
    # The alias is the <prefix>::<component name> name.
    __component_get_property(component_alias ${component_target} COMPONENT_ALIAS)
    add_library(${component_alias} ALIAS ${component_lib})

    # Perform other component processing, such as embedding binaries and processing linker
    # script fragments
    foreach(file ${__EMBED_FILES})
        target_add_binary_data(${component_lib} "${file}" "BINARY")
    endforeach()

    foreach(file ${__EMBED_TXTFILES})
        target_add_binary_data(${component_lib} "${file}" "TEXT")
    endforeach()

    if(__LDFRAGMENTS)
        __ldgen_add_fragment_files("${__LDFRAGMENTS}")
    endif()

    # Set dependencies
    __component_set_all_dependencies()

    # Make the COMPONENT_LIB variable available in the component CMakeLists.txt
    set(COMPONENT_LIB ${component_lib} PARENT_SCOPE)
    # COMPONENT_TARGET is deprecated but is made available with same function
    # as COMPONENT_LIB for compatibility.
    set(COMPONENT_TARGET ${component_lib} PARENT_SCOPE)

    __component_set_properties()
endfunction()
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 您好,ESP32是一款非常流行的微控制器,而ESP-IDF是它的开发框架。如果您想学习ESP-IDF的使用,可以参考以下步骤: 1. 准备开发环境:您需要安装ESP-IDF的开发环境,包括ESP-IDF工具链、Python、Git等。具体安装步骤可以参考ESP-IDF官方文档。 2. 学习ESP-IDF的基本概念:ESP-IDF是一个基于FreeRTOS的开发框架,它提供了许多API和组件,可以方便地开发ESP32的应用程序。您需要学习ESP-IDF的基本概念,包括任务、队列、定时器、中断等。 3. 编写应用程序:您可以使用ESP-IDF提供的示例程序作为参考,编写自己的应用程序。ESP-IDF提供了许多组件,包括WiFi、蓝牙、SPI、I2C等,您可以根据自己的需求选择相应的组件。 4. 调试和测试:在编写应用程序的过程中,您需要进行调试和测试。ESP-IDF提供了许多调试工具,包括GDB调试器、串口调试工具等。 总之,学习ESP-IDF需要一定的编程基础和硬件知识,但是它可以帮助您快速开发ESP32的应用程序。希望以上信息能对您有所帮助。 ### 回答2: ESP32Espressif Systems推出的一款支持Wi-Fi和蓝牙功能的微型芯片,由于其出色的性能和低成本,成为了物联网领域中广泛使用的芯片之一。ESP-IDFESP32的官方开发框架,为用户提供一套完善的开发工具和资源,以便于用户轻松开发出稳定和高效的应用程序。 ESP-IDF教学主要包括以下方面内容: 1.ESP-IDF开发环境搭建。 ESP-IDF适用于Linux、Windows和macOS系统,并且可以配合使用命令行、VS Code等工具,极大地方便了开发者的使用。搭建好开发环境对于后续的代码编写和调试至关重要。 2.ESP-IDF应用程序架构。 ESP-IDF的应用程序架构分为三层,包括API层、组件层、和应用层,这三个层次需要开发者熟练掌握。 3.ESP-IDF API函数使用。 ESP-IDF具有众多的API函数,可以实现GPIO控制、Wi-Fi和蓝牙通信、定时器、SPI和I2C接口等功能,学习这些函数的使用方法可以方便开发者快速搭建出各种应用程序。 4.ESP-IDF组件使用。 ESP-IDF还提供了多种组件,包括操作系统、TCP/IP协议栈、驱动程序、框架和示例,这些组件可以极大地提升开发效率,缩短开发周期。 5.ESP-IDF应用程序调试。 ESP-IDF提供了集成式调试器和自动化测试框架,可以帮助开发者快速排查问题并进行黑盒测试。 总结来说,学习ESP-IDF需要开发者具备一定的编程经验和开发基础,同时需要深入了解ESP32的基础知识和硬件架构,熟练使用ESP-IDF可以为开发者带来高效、稳定、可靠的应用程序开发体验。 ### 回答3: ESP32是一款非常流行的嵌入式设备,其配备的ESP-IDF系统是其开发的重要工具。ESP-IDF是一个开源的开发框架,可帮助开发者在ESP32中进行应用程序的高效开发。ESP-IDF使得开发人员可以使用C语言开发的单片机应用程序,从而可以控制硬件和访问数字传感器等设备。 在学习ESP-IDF教程之前,需要了解以下几个基本概念: 1. IIS:Integrated Development Environment (集成开发环境),用于编写、构建和调试ESP32应用程序。 2. SDK:Software Development Kit (软件开发工具包),包括必要的C库和头文件等,使开发人员可以编写ESP32应用程序。 3. IDF:IoT Development Framework (物联网开发框架),是ESP32的开发框架,包括了一系列API和头文件,可用于开发应用程序。 开始ESP-IDF教程的步骤如下: 1. 下载和安装基于ESP-IDF的IIS。可从ESP-IDF官网下载最新版本:https://docs.espressif.com/projects/esp-idf/en/latest/get-started/ 2. 创建ESP32项目。在IIS中创建一个ESP32项目,或使用ESP-IDF提供的模板项目。IIS可以自动生成项目文件。 3. 编译和烧写固件。编译ESP32应用程序并将其烧写到ESP32设备中。 4. 运行ESP32应用程序。使用IIS的调试器,在ESP32设备上启动ESP32应用程序。 ESP-IDF教程是ESP32开发的重要组成部分,可为开发人员提供快速上手和了解ESP32开发的指南。对于想要进入ESP32开发领域的初学者,建议先学会C语言基础,并熟悉ESP32开发板和其硬件接口。在进一步了解和掌握ESP-IDF教程之前,先掌握ESP32的基本知识,将有助于更好地理解ESP-IDF的学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Engineer-Jaylen_Sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值