首页 > 其他分享 >OpenHarmony南向之旅之HDF传感器驱动开发

OpenHarmony南向之旅之HDF传感器驱动开发

时间:2023-03-22 20:32:04浏览次数:52  
标签:__ OpenHarmony drvData return struct ret HDF 南向

(OpenHarmony南向之旅之HDF传感器驱动开发)

前言

南向小白在AlgoIdeas指导下的一次基于HDF框架开发温度传感器驱动之旅,主要内容为开发温度传感器通用驱动代码以及针对SHT31传感器开发差异化驱动代码,读者可以和基于HDF驱动框架的温度传感器驱动开发一起看。本文旨在分享笔者开发过程中的一些经验,文章可能存在用词不专业或说法有误之处,欢迎各位指正

开发准备

硬件环境

  1. Unionpi Tiger开发板
  2. SHT31温湿度传感器

关于SHT31温湿度传感器的介绍不在本篇的讨论范围内,在开发中需要用到的一些寄存器地址信息读者可以自行查找阅读该器件的datasheet

在开发之前我们先简单了解一下源码驱动目录

在OpenHarmony根目录下的drivers\hdf_core\framework\model\sensor\driver目录,这里存放的是通用传感器驱动代码目录,主要是由HDF Sensor驱动框架提供的Init、Enable、Disable等接口实现

image20230314144127510.png

而在drivers\peripheral\sensor\chipset目录下存放的是传感器差异化驱动代码,主要是根据不同的传感器芯片型号在通用传感器驱动代码提供出来的接口上进行开发适配,例如温度传感器有SHT3x、AHT系列等,这就需要分别进行开发

image20230314144404673.png

开发流程

添加配置

在HDF框架的配置文件(例如笔者这里是vendor\unionman\unionpi_tiger\hdf_config\khdf\device_info\device_info.hcs)中的Sensor Host添加该驱动的配置信息

/* 温度计传感器设备HCS配置 */
device_sensor_temperature :: device {
    device0 :: deviceNode {
        policy = 1;
        priority = 130;
        preload = 0;
        permission = 0664;
        moduleName = "HDF_SENSOR_TEMPERATURE";
        serviceName = "sensor_temperature";
        deviceMatchAttr = "hdf_sensor_temperature_driver";
    }
}
device_sensor_sht31 :: device {
    device0 :: deviceNode {
        policy = 1;              // 驱动对外发布服务的策略(0-4),具体可以查看参考资料[3]
        priority = 140;          // 驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序。
        preload = 0;             // 驱动按需加载字段。
        permission = 0664;       // 驱动创建设备节点权限
        moduleName = "HDF_SENSOR_TEMPERATURE_SHT31";      // 驱动名称,该字段的值必须和驱动入口结构的moduleName值一致。
        serviceName = "hdf_temperature_sht31";    // 驱动对外发布服务的名称,必须唯一。
        deviceMatchAttr = "hdf_sensor_temperature_sht31_driver"; // 驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等。
    }
}

对应关系如下 image20230315165628917.png

通用驱动代码开发

1.基于HDF驱动框架,按照驱动Driver Entry程序,完成温度抽象驱动开发,主要由Bind、Init、Release、Dispatch函数接口实现

  • 温度传感器驱动入口函数实现
/* 注册温度计传感器入口数据结构体对象 */
struct HdfDriverEntry g_sensorTemperatureDevEntry = {
    .moduleVersion = 1,                // 温度计传感器模块版本号
    .moduleName = "HDF_SENSOR_TEMPERATURE",  // 温度计传感器模块名,要与device_info.hcs文件里的温度计moduleName字段值一样
    .Bind = TemperatureBindDriver,           // 温度计传感器绑定函数
    .Init = TemperatureInitDriver,           // 温度计传感器初始化函数
    .Release = TemperatureReleaseDriver,     // 温度计传感器资源释放函数
};

/* 调用HDF_INIT将驱动入口注册到HDF框架中。在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动。当Init调用异常时,HDF框架会调用Release释放驱动资源并退出 */
HDF_INIT(g_sensorTemperatureDevEntry);
  • 温度传感器驱动操作接口实现
/* 温度计传感器驱动对外提供的服务绑定到HDF框架 */
int32_t TemperatureBindDriver(struct HdfDeviceObject *device)
{
    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);

    struct TemperatureDrvData *drvData = (struct TemperatureDrvData *)OsalMemCalloc(sizeof(*drvData));
    if (drvData == NULL) {
        HDF_LOGI("%s: malloc temperature drv data fail!", __func__);
        return HDF_ERR_MALLOC_FAIL;
    }

    drvData->ioService.Dispatch = DispatchTemperature;
    drvData->device = device;
    device->service = &drvData->ioService;
    g_temperatureDrvData = drvData;
    return HDF_SUCCESS;
}

/* 注册温度计传感器驱动归一化的接口函数 */
static int32_t InitTemperatureOps(struct SensorCfgData *config, struct SensorDeviceInfo *deviceInfo)
{
    CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);

    deviceInfo->ops.Enable = SetTemperatureEnable;
    deviceInfo->ops.Disable = SetTemperatureDisable;
    deviceInfo->ops.SetBatch = SetTemperatureBatch;
    deviceInfo->ops.SetMode = SetTemperatureMode;
    deviceInfo->ops.SetOption = SetTemperatureOption;

    if (memcpy_s(&deviceInfo->sensorInfo, sizeof(deviceInfo->sensorInfo), &config->sensorInfo,
            sizeof(config->sensorInfo)) != EOK) {
        HDF_LOGE("%s: Copy sensor info failed", __func__);
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}

/* 提供给差异化驱动的初始化接口,完成温度器件基本配置信息解析(温度信息、温度总线配置、温度器件探测寄存器配置)、器件探测、器件寄存器解析 */
static int32_t InitTemperatureAfterDetected(struct SensorCfgData *config)
{
    struct SensorDeviceInfo deviceInfo;
    CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);

    if (InitTemperatureOps(config, &deviceInfo) != HDF_SUCCESS) {
        HDF_LOGE("%s: Init temperature ops failed", __func__);
        return HDF_FAILURE;
    }

    if (AddSensorDevice(&deviceInfo) != HDF_SUCCESS) {
        HDF_LOGE("%s: Add temperature device failed", __func__);
        return HDF_FAILURE;
    }

    if (ParseSensorRegConfig(config) != HDF_SUCCESS) {
        HDF_LOGE("%s: Parse sensor register failed", __func__);
        (void)DeleteSensorDevice(&config->sensorInfo);
        ReleaseSensorAllRegConfig(config);
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

/* 温度计传感器驱动初始化入口函数,主要功能为对传感器私有数据的结构体对象进行初始化,传感器HCS数据配置对象空间分配,传感器HCS数据配置初始化入口函数调用,传感器设备探测是否在位功能,传感器数据上报定时器创建,传感器归一化接口注册,传感器设备注册功能 */ 
int32_t TemperatureInitDriver(struct HdfDeviceObject *device)
{
    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
    struct TemperatureDrvData *drvData = (struct TemperatureDrvData *)device->service;
    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
    /* 工作队列资源初始化 */
    if (InitTemperatureData(drvData) != HDF_SUCCESS) {
        HDF_LOGE("%s: Init accel config failed", __func__);
        return HDF_FAILURE;
    }
    /* 分配温度配置信息资源 */
    drvData->temperatureCfg = (struct SensorCfgData *)OsalMemCalloc(sizeof(*drvData->temperatureCfg));
    if (drvData->temperatureCfg == NULL) {
        HDF_LOGE("%s: Malloc temperature config data failed", __func__);
        return HDF_FAILURE;
    }
    /* 注册寄存器分组信息 */
    drvData->temperatureCfg->regCfgGroup = &g_regCfgGroup[0];

    HDF_LOGI("%s: Init temperature driver success", __func__);
    return HDF_SUCCESS;
}
/* 释放驱动初始化时分配的资源 */
void TemperatureReleaseDriver(struct HdfDeviceObject *device)
{
    CHECK_NULL_PTR_RETURN(device);

    struct TemperatureDrvData *drvData = (struct TemperatureDrvData *)device->service;
    CHECK_NULL_PTR_RETURN(drvData);
    /* 器件在位,释放已分配资源 */
    if (drvData->detectFlag && drvData->temperatureCfg != NULL) {
        TemperatureReleaseCfgData(drvData->temperatureCfg);
    }

    OsalMemFree(drvData->temperatureCfg);
    drvData->temperatureCfg = NULL;
    /* 器件在位,销毁工作队列资源 */
    HdfWorkDestroy(&drvData->temperatureWork);
    HdfWorkQueueDestroy(&drvData->temperatureWorkQueue);
    OsalMemFree(drvData);
}

2.完成温度传感器抽象驱动内部接口开发,包括Enable、Disable、SetBatch、SetMode、SetOption、AccelCreateCfgData、AccelReleaseCfgData、AccelRegisterChipOps接口实现

/* 下发使能寄存器组的配置 */
static int32_t SetTemperatureEnable(void)
{
    int32_t ret;
    struct TemperatureDrvData *drvData = TemperatureGetDrvData();

    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
    CHECK_NULL_PTR_RETURN_VALUE(drvData->temperatureCfg, HDF_ERR_INVALID_PARAM);

    if (drvData->enable) {
        HDF_LOGE("%s: temperature sensor is enabled", __func__);
        return HDF_SUCCESS;
    }

    ret = SetSensorRegCfgArray(
        &drvData->temperatureCfg->busCfg, drvData->temperatureCfg->regCfgGroup[SENSOR_ENABLE_GROUP]);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: temperature sensor enable config failed", __func__);
        return ret;
    }

    ret = OsalTimerCreate(&drvData->temperatureTimer, SENSOR_TIMER_MIN_TIME, TemperatureTimerEntry, (uintptr_t)drvData);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: temperature create timer failed[%d]", __func__, ret);
        return ret;
    }

    ret = OsalTimerStartLoop(&drvData->temperatureTimer);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: temperature start timer failed[%d]", __func__, ret);
        return ret;
    }
    drvData->enable = true;

    return HDF_SUCCESS;
}
/* 下发去使能寄存器组的配置 */
static int32_t SetTemperatureDisable(void)
{
    int32_t ret;
    struct TemperatureDrvData *drvData = TemperatureGetDrvData();

    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
    CHECK_NULL_PTR_RETURN_VALUE(drvData->temperatureCfg, HDF_ERR_INVALID_PARAM);

    if (!drvData->enable) {
        HDF_LOGE("%s: temperature sensor had disable", __func__);
        return HDF_SUCCESS;
    }

    ret = SetSensorRegCfgArray(
        &drvData->temperatureCfg->busCfg, drvData->temperatureCfg->regCfgGroup[SENSOR_DISABLE_GROUP]);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: temperature sensor disable config failed", __func__);
        return ret;
    }

    ret = OsalTimerDelete(&drvData->temperatureTimer);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: temperature delete timer failed", __func__);
        return ret;
    }
    drvData->enable = false;
    return HDF_SUCCESS;
}
/* 配置传感器采样率和数据上报间隔 */
static int32_t SetTemperatureBatch(int64_t samplingInterval, int64_t interval)
{
    (void)interval;

    struct TemperatureDrvData *drvData = NULL;

    drvData = TemperatureGetDrvData();
    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);

    drvData->interval = samplingInterval;

    return HDF_SUCCESS;
}
/* 设置传感器工作模式,当前支持实时模式 */
static int32_t SetTemperatureMode(int32_t mode)
{
    if (mode <= SENSOR_WORK_MODE_DEFAULT || mode >= SENSOR_WORK_MODE_MAX) {
        HDF_LOGE("%s: The current mode is not supported", __func__);
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}
/* 设置传感器可选配置 */
static int32_t SetTemperatureOption(uint32_t option)
{
    (void)option;
    return HDF_SUCCESS;
}
/* 创建传感器配置数据接口 */
struct SensorCfgData *TemperatureCreateCfgData(const struct DeviceResourceNode *node)
{
    struct TemperatureDrvData *drvData = TemperatureGetDrvData();

    if (drvData == NULL || node == NULL) {
        HDF_LOGE("%s: Temperature node pointer NULL", __func__);
        return NULL;
    }
    /* 如果探测不到器件在位,返回进行下个器件探测 */
    if (drvData->detectFlag) {
        HDF_LOGE("%s: Temperature sensor have detected", __func__);
        return NULL;
    }

    if (drvData->temperatureCfg == NULL) {
        HDF_LOGE("%s: Temperature temperatureCfg pointer NULL", __func__);
        return NULL;
    }
	/* 解析器件HCS私有配置信息 */
    if (GetSensorBaseConfigData(node, drvData->temperatureCfg) != HDF_SUCCESS) {
        HDF_LOGE("%s: Get sensor base config failed", __func__);
        goto BASE_CONFIG_EXIT;
    }
    /* 如果探测不到器件在位,返回进行下个器件探测 */
    if (DetectSensorDevice(drvData->temperatureCfg) != HDF_SUCCESS) {
        HDF_LOGI("%s: Temperature sensor detect device no exist", __func__);
        drvData->detectFlag = false;
        goto BASE_CONFIG_EXIT;
    }

    drvData->detectFlag = true;
    /* 器件寄存器解析 */
    if (InitTemperatureAfterDetected(drvData->temperatureCfg) != HDF_SUCCESS) {
        HDF_LOGE("%s: Temperature sensor detect device no exist", __func__);
        goto INIT_EXIT;
    }
    return drvData->temperatureCfg;

INIT_EXIT:
    (void)ReleaseSensorBusHandle(&drvData->temperatureCfg->busCfg);
BASE_CONFIG_EXIT:
    drvData->temperatureCfg->root = NULL;
    (void)memset_s(
        &drvData->temperatureCfg->sensorInfo, sizeof(struct SensorBasicInfo), 0, sizeof(struct SensorBasicInfo));
    (void)memset_s(&drvData->temperatureCfg->busCfg, sizeof(struct SensorBusCfg), 0, sizeof(struct SensorBusCfg));
    (void)memset_s(&drvData->temperatureCfg->sensorAttr, sizeof(struct SensorAttr), 0, sizeof(struct SensorAttr));
    return drvData->temperatureCfg;
}
/* 释放传感器配置数据接口 */
void TemperatureReleaseCfgData(struct SensorCfgData *temperatureCfg)
{
    CHECK_NULL_PTR_RETURN(temperatureCfg);

    (void)DeleteSensorDevice(&temperatureCfg->sensorInfo);
    ReleaseSensorAllRegConfig(temperatureCfg);
    (void)ReleaseSensorBusHandle(&temperatureCfg->busCfg);

    temperatureCfg->root = NULL;
    (void)memset_s(&temperatureCfg->sensorInfo, sizeof(struct SensorBasicInfo), 0, sizeof(struct SensorBasicInfo));
    (void)memset_s(&temperatureCfg->busCfg, sizeof(struct SensorBusCfg), 0, sizeof(struct SensorBusCfg));
    (void)memset_s(&temperatureCfg->sensorAttr, sizeof(struct SensorAttr), 0, sizeof(struct SensorAttr));
}
/* 注册传感器差异化接口 */
int32_t TemperatureRegisterChipOps(const struct TemperatureOpsCall *ops)
{
    struct TemperatureDrvData *drvData = TemperatureGetDrvData();

    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
    CHECK_NULL_PTR_RETURN_VALUE(ops, HDF_ERR_INVALID_PARAM);

    drvData->ops.Init = ops->Init;
    drvData->ops.ReadData = ops->ReadData;
    return HDF_SUCCESS;
}

差异化驱动代码开发

1.基于HDF驱动框架,按照驱动Driver Entry程序,完成温度传感器差异化驱动开发,主要由Bind、Init、Release、Dispatch函数接口实现

/* 温度计传感器差异化驱动消息交互 */
static int32_t DispatchSHT31(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
    (void)client;
    (void)cmd;
    (void)data;
    (void)reply;

    return HDF_SUCCESS;
}
/* 温度计传感器差异化驱动对外提供的服务绑定到HDF框架 */
int32_t SHT31BindDriver(struct HdfDeviceObject *device)
{
    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);

    struct SHT31DrvData *drvData = (struct SHT31DrvData *)OsalMemCalloc(sizeof(*drvData));
    if (drvData == NULL) {
        HDF_LOGE("%s: Malloc SHT31 drv data fail", __func__);
        return HDF_ERR_MALLOC_FAIL;
    }

    drvData->ioService.Dispatch = DispatchSHT31;
    drvData->device = device;
    device->service = &drvData->ioService;
    g_sht31DrvData = drvData;

    return HDF_SUCCESS;
}
/* 温度计传感器差异化驱动初始化 */
int32_t SHT31InitDriver(struct HdfDeviceObject *device)
{
    int32_t ret;
    struct TemperatureOpsCall ops;

    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
    struct SHT31DrvData *drvData = (struct SHT31DrvData *)device->service;
    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);

    drvData->sensorCfg = TemperatureCreateCfgData(device->property);
    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
        HDF_LOGD("%s: Creating temperaturecfg failed because detection failed", __func__);
        return HDF_ERR_NOT_SUPPORT;
    }

    ops.Init = NULL;
    ops.ReadData = ReadSHT31Data;
    ret = TemperatureRegisterChipOps(&ops);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: Register SHT31 temperature failed", __func__);
        return HDF_FAILURE;
    }

    ret = InitSHT31(drvData->sensorCfg);
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("%s: Init SHT31 temperature failed", __func__);
        return HDF_FAILURE;
    }

    HDF_LOGI("%s: Init SHT31 temperature success", __func__);
    return HDF_SUCCESS;
}
/* 释放驱动初始化时分配的资源 */
void SHT31ReleaseDriver(struct HdfDeviceObject *device)
{
    CHECK_NULL_PTR_RETURN(device);

    struct SHT31DrvData *drvData = (struct SHT31DrvData *)device->service;
    CHECK_NULL_PTR_RETURN(drvData);

    if (drvData->sensorCfg != NULL) {
        TemperatureReleaseCfgData(drvData->sensorCfg);
        drvData->sensorCfg = NULL;
    }
    OsalMemFree(drvData);
}
/* 温度传感器差异化驱动对应的HdfDriverEntry对象 */
struct HdfDriverEntry g_temperatureSHT31DevEntry = {
    .moduleVersion = 1,
    .moduleName = "HDF_SENSOR_TEMPERATURE_SHT31",
    .Bind = SHT31BindDriver,
    .Init = SHT31InitDriver,
    .Release = SHT31ReleaseDriver,
};

HDF_INIT(g_temperatureSHT31DevEntry);

2.完成温度传感器差异化驱动中差异化接口ReadData函数实现

int32_t ReadSHT31Data(struct SensorCfgData *data)
{
    int32_t ret;
    static int32_t tmp;
    struct TemperaturemeterData temperaturemeterData = {0};
    OsalTimespec time;
    struct SensorReportEvent event;

    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
    (void)memset_s(&event, sizeof(event), 0, sizeof(event));

    if (OsalGetTime(&time) != HDF_SUCCESS) {
        HDF_LOGE("%s: Get time failed", __func__);
        return HDF_FAILURE;
    }
    event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;

    ret = ReadSHT31RawData(data, &temperaturemeterData);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    event.sensorId = data->sensorInfo.sensorId;
    event.mode = SENSOR_WORK_MODE_REALTIME;

    tmp = temperaturemeterData.temperature;

    event.dataLen = sizeof(tmp);
    event.data = (uint8_t *)&tmp;
    ret = ReportSensorEvent(&event);
    return ret;
}

添加差异化配置文件

sht31_config.hcs文件参考

文件路径

vendor\unionman\unionpi_tiger\hdf_config\khdf\sensor\temperature\sht31_config.hcs
#include "../sensor_common.hcs"
root {
    temperature_sht31_chip_config : sensorConfig {
        match_attr = "hdf_sensor_temperature_sht31_driver";
        sensorInfo :: sensorDeviceInfo {
            sensorName = "temperaturemeter";
            vendorName = "sensirion_sht31"; // max string length is 16 bytes
            sensorTypeId = 9; // enum SensorTypeTag
            sensorId = 1; // user define sensor id
            power = 230;
            minDelay = 0;
            maxDelay = 0;
        }
        sensorBusConfig :: sensorBusInfo {
            busType = 0; // 0:i2c 1:spi
            busNum = 5;
            busAddr = 0x45;
            regWidth = 2; // 2 byte
        }
        sensorIdAttr :: sensorIdInfo {
            chipName = "sht31";
            chipIdRegister = 0xF32D;
            chipIdValue = 0x80;
        }
        sensorRegConfig {
            /*  regAddr: register address
                value: config register value
                len: size of value
                mask: mask of value
                delay: config register delay time (ms)
                opsType: enum SensorOpsType 0-none 1-read 2-write 3-read_check 4-update_bit
                calType: enum SensorBitCalType 0-none 1-set 2-revert 3-xor 4-left shift 5-right shift
                shiftNum: shift bits
                debug: 0-no debug 1-debug
                save: 0-no save 1-save
            */
            /* regAddr, value, mask, len, delay, opsType, calType, shiftNum, debug, save */
            initSeqConfig = [
                0x30A2,    0x0, 0x0,   0,     5,       2,       0,        0,     0,    0
            ];
            enableSeqConfig = [
                0x2C06,    0x0, 0x0,   0,     5,       2,       0,        0,     0,    0
            ];
            disableSeqConfig = [
                0x2400,    0x0, 0x0,   0,     5,       2,       0,        0,     0,    0
            ];
        }
    }
}

对应关系如下 image20230315145002098.png

编译准备

打开vendor\unionman\unionpi_tiger\hdf_config\khdf\sensor\sensor_config.hcs文件并include差异化配置文件

image20230228105249208.png

修改drivers\hdf_core\adapter\khdf\linux\model\sensor\Makefile编译文件

image20230228112754446.png

修改drivers\hdf_core\adapter\khdf\linux\model\sensor\Kconfig配置文件

image.png

device\board\unionman\unionpi_tiger\kernel\build\unionpi_tiger_standard_defconfig打开CONFIG_DRIVERS_HDF_SENSOR image.png 对应关系如下 image.png

运行结果

成功初始化SHT31传感器驱动 image20230303144351563.png

测试

测试代码参考

代码路径

vendor\unionman\unionpi_tiger\sample\hdf\temperature\temperature.cpp
#include <unistd.h>
#include <stdio.h>
#include "hdf_base.h"
#include "hdf_log.h"
#include "hdf_sbuf.h"
#include "hdf_io_service_if.h"
#include "sensor_if.h"
#include "sensor_type.h"

/* 创建回调函数 */
int32_t SensorDataCallback(const struct SensorEvents *event)
{
    if (event == NULL) {
        return HDF_FAILURE;
    }
    float *data = (float *)event->data;
    printf("sensor data %.2f°C\n", *data);
    return HDF_SUCCESS;
}

void SensorSample(void)
{
    int ret;
    struct SensorInformation *sensorInfo = NULL;
    int32_t count = 0;
    int32_t sensorInterval = 200000000; /* 数据采样率设置200毫秒,单位纳秒 */
    int32_t reportInterval = 400000000;

    /* 1.创建传感器接口实例 */
    const struct SensorInterface *sensorDev = NewSensorInterfaceInstance();
    if (sensorDev == NULL) {
        return;
    }
    printf("NewSensorInterfaceInstance success\n");
    /* 2.订阅者注册传感器数据回调处理函数 */
    ret = sensorDev->Register(0, SensorDataCallback);
    if (ret != 0) {
        return;
    }
    printf("Register success\n");
    /* 3.获取设备支持的Sensor列表 */
    ret = sensorDev->GetAllSensors(&sensorInfo, &count);
    if (ret != 0) {
        return;
    }
    printf("GetAllSensors success,count: %d\n sensorName:%s\n vendorName:%s\n sensorTypeId:%d\n sensorId:%d\n", count,
           sensorInfo->sensorName, sensorInfo->vendorName, sensorInfo->sensorTypeId, sensorInfo->sensorId);
    /* 4.设置传感器采样率 */
    ret = sensorDev->SetBatch(sensorInfo->sensorId, sensorInterval, reportInterval);
    if (ret != 0) {
        printf("SetBatch failed\n ,ret: %d", ret);
        return;
    }
    printf("SetBatch success\n");
    /* 5.使能传感器 */
    ret = sensorDev->Enable(sensorInfo->sensorId);
    if (ret != 0) {
        return;
    }
    printf("Enable success\n");

    usleep(2000 * 1000);

    /* 6.去使能传感器 */
    ret = sensorDev->Disable(sensorInfo->sensorId);
    if (ret != 0) {
        printf("Disable failed\n ,ret: %d", ret);
        return;
    }
    printf("Disable success\n");
    /* 7.取消传感器数据订阅函数 */
    ret = sensorDev->Unregister(0, SensorDataCallback);
    if (ret != 0) {
        return;
    }
    printf("Unregister success\n");
    /* 8.释放传感器接口实例 */
    ret = FreeSensorInterfaceInstance();
    if (ret != 0) {
        return;
    }
    printf("FreeSensorInterfaceInstance success\n");
}

int main(int argc, char *argv[])
{
    SensorSample();
    return HDF_SUCCESS;
}

BUILD.gn参考

import("//build/ohos.gni")

ohos_executable("temperature") {
  sources = [ "temperature.cpp" ]

  deps = [ "//drivers/peripheral/sensor/hal:hdi_sensor" ]

  external_deps = [
    "hiviewdfx_hilog_native:libhilog",
  ]

  cflags = [
    "-Wall",
    "-Wextra",
    "-Werror",
    "-Wno-format",
    "-Wno-format-extra-args",
  ]

  install_enable = true
  install_images = [ "vendor" ]
  module_install_dir = "bin"
  part_name = "unionman_products"
}

编译命令

./build.sh --product-name unionpi_tiger --build-target vendor/unionman/unionpi_tiger/sample/hdf/temperature:temperature

生成可执行文件所在位置

out\unionpi_tiger\device_unionpi_tiger\unionman_products

将可执行文件push到system/bin目录下,添加执行权限

hdc shell mount -o remount,rw /
hdc file send <PATH>\out\unionpi_tiger\device_unionpi_tiger\unionman_products\temperature /system/bin
hdc shell chmod 744 system/bin/temperature

运行结果

成功打印驱动上报的温度数据 image20230314110455597.png

踩坑记录

基本上按照HDF框架开发步骤来还是比较简单的,笔者是第一次玩驱动开发,相信对驱动开发熟悉的开发者来说HDF框架是很友好的。笔者踩的一些坑主要是因为笔者对南向开发不熟悉,没有玩过硬件

1.这款传感器的寄存器地址是两个字节,而HDF框架源码的逻辑只能处理一个字节,会出现以下错误 image20230315172716986.png

这里就不放出笔者修改的代码了,这部分源码后续官方应该会优化

2.由于这款传感器没有chipId,所以私有化配置文件中的chipIdValue字段的值填写打印出来的128,也就是0x80(这样做可能不太正规,有更好的做法欢迎指出)

image20230315163816789.png

3.由于笔者不太理解ReadSensor接口中的regAddr参数,传了不正确的值

int32_t ReadSensor(struct SensorBusCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen)

导致出现了如下结果

image20230315173501888.png

结语

以上仅为个人浅见,如有疑问,欢迎留言交流

参考文档

[1]Sensor驱动模型接口说明

[2]drivers_peripheral

[3]驱动服务管理

本文作者:Haoc_小源同学

想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com/#bkwz​

标签:__,OpenHarmony,drvData,return,struct,ret,HDF,南向
From: https://blog.51cto.com/harmonyos/6142252

相关文章

  • 探究鸿蒙系统底座OpenHarmony 在汽车软件领域的代码合规性
    前言  提到鸿蒙操作系统(HarmonyOS),想必大家并不陌生。其底座OpenHarmony是由华为捐出的鸿蒙开源系统,并且由开放原子开源基金会孵化及运营,目标是面向全场景、全连接......
  • 基于HDF驱动框架的温度传感器驱动开发
    概述​温度传感器(Temperature)Sensor驱动,对温度传感器进行上电,通过驱动入口,将温度传感器注册到HDF驱动框架,对温度传感器驱动进行初始化,探测器件是否在位,并解析配......
  • 2022 OpenHarmony年度运营报告
                                                 ......
  • hdfs的异构存储
    目录1背景2hdfs异构存储类型和存储策略2.1hdfs支持的存储类型2.2hdfs如何知道数据存储目录是那种存储类型2.3存储策略2.3.1在hdfs中支持如下存储策略2.3.2存储策略......
  • #创作者激励#【FFH】OpenHarmony轻量化系统声音收录
    【本文正在参加2023年第一期优质创作者激励计划】概括前一阵子想着语音识别作为物联网不可或缺的一部分,前提是获取到语音的声音数据。对于声音收录数字化,stm32有很多现成......
  • HDFS分布式文件系统
    目录1️⃣、HDFS的概述1.1、HDFS产出背景及定义1.2、HDFS优缺点1.3、HDFS组成架构1.4、HDFS文件块大小(面试重点)2️⃣、HDFS的Shell操作2.1、基本语法2.2、命令大全2.3、常用命......
  • 《Hadoop Operations》读书笔记 - 1 - 第二章 HDFS
    传统存储是SAN或者NAS,提供了集中化、低延时的块存储或者文件系统,以支持TB级数据。在面对关系型数据库之类的服务时,这是很好的选择。但是面对上万台计算机同时提取几百TB......
  • HDFS--机架感知
    一、背景介绍Hadoop的设计目的:解决海量大文件的处理问题,主要指大数据的存储和计算问题,其中,HDFS解决数据的存储问题;MapReduce解决数据的计算问题 Hadoop的设计考虑:设计分布......
  • #创作者激励#OpenHarmony富设备移植指南(6.2)GPU测试程序编译
    【本文正在参加2023年第一期优质创作者激励计划】上一篇文章讲解了编译开源gpu驱动,并把gpu驱动添加到编译框架中,此时理论上gpu已经可以调用,但是我们需要一些......
  • HDF格式遥感影像批量转为TIFF格式:ArcPy实现
      本文介绍基于Python中ArcPy模块,实现大量HDF格式栅格图像文件批量转换为TIFF格式的方法。  首先,来看看我们想要实现的需求。  在一个名为HDF的文件夹下,有五个子文......