基于ESP-IDF环境的ESP32-C3开发之工程分析
为搞清ESP32的代码框架,从观看入门工程"hello world"来分析源码,然后自建工程,并逐步添加自己的组件,达到便于移植和快速开发的目的。
以下为自建的文件夹及文件描述
项目构成
结构说明
demo
|---main
| |---main.c //主函数
| |---CMakeLists
| |---component.mk
|---CMakeLists
|---components
| |---button //boot按键引脚扫描例程
| | |---bsp_button.c
| | |---CMakeLists
| | |---include
| | | |---bsp_button.h
| |---confWIFI //smartconfig,手机APP配网例程,需要用到乐鑫的ESP_TOUCH.app
| | |---bsp_Fi.c
| | |---CMakeLists
| | |---include
| | | |---bsp_Fi.h
| |---led //板载LED驱动例程
| | |---bsp_led.c
| | |---CMakeLists
| | |---include
| | | |---bsp_led.h
| |---wifi //SoftAP配网例程
| | |---bsp_wifi.c
| | |---CMakeLists
| | |---include
| | | |---bsp_wifi.h
| |---blu_fi //蓝牙配网例程,需要用到乐鑫的EspBluFi.app
| | |---bsp_blu_fi.c
| | |---CMakeLists
| | |---include
| | | |---bsp_blu_fi.h
demo工程里面包含了几类,分别为:
main文件夹
main文件夹下面包含了main.c,CMakelists.txt,componet.mk,如果有必要,也可在文件夹下面添加include文件夹
main.c为一些基础类的函数调用,在这里就不多说了。
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_wifi.h"
#include "bsp_led.h"
#include "bsp_button.h"
#include "bsp_wifi.h"
#include "bsp_fi.h"
#include "driver/gpio.h"
// #include "esp_bt.h"
static const char *TAG = "smartconfig";
void app_main(void)
{
bsp_led_init();
led5_off();
bsp_btn_init();
// connect_wifi_test();
smartconfig_test();
while (1)
{
led4_on();
vTaskDelay(1000 / portTICK_PERIOD_MS);
led4_off();
vTaskDelay(1000 / portTICK_PERIOD_MS);
if ((btn_sta()) == 0)
{
led5_on();
ESP_LOGI(TAG, "clear wifi config data");
esp_wifi_restore();
esp_restart();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
else
{
led5_off();
}
}
}
CMakelists.txt为描述main文件夹下面所用到的.c以及.h
项目中main文件夹下面就一个main.c,因此CMakelists.txt内容如下
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "")
component.mk笔者也不知道干啥的,里面的内容也是全屏蔽的状态,就不在此多说了。
顶层CMakelists.txt
顶层CMakelists.txt描述了cmake的版本,componets路径以及包含cmake的文件
内容如下
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
#cmake版本
cmake_minimum_required(VERSION 3.5)
#指定需要用到componets的路径
set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}./components/led
${CMAKE_CURRENT_LIST_DIR}./components/button
${CMAKE_CURRENT_LIST_DIR}./components/wifi
${CMAKE_CURRENT_LIST_DIR}./components/confFiFi
${CMAKE_CURRENT_LIST_DIR}./components/blu_fi
)
#包含cmake文件
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(demo)
components
该文件夹下面的内容均为用户组件,为了规范用户组件,达到便于移植的目的,均采用以下结构,以button为例
| |---button //button文件夹
| | |---bsp_button.c //button.c
| | |---CMakeLists //CMakelists
| | |---include //include文件夹
| | | |---bsp_button.h //button.h
这里着重描述一下CMakelistst文件,该文件指定了组件的构成,以及所用到的用户组件和系统组件,内容如下
idf_component_register(SRCS "bsp_blu_fi.c"
INCLUDE_DIRS "include"
REQUIRES nvs_flash
esp_event
esp_wifi
wpa_supplicant
bt
)
# PRIV_REQUIRES 该参数指定对其他自定义组件的依赖,即私有依赖项
# REQUIRES 该参数指定对系统组件的依赖,即公共依赖项
#
# 假设 esp_wpa2.h 在components/wpa_supplicant/esp_supplicant/include下,则
# REQUIRES里面只需要添加 wpa_supplicant即可
PRIV_REQUIRES参数指定了用户组件的依赖路径,假设button.c里面使用到了用户自建的组件adc.h
则需要在该参数里面指定adc.h的路径
REQUIRES参数制定了系统组件的路径,假设button.c使用了系统组件esp_wifi.h,则需要指定eso_wifi.h所在的路径,否则编译的时候会提示"No such file or directory",导致项目编译失败。