在以前讲解esp-idf的文件结构时,曾经讲过component是esp-idf集成的功能块,这篇文章就来说解下,如何在esp-idf 中添加本身的component。php
这里以linux下开发为例,这里在本身的的工程中添加, 首先在本身的工程下建立components文件夹(这个名称不能错)(或者直接用esp-idf下的components文件夹),而后在components的文件夹中建立my_com(这个文件夹可根据需求填写)文件夹linux
并在其下建立include的子目录,include目录下主要用来存放组件所需的头文件。express
若是你们对linux开发比较熟悉的话,那么确定对内核编译须要的.config文件不陌生,在.config文件中,咱们发现有的模块被编译进了内核,有的只是生成了一个module。这中间,咱们如何让内核发现咱们编写的模块呢,这就须要在Kconfig中进行说明。至于如何生成模块,那么就须要利用Makefile告诉编译器,怎么编译生成这个模块。因此每一个component目录下都须要有make文件。网络
在my_com目录下建立一个名为component.mk(名字不能更改)的make文件,并在文件中输入COMPONENT_ADD_INCLUDEDIRS:=include,这表示逐渐源文件所需的文件都到component.mk所在的目录的include文件夹下找,别的组件若是引用本组件所包含的头文件,也会到这个include文件夹下找。(对于Makefile,这里有篇基础教程讲解的不错,能够看看http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=16880)app
在my_comp 目录下建立Kconfig文件,复制以下内容到该文件less
menu "MY_COM" config MY_COM_ENABLE bool "Enable my_com" default "y" endmenu
其中第一行表示在menucom中添加MY_COM这一选项,这是向用户展现的用户名,能够本身定义。eclipse
第二行config命令表示该配置文件会在sdkconfig中生成CONFIG_MY_COM_ENABLE的宏定义。函数
第三行表示在menuconfig中进入组件选项后会显示一个bool类型的选项,即yes or no。测试
第四行表示进入选项的默认状态"y"表示默认宣红,“n”表示非选中状态。ui
第五行表示menu命令结束,关于Kconfig的更多语法和功能,你们能够去网络上查询,这里再也不赘述。
在my_com目录下建立my_com.c文件,复制如下代码到该文件中
#include <stdio.h>
#include <my_com.h>
void my_com_test()
{
#if(CONFIG_MY_COM_ENABLE==y)
printf("my_com is configed\n");
#else
printf("not config\n");
#endif
}
这是一个测试程序,用来测试menuconfig中的my_com组件是否已经配置成功。
在include文件夹中添加my_com.h头文件,头文件中对my_com_test函数进行申明,代码以下
#ifndef _MY_COM_H_ #define _MY_COM_H_ void my_com_test(void); #endif
打开hello_world.c文件,将文件修改成以下所示
/* Hello World Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_spi_flash.h" #include "my_com.h" void app_main() { printf("Hello world!\n"); /* Print chip information */ esp_chip_info_t chip_info; esp_chip_info(&chip_info); printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ", chip_info.cores, (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); printf("silicon revision %d, ", chip_info.revision); printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024), (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); for (int i = 10; i >= 0; i--) { printf("Restarting in %d seconds...\n", i); vTaskDelay(1000 / portTICK_PERIOD_MS); } printf("Restarting now.\n"); my_com_test(); fflush(stdout); esp_restart(); }
此时到hello_world 的工程目录下,运行指令
cd ~/esp32/workspace/hello_world/hello_world
make menuconfig
能够打开以下配置窗口,选择component config
选择进入以后能够看到一个MY_COM选项
选择进入以后有一个[*]Enable my_com(进入默认状态是勾选的,这和咱们编写的Kconfig文件是同样的),咱们能够经过空格键选择是否用*勾选,
设定完成后,选择save而后exit退出,经过make flash 指令将程序烧写到esp32开发板中,经过minicom查看输出。观察到的输出以下所示
这说明咱们本身建立的组件my_com 已经添加成功了。
显而意见,添加了component后,若是要用eclipse打开工程,还须要将添加的component的路径添加到eclispe中,添加方法以下
至此,一个本身建立的component就添加完成了