想了解更多关于开源的内容,请访问:
和硕网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
开源基础软件社区
https://ost.51cto.com
温度传感器(Temperature)Sensor驱动,对温度传感器进行上电,通过驱动入口,将温度传感器注册到HDF驱动框架,对温度传感器驱动进行初始化,探测器件是否在位,并解析配置文件,最后通过温度传感器差异化代码实现,为上层服务提供稳定的实时数据。温度传感器sensor开发主要包括三个部分:温度传感器通用传感器驱动、温度传感器通用传感器配置和器件差异化驱动。基于HDF(Hardware Driver Foundation)驱动框架的Sensor驱动模型,实现跨操作系统迁移,器件差异配置等功能。
备注:后续会考虑进入到OpenHarmony主线,湿度传感器相关开发类似温度传感器
(1)从device info HCS 的Sensor Host里读取温度传感器驱动配置信息。
(2)加载温度传感器抽象驱动,调用初始化接口,完成Sensor器件驱动资源分配和数据处理队列创建。
(3)从temperature_xxx_config HCS里读取温度传感器器件差异化驱动配置和私有化配置信息。
(4)温度传感器差异化驱动,调用通用配置解析接口,完成器件属性信息解析,器件寄存器解析。
(5)温度传感器差异化驱动完成器件探测,并分配温度传感器配置资源,完成温度传感器差异化接口注册。
(6)温度传感器器件探测成功之后,温度传感器差异化驱动通知温度传感器抽象驱动,注册温度传感器设备到Sensor设备管理中。
Sensor驱动模型提供标准化的器件驱动,开发者无需独立开发,通过配置即可完成驱动的部署。对驱动模型抽象,屏蔽驱动与不同系统组件间的交互,使得驱动更具备通用性。基于Sensor驱动模型,加载温度传感器驱动。温度传感器选择通讯接口方式为IIC,厂家选择Sensirion,器件类型为SHT30的温度传感器传器。
1.温度传感器抽象驱动实现。
/* 温度传感器设备HCS配置 */
device_sensor_temperature :: device {
device0 :: deviceNode {
policy = 1; /* policy字段是驱动服务发布的策略 */
priority = 130; /* 温度传感器驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序 */
preload = 0; /* 驱动按需加载字段,0表示加载,2表示不加载 */
permission = 0664; /* 驱动创建设备节点权限 */
moduleName = "HDF_SENSOR_TEMPERATURE"; /* 温度传感器驱动名称,该字段的值必须和驱动入口结构的moduleName值一致 */
serviceName = "hdf_sensor_temperature"; /* 温度传感器驱动对外发布服务的名称,必须唯一 */
deviceMatchAttr = "hdf_sensor_temperature_driver"; /* 驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等 */
}
}
/*注册温度传感器入口数据结构体对象*/
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);
Bind接口实现驱动接口实例化,实现示例:
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_LOGE("%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;
}
Init接口实现驱动接口实例化,实现示例:
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 Temperature 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;
}
Release接口在驱动卸载或者Init执行失败时,会调用此接口释放资源:
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);
}
温度传感器抽象驱动内部接口开发实现示例:
提供给差异化驱动的初始化接口,完成温度传感器器件的基本配置信息解析(温度传感器信息,温度传感器总线配置,温度传感器器件探测寄存器配置),器件探测,器件寄存器解析。
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);
ReleaseSensorDirectionConfig(config);
return HDF_FAILURE;
}
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;
}
//设备基本配置信息解析
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_LOGI("%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;
}
2.温度传感器差异化驱动实现。
/* 温度传感器SHT30设备HCS配置 */
device_temperature_sht30 :: device {
device0 :: deviceNode {
policy = 1; /* policy字段是驱动服务发布的策略 */
priority = 140; /* 温度传感器SHT30驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序 */
preload = 0; /* 驱动按需加载字段,0表示加载,2表示不加载 */
permission = 0664; /* 驱动创建设备节点权限 */
moduleName = "HDF_SENSOR_TEMPERATURE_SHT30"; /* 温度传感器结构的moduleName值一致 */
serviceName = "hdf_temperature_sht30"; /* 温度传感器SHT30驱动对外发布服务的名称,必须唯一 */
deviceMatchAttr = "hdf_sensor_temperature_sht30_driver"; /* 温度传感器SHT30驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等 */
}
}
温度传感器差异化驱动私有HCS配置。
代码路径如下:
vendor\hihope\rk3568\hdf_config\khdf\sensor\temperature\temperature_sht30_config.hcs
vendor\unionman\unionpi_tiger\hdf_config\khdf\sensor\temperature\temperature_sht30_config.hcs
#include "../sensor_common.hcs"
root {
temperature_sht30_chip_config : sensorConfig {
match_attr = "hdf_sensor_temperature_sht30_driver";
sensorInfo :: sensorDeviceInfo {
sensorName = "temperature_sht30";
vendorName = "sensirion"; // max string length is 16 bytes
sensorTypeId = 9; // enum SensorTypeTag
sensorId = 2; // user define sensor id
power = 230;
minDelay = 0;
maxDelay = 0;
}
sensorBusConfig :: sensorBusInfo {
busType = 0; // 0:i2c 1:spi
busNum = 3;
busAddr = 0x44;
regWidth = 2; // 2 byte
}
sensorIdAttr :: sensorIdInfo {
chipName = "sht30";
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
];
}
}
}
/*注册温度传感器SHT30入口数据结构体对象*/
struct HdfDriverEntry g_temperatureSht30DevEntry = {
.moduleVersion = 1, /*温度传感器模块版本号*/
.moduleName = "HDF_SENSOR_TEMPERATURE_SHT30", /*温度传感器SHT30模块名,要与device_info.hcs文件里温度传感器SHT30 moduleName字段值一样*/
.Bind = Sht30BindDriver, /*温度传感器SHT30绑定函数*/
.Init = Sht30InitDriver, /*温度传感器SHT30初始化函数*/
.Release = Sht30ReleaseDriver, /*温度传感器SHT30资源释放函数*/
};
/* 调用HDF_INIT将驱动入口注册到HDF框架中,在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动,当Init调用异常时,HDF框架会调用Release释放驱动资源并退出 */
HDF_INIT(g_temperatureSht30DevEntry);
Bind驱动接口实例化,实现示例:
static int32_t Sht30BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Sht30DrvData *drvData = (struct Sht30DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: malloc drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchSht30;
drvData->device = device;
device->service = &drvData->ioService;
g_sht30DrvData = drvData;
return HDF_SUCCESS;
}
Init驱动接口实例化,实现示例:
static int32_t Sht30InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct TemperatureOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Sht30DrvData *drvData = (struct Sht30DrvData *)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 temperature cfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
//注册差异化接口
ops.Init = NULL;
ops.ReadData = ReadSht30Data;
ret = TemperatureRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register temperature failed", __func__);
return HDF_FAILURE;
}
//初始化器件配置
ret = InitSht30(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init SHT30 temperature sensor failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
Release驱动接口实例化,实现示例:
static void Sht30ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Sht30DrvData *drvData = (struct Sht30DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
TemperatureReleaseCf gData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
温度传感器差异化函数接口实现示例。
需要开发者实现的ReadSht30Data接口函数,在Sht30InitDriver函数里面注册此函数。
int32_t ReadSht30Data(struct SensorCfgData *data)
{
int32_t ret;
static int32_t temperature;
struct TemperatureData rawData = { 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 = ReadSht30RawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: SHT30 read raw data failed", __func__);
return HDF_FAILURE;
}
temperature = rawData.temperature;
event.sensorId = data->sensorInfo.sensorId;
event.mode = SENSOR_WORK_MODE_REALTIME;
event.dataLen = sizeof(temperature);
event.data = (uint8_t *)&temperature;
ret = ReportSensorEvent(&event);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: report data failed", __func__);
}
return ret;
}
在编译的过程中,首先需要在Sensor Host中的配置信息中设置具体器件是否要驱动加载。
HCS路径:
vendor\hihope\rk3568\hdf_config\khdf\device_info\device_info.hcs
device_sensor_temperature :: device {
device0 :: deviceNode {
policy = 1;
priority = 130;
preload = 0; /* 驱动按需加载字段,0表示加载,2表示不加载 */
permission = 0664;
moduleName = "HDF_SENSOR_TEMPERATURE";
serviceName = "hdf_sensor_temperature";
deviceMatchAttr = "hdf_sensor_temperature_driver";
}
}
device_temperature_sht30 :: device {
device0 :: deviceNode {
policy = 1;
priority = 140;
preload = 0; /* 驱动按需加载字段,0表示加载,2表示不加载 */
permission = 0664;
moduleName = "HDF_SENSOR_TEMPERATURE_SHT30";
serviceName = "hdf_temperature_sht30";
deviceMatchAttr = "hdf_sensor_temperature_sht30_driver";
}
}
传感器驱动实现在内核态,代码参与编译通过适配makefile实现,并通过内核模块宏定义,控制温度传感器设备驱动是否参与编译。
Makefile路径:
drivers\hdf_core\adapter\khdf\linux\model\sensor\Makefile
......
obj-$(CONFIG_DRIVERS_HDF_SENSOR_TEMPERATURE) += $(SENSOR_ROOT_DIR)/temperature/sensor_temperature_driver.o
obj-$(CONFIG_DRIVERS_HDF_SENSOR_TEMPERATURE_SHT30) += $(SENSOR_ROOT_DIR)/chipset/temperature/temperature_sht30.o
.....
-Idrivers/peripheral/sensor/driver/temperature \
-Idrivers/peripheral/sensor/driver/chipset/temperature \
......
Kconfig路径:
drivers\hdf_core\adapter\khdf\linux\model\sensor\Kconfig
温度传感器驱动加载,将DRIVERS_HDF_SENSOR_TEMPERATURE;
DRIVERS_HDF_SENSOR_TEMPERATURE_SHT30宏字段设置为y即可。
温度传感器驱动不加载,将DRIVERS_HDF_SENSOR_TEMPERATURE; DRIVERS_HDF_SENSOR_TEMPERATURE_SHT30宏字段设置为is not set即可。
内核配置路径(以RK3568为例):
kernel/linux/config/linux-5.10/arch/arm64/configs/rk3568_standard_defconfig
......
# CONFIG_DRIVERS_HDF_INPUT_INFRARED is not set
# CONFIG_DRIVERS_HDF_WIFI is not set
# CONFIG_DRIVERS_HDF_BT is not set
CONFIG_DRIVERS_HDF_SENSOR=y
CONFIG_DRIVERS_HDF_SENSOR_ACCEL=y
CONFIG_DRIVERS_HDF_SENSOR_ACCEL_MXC6655XA=y
CONFIG_DRIVERS_HDF_SENSOR_TEMPERATURE=y
CONFIG_DRIVERS_HDF_SENSOR_TEMPERATURE_SHT30=y
.....
参考代码:
#include
#include
#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"
/* 创建回调函数 */
static int32_t SensorDataCallback(const struct SensorEvents *event)
{
if (event == NULL) {
return HDF_FAILURE;
}
float sensorData =*((float *)event->data);
printf("sensor id [%d], data : %.2f\n", event->sensorId, sensorData);
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(TRADITIONAL_SENSOR_TYPE, SensorDataCallback);
if (ret != 0) {
return;
}
printf("Register success\n");
/* 3.获取设备支持的Sensor列表 */
ret = sensorDev->GetAllSensors(&sensorInfo, &count);
if (ret != 0) {
return;
}
printf("GetAllSensors count: %d\n", count);
for (int i = 0; i < count; i++)
{
printf("sensor [%d] : sensorName:%s, vendorName:%s, sensorTypeId:%d, sensorId:%d\n", i,
sensorInfo[i].sensorName, sensorInfo[i].vendorName, sensorInfo[i].sensorTypeId, sensorInfo[i].sensorId);
}
for (int i = 0; i < count; i++)
{
/* 4.设置传感器采样率 */
ret = sensorDev->SetBatch(sensorInfo[i].sensorId, sensorInterval, reportInterval);
if (ret != 0) {
printf("SetBatch failed\n ,ret: %d",ret);
continue;
}
printf("SetBatch success\n");
/* 5.使能传感器 */
ret = sensorDev->Enable(sensorInfo[i].sensorId);
if (ret != 0) {
continue;
}
printf("Enable success\n");
usleep(1000 * 1000);
/* 6.去使能传感器 */
ret = sensorDev->Disable(sensorInfo[i].sensorId);
if (ret != 0) {
continue;
}
printf("Disable success\n");
}
/* 7.取消传感器数据订阅函数 */
ret = sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, 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;
}
RK3568运行日志如下:
A311D运行日志:
想了解更多关于开源的内容,请访问:
开源基础软件社区
https://ost.51cto.com
网页题目:基于HDF驱动框架的温度传感器驱动开发
文章链接:http://www.shufengxianlan.com/qtweb/news26/526176.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联