一、点亮LED业务代码
1.1.创建目录
编写业务时,务必先在./applications/BearPi/BearPi-HM_Micro/samples路径下新建一个目录(或一套目录结构),用于存放业务源码文件.
在samples文件夹下增加my_led_app文件夹,并新建BUILD.gn和my_led_app.c两个文件
1.2.编写业务代码
在my_led_app.c中添加以下业务代码
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include "hdf_sbuf.h"
#include "hdf_io_service_if.h"
#define LED_WRITE_READ 1
#define LED_SERVICE "hdf_led"
static int SendEvent(struct HdfIoService *serv, uint8_t eventData)
{
int ret = 0;
struct HdfSBuf *data = HdfSBufObtainDefaultSize();
if (data == NULL)
{
printf("fail to obtain sbuf data!\r\n");
return 1;
}
struct HdfSBuf *reply = HdfSBufObtainDefaultSize();
if (reply == NULL)
{
printf("fail to obtain sbuf reply!\r\n");
ret = HDF_DEV_ERR_NO_MEMORY;
goto out;
}
/* 写入数据 */
if (!HdfSbufWriteUint8(data, eventData))
{
printf("fail to write sbuf!\r\n");
ret = HDF_FAILURE;
goto out;
}
/* 通过Dispatch发送到驱动 */
ret = serv->dispatcher->Dispatch(&serv->object, LED_WRITE_READ, data, reply);
if (ret != HDF_SUCCESS)
{
printf("fail to send service call!\r\n");
goto out;
}
int replyData = 0;
/* 读取驱动的回复数据 */
if (!HdfSbufReadInt32(reply, &replyData))
{
printf("fail to get service call reply!\r\n");
ret = HDF_ERR_INVALID_OBJECT;
goto out;
}
printf("\r\nGet reply is: %d\r\n", replyData);
out:
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
int main(int argc, char **argv)
{
int i;
/* 获取服务 */
struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE);
if (serv == NULL)
{
printf("fail to get service %s!\r\n", LED_SERVICE);
return HDF_FAILURE;
}
for (i=0; i < argc; i++)
{
printf("\r\nArgument %d is %s.\r\n", i, argv[i]);
}
SendEvent(serv, atoi(argv[1]));
HdfIoServiceRecycle(serv);
printf("exit");
return HDF_SUCCESS;
}
1.3.编写将构建业务代码的BUILD.gn文件
BUILD.gn文件由三部分内容(目标、源文件、头文件路径)构成,需由开发者完成填写。以my_led_app为例,需要创建./applications/BearPi/BearPi-HM_Micro/samples/my_led_app/BUILD.gn,并完如下配置
import("//build/lite/config/component/lite_component.gni")
HDF_FRAMEWORKS = "//drivers/framework"
executable("led_lib") {
output_name = "my_led"
sources = [
"my_led_app.c",
]
include_dirs = [
"$HDF_FRAMEWORKS/ability/sbuf/include",
"$HDF_FRAMEWORKS/core/shared/include",
"$HDF_FRAMEWORKS/core/host/include",
"$HDF_FRAMEWORKS/core/master/include",
"$HDF_FRAMEWORKS/include/core",
"$HDF_FRAMEWORKS/include/utils",
"$HDF_FRAMEWORKS/utils/include",
"$HDF_FRAMEWORKS/include/osal",
"//drivers/adapter/uhdf/posix/include",
"//third_party/bounds_checking_function/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
]
deps = [
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
"//drivers/adapter/uhdf/manager:hdf_core",
"//drivers/adapter/uhdf/posix:hdf_posix_osal",
]
}
lite_component("my_led_app") {
features = [
":led_lib",
]
}
- 首先导入 gni 组件,将源码my_led_app.c编译成led_lib库文件
- 输出的可执行文件名称由 output_name 定义为my_led
- include_dirs 里面加入my_led_app.c里面需要用到的.h的头文件路径
- deps 里面加入所依赖的库。
- 然后将led_lib打包成 lite_component,命名为my_led_app组件。
1.4.添加新组件
修改文件build/lite/components/applications.json,添加组件my_sample的配置
{
"component": "my_sample",
"description": "my samples",
"optional": "true",
"dirs": [
"applications/BearPi/BearPi-HM_Micro/samples/my_first_app",
"applications/BearPi/BearPi-HM_Micro/samples/my_led_app"
],
"targets": [
"//applications/BearPi/BearPi-HM_Micro/samples/my_first_app:my_app",
"//applications/BearPi/BearPi-HM_Micro/samples/my_led_app:my_led_app"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_a" ],
"features": [],
"deps": {
"components": [],
"third_party": [ ]
}
},
1.5.修改单板配置文件
修改文件vendor/bearpi/bearpi_hm_micro/config.json,新增my_sample组件的条目
{
"subsystem": "applications",
"components": [
{ "component": "my_sample", "features":[] },
{ "component": "bearpi_sample_app", "features":[] },
{ "component": "bearpi_screensaver_app", "features":[] }
]
},
二、编译
在项目根目录下执行hb set 设置开发板,只有一个,回车即可
执行编译命令
hb build -t notest --tee -f
等待编译完成后,屏幕出现:build success字样,说明编译成功。
当编译完后,可以直接查看到最终编译的固件,具体路径在: \project\bearpi-hm_micro_small\out\bearpi-hm_micro\bearpi-hm_micro
文件夹结构说明
- OHOS_Image.stm32:系统镜像文件
- rootfs_vfat.img:根文件系统
- userfs_vfat.img:用户文件系统
执行以下三条指令将以上三个文件复制到applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/下,以便后续烧录系统使用
cp out/bearpi_hm_micro/bearpi_hm_micro/OHOS_Image.stm32 applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/
cp out/bearpi_hm_micro/bearpi_hm_micro/rootfs_vfat.img applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/
cp out/bearpi_hm_micro/bearpi_hm_micro/userfs_vfat.img applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/
三、总结
至此,完整的项目工程就开发好了,下一步就是烧录和运行了。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END