前言
概述
前面的文章只是涉及到内核态驱动程序的实现,并未涉及到用户态应用程序,以及用户态应用程序与内核态驱动程序之间的数据交互流程,在本示例将演示如何在开发板上运行一个控制LED灯的程序,达到能关闭灯、开启灯以及翻转灯的状态。希望通过本教程的学习,开发者能掌握如何开发一个设备驱动,以及如何在应用层调用驱动。
开发环境
- 硬件平台:润和AI_Camera_Hi3516DV300开发板
- 源码:OpenHarmony 3.1 release
- 环境:Windows+Ubuntu混合开发环境
- 工具:Deveco Device Tool
- 产品:ipcamera_hispark_taurus LiteOS内核OpenHarmony小型系统
参考文章
以下三篇是一些环境的准备以及流程的解析
【FFH】Hi3516DV300 OpenHarmony3.1环境配置及烧录
【FFH】DevEco Device Tool:HDF框架一键生成!
本次编写点亮LED灯程序参考的是BearPi-HM_Micro开发板的示例进行改写实现的在HI3516DV300上的适配。
也参考了大佬的案例代码,也给了很多启发。
任务流程
点亮LED主要包含以下任务:
1、生成HDF框架HDF框架由Deveco Device Tool自动生成,相比传统开发模式,可以省去驱动编译文件实现这一步骤,简化hcs配置文件修改以及驱动代码编写。当然,目前只支持Hi3516DV300开发板,如果不支持的话可以参考官方文档自己手动进行编写,配合参考样例代码食用更佳(参考文章已给出)。
2、LED驱动代码开发
- 编写驱动代码
- 编写驱动配置文件
3、点亮LED业务代码开发
- 编写业务代码
- 编写业务代码编译文件
一、生成HDF驱动框架
打开Deveco Device Tool插件选择HDF并选择好产品,点击+号添加驱动模块,命名为led。
二、LED驱动代码开发
1、确定目录结构驱动代码位于drivers/framework/model/led/driver目录下。
.
└── drivers
└── framework
└── model
└─── led
└── driver
└── led_driver.c
驱动编译文件位于drivers/adapter/khdf/liteos/model/led目录下。
.
└── drivers
└── adapter
└── khdf
└──liteos
└──model
└──led
├── BUILD.gn
├── Kconfig
└── Makefile
板级配置入口文件hdf.hcs位于vendor/hisilicon/hispark_taurus/hdf_config目录下,hcs驱动设备描述位于device_info/device_info.hcs,在hdf_config目录下新建led文件夹,并创建私有驱动配置文件led_config.hcs。
.
└── vendor
└── hisilicon
└── hispark_taurus
└──hdf_config
├── hdf.hcs
├── device_info
│ └── device_info.hcs
├── led
└── led_config.hcs
2、编写驱动代码点击c/c++即可快速跳转到驱动代码目录文件下。
修改驱动代码如下:
#include "hdf_device_desc.h"
#include "hdf_log.h"
#include "device_resource_if.h"
#include "gpio_if.h"
#define HDF_LOG_TAG led_driver // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签
#define LED_WRITE_READ 1 // 读写操作码1
//0表示关闭,1表示开启,2表示翻转
enum LedOps {
LED_OFF,
LED_ON,
LED_TOGGLE,
};
struct Hi3516Led {
uint32_t gpioNum;
};
static struct Hi3516Led g_Hi3516Led;
uint8_t status = 0;
// Dispatch是用来处理用户态发下来的消息
int32_t LedDriverDispatch(struct HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply)
{
uint8_t contrl;
HDF_LOGE("Led driver dispatch");
if (client == NULL || client->device == NULL)
{
HDF_LOGE("Led driver device is NULL");
return HDF_ERR_INVALID_OBJECT;
}
switch (cmdCode)
{
/* 接收到用户态发来的LED_WRITE_READ命令 */
case LED_WRITE_READ:
/* 读取data里的数据,赋值给contrl */
HdfSbufReadUint8(data,&contrl);
switch (contrl)
{
/* 开灯 */
case LED_ON:
GpioWrite(g_Hi3516Led.gpioNum, GPIO_VAL_HIGH);
status = 1;
break;
/* 关灯 */
case LED_OFF:
GpioWrite(g_Hi3516Led.gpioNum, GPIO_VAL_LOW);
status = 0;
break;
/* 状态翻转 */
case LED_TOGGLE:
if(status == 0)
{
GpioWrite(g_Hi3516Led.gpioNum, GPIO_VAL_HIGH);
status = 1;
}
else
{
GpioWrite(g_Hi3516Led.gpioNum, GPIO_VAL_LOW);
status = 0;
}
break;
default:
break;
}
/* 把LED的状态值写入reply, 可被带至用户程序 */
if (!HdfSbufWriteInt32(reply, status))
{
HDF_LOGE("replay is fail");
return HDF_FAILURE;
}
break;
default:
break;
}
return HDF_SUCCESS;
}
// 读取驱动私有配置
static int32_t Hi3516LedReadDrs(struct Hi3516Led *led, const struct DeviceResourceNode *node)
{
int32_t ret;
struct DeviceResourceIface *drsOps = NULL;
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetUint32 == NULL) {
HDF_LOGE("%s: invalid drs ops!", __func__);
return HDF_FAILURE;
}
/* 读取led.hcs里面led_gpio_num的值 */
ret = drsOps->GetUint32(node, "led_gpio_num", &led->gpioNum, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read led gpio num fail!", __func__);
return ret;
}
return HDF_SUCCESS;
}
//驱动对外提供的服务能力,将相关的服务接口绑定到HDF框架
int32_t HdfLedDriverBind(struct HdfDeviceObject *deviceObject)
{
if (deviceObject == NULL)
{
HDF_LOGE("Led driver bind failed!");
return HDF_ERR_INVALID_OBJECT;
}
static struct IDeviceIoService ledDriver = {
.Dispatch = LedDriverDispatch,
};
deviceObject->service = (struct IDeviceIoService *)(&ledDriver);
HDF_LOGD("Led driver bind success");
return HDF_SUCCESS;
}
// 驱动自身业务初始的接口
int32_t HdfLedDriverInit(struct HdfDeviceObject *device)
{
struct Hi3516Led *led = &g_Hi3516Led;
int32_t ret;
if (device == NULL || device->property == NULL) {
HDF_LOGE("%s: device or property NULL!", __func__);
return HDF_ERR_INVALID_OBJECT;
}
/* 读取hcs私有属性值 */
ret = Hi3516LedReadDrs(led, device->property);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get led device resource fail:%d", __func__, ret);
return ret;
}
/* 将GPIO管脚配置为输出 */
ret = GpioSetDir(led->gpioNum, GPIO_DIR_OUT);
if (ret != 0)
{
HDF_LOGE("GpioSerDir: failed, ret %d\n", ret);
return ret;
}
HDF_LOGD("Led driver Init success");
return HDF_SUCCESS;
}
// 驱动资源释放的接口
void HdfLedDriverRelease(struct HdfDeviceObject *deviceObject)
{
if (deviceObject == NULL)
{
HDF_LOGE("Led driver release failed!");
return;
}
HDF_LOGD("Led driver release success");
return;
}
// 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量
struct HdfDriverEntry g_ledDriverEntry = {
.moduleVersion = 1,
.moduleName = "led_driver",//必须和device_info.hcs文件定义的modeleNmae一样
.Bind = HdfLedDriverBind,
.Init = HdfLedDriverInit,
.Release = HdfLedDriverRelease,
};
// 调用HDF_INIT将驱动入口注册到HDF框架中
HDF_INIT(g_ledDriverEntry);
3、编写驱动配置文件。
(1)修改驱动设备描述点击hcs即可快速跳转到驱动设备描述文件下:
添加驱动私有数据匹配的关键字,用于获取私有配置信息。
led :: host{
hostName = "led_host"; // Host name. The host node is used to store a certain type of drivers.
priority = 100; //Host startup priority (0-200). A larger value indicates a lower priority. The default value 100 is recommended. If the priorities are the same, the host loading sequence is random.
device_led :: device { // Device node of sample
device0 :: deviceNode { // DeviceNode of the sample driver
policy = 2; // Driver service release policy. For details, see section Driver Service Management.
priority= 100; // Driver startup priority (0-200). A larger value indicates a lower priority. The default value 100 is recommended. If the priorities are the same, the device loading sequence is random.
preload = 0; // On-demand loading of the driver. For details, see "NOTE" at the end of this section.
permission = 0664; // Permission for the driver to create device nodes.
moduleName = "led_driver"; // Driver name. The value of this field must be the same as the value of moduleName in the driver entry structure.
serviceName = "led_service"; // Name of the service released by the driver. The name must be unique.
deviceMatchAttr = "led_config"; // Keyword matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver.
(2)编写驱动私有配置信息
在vendor/hisilicon/hispark_taurus/hdf_config/led/led_config.hcs中添加LED私有配置描述。Hi3516DV300的绿色LED引脚号为19,设置led_gpio_num = 19,可以修改led_gpio_num的值来改变要操作的gpio,通过私有配置的好处是使驱动能够更好的适配各种设备以及开发板。
root {
LedDriverConfig {
led_gpio_num = 19;
match_attr = "led_config"; //该字段的值必须和device_info.hcs中的deviceMatchAttr值一致
}
}
配置信息定义之后,需要将该配置文件添加到板级配置入口文件vendor/hisilicon/hispark_taurus/hdf_config/hdf.hcs,示例如下:
#include "led/led_config.hcs"
小结
- device_info.hcs文件中的moduleName必须要和驱动文件中的moduleName字段匹配,这样驱动才会加载起来。
- device_info.hcs文件中的deviceMatchAttr的字段必须和私有配置文件中led_config.hcs的match_attr的字段匹配,这样私有配置才能生效。
- device_info.hcs文件中的serviceName的字段需和业务代码获取服务用到的函数的serviceName参数匹配,这样用户态才能成功获取到服务。用户程序是无法直接访问驱动的,当只有驱动程序向用户态暴露server后,用户程序才能通过Dispatch的方式发送指令到驱动程序,这是用户态程序与驱动程序数据交互的关键。
struct HdfIoService *HdfIoServiceBind(const char *serviceName)
三、点亮LED灯业务代码
1、确定目录结构。开发者编写Hi3516DV300业务时,务必先在./applications/sample/camera路径下新建一个目录(或一套目录结构),用于存放业务源码文件。例如:在app下新增业务my_led_app,其中my_led_app.c为业务代码,BUILD.gn为编译脚本,具体规划目录结构如下:
.
└── applications
└── sample
└── camera
│── my_led_app
│── my_led_app.c
└── BUILD.gn
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 "led_service"//与device_info.hcs文件中的serviceName的字段匹配
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;
}
3、编写将构建业务代码的BUILD.gn文件。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组件。
4、添加新组件在./build/lite/components/applications.json中为application子系统添加组件my_sample,将目标和路径添加到组件配置中。
"components": [
{
"component": "my_sample",
"description": "my samples",
"optional": "true",
"dirs": [
"applications/sample/camera/my_led_app"
],
"targets": [
"//applications/sample/camera/my_led_app:my_led_app"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_a" ],
"features": [],
"deps": {
"components": [],
"third_party": [ ]
}
},
5、修改单板配置文件在vendor/hisilicon/hispark_taurus/config.json文件中为开发板的application子系统添加组件,新增my_sample组件的条目,使样例能编译进开发板工程中。
{ "component": "my_sample", "features":[] },
四、运行结果
示例代码编译、烧录后,在命令行输入以下指令可控制开发板的LED灯。
关闭LED:
./bin/my_led 0
开启LED:
./bin/my_led 1
翻转LED:
./bin/my_led 2
从以下日志的Get reply中可以收到驱动上报的当前灯的状态,”0″表示当前灯为关闭状态,”1″表示当前灯为打开状态。
OHOS #
OHOS # ./bin/my_led 1
Argument 0 is bin/my_led.
Argument 1 is 1.
01-01 00:01:07.847 16 48 E 02500/led_driver: Led driver dispatch
Get reply is: 1
exitOHOS #
OHOS # ./bin/my_led 0
Argument 0 is bin/my_led.
Argument 1 is 0.
01-01 00:01:11.878 17 48 E 02500/led_driver: Led driver dispatch
Get reply is: 0
exitOHOS #
OHOS # ./bin/my_led 2
Argument 0 is bin/my_led.
Argument 1 is 2.
Get reply is: 1
演示视频地址:
文章相关附件可以点击下面的原文链接前往下载: