https://harmonyos.51cto.com
一、流程总览
创建驱动目录并添加驱动源码文件和编译脚本–>添加驱动配置目录和驱动配置文件
二、添加驱动文件
2.1 创建驱动文件夹
在./device/st/drivers路径下新建一个led目录,用于存放驱动源码文件。
2.2 创建驱动源码文件
新建led.c文件,编写驱动源码
#include "hdf_device_desc.h"
#include "hdf_log.h"
#include "device_resource_if.h"
#include "osal_io.h"
#include "osal.h"
#include "osal_mem.h"
#include "gpio_if.h"
#define HDF_LOG_TAG led_driver // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签
#define LED_WRITE_READ 1 // 读写操作码1
enum LedOps {
LED_OFF,
LED_ON,
LED_TOGGLE,
};
struct Stm32Mp1ILed {
uint32_t gpioNum;
};
static struct Stm32Mp1ILed g_Stm32Mp1ILed;
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)
{
case LED_WRITE_READ:
HdfSbufReadUint8(data,&contrl);
switch (contrl)
{
case LED_ON:
GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW);
status = 1;
break;
case LED_OFF:
GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH);
status = 0;
break;
case LED_TOGGLE:
if(status == 0)
{
GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW);
status = 1;
}
else
{
GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH);
status = 0;
}
break;
default:
break;
}
if (!HdfSbufWriteInt32(reply, status))
{
HDF_LOGE("replay is fail");
return HDF_FAILURE;
}
break;
default:
break;
}
return HDF_SUCCESS;
}
// 读取驱动私有配置
static int32_t Stm32LedReadDrs(struct Stm32Mp1ILed *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;
}
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 Stm32Mp1ILed *led = &g_Stm32Mp1ILed;
int32_t ret;
if (device == NULL || device->property == NULL) {
HDF_LOGE("%s: device or property NULL!", __func__);
return HDF_ERR_INVALID_OBJECT;
}
ret = Stm32LedReadDrs(led, device->property);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get led device resource fail:%d", __func__, ret);
return ret;
}
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 = "HDF_LED",
.Bind = HdfLedDriverBind,
.Init = HdfLedDriverInit,
.Release = HdfLedDriverRelease,
};
// 调用HDF_INIT将驱动入口注册到HDF框架中
HDF_INIT(g_ledDriverEntry);
2.3 创建编译脚本文件
新建BUILD.gn文件,添加以下内容
import("//drivers/adapter/khdf/liteos/hdf.gni")
hdf_driver("hdf_led") {
sources = [
"led.c",
]
}
2.4 修改drivers的编译脚本使之编译进内核
在/device/st/drivers/BUILD.gn的deps中加入"led"
三、添加驱动配置文件
驱动配置包含两部分,HDF框架定义的驱动的私有配置信息和驱动设备描述
3.1 创建驱动配置文件夹
在./device/st/bearpi_hm_micro/liteos_a/hdf_config路径下新建一个led目录,用于存放led驱动配置文件。
3.2 创建驱动私有配置文件
在led文件夹下创建led_config.hcs文件,该文件为驱动配置文件。
添加以下内容
root {
LedDriverConfig {
led_gpio_num = 13;
match_attr = "st_stm32mp157_led"; //该字段的值必须和device_info.hcs中的deviceMatchAttr值一致
}
}
3.3 添加驱动设备描述
在device\st\bearpi_hm_micro\liteos_a\hdf_config\device_info\device_info.hcs文件中添加以下代码
device_led :: device { // led设备节点
device0 :: deviceNode { // led驱动的DeviceNode节点
policy = 2; // policy字段是驱动服务发布的策略,在驱动服务管理章节有详细介绍
priority = 10; // 驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序
preload = 1; // 驱动按需加载字段
permission = 0777; // 驱动创建设备节点权限
moduleName = "HDF_LED"; // 驱动名称,该字段的值必须和驱动入口结构的moduleName值一致
serviceName = "hdf_led"; // 驱动对外发布服务的名称,必须唯一
deviceMatchAttr = "st_stm32mp157_led"; //驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等
}
}
如图所示
- moduleName要与led.c中的**.moduleName = “HDF_LED”**参数一致
- serviceName要与led_config.hcs中的**match_attr = “st_stm32mp157_led”**参数一致
3.4 将写好的驱动添加到板级配置入口
在在device\st\bearpi_hm_micro\liteos_a\hdf_config\hdf.hcs文件中添加如下代码
#include "led/led_config.hcs"
四、小结
到此为止,以LED驱动开发的流程到此结束。