回复
OpenHarmony开发——传感器驱动开发实例
jiecho
发布于 2021-7-2 11:51
浏览
3收藏
传感器驱动开发实例
基于HDF驱动模型,加载启动加速度计传感器驱动,代码形式如下,具体原理可参考HDF驱动开发指南。加速度传感器选择通讯接口方式为I2C,厂家选择博世BMI160加速度传感器。
- 加速度计传感器驱动入口注册
- 加速度计传感器驱动入口函数实现
/* 注册加速度计传感器入口数据结构体对象 */
struct HdfDriverEntry g_sensorAccelDevEntry = {
.moduleVersion = 1, /* 加速度计传感器模块版本号 */
.moduleName = "HDF_SENSOR_ACCEL", /* 加速度计传感器模块名,要与device_info.hcs文件里的加速度计moduleName字段值一样*/
.Bind = BindAccelDriver, /* 加速度计传感器绑定函数 */
.Init = InitAccelDriver, /* 加速度计传感器初始化函数 */
.Release = ReleaseAccelDriver, /* 加速度计传感器资源释放函数 */
};
/* 调用HDF_INIT将驱动入口注册到HDF框架中,在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动,当Init调用异常时,HDF框架会调用Release释放驱动资源并退出 */
HDF_INIT(g_sensorAccelDevEntry);
- 加速度计传感器设备配置描述
加速度传感器模型使用HCS作为配置描述源码,HCS配置字段详细介绍参考配置管理介绍。
/* 加速度计传感器设备HCS配置 */
device_sensor_accel :: device {
device0 :: deviceNode {
policy = 1; /* policy字段是驱动服务发布的策略 */
priority = 105; /* 驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序 */
preload = 2; /* 驱动按需加载字段,0表示加载,2表示不加载 */
permission = 0664; /* 驱动创建设备节点权限 */
moduleName = "HDF_SENSOR_ACCEL"; /* 驱动名称,该字段的值必须和驱动入口结构的moduleName值一致 */
serviceName = "sensor_accel"; /* 驱动对外发布服务的名称,必须唯一 */
deviceMatchAttr = "hdf_sensor_accel_driver"; /* 驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等 */
}
}
- 加速度计传感器驱动初始化和去初始化
- 初始化入口函数init
/* 加速度计传感器驱动对外提供的服务绑定到HDF框架 */ int32_t BindAccelDriver(struct HdfDeviceObject *device) { CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); static struct IDeviceIoService service = { .object = {0}, .Dispatch = DispatchAccel, }; device->service = &service; return HDF_SUCCESS; } /*在探测到器件在位后,需要调用RegisterAccelChipOps注册差异化适配函数*/ int32_t RegisterAccelChipOps(struct AccelOpsCall *ops) { struct AccelDrvData *drvData = NULL; CHECK_NULL_PTR_RETURN_VALUE(ops, HDF_ERR_INVALID_PARAM); drvData = AccelGetDrvData(); drvData->ops.Init = ops->Init; drvData->ops.ReadData = ops->ReadData; return HDF_SUCCESS; } /* 挂载加速度计传感器驱动归一化的接口函数 */ static int32_t InitAccelOps(struct SensorDeviceInfo *deviceInfo) { struct AccelDrvData *drvData = AccelGetDrvData(); (void)memset_s((void *)deviceInfo, sizeof(*deviceInfo), 0, sizeof(*deviceInfo)); deviceInfo->ops.GetInfo = SetAccelInfo; deviceInfo->ops.Enable = SetAccelEnable; deviceInfo->ops.Disable = SetAccelDisable; deviceInfo->ops.SetBatch = SetAccelBatch; deviceInfo->ops.SetMode = SetAccelMode; deviceInfo->ops.SetOption = SetAccelOption; if (memcpy_s(&deviceInfo->sensorInfo, sizeof(deviceInfo->sensorInfo), &drvData->accelCfg->sensorInfo, sizeof(drvData->accelCfg->sensorInfo)) != EOK) { HDF_LOGE("%s: copy sensor info failed", __func__); return HDF_FAILURE; } /* 传感器类型标识可以在数据HCS配置文件里面配置,也可以在此处 */ drvData->accelCfg->sensorInfo.sensorTypeId = SENSOR_TAG_ACCELEROMETER; drvData->accelCfg->sensorInfo.sensorId = SENSOR_TAG_ACCELEROMETER; return HDF_SUCCESS; } /* 传感器寄存器初始化操作 */ static int32_t InitAccelAfterConfig(void) { struct SensorDeviceInfo deviceInfo; if (InitAccelConfig() != HDF_SUCCESS) { HDF_LOGE("%s: init accel config failed", __func__); return HDF_FAILURE; } if (InitAccelOps(&deviceInfo) != HDF_SUCCESS) { HDF_LOGE("%s: init accel ops failed", __func__); return HDF_FAILURE; } if (AddSensorDevice(&deviceInfo) != HDF_SUCCESS) { HDF_LOGE("%s: add accel device failed", __func__); return HDF_FAILURE; } return HDF_SUCCESS; } /*通过器件探测函数,挂载器件差异化函数接口*/ static int32_t DetectAccelChip(void) { int32_t num; int32_t ret; int32_t loop; struct AccelDrvData *drvData = AccelGetDrvData(); CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM); num = sizeof(g_accelDetectIfList) / sizeof(g_accelDetectIfList[0]); for (loop = 0; loop < num; ++loop) { if (g_accelDetectIfList[loop].DetectChip != NULL) { ret = g_accelDetectIfList[loop].DetectChip(drvData->accelCfg); if (ret == HDF_SUCCESS) { drvData->detectFlag = true; break; } } } if (loop == num) { HDF_LOGE("%s: detect accel device failed", __func__); drvData->detectFlag = false; return HDF_FAILURE; } return HDF_SUCCESS; } /* 加速度计传感器驱动初始化入口函数,主要功能为对传感器私有数据的结构体对象进行初始化,传感器HCS数据配置对象空间分配,传感器HCS数据配置初始化入口函数调用,传感器设备探测是否在位功能,传感器数据上报定时器创建,传感器归一化接口挂载,传感器设备注册功能 */ int32_t InitAccelDriver(struct HdfDeviceObject *device) { /* 获取传感器私有数据结构体对象 */ struct AccelDrvData *drvData = AccelGetDrvData(); /* 同类型传感器不同厂家设备探测时,判断此类型传感器是否已经在位,若已经在位,无需再继续探测,直接返回 */ if (drvData->detectFlag) { HDF_LOGE("%s: accel sensor have detected", __func__); return HDF_SUCCESS; } CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); /* 分配存放传感器数据配置的私有结构体数据对象,需要在驱动释放时释放分配的资源空间 */ drvData->accelCfg = (struct SensorCfgData *)OsalMemCalloc(sizeof(*cfg)); if (drvData->accelCfg == NULL) { HDF_LOGE("%s: malloc sensor config data failed", __func__); return HDF_FAILURE; } drvData->accelCfg->regCfgGroup = &g_regCfgGroup[0]; /* 初始化传感器配置数据主要是解析传感器通讯总线配置类型信息,传感器基本信息,传感器属性信息,传感器是否在位信息,寄存器分组信息 */ if (GetSensorBaseConfigData(device->property, drvData->accelCfg) != HDF_SUCCESS) { HDF_LOGE("%s: get sensor base config failed", __func__); goto Base_CONFIG_EXIT; } if (DetectAccelChip() != HDF_SUCCESS) { HDF_LOGE("%s: accel sensor detect device no exist", __func__); goto DETECT_CHIP_EXIT; } drvData->detectFlag = true; if (ParseSensorRegConfig(drvData->accelCfg) != HDF_SUCCESS) { HDF_LOGE("%s: detect sensor device failed", __func__); goto REG_CONFIG_EXIT; } if (InitAccelAfterConfig() != HDF_SUCCESS) { HDF_LOGE("%s: init accel after config failed", __func__); goto INIT_EXIT; } HDF_LOGI("%s: init accel driver success", __func__); return HDF_SUCCESS; INIT_EXIT: DestroySensorThread(&drvData->thread, &drvData->threadStatus); (void)DeleteSensorDevice(SENSOR_TAG_ACCELEROMETER); REG_CONFIG_EXIT: ReleaseSensorAllRegConfig(drvData->accelCfg); (void)ReleaseSensorBusHandle(&drvData->accelCfg->busCfg); DETECT_CHIP_EXIT: drvData->detectFlag = false; BASE_CONFIG_EXIT: drvData->accelCfg->root = NULL; drvData->accelCfg->regCfgGroup = NULL; OsalMemFree(drvData->accelCfg); drvData->accelCfg = NULL; return HDF_FAILURE; } /* 释放驱动初始化时分配的资源 */ void ReleaseAccelDriver(struct HdfDeviceObject *device) { (void)device; struct AccelDrvData *drvData = NULL; drvData = AccelGetDrvData(); (void)DestroySensorThread(&drvData->thread, &drvData->threadStatus); (void)DeleteSensorDevice(SENSOR_TAG_ACCELEROMETER); drvData->detectFlag = false; if (drvData->accelCfg != NULL) { drvData->accelCfg->root = NULL; drvData->accelCfg->regCfgGroup = NULL; ReleaseSensorAllRegConfig(drvData->accelCfg); (void)ReleaseSensorBusHandle(&drvData->accelCfg->busCfg); OsalMemFree(drvData->accelCfg); drvData->accelCfg = NULL; } drvData->initStatus = false; }
- 加速度计传感器寄存器组配置信息
加速度计传感器数据配置只需要按照模板配置即可,基于模板配置的解析功能已经在InitSensorConfigData函数完成,只需初始化时调用即可。如果有新增配置项,需要同步修改此函数。
加速度传感器数据配置模板(accel_config.hcs)
root {
sensorAccelConfig {
accelChipConfig {
/* 传感器设备信息模板 */
template sensorInfo {
sensorName = "accelerometer"; /* 加速度计名字,字符最大长度16字节 */
vendorName = "borsh_bmi160"; /* 传感器设备厂商,字符最大长度16字节 */
firmwareVersion = "1.0"; /* 传感器固件版本号,默认1.0,字符最大长度16字节 */
hardwareVersion = "1.0"; /* 传感器硬件版本号,默认1.0,字符最大长度16字节 */
sensorTypeId = 1; /* 传感器类型编号,详见{@link SensorTypeTag} */
sensorId = 1; /* 传感器的标识号,有传感器驱动开发者定义,推荐用{@link SensorTypeTag}枚举 */
maxRange = 8; /* 传感器的最大量程,根据开发者需要配置 */
precision = 0; /* 传感器的精度,与上报数据配合使用,上报数据结构体{@link SensorEvents } */
power = 230; /* 传感器的功耗 */
}
/* 传感器使用的总线类型和配置信息模板 */
template sensorBusConfig {
busType = 0; /* 0:i2c 1:spi */
busNum = 6; /* 芯片上分配给传感器的器件号 */
busAddr = 0; /* 芯片上分配给传感器的地址 */
regWidth = 1; /* 传感器寄存器地址宽度 */
regBigEndian = 0; /* 传感器寄存器大小端 */
}
/* 传感器设备属性模板 */
template sensorAttr {
chipName = ""; /* 传感器芯片名字 */
chipIdRegister = 0xf; /* 传感器在位检测寄存器地址 */
chipIdValue = 0xd1; /* 校验传感器在位检测寄存器值 */
}
}
}
}
/* 根据不同器件硬件差异,修改模板配置,不修改的就会默认采用模板配置 */
root {
sensorAccelConfig {
accel_bmi160_chip_config : accelChipConfig {
match_attr = "hdf_sensor_accel_driver"; /* 需要和加速度传感器设备配置match_attr字段保持一致 */
accelInfo :: sensorInfo {
vendorName = "borsh_bmi160";
sensorTypeId = 1;
sensorId = 1;
}
accelBusConfig :: sensorBusConfig {
busType = 0; /* i2c通讯方式 */
busNum = 6;
busAddr = 0x68;
regWidth = 1; /* 1字节位宽 */
}
accelAttr :: sensorAttr {
chipName = "bmi160";
chipIdRegister = 0x00;
chipIdValue = 0xd1;
}
accelRegConfig {
/* regAddr: 寄存器地址
value: 寄存器值
mask: 寄存器值的掩码
len: 寄存器值的数据长度(字节)
delay: 配置寄存器延时(ms)
opsType:操作类型 0-无 1-读 2-写 3-读并检查 4-位更新
calType: 计算类型 0-无 1-写 2-取反 3-异或 4-左移 5-右移
shiftNum: 移动位数
debug: 调试开关,0-调试关闭 1-调试打开
save: 保存数据开关,0-不保存数据 1-保存数据
*/
/* 传感器寄存器操作分组,按照分组进行有序配置 */
/* 寄存器地址, 寄存器值, 寄存器值的掩码, 寄存器值的数据长度, 配置寄存器延时, 操作类型, 计算类型, 移动位数, 调试开关, 保存开关 */
/* 初始化寄存器组 */
initSeqConfig = [
0x7e, 0xb6, 0xff, 1, 5, 2, 0, 0, 0, 0,
0x7e, 0x10, 0xff, 1, 5, 2, 0, 0, 0, 0
];
/* 使能寄存器组 */
enableSeqConfig = [
0x7e, 0x11, 0xff, 1, 5, 2, 0, 0, 0, 0,
0x41, 0x03, 0xff, 1, 0, 2, 0, 0, 0, 0,
0x40, 0x08, 0xff, 1, 0, 2, 0, 0, 0, 0
];
/* 去使能寄存器组 */
disableSeqConfig = [
0x7e, 0x10, 0xff, 1, 5, 2, 0, 0, 0, 0
];
}
}
}
}
- 加速度计传感器驱动操作接口实现
开发者需要根据每种类型的传感器实现归一化接口。
/* 不使用函数暂时置空 */
static int32_t SetAccelInfo(struct SensorBasicInfo *info)
{
(void)info;
return HDF_ERR_NOT_SUPPORT;
}
/* 下发使能寄存器组的配置 */
static int32_t SetAccelEnable(void)
{
int32_t ret;
struct AccelDrvData *drvData = AccelGetDrvData();
CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_ENABLE_GROUP]);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: accel sensor disable config failed", __func__);
return HDF_FAILURE;
}
drvData->threadStatus = SENSOR_THREAD_RUNNING;
return HDF_SUCCESS;
}
/* 下发去使能寄存器组的配置 */
static int32_t SetAccelDisable(void)
{
int32_t ret;
struct AccelDrvData *drvData = AccelGetDrvData();
CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_DISABLE_GROUP]);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: accel sensor disable config failed", __func__);
return HDF_FAILURE;
}
drvData->threadStatus = SENSOR_THREAD_STOPPED;
return HDF_SUCCESS;
}
/* 配置传感器采样率和数据上报间隔 */
static int32_t SetAccelBatch(int64_t samplingInterval, int64_t interval)
{
(void)interval;
struct AccelDrvData *drvData = AccelGetDrvData();
drvData->interval = samplingInterval;
return HDF_SUCCESS;
}
/* 设置传感器工作模式,当前支持实时模式 */
static int32_t SetAccelMode(int32_t mode)
{
return (mode == SENSOR_WORK_MODE_REALTIME) ? HDF_SUCCESS : HDF_FAILURE;
}
/* 设置传感器可选配置 */
static int32_t SetAccelOption(uint32_t option)
{
(void)option;
return HDF_ERR_NOT_SUPPORT;
}
差异化处理接口
/* 器件探测时,如果探测成功,则注册差异化处理函数到accel驱动模型里 */
int32_t DetectAccelBim160Chip(struct SensorCfgData *data)
{
int32_t ret;
struct AccelOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (strcmp(ACCEL_CHIP_NAME_BMI160, data->sensorAttr.chipName) != 0) {
return HDF_SUCCESS;
}
ret = InitAccelPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: init BMI160 bus mux config", __func__);
return HDF_FAILURE;
}
if (DetectSensorDevice(data) != HDF_SUCCESS) {
return HDF_FAILURE;
}
/* 差异化处理函数 */
ops.Init = InitBmi160;
ops.ReadData = ReadBmi160Data;
ret = RegisterAccelChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: register BMI160 accel failed", __func__);
(void)ReleaseSensorBusHandle(&data->busCfg);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
/* 初始化处理函数 */
static int32_t InitBmi160(struct SensorCfgData *data)
{
int32_t ret;
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: bmi160 sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
/* 数据处理函数 */
int32_t ReadBmi160Data(struct SensorCfgData *data)
{
int32_t ret;
struct AccelData rawData = { 0, 0, 0 };
int32_t tmp[ACCEL_AXIS_NUM];
struct SensorReportEvent event;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
ret = ReadBmi160RawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_ACCELEROMETER;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
rawData.x = rawData.x * BMI160_ACC_SENSITIVITY_2G;
rawData.y = rawData.y * BMI160_ACC_SENSITIVITY_2G;
rawData.z = rawData.z * BMI160_ACC_SENSITIVITY_2G;
tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
return ret;
}
-
数据处理函数
创建传感器定时器,按照配置的采样率定时采样,并上报给数据订阅者。
/* 传感器定时工作线程 */
static int32_t ReadAccelDataThreadWorker(void *arg)
{
(void)arg;
int64_t interval;
struct AccelDrvData *drvData = AccelGetDrvData();
drvData->threadStatus = SENSOR_THREAD_START;
while (true) {
if (drvData->threadStatus == SENSOR_THREAD_RUNNING) {
if (drvData->ops.ReadData != NULL) {
(void)drvData->ops.ReadData(drvData->accelCfg);
}
interval = OsalDivS64(drvData->interval, (SENSOR_CONVERT_UNIT * SENSOR_CONVERT_UNIT));
OsalMSleep(interval);
} else if (drvData->threadStatus == SENSOR_THREAD_DESTROY) {
break;
} else {
OsalMSleep(ACC_DEFAULT_SAMPLING_200_MS / SENSOR_CONVERT_UNIT / SENSOR_CONVERT_UNIT);
}
if ((!drvData->initStatus) || (drvData->interval < 0) || drvData->threadStatus != SENSOR_THREAD_RUNNING) {
continue;
}
}
return HDF_SUCCESS;
}
/* 创建传感器定时器和器件初始化 */
static int32_t InitAccelConfig(void)
{
int32_t ret;
struct AccelDrvData *drvData = AccelGetDrvData();
if (drvData->threadStatus != SENSOR_THREAD_NONE && drvData->threadStatus != SENSOR_THREAD_DESTROY) {
HDF_LOGE("%s: accel thread have created", __func__);
return HDF_SUCCESS;
}
ret = CreateSensorThread(&drvData->thread, ReadAccelDataThreadWorker, "hdf_sensor_accel", drvData);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: accel create thread failed", __func__);
drvData->threadStatus = SENSOR_THREAD_NONE;
return HDF_FAILURE;
}
CHECK_NULL_PTR_RETURN_VALUE(drvData->ops.Init, HDF_ERR_INVALID_PARAM);
ret = drvData->ops.Init(drvData->accelCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: accel create thread failed", __func__);
drvData->threadStatus = SENSOR_THREAD_NONE;
return HDF_FAILURE;
}
drvData->initStatus = true;
return HDF_SUCCESS;
}
- 主要的数据结构
/* 传感器转换单位*/ #define SENSOR_CONVERT_UNIT 1000 #define SENSOR_1K_UNIT 1024 /* 传感器2g对应灵敏度转换值 */ #define BMI160_ACC_SENSITIVITY_2G 61 /* 传感器数据采样寄存器地址 */ #define BMI160_ACCEL_X_LSB_ADDR 0X12 #define BMI160_ACCEL_X_MSB_ADDR 0X13 #define BMI160_ACCEL_Y_LSB_ADDR 0X14 #define BMI160_ACCEL_Y_MSB_ADDR 0X15 #define BMI160_ACCEL_Z_LSB_ADDR 0X16 #define BMI160_ACCEL_Z_MSB_ADDR 0X17 /* 传感器数据维度 */ enum AccelAxisNum { ACCEL_X_AXIS = 0, ACCEL_Y_AXIS = 1, ACCEL_Z_AXIS = 2, ACCEL_AXIS_NUM = 3, }; /* 传感器每个维度值 */ struct AccelData { int32_t x; int32_t y; int32_t z; }; /* 传感器私有数据结构体 */ struct AccelDrvData { bool detectFlag; uint8_t threadStatus; uint8_t initStatus; int64_t interval; struct SensorCfgData *accelCfg; struct OsalThread thread; struct AccelOpsCall ops; }; /* 差异化适配函数 */ struct AccelOpsCall { int32_t (*Init)(struct SensorCfgData *data); int32_t (*ReadData)(struct SensorCfgData *data); };
已于2021-7-2 11:51:32修改
赞
2
收藏 3
回复
相关推荐