feat:sensor/vibrator chipset optimization

Signed-off-by: sunxuejiao <sunxuejiao5@huawei.com>
This commit is contained in:
sunxuejiao 2022-05-24 15:26:29 +08:00
parent 5aa2f8336d
commit 2f63e9f942
28 changed files with 2 additions and 4098 deletions

View File

@ -50,9 +50,10 @@ Note:If the text contains special characters, please escape them according to th
<policy name="projectPolicy" desc="">
<policyitem type="license" name="GPL" path=".*" desc="linux kernel adapter codes"/>
<policyitem type="license" name="BSD" path=".*" desc="huawei codes"/>
<policyitem type="copyright" name="xu" path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="kernel ps driver codes"/>
<policyitem type="copyright" name="Chipsea Technologies (Shenzhen) Corp., Ltd." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="kaihong codes"/>
<policyitem type="copyright" name="Shenzhen Kaihong Digital" path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="kaihong codes"/>
<!--policyitem type="compatibility" name="GPL" path=".*" desc="linux kernel adapter codes"/-->
<!--policyitem type="compatibility" name="GPL-2.0+" path=".*" desc="linux kernel adapter codes"/-->
</policy>
</policylist>
<filefilterlist>

View File

@ -1,188 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "vibrator_linear_driver.h"
#include <securec.h>
#include "device_resource_if.h"
#include "gpio_if.h"
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "osal_mem.h"
#include "vibrator_driver.h"
#include "vibrator_driver_type.h"
#define HDF_LOG_TAG hdf_vibrator_driver
struct VibratorLinearDriverData *g_linearVibratorData = NULL;
static struct VibratorLinearDriverData *GetLinearVibratorData(void)
{
return g_linearVibratorData;
}
static int32_t StartLinearVibrator()
{
int32_t ret;
struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
if (drvData->busType != VIBRATOR_BUS_GPIO) {
HDF_LOGE("%s: vibrator bus type not gpio", __func__);
return HDF_FAILURE;
}
ret = GpioWrite(drvData->gpioNum, GPIO_VAL_LOW);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: pull gpio%d to %d level failed", __func__, drvData->gpioNum, GPIO_VAL_LOW);
return ret;
}
return HDF_SUCCESS;
}
static int32_t StartEffectLinearVibrator(uint32_t effectType)
{
(void)effectType;
HDF_LOGE("%s: vibrator set build-in effect no support!", __func__);
return HDF_SUCCESS;
}
static int32_t StopLinearVibrator()
{
int32_t ret;
struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
if (drvData->busType != VIBRATOR_BUS_GPIO) {
HDF_LOGE("%s: vibrator bus type not gpio", __func__);
return HDF_FAILURE;
}
ret = GpioWrite(drvData->gpioNum, GPIO_VAL_HIGH);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: pull gpio%d to %d level failed", __func__, drvData->gpioNum, GPIO_VAL_HIGH);
return ret;
}
return HDF_SUCCESS;
}
static int32_t DispatchLinearVibrator(struct HdfDeviceIoClient *client,
int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t BindLinearVibratorDriver(struct HdfDeviceObject *device)
{
struct VibratorLinearDriverData *drvData = NULL;
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
drvData = (struct VibratorLinearDriverData *)OsalMemCalloc(sizeof(*drvData));
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_MALLOC_FAIL);
drvData->ioService.Dispatch = DispatchLinearVibrator;
drvData->device = device;
device->service = &drvData->ioService;
g_linearVibratorData = drvData;
return HDF_SUCCESS;
}
static int32_t ParserLinearConfig(const struct DeviceResourceNode *node, struct VibratorLinearDriverData *drvData)
{
int32_t ret;
struct DeviceResourceIface *parser = NULL;
const struct DeviceResourceNode *configNode = NULL;
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(node, HDF_FAILURE);
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
parser = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser->GetChildNode, HDF_ERR_INVALID_PARAM);
configNode = parser->GetChildNode(node, "vibratorChipConfig");
ret = parser->GetUint32(configNode, "busType", &drvData->busType, 0);
CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "busType");
if (drvData->busType == VIBRATOR_BUS_GPIO) {
ret = parser->GetUint32(configNode, "gpioNum", &drvData->gpioNum, 0);
CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "gpioNum");
}
ret = parser->GetUint32(configNode, "startReg", &drvData->startReg, 0);
CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "startReg");
ret = parser->GetUint32(configNode, "stopReg", &drvData->stopReg, 0);
CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "stopReg");
ret = parser->GetUint32(configNode, "startMask", &drvData->mask, 0);
CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "startMask");
return HDF_SUCCESS;
}
int32_t InitLinearVibratorDriver(struct HdfDeviceObject *device)
{
static struct VibratorOps ops;
struct VibratorLinearDriverData *drvData = NULL;
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
drvData = (struct VibratorLinearDriverData *)device->service;
CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
ops.Start = StartLinearVibrator;
ops.StartEffect = StartEffectLinearVibrator;
ops.Stop = StopLinearVibrator;
if (RegisterVibrator(&ops) != HDF_SUCCESS) {
HDF_LOGE("%s: register vibrator ops fail", __func__);
return HDF_FAILURE;
}
if (ParserLinearConfig(device->property, drvData) != HDF_SUCCESS) {
HDF_LOGE("%s: parser vibrator cfg fail", __func__);
return HDF_FAILURE;
}
if (GpioSetDir(drvData->gpioNum, GPIO_DIR_OUT) != HDF_SUCCESS) {
HDF_LOGE("%s: set vibrator gpio fail", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void ReleaseLinearVibratorDriver(struct HdfDeviceObject *device)
{
struct VibratorLinearDriverData *drvData = NULL;
if (device == NULL) {
HDF_LOGE("%s: Device is null", __func__);
return;
}
drvData = (struct VibratorLinearDriverData *)device->service;
if (drvData == NULL) {
HDF_LOGE("%s: DrvData pointer is null", __func__);
return;
}
(void)OsalMemFree(drvData);
g_linearVibratorData = NULL;
}
struct HdfDriverEntry g_linearVibratorDriverEntry = {
.moduleVersion = 1,
.moduleName = "HDF_LINEAR_VIBRATOR",
.Bind = BindLinearVibratorDriver,
.Init = InitLinearVibratorDriver,
.Release = ReleaseLinearVibratorDriver,
};
HDF_INIT(g_linearVibratorDriverEntry);

View File

@ -1,24 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef VIBRATOR_LINEAR_DRIVER_H
#define VIBRATOR_LINEAR_DRIVER_H
#include "hdf_device_desc.h"
struct VibratorLinearDriverData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
uint32_t busType;
uint32_t gpioNum;
uint32_t startReg;
uint32_t stopReg;
uint32_t mask;
};
#endif /* VIBRATOR_LINEAR_DRIVER_H */

View File

@ -8,7 +8,6 @@
#include "sensor_als_driver.h"
#include <securec.h>
#include "als_bh1745.h"
#include "osal_math.h"
#include "osal_mem.h"
#include "sensor_config_controller.h"

View File

@ -1,237 +0,0 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "accel_bmi160.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_accel_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#define HDF_LOG_TAG hdf_sensor_accel
static struct Bmi160DrvData *g_bmi160DrvData = NULL;
struct Bmi160DrvData *Bmi160GetDrvData(void)
{
return g_bmi160DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static int32_t ReadBmi160RawData(struct SensorCfgData *data, struct AccelData *rawData, int64_t *timestamp)
{
uint8_t status = 0;
uint8_t reg[ACCEL_AXIS_BUTT];
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, BMI160_STATUS_ADDR, &status, sizeof(uint8_t));
if (!(status & BMI160_ACCEL_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
HDF_LOGE("%s: data status [%hhu] ret [%d]", __func__, status, ret);
return HDF_FAILURE;
}
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_X_LSB_ADDR, &reg[ACCEL_X_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_X_MSB_ADDR, &reg[ACCEL_X_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Y_LSB_ADDR, &reg[ACCEL_Y_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Y_MSB_ADDR, &reg[ACCEL_Y_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Z_LSB_ADDR, &reg[ACCEL_Z_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Z_MSB_ADDR, &reg[ACCEL_Z_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ACCEL_X_AXIS_LSB]);
rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ACCEL_Y_AXIS_LSB]);
rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ACCEL_Z_AXIS_LSB]);
return HDF_SUCCESS;
}
int32_t ReadBmi160Data(struct SensorCfgData *cfg, struct SensorReportEvent *event)
{
int32_t ret;
struct AccelData rawData = { 0, 0, 0 };
static int32_t tmp[ACCEL_AXIS_NUM];
CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM);
ret = ReadBmi160RawData(cfg, &rawData, &event->timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMI160 read raw data failed", __func__);
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_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
ret = SensorRawDataToRemapData(cfg->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMI160 convert raw data failed", __func__);
return HDF_FAILURE;
}
event->dataLen = sizeof(tmp);
event->data = (uint8_t *)&tmp;
return ret;
}
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;
}
static int32_t InitAccelPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Bmi160BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchBMI160;
drvData->device = device;
device->service = &drvData->ioService;
g_bmi160DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Bmi160InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct AccelOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitAccelPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = AccelCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadBmi160Data;
ret = AccelRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register BMI160 accel failed", __func__);
return HDF_FAILURE;
}
ret = InitBmi160(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 accel failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Bmi160ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
AccelReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_accelBmi160DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_ACCEL_BMI160",
.Bind = Bmi160BindDriver,
.Init = Bmi160InitDriver,
.Release = Bmi160ReleaseDriver,
};
HDF_INIT(g_accelBmi160DevEntry);

View File

@ -1,69 +0,0 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef ACCEL_BMI160_H
#define ACCEL_BMI160_H
#include "sensor_accel_driver.h"
#include "sensor_config_parser.h"
/* ACCEL DATA REGISTERS ADDR */
#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
#define BMI160_STATUS_ADDR 0X1B
/* ACCEL ODR */
#define BMI160_ACCEL_ODR_RESERVED 0x00
#define BMI160_ACCEL_ODR_0_78HZ 0x01
#define BMI160_ACCEL_ODR_1_56HZ 0x02
#define BMI160_ACCEL_ODR_3_12HZ 0x03
#define BMI160_ACCEL_ODR_6_25HZ 0x04
#define BMI160_ACCEL_ODR_12_5HZ 0x05
#define BMI160_ACCEL_ODR_25HZ 0x06
#define BMI160_ACCEL_ODR_50HZ 0x07
#define BMI160_ACCEL_ODR_100HZ 0x08
#define BMI160_ACCEL_ODR_200HZ 0x09
#define BMI160_ACCEL_ODR_400HZ 0x0A
#define BMI160_ACCEL_ODR_800HZ 0x0B
#define BMI160_ACCEL_ODR_1600HZ 0x0C
#define BMI160_ACCEL_ODR_RESERVED0 0x0D
#define BMI160_ACCEL_ODR_RESERVED1 0x0E
#define BMI160_ACCEL_ODR_RESERVED2 0x0F
/* default HZ */
#define BMI160_ACCEL_DEFAULT_ODR_100HZ 100
#define BMI160_ACCEL_DEFAULT_ODR_25HZ 25
/* ACCEL RANGE */
#define BMI160_ACCEL_RANGE_2G 0X03
#define BMI160_ACCEL_RANGE_4G 0X05
#define BMI160_ACCEL_RANGE_8G 0X08
#define BMI160_ACCEL_RANGE_16G 0X0C
/* ACC sensitivity */
#define BMI160_ACC_SENSITIVITY_2G 61
#define BMI160_ACC_SENSITIVITY_4G 122
#define BMI160_ACC_SENSITIVITY_8G 244
#define BMI160_ACC_SENSITIVITY_16G 488
/* ACCEL DATA READY */
#define BMI160_ACCEL_DATA_READY_MASK 0x80
int32_t DetectAccelBim160Chip(struct SensorCfgData *data);
int32_t ReadBmi160Data(struct SensorCfgData *cfg, struct SensorReportEvent *event);
struct Bmi160DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* ACCEL_BMI160_H */

View File

@ -1,239 +0,0 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "accel_mxc6655xa.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_accel_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#define HDF_LOG_TAG hdf_sensor_accel
#define MXC6655_RANGE (16384 * 2)
#define MXC6655_PRECISION 12
#define MXC6655_BOUNDARY (0x1 << (MXC6655_PRECISION - 1))
#define MXC6655_GRAVITY_STEP (MXC6655_RANGE / MXC6655_BOUNDARY)
#define MXC6655_MASK 0x7fff
#define MXC6655_ACCEL_OUTPUT_16BIT 16
#define MXC6655_ACCEL_OUTPUT_MSB 8
static struct Mxc6655xaDrvData *g_Mxc6655xaDrvData = NULL;
struct Mxc6655xaDrvData *Mxc6655xaGetDrvData(void)
{
return g_Mxc6655xaDrvData;
}
static int sensor_convert_data(char high_byte, char low_byte)
{
int32_t result;
result = ((uint32_t)high_byte << (MXC6655_PRECISION - MXC6655_ACCEL_OUTPUT_MSB)) |
((uint32_t)low_byte >> (MXC6655_ACCEL_OUTPUT_16BIT - MXC6655_PRECISION));
if (result < MXC6655_BOUNDARY) {
result = result * MXC6655_GRAVITY_STEP;
} else {
result = ~(((~result & (MXC6655_MASK >> (MXC6655_ACCEL_OUTPUT_16BIT - MXC6655_PRECISION))) + 1) *
MXC6655_GRAVITY_STEP) + 1;
}
return result;
}
static int32_t ReadMxc6655xaRawData(struct SensorCfgData *data, struct AccelData *rawData, int64_t *timestamp)
{
int32_t status = 0;
int32_t reg[ACCEL_AXIS_BUTT];
OsalTimespec time;
int32_t x;
int32_t y;
int32_t z;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, MXC6655XA_STATUS_ADDR, &status, sizeof(int32_t));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret);
return HDF_FAILURE;
}
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_X_LSB_ADDR, &reg[ACCEL_X_AXIS_LSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_X_MSB_ADDR, &reg[ACCEL_X_AXIS_MSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Y_LSB_ADDR, &reg[ACCEL_Y_AXIS_LSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Y_MSB_ADDR, &reg[ACCEL_Y_AXIS_MSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Z_LSB_ADDR, &reg[ACCEL_Z_AXIS_LSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Z_MSB_ADDR, &reg[ACCEL_Z_AXIS_MSB], sizeof(int32_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
x = sensor_convert_data(reg[ACCEL_X_AXIS_MSB], reg[ACCEL_X_AXIS_LSB]);
y = sensor_convert_data(reg[ACCEL_Y_AXIS_MSB], reg[ACCEL_Y_AXIS_LSB]);
z = sensor_convert_data(reg[ACCEL_Z_AXIS_MSB], reg[ACCEL_Z_AXIS_LSB]);
rawData->x = x;
rawData->y = y;
rawData->z = z;
return HDF_SUCCESS;
}
int32_t ReadMxc6655xaData(struct SensorCfgData *cfg, struct SensorReportEvent *event)
{
int32_t ret;
struct AccelData rawData = { 0, 0, 0 };
static int32_t tmp[ACCEL_AXIS_NUM];
CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM);
ret = ReadMxc6655xaRawData(cfg, &rawData, &event->timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: MXC6655XA read raw data failed", __func__);
return HDF_FAILURE;
}
event->sensorId = SENSOR_TAG_ACCELEROMETER;
event->option = 0;
event->mode = SENSOR_WORK_MODE_REALTIME;
rawData.x = rawData.x * MXC6655XA_ACC_SENSITIVITY_2G;
rawData.y = rawData.y * MXC6655XA_ACC_SENSITIVITY_2G;
rawData.z = rawData.z * MXC6655XA_ACC_SENSITIVITY_2G;
tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
ret = SensorRawDataToRemapData(cfg->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: MXC6655XA convert raw data failed", __func__);
return HDF_FAILURE;
}
event->dataLen = sizeof(tmp);
event->data = (uint8_t *)&tmp;
return ret;
}
static int32_t InitMxc6655xa(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: MXC6655XA sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchMXC6655xa(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Mxc6655xaBindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc MXC6655XA drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchMXC6655xa;
drvData->device = device;
device->service = &drvData->ioService;
g_Mxc6655xaDrvData = drvData;
return HDF_SUCCESS;
}
int32_t Mxc6655xaInitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct AccelOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
drvData->sensorCfg = AccelCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadMxc6655xaData;
ret = AccelRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register MXC6655XA accel failed", __func__);
return HDF_FAILURE;
}
ret = InitMxc6655xa(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init MXC6655XA accel failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Mxc6655xaReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
AccelReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_accelMxc6655xaDevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_ACCEL_MXC6655XA",
.Bind = Mxc6655xaBindDriver,
.Init = Mxc6655xaInitDriver,
.Release = Mxc6655xaReleaseDriver,
};
HDF_INIT(g_accelMxc6655xaDevEntry);

View File

@ -1,50 +0,0 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef ACCEL_MXC6655XA_H
#define ACCEL_MXC6655XA_H
#include "sensor_accel_driver.h"
#include "sensor_config_parser.h"
/* ACCEL DATA REGISTERS ADDR */
#define MXC6655XA_ACCEL_X_MSB_ADDR 0X03
#define MXC6655XA_ACCEL_X_LSB_ADDR 0X04
#define MXC6655XA_ACCEL_Y_MSB_ADDR 0X05
#define MXC6655XA_ACCEL_Y_LSB_ADDR 0X06
#define MXC6655XA_ACCEL_Z_MSB_ADDR 0X07
#define MXC6655XA_ACCEL_Z_LSB_ADDR 0X08
#define MXC6655XA_STATUS_ADDR 0X02
/* default HZ */
#define MXC6655XA_ACCEL_DEFAULT_ODR_100HZ 100
#define MXC6655XA_ACCEL_DEFAULT_ODR_25HZ 25
/* ACCEL RANGE */
#define MXC6655XA_ACCEL_RANGE_2G 0X03
#define MXC6655XA_ACCEL_RANGE_4G 0X05
#define MXC6655XA_ACCEL_RANGE_8G 0X08
#define MXC6655XA_ACCEL_RANGE_16G 0X0C
/* ACC sensitivity */
#define MXC6655XA_ACC_SENSITIVITY_2G 61
#define MXC6655XA_ACC_SENSITIVITY_4G 122
#define MXC6655XA_ACC_SENSITIVITY_8G 244
#define MXC6655XA_ACC_SENSITIVITY_16G 488
/* ACCEL DATA READY */
#define MXC6655XA_ACCEL_DATA_READY_MASK 0x80
int32_t ReadMxc6655xaData(struct SensorCfgData *cfg, struct SensorReportEvent *event);
struct Mxc6655xaDrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* ACCEL_MXC6655XA_H */

View File

@ -1,411 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "als_bh1745.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_als_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#define HDF_LOG_TAG hdf_sensor_als
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
#define SENSOR_TIME_INCREASE 0
#define SENSOR_TIME_DECREASE 1
static struct Bh1745DrvData *g_bh1745DrvData = NULL;
static uint32_t g_timeChangeStatus = SENSOR_TIME_DECREASE;
static struct TimeRegAddrValueMap g_timeMap[EXTENDED_ALS_TIME_GROUP_INDEX_MAX] = {
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_0, BH1745_TIME_160MSEC },
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_1, BH1745_TIME_320MSEC },
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_2, BH1745_TIME_640MSEC },
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_3, BH1745_TIME_1280MSEC },
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_4, BH1745_TIME_2560MSEC },
{ EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_5, BH1745_TIME_5120MSEC }
};
static struct GainRegAddrValueMap g_gainMap[EXTENDED_ALS_GAIN_GROUP_INDEX_MAX] = {
{ EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_0, BH1745_GAIN_1X },
{ EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_1, BH1745_GAIN_2X },
{ EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_2, BH1745_GAIN_16X }
};
static uint32_t g_red[BH1745_COEFFICIENT_RED] = {
BH1745_COEFFICIENT_RED_LEVEL_0,
BH1745_COEFFICIENT_RED_LEVEL_1
};
static uint32_t g_green[BH1745_COEFFICIENT_GREEN] = {
BH1745_COEFFICIENT_GREEN_LEVEL_0,
BH1745_COEFFICIENT_GREEN_LEVEL_1
};
struct Bh1745DrvData *Bh1745GetDrvData(void)
{
return g_bh1745DrvData;
}
static int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcData)
{
int32_t ret;
uint8_t regValue;
uint32_t temp;
uint8_t timeItemNum;
struct SensorRegCfgGroupNode *timeGroupNode = NULL;
int32_t index = EXTENDED_ALS_TIME_GROUP_INDEX_0;
timeGroupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_TIME_GROUP];
timeItemNum = timeGroupNode->itemNum;
if (timeItemNum > EXTENDED_ALS_TIME_GROUP_INDEX_MAX) {
HDF_LOGE("%s: TimeItemNum out of range ", __func__);
return HDF_FAILURE;
}
ret = ReadSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, &regValue, sizeof(regValue));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to read sensor register array ", __func__);
return HDF_FAILURE;
}
regValue &= timeGroupNode->regCfgItem->mask;
temp = GetTimeByRegValue(regValue, g_timeMap, timeItemNum);
index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
if (index < 0) {
HDF_LOGE("%s: Index out of range ", __func__);
return HDF_FAILURE;
}
if (((rgbcData[ALS_R] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX) ||
(rgbcData[ALS_G] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX)) && (temp >= BH1745_TIME_320MSEC)) {
g_timeChangeStatus = SENSOR_TIME_DECREASE;
index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
index--;
ret = WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to write sensor register array ", __func__);
return HDF_FAILURE;
}
} else if (((rgbcData[ALS_R] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN) ||
(rgbcData[ALS_G] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN)) && (g_timeChangeStatus == SENSOR_TIME_DECREASE)) {
g_timeChangeStatus = SENSOR_TIME_INCREASE;
index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
index++;
if (index >= timeItemNum) {
HDF_LOGE("%s: Index out of range ", __func__);
return HDF_FAILURE;
}
ret = WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to write sensor register array ", __func__);
return HDF_FAILURE;
}
}
return HDF_SUCCESS;
}
static int32_t CalLux(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t *rgbcData)
{
int32_t ret;
uint32_t time;
uint32_t gain;
uint8_t regValue;
uint32_t index = 1;
uint32_t luxTemp;
uint8_t itemNum;
struct SensorRegCfgGroupNode *GroupNode = NULL;
int32_t timeIndex = EXTENDED_ALS_TIME_GROUP_INDEX_0;
int32_t gainIndex = EXTENDED_ALS_GAIN_GROUP_INDEX_0;
if (rgbcData[ALS_G] <= 0) {
HDF_LOGE("%s: RgbcData out of range ", __func__);
return HDF_FAILURE;
}
if (BH1745_MULTIPLE_100 * rgbcData[ALS_C] / rgbcData[ALS_G] < BH1745_COEFFICIENT_JUDGE) {
index = 0;
}
luxTemp = g_red[index] * rgbcData[ALS_R] + g_green[index] * rgbcData[ALS_G];
GroupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_TIME_GROUP];
itemNum = GroupNode->itemNum;
if (itemNum > EXTENDED_ALS_TIME_GROUP_INDEX_MAX) {
HDF_LOGE("%s: ItemNum out of range ", __func__);
return HDF_FAILURE;
}
ret = ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, timeIndex, &regValue, sizeof(regValue));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to read sensor register array ", __func__);
return HDF_FAILURE;
}
regValue &= GroupNode->regCfgItem->mask;
time = GetTimeByRegValue(regValue, g_timeMap, itemNum);
regValue = 0;
GroupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_GAIN_GROUP];
itemNum = GroupNode->itemNum;
if (itemNum > EXTENDED_ALS_GAIN_GROUP_INDEX_MAX) {
HDF_LOGE("%s: ItemNum out of range ", __func__);
return HDF_FAILURE;
}
ret = ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, gainIndex, &regValue, sizeof(regValue));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to read sensor register array ", __func__);
return HDF_FAILURE;
}
regValue &= GroupNode->regCfgItem->mask;
gain = GetGainByRegValue(regValue, g_gainMap, itemNum);
reportData->als = ((luxTemp / gain) * BH1745_GAIN_16X / time) * BH1745_TIME_160MSEC;
return HDF_SUCCESS;
}
static int32_t RawDataConvert(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t *rgbcData)
{
int ret;
ret = CalLux(CfgData, reportData, rgbcData);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Failed to calculate sensor brightness ", __func__);
return HDF_FAILURE;
}
reportData->als = (reportData->als > 0) ? reportData->als : 0;
ret = DynamicRangCovert(CfgData, rgbcData);
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "DynamicRangCovert");
return HDF_SUCCESS;
}
static int32_t ReadBh1745RawData(struct SensorCfgData *data, struct AlsData *rawData, int64_t *timestamp)
{
uint8_t status = 0;
uint8_t reg[ALS_LIGHT_BUTT];
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, BH1745_MODECONTROL3_ADDR, &status, sizeof(uint8_t));
if (!(status & BH1745_ALS_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret);
return HDF_FAILURE;
}
ret = ReadSensor(&data->busCfg, BH1745_ALS_R_LSB_ADDR, &reg[ALS_R_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_R_MSB_ADDR, &reg[ALS_R_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_G_LSB_ADDR, &reg[ALS_G_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_G_MSB_ADDR, &reg[ALS_G_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_B_LSB_ADDR, &reg[ALS_B_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_B_MSB_ADDR, &reg[ALS_B_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_C_LSB_ADDR, &reg[ALS_C_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BH1745_ALS_C_MSB_ADDR, &reg[ALS_C_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
rawData->red = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_R_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ALS_R_LSB]);
rawData->green = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_G_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ALS_G_LSB]);
rawData->blue = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_B_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ALS_B_LSB]);
rawData->clear = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_C_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[ALS_C_LSB]);
return HDF_SUCCESS;
}
int32_t ReadBh1745Data(struct SensorCfgData *data)
{
int32_t ret;
struct AlsData rawData = { 0, 0, 0, 0 };
uint32_t tmp[ALS_LIGHT_NUM];
struct SensorReportEvent event;
struct AlsReportData reportData;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
ret = ReadBh1745RawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BH1745 read raw data failed", __func__);
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_AMBIENT_LIGHT;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
tmp[ALS_R] = rawData.red;
tmp[ALS_G] = rawData.green;
tmp[ALS_B] = rawData.blue;
tmp[ALS_C] = rawData.clear;
ret = RawDataConvert(data, &reportData, tmp);
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "RawDataConvert");
event.dataLen = sizeof(reportData.als);
event.data = (uint8_t *)&reportData.als;
ret = ReportSensorEvent(&event);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BH1745 report data failed", __func__);
}
return ret;
}
static int32_t InitBh1745(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: BH1745 sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t InitAlsPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchBH1745(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Bh1745BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Bh1745 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchBH1745;
drvData->device = device;
device->service = &drvData->ioService;
g_bh1745DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Bh1745InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct AlsOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitAlsPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BH1745 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = AlsCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating alscfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadBh1745Data;
ret = AlsRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register BH1745 als failed", __func__);
return HDF_FAILURE;
}
ret = InitBh1745(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BH1745 als failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Bh1745ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
AlsReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_alsBh1745DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_ALS_BH1745",
.Bind = Bh1745BindDriver,
.Init = Bh1745InitDriver,
.Release = Bh1745ReleaseDriver,
};
HDF_INIT(g_alsBh1745DevEntry);

View File

@ -1,95 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef ALS_BH1745_H
#define ALS_BH1745_H
#include "sensor_als_driver.h"
#include "sensor_config_parser.h"
/* ALS DATA REGISTERS ADDR */
#define BH1745_ALS_R_LSB_ADDR 0X50
#define BH1745_ALS_R_MSB_ADDR 0X51
#define BH1745_ALS_G_LSB_ADDR 0X52
#define BH1745_ALS_G_MSB_ADDR 0X53
#define BH1745_ALS_B_LSB_ADDR 0X54
#define BH1745_ALS_B_MSB_ADDR 0X55
#define BH1745_ALS_C_LSB_ADDR 0X56
#define BH1745_ALS_C_MSB_ADDR 0X57
#define BH1745_MODECONTROL3_ADDR 0X44
/* ALS DATA READY */
#define BH1745_ALS_DATA_READY_MASK 0X02
/* ALS MULTIPLE */
#define BH1745_MULTIPLE_100 100
/* ALS COEFFICIENT */
#define BH1745_COEFFICIENT_RED 2
#define BH1745_COEFFICIENT_GREEN 2
#define BH1745_COEFFICIENT_RED_LEVEL_0 7730 // 7.73 * 1000
#define BH1745_COEFFICIENT_RED_LEVEL_1 9271 // 9.2715 * 1000
#define BH1745_COEFFICIENT_GREEN_LEVEL_0 13192 // 1.3192 * 1000
#define BH1745_COEFFICIENT_GREEN_LEVEL_1 21477 // 2.1477 * 1000
#define BH1745_COEFFICIENT_JUDGE 78 // 0.78*100
/* ALS TIME */
#define BH1745_TIME_160MSEC 160
#define BH1745_TIME_320MSEC 320
#define BH1745_TIME_640MSEC 640
#define BH1745_TIME_1280MSEC 1280
#define BH1745_TIME_2560MSEC 2560
#define BH1745_TIME_5120MSEC 5120
#define BH1745_TIME_MAX 5570475 // 65535*0.85*100
#define BH1745_TIME_MIN 1638375 // 65535*0.25*100
/* ALS GAIN */
#define BH1745_GAIN_1X 1
#define BH1745_GAIN_2X 2
#define BH1745_GAIN_16X 16
/* ALS TIME REG VALUE */
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_0 0x00
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_1 0x01
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_2 0x02
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_3 0x03
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_4 0x04
#define EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_5 0x05
/* ALS GAIN REG VALUE */
#define EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_0 0x00
#define EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_1 0x01
#define EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_2 0x02
enum ExtendedAlsTimeRegGroupIndex {
EXTENDED_ALS_TIME_GROUP_INDEX_0 = 0,
EXTENDED_ALS_TIME_GROUP_INDEX_1,
EXTENDED_ALS_TIME_GROUP_INDEX_2,
EXTENDED_ALS_TIME_GROUP_INDEX_3,
EXTENDED_ALS_TIME_GROUP_INDEX_4,
EXTENDED_ALS_TIME_GROUP_INDEX_5,
EXTENDED_ALS_TIME_GROUP_INDEX_MAX,
};
enum ExtendedAlsGainRegGroupIndex {
EXTENDED_ALS_GAIN_GROUP_INDEX_0 = 0,
EXTENDED_ALS_GAIN_GROUP_INDEX_1,
EXTENDED_ALS_GAIN_GROUP_INDEX_2,
EXTENDED_ALS_GAIN_GROUP_INDEX_MAX,
};
int32_t DetectAlsBim160Chip(struct SensorCfgData *data);
int32_t ReadBh1745Data(struct SensorCfgData *data);
struct Bh1745DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* ALS_BH1745_H */

View File

@ -1,416 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "barometer_bmp180.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_barometer_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#include "sensor_platform_if.h"
#define HDF_LOG_TAG hdf_sensor_barometer
static struct Bmp180DrvData *g_bmp180DrvData = NULL;
struct Bmp180DrvData *Bmp180GetDrvData(void)
{
return g_bmp180DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static struct BarometerEepromData g_calibraData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static int32_t ReadEepromRawData(struct SensorCfgData *data, uint8_t rfg[BAROMETER_EEPROM_SUM])
{
int32_t ret;
ret = ReadSensor(&data->busCfg, BMP180_AC1_MSB_ADDR, &rfg[BAROMETER_AC1_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC1_LSB_ADDR, &rfg[BAROMETER_AC1_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC2_MSB_ADDR, &rfg[BAROMETER_AC2_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC2_LSB_ADDR, &rfg[BAROMETER_AC2_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC3_MSB_ADDR, &rfg[BAROMETER_AC3_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC3_LSB_ADDR, &rfg[BAROMETER_AC3_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC4_MSB_ADDR, &rfg[BAROMETER_AC4_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC4_LSB_ADDR, &rfg[BAROMETER_AC4_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC5_MSB_ADDR, &rfg[BAROMETER_AC5_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC5_LSB_ADDR, &rfg[BAROMETER_AC5_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC6_MSB_ADDR, &rfg[BAROMETER_AC6_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_AC6_LSB_ADDR, &rfg[BAROMETER_AC6_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_B1_MSB_ADDR, &rfg[BAROMETER_B1_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_B1_LSB_ADDR, &rfg[BAROMETER_B1_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_B2_MSB_ADDR, &rfg[BAROMETER_B2_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_B2_LSB_ADDR, &rfg[BAROMETER_B2_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MB_MSB_ADDR, &rfg[BAROMETER_MB_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MB_LSB_ADDR, &rfg[BAROMETER_MB_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MC_MSB_ADDR, &rfg[BAROMETER_MC_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MC_LSB_ADDR, &rfg[BAROMETER_MC_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MD_MSB_ADDR, &rfg[BAROMETER_MD_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_MD_LSB_ADDR, &rfg[BAROMETER_MD_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
return ret;
}
static int32_t ReadEepromData(struct SensorCfgData *data, struct BarometerEepromData *g_calibraData)
{
int32_t ret;
uint8_t reg[BAROMETER_EEPROM_SUM];
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
ret = ReadEepromRawData(data, reg);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
g_calibraData->ac1 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC1_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC1_LSB]);
g_calibraData->ac2 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC2_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC2_LSB]);
g_calibraData->ac3 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC3_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC3_LSB]);
g_calibraData->ac4 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC4_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC4_LSB]);
g_calibraData->ac5 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC5_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC5_LSB]);
g_calibraData->ac6 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC6_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_AC6_LSB]);
g_calibraData->b1 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_B1_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_B1_LSB]);
g_calibraData->b2 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_B2_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_B2_LSB]);
g_calibraData->mb = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MB_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_MB_LSB]);
g_calibraData->mc = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MC_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_MC_LSB]);
g_calibraData->md = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MD_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_MD_LSB]);
return ret;
}
static int32_t ReadTempData(struct SensorCfgData *data, struct BarometerData *Temp)
{
int32_t ret;
uint8_t status = 0;
uint8_t reg[BAROMETER_TEM_SUM];
uint8_t value[SENSOR_VALUE_BUTT];
value[SENSOR_ADDR_INDEX] = BMP180_CONTROL_REG_ADDR;
value[SENSOR_VALUE_INDEX] = BMP180_COVERT_TEMP;
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
ret = ReadSensor(&data->busCfg, BMP180_COVERT_PRES_3, &status, sizeof(uint8_t));
if ((status & BMP180_STATUS_ADDR) == BMP180_STATUS_JUDGE) {
WriteSensor(&data->busCfg, value, sizeof(value));
OsalMDelay(DELAY_0);
ret = ReadSensor(&data->busCfg, BMP180_OUT_MSB_ADDR, &reg[BAROMETER_TEM_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_OUT_LSB_ADDR, &reg[BAROMETER_TEM_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
Temp->unpensateTemp = (int32_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_TEM_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[BAROMETER_TEM_LSB]);
}
return ret;
}
static int32_t ReadBarometerData(struct SensorCfgData *data, struct BarometerData *Barom)
{
int32_t ret;
uint8_t status = 0;
uint8_t reg[BAROMETER_BAR_SUM];
uint8_t value[SENSOR_VALUE_BUTT];
value[SENSOR_ADDR_INDEX] = BMP180_CONTROL_REG_ADDR;
value[SENSOR_VALUE_INDEX] = BMP180_COVERT_PRES_1;
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
ret = ReadSensor(&data->busCfg, BMP180_COVERT_PRES_3, &status, sizeof(uint8_t));
if ((status & BMP180_STATUS_ADDR) == BMP180_STATUS_JUDGE) {
WriteSensor(&data->busCfg, value, sizeof(value));
OsalMDelay(DELAY_1);
ret = ReadSensor(&data->busCfg, BMP180_OUT_MSB_ADDR, &reg[BAROMETER_BAR_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_OUT_LSB_ADDR, &reg[BAROMETER_BAR_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMP180_OUT_XLSB_ADDR, &reg[BAROMETER_BAR_XLSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
Barom->unpensatePre = (int32_t)(SENSOR_DATA_SHIFT_RIGHT(
(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_BAR_MSB], SENSOR_DATA_WIDTH_16_BIT) |
SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_BAR_LSB], SENSOR_DATA_WIDTH_8_BIT) | reg[BAROMETER_BAR_XLSB]),
(BMP180_CONSTANT_4 - OSSETTING)));
}
return ret;
}
static int32_t CalcBarometerData(struct BarometerData *barometerData, int32_t tnp[BAROMETER_SUM])
{
struct Coefficient coefficientData = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Calculated temperature
coefficientData.x1 = ((barometerData->unpensateTemp - g_calibraData.ac6) * (g_calibraData.ac5))
>> BMP180_CONSTANT_8;
coefficientData.x2 = (g_calibraData.mc << BMP180_CONSTANT_5) / (coefficientData.x1 + g_calibraData.md);
coefficientData.b5 = coefficientData.x1 + coefficientData.x2;
tnp[BAROMETER_TEMPERATURE] = (coefficientData.b5 + BMP180_CONSTANT_4) >> BMP180_CONSTANT_3;
// Calculated pressure
coefficientData.b6 = coefficientData.b5 - BMP180_CONSTANT_12;
coefficientData.x1 = (g_calibraData.b2 * ((coefficientData.b6 * coefficientData.b6) >> BMP180_CONSTANT_6))
>> BMP180_CONSTANT_5;
coefficientData.x2 = (g_calibraData.ac2 * coefficientData.b6) >> BMP180_CONSTANT_5;
coefficientData.x3 = coefficientData.x1 + coefficientData.x2;
coefficientData.b3 = (((((int32_t)g_calibraData.ac1) * BMP180_CONSTANT_3 + coefficientData.x3) << OSSETTING)
+ BMP180_CONSTANT_2) >> BMP180_CONSTANT_2;
coefficientData.x1 = (g_calibraData.ac3 * coefficientData.b6) >> BMP180_CONSTANT_7;
coefficientData.x2 = (g_calibraData.b1 * ((coefficientData.b6 * coefficientData.b6) >> BMP180_CONSTANT_6))
>> BMP180_CONSTANT_9;
coefficientData.x3 = ((coefficientData.x1 + coefficientData.x2) + BMP180_CONSTANT_2) >> BMP180_CONSTANT_2;
coefficientData.b4 = (g_calibraData.ac4 * (uint32_t)(coefficientData.x3 + BMP180_CONSTANT_13))
>> BMP180_CONSTANT_8;
coefficientData.b7 = ((uint32_t)(barometerData->unpensatePre) - (uint32_t)coefficientData.b3)
* (BMP180_CONSTANT_14 >> OSSETTING);
if (coefficientData.b7 < BMP180_CONSTANT_15) {
coefficientData.p = (coefficientData.b7 << BMP180_CONSTANT_1) / coefficientData.b4;
} else {
coefficientData.p = (coefficientData.b7 / coefficientData.b4) << BMP180_CONSTANT_1;
}
coefficientData.x1 = (coefficientData.p >> BMP180_CONSTANT_4) * (coefficientData.p >> BMP180_CONSTANT_4);
coefficientData.x1 = (coefficientData.x1 * BMP180_CONSTANT_10) >> BMP180_CONSTANT_9;
coefficientData.x2 = (BMP180_CONSTANT_0 * coefficientData.p) >> BMP180_CONSTANT_9;
tnp[BAROMETER_BAROMETER] = coefficientData.p + ((coefficientData.x1 + coefficientData.x2
+ BMP180_CONSTANT_11) >> BMP180_CONSTANT_3);
return HDF_SUCCESS;
}
int32_t ReadBmp180Data(struct SensorCfgData *data)
{
int32_t ret;
int32_t tmp[BAROMETER_SUM];
struct BarometerData barometerData = {0, 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 = ReadTempData(data, &barometerData);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
ret = ReadBarometerData(data, &barometerData);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
ret = CalcBarometerData(&barometerData, tmp);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_BAROMETER;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
return ret;
}
static int32_t InitBmp180(struct SensorCfgData *data)
{
int32_t ret;
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
struct SensorReportEvent event;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
ret = ReadEepromData(data, &g_calibraData);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMP180 sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t InitBarometerPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchBMP180(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Bmp180BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchBMP180;
drvData->device = device;
device->service = &drvData->ioService;
g_bmp180DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Bmp180InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct BarometerOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitBarometerPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMp180 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = BarometerCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating barometercfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadBmp180Data;
ret = BarometerRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register BMp180 barometer failed", __func__);
return HDF_FAILURE;
}
ret = InitBmp180(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMP180 barometer failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Bmp180ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
BarometerReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_barometerBmp180DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_BAROMETER_BMP180",
.Bind = Bmp180BindDriver,
.Init = Bmp180InitDriver,
.Release = Bmp180ReleaseDriver,
};
HDF_INIT(g_barometerBmp180DevEntry);

View File

@ -1,96 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef BAROMETER_BMP180_H
#define BAROMETER_BMP180_H
#include "sensor_barometer_driver.h"
#include "sensor_config_parser.h"
#define BMP180_REG_CHIP_ID 0xD0
// i2c slave address
#define BMP180_ADDR 0x77
// Define calibration register address
#define BMP180_AC1_MSB_ADDR 0xAA
#define BMP180_AC1_LSB_ADDR 0xAB
#define BMP180_AC2_MSB_ADDR 0xAC
#define BMP180_AC2_LSB_ADDR 0xAD
#define BMP180_AC3_MSB_ADDR 0xAE
#define BMP180_AC3_LSB_ADDR 0xAF
#define BMP180_AC4_MSB_ADDR 0xB0
#define BMP180_AC4_LSB_ADDR 0xB1
#define BMP180_AC5_MSB_ADDR 0xB2
#define BMP180_AC5_LSB_ADDR 0xB3
#define BMP180_AC6_MSB_ADDR 0xB4
#define BMP180_AC6_LSB_ADDR 0xB5
#define BMP180_B1_MSB_ADDR 0xB6
#define BMP180_B1_LSB_ADDR 0xB7
#define BMP180_B2_MSB_ADDR 0xB8
#define BMP180_B2_LSB_ADDR 0xB9
#define BMP180_MB_MSB_ADDR 0xBA
#define BMP180_MB_LSB_ADDR 0xBB
#define BMP180_MC_MSB_ADDR 0xBC
#define BMP180_MC_LSB_ADDR 0xBD
#define BMP180_MD_MSB_ADDR 0xBE
#define BMP180_MD_LSB_ADDR 0xBf
// Control register
#define BMP180_CONTROL_REG_ADDR 0xF4
#define BMP180_COVERT_TEMP 0x2E
#define BMP180_COVERT_PRES_0 0x34
#define BMP180_COVERT_PRES_1 0x74
#define BMP180_COVERT_PRES_2 0xB4
#define BMP180_COVERT_PRES_3 0xF4
#define BMP180_OUT_MSB_ADDR 0xF6
#define BMP180_OUT_LSB_ADDR 0xF7
#define BMP180_OUT_XLSB_ADDR 0xF8
#define BMP180_STATUS_ADDR 0X20
#define BMP180_STATUS_JUDGE 0X00
#define SENSOR_DATA_WIDTH_16_BIT 16
#define OSSETTING 1
#define DELAY_0 5
#define DELAY_1 8
#define OSS_TIME_MS 26
#define BMP180_CONSTANT_0 (-7357)
#define BMP180_CONSTANT_1 1
#define BMP180_CONSTANT_2 2
#define BMP180_CONSTANT_3 4
#define BMP180_CONSTANT_4 8
#define BMP180_CONSTANT_5 11
#define BMP180_CONSTANT_6 12
#define BMP180_CONSTANT_7 13
#define BMP180_CONSTANT_8 15
#define BMP180_CONSTANT_9 16
#define BMP180_CONSTANT_10 3038
#define BMP180_CONSTANT_11 3791
#define BMP180_CONSTANT_12 4000
#define BMP180_CONSTANT_13 32768
#define BMP180_CONSTANT_14 50000
#define BMP180_CONSTANT_15 0x80000000
int32_t DetectBarometerBmp180Chip(struct SensorCfgData *data);
int32_t ReadBmp180Data(struct SensorCfgData *data);
struct Bmp180DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* BAROMETER_BMP180_H */

View File

@ -1,231 +0,0 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "gyro_bmi160.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_gyro_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#define HDF_LOG_TAG hdf_sensor_gyro
static struct Bmi160DrvData *g_bmi160DrvData = NULL;
struct Bmi160DrvData *GyroBmi160GetDrvData(void)
{
return g_bmi160DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static int32_t ReadBmi160GyroRawData(struct SensorCfgData *data, struct GyroData *rawData, int64_t *timestamp)
{
uint8_t status = 0;
uint8_t reg[GYRO_AXIS_BUTT];
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, BMI160_STATUS_ADDR, &status, sizeof(uint8_t));
if (!(status & BMI160_GYRO_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
return HDF_FAILURE;
}
ret = ReadSensor(&data->busCfg, BMI160_GYRO_X_LSB_ADDR, &reg[GYRO_X_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_GYRO_X_MSB_ADDR, &reg[GYRO_X_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_GYRO_Y_LSB_ADDR, &reg[GYRO_Y_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_GYRO_Y_MSB_ADDR, &reg[GYRO_Y_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_GYRO_Z_LSB_ADDR, &reg[GYRO_Z_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_GYRO_Z_MSB_ADDR, &reg[GYRO_Z_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[GYRO_X_AXIS_LSB]);
rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[GYRO_Y_AXIS_LSB]);
rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[GYRO_Z_AXIS_LSB]);
return ret;
}
int32_t ReadBmi160GyroData(struct SensorCfgData *data)
{
int32_t ret;
struct GyroData rawData = { 0, 0, 0 };
int32_t tmp[GYRO_AXIS_NUM];
struct SensorReportEvent event;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
ret = ReadBmi160GyroRawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_GYROSCOPE;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
tmp[GYRO_X_AXIS] = rawData.x * BMI160_GYRO_SENSITIVITY_2000DPS;
tmp[GYRO_Y_AXIS] = rawData.y * BMI160_GYRO_SENSITIVITY_2000DPS;
tmp[GYRO_Z_AXIS] = rawData.z * BMI160_GYRO_SENSITIVITY_2000DPS;
ret = SensorRawDataToRemapData(data->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMI160 convert raw data failed", __func__);
return HDF_FAILURE;
}
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
return ret;
}
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;
}
static int32_t InitGyroPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t GyroBmi160BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchBMI160;
drvData->device = device;
device->service = &drvData->ioService;
g_bmi160DrvData = drvData;
return HDF_SUCCESS;
}
int32_t GyroBmi160InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct GyroOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitGyroPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = GyroCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating gyrocfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadBmi160GyroData;
ret = GyroRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register BMI160 gyro failed", __func__);
return HDF_FAILURE;
}
ret = InitBmi160(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 gyro failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void GyroBmi160ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
GyroReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_gyroBmi160DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_GYRO_BMI160",
.Bind = GyroBmi160BindDriver,
.Init = GyroBmi160InitDriver,
.Release = GyroBmi160ReleaseDriver,
};
HDF_INIT(g_gyroBmi160DevEntry);

View File

@ -1,61 +0,0 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef GYRO_BMI160_H
#define GYRO_BMI160_H
#include "sensor_gyro_driver.h"
#include "sensor_config_parser.h"
/* GYRO DATA REGISTERS ADDR */
#define BMI160_GYRO_X_LSB_ADDR 0X0C
#define BMI160_GYRO_X_MSB_ADDR 0X0D
#define BMI160_GYRO_Y_LSB_ADDR 0X0E
#define BMI160_GYRO_Y_MSB_ADDR 0X0F
#define BMI160_GYRO_Z_LSB_ADDR 0X10
#define BMI160_GYRO_Z_MSB_ADDR 0X11
#define BMI160_STATUS_ADDR 0X1B
/* GYRO ODR */
#define BMI160_GYRO_ODR_RESERVED 0x00
#define BMI160_GYRO_ODR_25HZ 0x06
#define BMI160_GYRO_ODR_50HZ 0x07
#define BMI160_GYRO_ODR_100HZ 0x08
#define BMI160_GYRO_ODR_200HZ 0x09
#define BMI160_GYRO_ODR_400HZ 0x0A
#define BMI160_GYRO_ODR_800HZ 0x0B
#define BMI160_GYRO_ODR_1600HZ 0x0C
#define BMI160_GYRO_ODR_3200HZ 0x0D
/* default HZ */
#define BMI160_GYRO_DEFAULT_ODR_100HZ 100
#define BMI160_GYRO_DEFAULT_ODR_25HZ 25
/* GYRO RANGE */
#define BMI160_GYRO_RANGE_2000DPS 0X00
#define BMI160_GYRO_RANGE_1000DPS 0X01
#define BMI160_GYRO_RANGE_500DPS 0X02
#define BMI160_GYRO_RANGE_250DPS 0X03
#define BMI160_GYRO_RANGE_125DPS 0X04
/* GYRO sensitivity */
#define BMI160_GYRO_SENSITIVITY_2000DPS 61
/* GYRO DATA READY */
#define BMI160_GYRO_DATA_READY_MASK 0x40
int32_t DetectGyroBim160Chip(struct SensorCfgData *data);
int32_t ReadBmi160Data(struct SensorCfgData *data);
struct Bmi160DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* GYRO_BMI160_H */

View File

@ -1,123 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "hall_ak8789.h"
#include "osal_irq.h"
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#include "sensor_hall_driver.h"
#define HDF_LOG_TAG hdf_sensor_hall
static struct Ak8789DrvData *g_ak8789DrvData = NULL;
struct Ak8789DrvData *Ak8789GetDrvData(void)
{
return g_ak8789DrvData;
}
/* IO config for int-pin and Gpio-pin */
#define SENSOR_HALL_DATA_REG_ADDR 0x114f0040
#define SENSOR_HALL_CLK_REG_ADDR 0x114f0044
#define SENSOR_HALL_REG_CFG 0x400
static int32_t InitHallPreConfig(void)
{
if (SetSensorPinMux(SENSOR_HALL_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_HALL_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchAK8789(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Ak8789BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Ak8789 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchAK8789;
drvData->device = device;
device->service = &drvData->ioService;
g_ak8789DrvData = drvData;
return HDF_SUCCESS;
}
int32_t AK8789InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct HallOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitHallPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init AK8789 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = HallCreateCfgData(device->property);
if (drvData->sensorCfg == NULL) {
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = NULL;
ret = HallRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register AK8789 hall failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Ak8789ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
HallReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
OsalMemFree(drvData);
}
struct HdfDriverEntry g_hallAk8789DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_HALL_AK8789",
.Bind = Ak8789BindDriver,
.Init = AK8789InitDriver,
.Release = Ak8789ReleaseDriver,
};
HDF_INIT(g_hallAk8789DevEntry);

View File

@ -1,24 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef HALL_AK8789_H
#define HALL_AK8789_H
#include "sensor_config_parser.h"
#include "sensor_hall_driver.h"
int32_t DetectHallAk8789Chip(struct SensorCfgData *data);
int32_t ReadAk8789Data(struct SensorCfgData *data);
struct Ak8789DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* HALL_AK8789_H */

View File

@ -1,241 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "magnetic_lsm303.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#include "sensor_magnetic_driver.h"
#define HDF_LOG_TAG hdf_sensor_magnetic
static struct Lsm303DrvData *g_lsm303DrvData = NULL;
struct Lsm303DrvData *Lsm303GetDrvData(void)
{
return g_lsm303DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static int32_t ReadLsm303RawData(struct SensorCfgData *data, struct MagneticData *rawData, int64_t *timestamp)
{
uint8_t status = 0;
uint8_t reg[MAGNETIC_AXIS_BUTT];
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, LSM303_STATUS_ADDR, &status, sizeof(uint8_t));
if (!(status & LSM303_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret);
return HDF_FAILURE;
}
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_X_MSB_ADDR, &reg[MAGNETIC_X_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_X_LSB_ADDR, &reg[MAGNETIC_X_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Y_MSB_ADDR, &reg[MAGNETIC_Y_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Y_LSB_ADDR, &reg[MAGNETIC_Y_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Z_MSB_ADDR, &reg[MAGNETIC_Z_AXIS_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Z_LSB_ADDR, &reg[MAGNETIC_Z_AXIS_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[MAGNETIC_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[MAGNETIC_X_AXIS_LSB]);
rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[MAGNETIC_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[MAGNETIC_Y_AXIS_LSB]);
rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[MAGNETIC_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[MAGNETIC_Z_AXIS_LSB]);
return HDF_SUCCESS;
}
int32_t ReadLsm303Data(struct SensorCfgData *data)
{
struct MagneticData rawData = { 0, 0, 0 };
int32_t tmp[MAGNETIC_AXIS_NUM];
struct SensorReportEvent event;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
(void)memset_s(tmp, sizeof(tmp), 0, sizeof(tmp));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
int32_t ret = ReadLsm303RawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: LSM303 read raw data failed", __func__);
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_MAGNETIC_FIELD;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
tmp[MAGNETIC_X_AXIS] = rawData.x * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_XY47GA;
tmp[MAGNETIC_Y_AXIS] = rawData.y * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_XY47GA;
tmp[MAGNETIC_Z_AXIS] = rawData.z * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_Z47GA;
ret = SensorRawDataToRemapData(data->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: LSM303 convert raw data failed", __func__);
return HDF_FAILURE;
}
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: LSM303 report data failed", __func__);
}
return ret;
}
static int32_t InitLsm303(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: Lsm303 sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t InitMagneticPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchLsm303(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Lsm303BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Lsm303 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchLsm303;
drvData->device = device;
device->service = &drvData->ioService;
g_lsm303DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Lsm303InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct MagneticOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitMagneticPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init Lsm303 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = MagneticCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating magneticcfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadLsm303Data;
ret = MagneticRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register lsm303 magnetic failed", __func__);
return HDF_FAILURE;
}
ret = InitLsm303(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init lsm303 magnetic failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Lsm303ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
MagneticReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_magneticLsm303DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_MAGNETIC_LSM303",
.Bind = Lsm303BindDriver,
.Init = Lsm303InitDriver,
.Release = Lsm303ReleaseDriver,
};
HDF_INIT(g_magneticLsm303DevEntry);

View File

@ -1,84 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef MAGNETIC_LSM303_H
#define MAGNETIC_LSM303_H
#include "sensor_config_parser.h"
#include "sensor_magnetic_driver.h"
#define LSM303_MAGNETIC_GIN 1000
/* MAGNETIC SET RATE AND MODE ADDR */
#define LSM303_CRA_REG_ADDR 0X00
#define LSM303_CRB_REG_ADDR 0X01
#define LSM303_MR_REG_ADDR 0X02
/* MAGNETIC DATA REGISTERS ADDR */
#define LSM303_MAGNETIC_X_MSB_ADDR 0X03
#define LSM303_MAGNETIC_X_LSB_ADDR 0X04
#define LSM303_MAGNETIC_Y_MSB_ADDR 0X05
#define LSM303_MAGNETIC_Y_LSB_ADDR 0X06
#define LSM303_MAGNETIC_Z_MSB_ADDR 0X07
#define LSM303_MAGNETIC_Z_LSB_ADDR 0X08
#define LSM303_STATUS_ADDR 0X09
/* MAGNETIC DATA RATE CONFIG HZ */
#define LSM303_DATA_RATE_0 0X00
#define LSM303_DATA_RATE_1 0X04
#define LSM303_DATA_RATE_2 0X08
#define LSM303_DATA_RATE_3 0X0C
#define LSM303_DATA_RATE_4 0X10
#define LSM303_DATA_RATE_5 0X14
#define LSM303_DATA_RATE_6 0X18
#define LSM303_DATA_RATE_7 0X1C
/* MAGNETIC GAIN CONFIG GAUSS */
#define LSM303_GAIN_RATE_0 0X20
#define LSM303_GAIN_RATE_1 0X40
#define LSM303_GAIN_RATE_2 0X60
#define LSM303_GAIN_RATE_3 0X80
#define LSM303_GAIN_RATE_4 0XA0
#define LSM303_GAIN_RATE_5 0XC0
#define LSM303_GAIN_RATE_6 0XE0
/* MAGNETIC GAIN SENSITIVITY RANGE */
#define LSM303DLHC_SENSITIVITY_XY13GA 1100
#define LSM303DLHC_SENSITIVITY_XY19GA 855
#define LSM303DLHC_SENSITIVITY_XY25GA 670
#define LSM303DLHC_SENSITIVITY_XY40GA 450
#define LSM303DLHC_SENSITIVITY_XY47GA 400
#define LSM303DLHC_SENSITIVITY_XY56GA 330
#define LSM303DLHC_SENSITIVITY_XY81GA 230
#define LSM303DLHC_SENSITIVITY_Z13GA 980
#define LSM303DLHC_SENSITIVITY_Z19GA 760
#define LSM303DLHC_SENSITIVITY_Z25GA 600
#define LSM303DLHC_SENSITIVITY_Z40GA 400
#define LSM303DLHC_SENSITIVITY_Z47GA 355
#define LSM303DLHC_SENSITIVITY_Z56GA 295
#define LSM303DLHC_SENSITIVITY_Z81GA 205
/* MAGNETIC MODE CONFIG */
#define LSM303_OPERATING_MODE_1 0X00
#define LSM303_OPERATING_MODE_2 0X01
#define LSM303_OPERATING_MODE_3 0X02
#define LSM303_OPERATING_MODE_4 0X03
/* MAGNETIC DATA READY */
#define LSM303_DATA_READY_MASK 0x01
int32_t DetectMagneticLsm303Chip(struct SensorCfgData *data);
int32_t ReadLsm303Data(struct SensorCfgData *data);
struct Lsm303DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* MAGNETIC_LSM303_H */

View File

@ -1,209 +0,0 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "pedometer_bmi160.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#include "sensor_pedometer_driver.h"
#define HDF_LOG_TAG hdf_sensor_pedometer
static struct Bmi160DrvData *g_bmi160DrvData = NULL;
struct Bmi160DrvData *PedometerBmi160GetDrvData(void)
{
return g_bmi160DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static int32_t ReadBmi160PedometerRawData(struct SensorCfgData *data, struct PedometerData *rawData, int64_t *timestamp)
{
uint8_t reg[PEDOMETER_BUTT];
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
(void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;
int32_t ret = ReadSensor(&data->busCfg, BMI160_PEDOMETER_LSB_ADDR, &reg[PEDOMETER_NU_LSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
ret = ReadSensor(&data->busCfg, BMI160_PEDOMETER_MSB_ADDR, &reg[PEDOMETER_NU_MSB], sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
rawData->pedometer = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[PEDOMETER_NU_MSB], SENSOR_DATA_WIDTH_8_BIT) |
reg[PEDOMETER_NU_LSB]);
return HDF_SUCCESS;
}
int32_t ReadBmi160PedometerData(struct SensorCfgData *data)
{
int32_t ret;
struct PedometerData rawData = { 0 };
static int32_t tmp;
struct SensorReportEvent event;
ret = memset_s(&event, sizeof(event), 0, sizeof(event));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "memset_s");
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
ret = ReadBmi160PedometerRawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMI160 read raw data failed", __func__);
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_PEDOMETER;
event.option = 0;
event.mode = SENSOR_WORK_MODE_REALTIME;
tmp = rawData.pedometer;
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: BMI160 report data failed", __func__);
}
return ret;
}
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;
}
static int32_t InitPedometerPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t PedometerBmi160BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchBMI160;
drvData->device = device;
device->service = &drvData->ioService;
g_bmi160DrvData = drvData;
return HDF_SUCCESS;
}
int32_t PedometerBmi160InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct PedometerOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitPedometerPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = PedometerCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating pedometercfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadBmi160PedometerData;
ret = PedometerRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register BMI160 pedometer failed", __func__);
return HDF_FAILURE;
}
ret = InitBmi160(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init BMI160 pedometer failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void PedometerBmi160ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
PedometerReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_pedometerBmi160DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_PEDOMETER_BMI160",
.Bind = PedometerBmi160BindDriver,
.Init = PedometerBmi160InitDriver,
.Release = PedometerBmi160ReleaseDriver,
};
HDF_INIT(g_pedometerBmi160DevEntry);

View File

@ -1,33 +0,0 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef PEDOMETER_BMI160_H
#define PEDOMETER_BMI160_H
#include "sensor_pedometer_driver.h"
#include "sensor_config_parser.h"
#define PEDOMETER_TEST_NUM 1000
/* PEDOMETER DATA REGISTERS ADDR */
#define BMI160_PEDOMETER_LSB_ADDR 0x78
#define BMI160_PEDOMETER_MSB_ADDR 0x79
#define BMI160_STATUS_ADDR 0x1B
/* PEDOMETER DATA READY */
#define BMI160_PEDOMETER_DATA_READY_MASK 0x80
int32_t DetectPedometerBim160Chip(struct SensorCfgData *data);
int32_t ReadBmi160PedometerData(struct SensorCfgData *data);
struct Bmi160DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* PEDOMETER_BMI160_H */

View File

@ -1,431 +0,0 @@
/*
* Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "ppg_cs1262.h"
#include <securec.h>
#include "sensor_ppg_driver.h"
#include "sensor_device_if.h"
#include "osal_mem.h"
#include "osal_time.h"
#define HDF_LOG_TAG hdf_sensor_cs1262
static struct Cs1262DrvData *g_cs1262DrvData = NULL;
static struct Cs1262DrvData *GetDrvData(void)
{
return g_cs1262DrvData;
}
static int32_t ResetChip()
{
uint8_t resetCmd[] = { CS1262_CHIP_REST_CMD, CS1262_SPI_DUMMY_DATA, CS1262_SPI_DUMMY_DATA };
if (Cs1262WriteData(resetCmd, HDF_ARRAY_SIZE(resetCmd)) != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262WriteData fail", __func__);
return HDF_FAILURE;
}
// delay 1 ms
OsalMDelay(1);
return HDF_SUCCESS;
}
static int32_t ResetModule(uint16_t moduleOffset)
{
uint8_t ret;
ret = Cs1262WriteRegbit(CS1262_RSTCON_REG, moduleOffset, CS1262_REG_BIT_SET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: CS1262_RST module fail CS1262_REG_BIT_SET", __func__);
return HDF_FAILURE;
}
ret = Cs1262WriteRegbit(CS1262_RSTCON_REG, moduleOffset, CS1262_REG_BIT_RESET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: CS1262_RST module fail CS1262_REG_BIT_RESET", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t RegLock(Cs1262LockStatus lockStatus)
{
int32_t ret;
uint16_t regRead = 0;
Cs1262RegGroup lockGroup[] = {
{.regAddr = CS1262_WRPROT_REG, .regVal = CS1262_LOCK, 1},
{.regAddr = CS1262_WRPROT_REG, .regVal = CS1262_UN_LOCK1, 1},
{.regAddr = CS1262_WRPROT_REG, .regVal = CS1262_UN_LOCK2, 1},
{.regAddr = CS1262_WRPROT_REG, .regVal = CS1262_UN_LOCK3, 1}
};
// lock write 1 byte 'CS1262_LOCK'; unlock need write 3 bytes
uint16_t lockGroupLen = (lockStatus == CS1262_REG_LOCK) ? 1 : 3;
ret = Cs1262WriteGroup(&lockGroup[lockStatus], lockGroupLen);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262WriteGroup fail", __func__);
return HDF_FAILURE;
}
ret = Cs1262ReadRegs(CS1262_SYS_STATE_REG, &regRead, 1);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262ReadRegs CS1262_SYS_STATE_REG fail", __func__);
return HDF_FAILURE;
}
if ((regRead & LOCK_REG_OFFSET) == lockStatus) {
return HDF_SUCCESS;
}
HDF_LOGE("%s: Cs1262 Lock fail status = %d", __func__, lockStatus);
return HDF_FAILURE;
}
static int32_t ReadFifo(uint8_t *outBuf, uint16_t outBufMaxLen, uint16_t *outLen)
{
uint8_t ret;
uint16_t fifoState = 0;
uint16_t fifoNum;
ret = Cs1262ReadRegs(CS1262_FIFO_STATE_REG, &fifoState, 1);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read reg %d fail", __func__, CS1262_FIFO_STATE_REG);
return HDF_FAILURE;
}
fifoNum = fifoState & FIFO_NUM_OFFSET;
// empty
if ((fifoNum == 0) || (fifoState & FIFO_EMPTY_OFFSET)) {
HDF_LOGI("%s: data FIFO is empty, no need read.", __func__);
return HDF_SUCCESS;
}
// full
if (fifoState & FIFO_FULL_OFFSET) {
fifoNum = CS1262_MAX_FIFO_READ_NUM;
}
// overflow
if ((fifoNum * sizeof(Cs1262FifoVal)) > outBufMaxLen) {
fifoNum = (outBufMaxLen / sizeof(Cs1262FifoVal));
}
(void)memset_s(outBuf, outBufMaxLen, 0, (fifoNum * sizeof(Cs1262FifoVal)));
ret = Cs1262ReadFifoReg((Cs1262FifoVal *)outBuf, fifoNum);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: ReadFifoReg fail", __func__);
return HDF_FAILURE;
}
*outLen = fifoNum * sizeof(Cs1262FifoVal);
return HDF_SUCCESS;
}
static inline int32_t SetIER(uint16_t it, uint8_t status)
{
return Cs1262WriteRegbit(CS1262_IER_REG, it, status);
}
static inline int32_t ClearIFR(uint16_t it)
{
return Cs1262WriteRegbit(CS1262_IFR_REG, it, CS1262_REG_BIT_SET);
}
int32_t Cs1262SetOption(uint32_t option)
{
HDF_LOGI("%s: cs1262 setOption :%d", __func__, option);
return HDF_SUCCESS;
}
static int32_t Writefw(Cs1262RegConfigTab *regTab)
{
int32_t ret;
Cs1262RegGroup regGroup[] = {
{ .regAddr = CS1262_CLOCK_REG, .regVal = regTab->clock, 1 },
{ .regAddr = CS1262_TL_BA, .regValGroup = regTab->tlTab, .regLen = TL_REGS_NUM },
{ .regAddr = CS1262_TX_BA, .regValGroup = regTab->txTab, .regLen = TX_REGS_NUM },
{ .regAddr = CS1262_RX_BA, .regValGroup = regTab->rxTab, .regLen = RX_REGS_NUM },
{ .regAddr = CS1262_TE_BA, .regValGroup = regTab->teTab, .regLen = TE_REGS_NUM },
{ .regAddr = CS1262_FIFO_OFFSET_REG, .regValGroup = (regTab->fifoTab + FIFO_WRITE_OFFSET),
.regLen = (FIFO_REGS_NUM - FIFO_WRITE_OFFSET)}
};
if (RegLock(CS1262_REG_UNLOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg unlock failed", __func__);
return HDF_FAILURE;
}
ret = Cs1262WriteGroup(regGroup, HDF_ARRAY_SIZE(regGroup));
HDF_LOGI("%s: cs1262 init ret :%d", __func__, ret);
if (RegLock(CS1262_REG_LOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg lock failed", __func__);
return HDF_FAILURE;
}
return ret;
}
int32_t Cs1262SetMode(uint32_t mode)
{
int32_t ret;
Cs1262RegConfigTab *regTab = NULL;
struct Cs1262DrvData *drvData = GetDrvData();
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
if ((mode == NONE_MODE) || (drvData->regMode == mode)) {
HDF_LOGI("%s: mode = %d, drvData->regMode = %d", __func__, mode, drvData->regMode);
drvData->regMode = mode;
return HDF_SUCCESS;
}
ret = Cs1262Loadfw(mode, &regTab);
if ((ret != HDF_SUCCESS) || (regTab == NULL)) {
HDF_LOGE("%s: Cs1262Loadfw failed", __func__);
return HDF_FAILURE;
}
ret = Writefw(regTab);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Writefw failed", __func__);
return HDF_FAILURE;
}
drvData->regMode = mode;
HDF_LOGI("%s: set mode success", __func__);
return HDF_SUCCESS;
}
int32_t Cs1262ReadData(uint8_t *outBuf, uint16_t outBufMaxLen, uint16_t *outLen)
{
uint8_t ret;
uint16_t ifr = 0;
CHECK_NULL_PTR_RETURN_VALUE(outBuf, HDF_ERR_INVALID_PARAM);
ret = Cs1262ReadRegs(CS1262_IFR_REG, &ifr, 1);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262ReadRegs CS1262_IFR_REG fail", __func__);
return HDF_FAILURE;
}
if (ifr & IFR_RDY_FLAG) {
ret = ReadFifo(outBuf, outBufMaxLen, outLen);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: ReadFifo fail", __func__);
}
ret = ClearIFR(FIFO_RDY_IFR_OFFSET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: ClearIFR fail", __func__);
return HDF_FAILURE;
}
}
return HDF_SUCCESS;
}
int32_t Cs1262Enable()
{
int32_t ret;
struct Cs1262DrvData *drvData = GetDrvData();
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
if (drvData->regMode == NONE_MODE) {
HDF_LOGW("%s: drvData->regMode == NONE_MODE, need set default mode when enable", __func__);
if (Cs1262SetMode(DEFAULT_MODE) != HDF_SUCCESS) {
HDF_LOGE("%s: set default mode failed", __func__);
return HDF_FAILURE;
}
}
if (RegLock(CS1262_REG_UNLOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg unlock failed", __func__);
return HDF_FAILURE;
}
ret = SetIER(FIFO_RDY_IER_OFFSET, CS1262_REG_BIT_SET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262Enable fail", __func__);
return HDF_FAILURE;
}
ret = ResetModule(CS1262_TE_RST_OFFSET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: reset failed", __func__);
return HDF_FAILURE;
}
ret = Cs1262WriteRegbit(CS1262_TE_CTRL_REG, PRF_START_BIT, CS1262_REG_BIT_SET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Cs1262WriteRegbit CS1262_TE_CTRL_REG fail", __func__);
return HDF_FAILURE;
}
if (RegLock(CS1262_REG_LOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg lock failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
int32_t Cs1262Disable()
{
int32_t ret;
if (RegLock(CS1262_REG_UNLOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg unlock failed", __func__);
return HDF_FAILURE;
}
ret = SetIER(FIFO_RDY_IER_OFFSET, CS1262_REG_BIT_RESET);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: SetIER fail", __func__);
return HDF_FAILURE;
}
ret = Cs1262WriteRegbit(CS1262_TE_CTRL_REG, PRF_START_BIT, CS1262_REG_BIT_RESET);
HDF_LOGI("%s: Cs1262 disable ret = %d", __func__, ret);
if (RegLock(CS1262_REG_LOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: reg unlock failed", __func__);
return HDF_FAILURE;
}
return ret;
}
static int32_t InitChip()
{
(void)Cs1262SetMode(NONE_MODE);
return ResetChip();
}
static int32_t CheckChipId(struct PpgCfgData *cfgData)
{
uint16_t regRead = 0;
uint8_t cnt = 0;
uint16_t reg = cfgData->sensorCfg.sensorAttr.chipIdReg;
uint16_t val = cfgData->sensorCfg.sensorAttr.chipIdValue;
// retry 3 times
while (cnt++ < 3) {
if ((Cs1262ReadRegs(reg, &regRead, 1) == HDF_SUCCESS) && (regRead == val)) {
HDF_LOGI("%s: cs1262 read chip id success!", __func__);
return HDF_SUCCESS;
}
}
HDF_LOGE("%s: cs1262 read chip id fail, reg=%u!", __func__, reg);
return HDF_FAILURE;
}
static int32_t DispatchCs1262(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Cs1262BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Cs1262DrvData *drvData = (struct Cs1262DrvData *)OsalMemCalloc(sizeof(*drvData));
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_MALLOC_FAIL);
(void)memset_s(drvData, sizeof(struct Cs1262DrvData), 0, sizeof(struct Cs1262DrvData));
drvData->ioService.Dispatch = DispatchCs1262;
drvData->device = device;
device->service = &drvData->ioService;
g_cs1262DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Cs1262InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Cs1262DrvData *drvData = (struct Cs1262DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = ParsePpgCfgData(device->property, &(drvData->ppgCfg));
if ((ret != HDF_SUCCESS) || (drvData->ppgCfg == NULL)) {
HDF_LOGE("%s: cs1262 construct fail!", __func__);
return HDF_FAILURE;
}
ret = Cs1262InitSpi(&drvData->ppgCfg->sensorCfg.busCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: cs1262 init spi!", __func__);
return HDF_FAILURE;
}
ret = CheckChipId(drvData->ppgCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: cs1262 check chip fail!", __func__);
return HDF_FAILURE;
}
ret = InitChip();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: cs1262 init chip fail!", __func__);
return HDF_FAILURE;
}
struct PpgChipData chipData = {
.cfgData = drvData->ppgCfg,
.opsCall = {
.ReadData = Cs1262ReadData,
.Enable = Cs1262Enable,
.Disable = Cs1262Disable,
.SetOption = Cs1262SetOption,
.SetMode = Cs1262SetMode,
}
};
ret = RegisterPpgChip(&chipData);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register CS1262 failed", __func__);
return HDF_FAILURE;
}
HDF_LOGI("%s: cs1262 init driver success", __func__);
return HDF_SUCCESS;
}
void Cs1262ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Cs1262DrvData *drvData = (struct Cs1262DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
CHECK_NULL_PTR_RETURN(drvData->ppgCfg);
Cs1262ReleaseSpi(&drvData->ppgCfg->sensorCfg.busCfg);
(void)memset_s(drvData, sizeof(struct Cs1262DrvData), 0, sizeof(struct Cs1262DrvData));
OsalMemFree(drvData);
}
struct HdfDriverEntry g_ppgCs1262DevEntry = {
.moduleVersion = CS1262_MODULE_VER,
.moduleName = "HDF_SENSOR_PPG_CS1262",
.Bind = Cs1262BindDriver,
.Init = Cs1262InitDriver,
.Release = Cs1262ReleaseDriver,
};
HDF_INIT(g_ppgCs1262DevEntry);

View File

@ -1,72 +0,0 @@
/*
* Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef MODEL_SENSOR_DRIVER_CHIPSET_PPG_PPG_CS1262_H_
#define MODEL_SENSOR_DRIVER_CHIPSET_PPG_PPG_CS1262_H_
#include "ppg_cs1262_spi.h"
#include "hdf_device_desc.h"
/****************************************** DEFINE ******************************************/
#ifdef __LITEOS__
#define HEART_REG_TAB "/etc/cs1262_default.bin"
#else
#define HEART_REG_TAB "/system/etc/ppgconfig/cs1262_default.bin"
#endif
#define CS1262_MAX_FIFO_READ_NUM 800
#define CS1262_DEVICE_ID 0x1262
#define CS1262_MODULE_VER 1
/******************************************* ENUM ********************************************/
typedef enum {
CS1262_REG_LOCK = 0,
CS1262_REG_UNLOCK,
} Cs1262LockStatus;
/***************************************** TYPEDEF ******************************************/
// tab regs num
#define SYS_REGS_NUM 6
#define TL_REGS_NUM 40
#define TX_REGS_NUM 15
#define RX_REGS_NUM 77
#define TE_REGS_NUM 38
#define WEAR_REGS_NUM 25
#define FIFO_REGS_NUM 10
#define FIFO_WRITE_OFFSET 1
typedef struct {
uint16_t startMagic;
uint16_t clock;
uint16_t tlTab[TL_REGS_NUM];
uint16_t txTab[TX_REGS_NUM];
uint16_t rxTab[RX_REGS_NUM];
uint16_t teTab[TE_REGS_NUM];
uint16_t wearTab[WEAR_REGS_NUM];
uint16_t fifoTab[FIFO_REGS_NUM];
uint16_t checksum;
} Cs1262RegConfigTab;
enum PpgMode {
NONE_MODE = 0,
DEFAULT_MODE,
HEART_RATE_MODE = DEFAULT_MODE,
REG_MODE_MAX,
};
struct Cs1262DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct PpgCfgData *ppgCfg;
enum PpgMode regMode;
};
struct PpgModeTab {
enum PpgMode mode;
Cs1262RegConfigTab *regTab;
};
int32_t Cs1262Loadfw(enum PpgMode mode, Cs1262RegConfigTab **configTab);
#endif // MODEL_SENSOR_DRIVER_CHIPSET_PPG_PPG_CS1262_H_

View File

@ -1,100 +0,0 @@
/*
* Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include <securec.h>
#include "osal_file.h"
#include "hdf_log.h"
#include "ppg_cs1262.h"
#define PPG_CONFIG_START_MAGIC 0x1262
#define HDF_LOG_TAG hdf_sensor_cs1262_fw
static Cs1262RegConfigTab g_heartRegTab = {
0x1262,
0x0020,
{
0x0029, 0x473D, 0x003D, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{
0x02C2, 0x0040, 0x0080, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x00FF, 0xFFFF
},
{
0x4224, 0x2224, 0x2222, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0600, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x7000, 0x0050, 0x70A0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x7028, 0x0050, 0x70C8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x7028, 0x0028, 0x00A0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x7000, 0x00F0, 0x70A0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0010, 0x0000, 0x0000, 0x0000, 0x0000
},
{
0x0300, 0x0000, 0x0000, 0x0500, 0x000A, 0x0001, 0x0000, 0x0000, 0x0000, 0x1E0F, 0x0002, 0x0028,
0x579E, 0x579E, 0x579E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0840, 0x0800, 0x0800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000
},
{
0x6160, 0x0020, 0x0202, 0x0000, 0x2200, 0x7FFF, 0x00C0, 0x01F4, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000
},
{
0x0429, 0x0000, 0x0064, 0x000A, 0x0002, 0x0882, 0x00DF, 0x0223, 0x0002, 0x0000,
},
0x00F9
};
static struct PpgModeTab g_ppgModeTab[] = {
{ .mode = HEART_RATE_MODE, .regTab = &g_heartRegTab },
};
static int32_t validateTab(uint8_t *regTab, uint16_t valiSize)
{
uint8_t checksum = 0;
uint16_t index = 0;
if (((uint16_t *)regTab)[0] != PPG_CONFIG_START_MAGIC) {
HDF_LOGE("%s: validateTab invalid", __func__);
return HDF_FAILURE;
}
/* real checksum stored in the last 2 bytes, needs to be removed during calculation */
for (index = 0; index < valiSize - 2; index++) {
checksum += regTab[index];
}
/* real checksum stored in the last 2 bytes, and one item of regTab has 2 bytes,
* needs to be divided by 2 when calculating the real checksum's index.
*/
index = (valiSize / 2) - 1;
return (checksum == ((uint16_t *)regTab)[index]) ? HDF_SUCCESS : HDF_FAILURE;
}
int32_t Cs1262Loadfw(enum PpgMode mode, Cs1262RegConfigTab **configTab)
{
int32_t ret;
for (uint8_t index = 0; index < HDF_ARRAY_SIZE(g_ppgModeTab); index++) {
if (g_ppgModeTab[index].mode == mode) {
ret = validateTab((uint8_t *)(g_ppgModeTab[index].regTab), sizeof(*g_ppgModeTab[index].regTab));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: validateTab fail mode = %d", __func__, mode);
return HDF_FAILURE;
}
*configTab = g_ppgModeTab[index].regTab;
HDF_LOGI("%s: load success", __func__);
return HDF_SUCCESS;
}
}
HDF_LOGE("%s: Cs1262Loadfw not match, mode = %d", __func__, mode);
return HDF_FAILURE;
}

View File

@ -1,281 +0,0 @@
/*
* Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "ppg_cs1262_spi.h"
#include "spi_if.h"
#define HDF_LOG_TAG hdf_sensor_cs1262_spi
#define CS1262_HEAD_BUF_SINGLEREG_LEN 4
#define CS1262_HEAD_BUF_FULL_LEN 6
static struct SensorBusCfg *g_busCfg = NULL;
static inline uint16_t ReverseEndianInt16(uint16_t data)
{
return (((data & 0x00FF) << 8) |
((data & 0xFF00) >> 8));
}
static inline uint16_t Cs1262RegAddrConvert(uint16_t reg)
{
/* CS1262 takes the register address from bit[2] to bit[15], bit[1~0] fixed as 00,
* so reg needs to be shifted left by 2 bits to convert it to the address format required by CS1262
*/
return (reg << 2) & 0x0FFC;
}
static inline uint8_t Cs1262GetHighByteInt16(uint16_t data)
{
return (data & 0xFF00) >> 8;
}
static inline uint8_t Cs1262GetLowByteInt16(uint16_t data)
{
return data & 0x00FF;
}
void Cs1262ReleaseSpi(struct SensorBusCfg *busCfg)
{
SpiClose(busCfg->spiCfg.handle);
busCfg->spiCfg.handle = NULL;
g_busCfg = NULL;
}
int32_t Cs1262InitSpi(struct SensorBusCfg *busCfg)
{
int32_t ret;
CHECK_NULL_PTR_RETURN_VALUE(busCfg, HDF_ERR_INVALID_PARAM);
struct SpiDevInfo spiDevinfo = {
.busNum = busCfg->spiCfg.busNum,
.csNum = busCfg->spiCfg.csNum,
};
HDF_LOGI("%s: SpiOpen busNum = %d, csNum = %d", __func__, spiDevinfo.busNum, spiDevinfo.csNum);
busCfg->spiCfg.handle = SpiOpen(&spiDevinfo);
if (busCfg->spiCfg.handle == NULL) {
HDF_LOGE("%s: SpiOpen failed", __func__);
return HDF_FAILURE;
}
HDF_LOGD("%s: SpiSetCfg: maxSpeedHz:%d, mode=%d, transferMode=%d, bitsPerWord=%d",
__func__, busCfg->spiCfg.spi.maxSpeedHz, busCfg->spiCfg.spi.mode,
busCfg->spiCfg.spi.transferMode, busCfg->spiCfg.spi.bitsPerWord);
ret = SpiSetCfg(busCfg->spiCfg.handle, &busCfg->spiCfg.spi);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: SpiSetCfg failed", __func__);
SpiClose(busCfg->spiCfg.handle);
return ret;
}
g_busCfg = busCfg;
return HDF_SUCCESS;
}
int32_t Cs1262ReadFifoReg(Cs1262FifoVal *fifoBuf, uint16_t fifoLen)
{
CHECK_NULL_PTR_RETURN_VALUE(fifoBuf, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(g_busCfg, HDF_ERR_NOT_SUPPORT);
uint8_t headBuf[CS1262_HEAD_BUF_FULL_LEN] = {
CS1262_SPI_NOCHECK_CONTINUOUSREAD,
0,
Cs1262GetHighByteInt16(Cs1262RegAddrConvert(CS1262_FIFO_DATA_REG)),
Cs1262GetLowByteInt16(Cs1262RegAddrConvert(CS1262_FIFO_DATA_REG)),
Cs1262GetHighByteInt16(fifoLen - 1),
Cs1262GetLowByteInt16(fifoLen - 1),
};
struct SpiMsg msg[] = {
{
.wbuf = headBuf,
.rbuf = NULL,
.len = CS1262_HEAD_BUF_FULL_LEN,
.keepCs = 0, // cs low
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz,
},
{
.wbuf = NULL,
.rbuf = (uint8_t *)fifoBuf,
.len = fifoLen * sizeof(Cs1262FifoVal),
.keepCs = 1, // cs high
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
}
};
if (SpiTransfer(g_busCfg->spiCfg.handle, msg, HDF_ARRAY_SIZE(msg)) != HDF_SUCCESS) {
HDF_LOGE("%s: Read Fifo data use spi failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
int32_t Cs1262ReadRegs(uint16_t regAddr, uint16_t *dataBuf, uint16_t dataLen)
{
CHECK_NULL_PTR_RETURN_VALUE(dataBuf, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(g_busCfg, HDF_ERR_NOT_SUPPORT);
uint8_t headBuf[CS1262_HEAD_BUF_FULL_LEN] = {
CS1262_SPI_NOCHECK_SINGLEREAD,
0,
Cs1262GetHighByteInt16(Cs1262RegAddrConvert(regAddr)),
Cs1262GetLowByteInt16(Cs1262RegAddrConvert(regAddr))
};
uint8_t bufLen = CS1262_HEAD_BUF_SINGLEREG_LEN;
if (dataLen > 1) {
headBuf[0] = CS1262_SPI_NOCHECK_CONTINUOUSREAD;
headBuf[bufLen++] = Cs1262GetHighByteInt16(dataLen - 1);
headBuf[bufLen++] = Cs1262GetLowByteInt16(dataLen - 1);
}
struct SpiMsg msg[] = {
{
.wbuf = headBuf,
.rbuf = NULL,
.len = bufLen,
.keepCs = 0,
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
},
{
.wbuf = NULL,
.rbuf = (uint8_t *)dataBuf,
.len = dataLen * 2, // 2 bytes reg
.keepCs = 1, // 1 means enable
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
}
};
if (SpiTransfer(g_busCfg->spiCfg.handle, msg, HDF_ARRAY_SIZE(msg)) != HDF_SUCCESS) {
HDF_LOGE("%s: Read data use spi failed", __func__);
return HDF_FAILURE;
}
for (uint16_t index = 0; index < dataLen; index++) {
*(dataBuf + index) = ReverseEndianInt16(*(dataBuf + index));
}
return HDF_SUCCESS;
}
int32_t Cs1262WriteRegs(uint16_t regAddr, uint16_t *dataBuf, uint16_t dataLen)
{
CHECK_NULL_PTR_RETURN_VALUE(dataBuf, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(g_busCfg, HDF_ERR_NOT_SUPPORT);
uint8_t headBuf[CS1262_HEAD_BUF_FULL_LEN] = {
CS1262_SPI_NOCHECK_SINGLEWRITE,
0,
Cs1262GetHighByteInt16(Cs1262RegAddrConvert(regAddr)),
Cs1262GetLowByteInt16(Cs1262RegAddrConvert(regAddr))
};
uint8_t bufLen = CS1262_HEAD_BUF_SINGLEREG_LEN;
if (dataLen > 1) {
headBuf[0] = CS1262_SPI_NOCHECK_CONTINUOUSWRITE;
headBuf[bufLen++] = Cs1262GetHighByteInt16(dataLen - 1);
headBuf[bufLen++] = Cs1262GetLowByteInt16(dataLen - 1);
}
for (uint16_t index = 0; index < dataLen; index++) {
*(dataBuf + index) = ReverseEndianInt16(*(dataBuf + index));
}
struct SpiMsg msg[] = {
{
.wbuf = headBuf,
.rbuf = NULL,
.len = bufLen,
.keepCs = 0,
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
},
{
.wbuf = (uint8_t *)dataBuf,
.rbuf = NULL,
.len = dataLen * 2, // 1 data has 2 bytes
.keepCs = 1, // 1 means enable
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
}
};
if (SpiTransfer(g_busCfg->spiCfg.handle, msg, HDF_ARRAY_SIZE(msg)) != HDF_SUCCESS) {
HDF_LOGE("%s: Write data use spi failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
inline int32_t Cs1262WriteReg(uint16_t regAddr, uint16_t data)
{
return Cs1262WriteRegs(regAddr, &data, 1);
}
int32_t Cs1262WriteData(uint8_t *data, uint16_t dataLen)
{
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
CHECK_NULL_PTR_RETURN_VALUE(g_busCfg, HDF_ERR_NOT_SUPPORT);
struct SpiMsg msg = {
.wbuf = data,
.rbuf = NULL,
.len = dataLen,
.keepCs = 1, // 1 means enable
.delayUs = 0,
.speed = g_busCfg->spiCfg.spi.maxSpeedHz
};
return SpiTransfer(g_busCfg->spiCfg.handle, &msg, 1);
}
int32_t Cs1262WriteRegbit(uint16_t regAddr, uint16_t setbit, Cs1262BitStatus bitval)
{
uint16_t regData = 0;
if (Cs1262ReadRegs(regAddr, &regData, 1) != HDF_SUCCESS) {
HDF_LOGE("%s: failed", __func__);
return HDF_FAILURE;
}
if (bitval == CS1262_REG_BIT_SET) {
regData |= (uint16_t)(1 << setbit);
} else {
regData &= (uint16_t)(~(1 << setbit));
}
return Cs1262WriteRegs(regAddr, &regData, 1);
}
int32_t Cs1262WriteGroup(Cs1262RegGroup *regGroup, uint16_t groupLen)
{
int32_t ret;
CHECK_NULL_PTR_RETURN_VALUE(regGroup, HDF_ERR_INVALID_PARAM);
for (uint16_t index = 0; index < groupLen; index++) {
if (regGroup[index].regLen == 1) {
ret = Cs1262WriteReg(regGroup[index].regAddr, regGroup[index].regVal);
} else {
ret = Cs1262WriteRegs(regGroup[index].regAddr, regGroup[index].regValGroup, regGroup[index].regLen);
}
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: failed, index = %u", __func__, index);
return HDF_FAILURE;
}
}
return HDF_SUCCESS;
}

View File

@ -1,147 +0,0 @@
/*
* Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef PPG_CS1262_SPI_H
#define PPG_CS1262_SPI_H
#include "hdf_types.h"
#include "sensor_platform_if.h"
/***************************************** TYPEDEF ******************************************/
typedef enum {
CS1262_REG_BIT_RESET = 0,
CS1262_REG_BIT_SET = 1,
} Cs1262BitStatus;
typedef struct {
uint16_t regAddr;
union {
uint16_t regVal;
uint16_t *regValGroup;
};
uint16_t regLen;
} Cs1262RegGroup;
typedef struct {
uint32_t reserve : 2;
uint32_t adc_data : 22;
uint32_t tl : 2;
uint32_t rx : 2;
uint32_t phaseGroup : 4;
} Cs1262FifoVal;
/****************************************** DEFINE ******************************************/
// lock reg
#define CS1262_LOCK 0x0000
// unlock reg
#define CS1262_UN_LOCK1 0x0059
#define CS1262_UN_LOCK2 0x0016
#define CS1262_UN_LOCK3 0x0088
/****************************************** OFFSET ******************************************/
// PRF
#define PRF_START_BIT (0x0000)
// REG WR PROT
#define LOCK_REG_OFFSET (0x01u << 0)
// RESET CON
#define FIFORST_REG_OFFSET (0x01u << 1)
#define TERST_REG_OFFSET (0x01u << 2)
#define ADCRST_REG_OFFSET (0x01u << 3)
// IER IFR
#define INT_MODE_TRIGER (0 << 15) // 0 triger
#define LED_WARN_IER_OFFSET 5
#define REG_ERR_IER_OFFSET 4
#define TS_RDY_IER_OFFSET 3
#define DATA_RDY_IER_OFFSET 2
#define THR_DET_IER_OFFSET 1
#define FIFO_RDY_IER_OFFSET 0
#define LED_WARN_IFR_OFFSET 5
#define REG_ERR_IFR_OFFSET 4
#define TS_RDY_IFR_OFFSET 3
#define DATA_RDY_IFR_OFFSET 2
#define THR_DET_IFR_OFFSET 1
#define FIFO_RDY_IFR_OFFSET 0
#define IFR_RDY_FLAG 0x0001
// TE CTRL
#define PRF_START_OFFSET (0x01u << 0)
// FIFO STATE
#define FIFO_FULL_OFFSET (0x01u << 12)
#define FIFO_EMPTY_OFFSET (0x01u << 11)
#define FIFO_NUM_OFFSET 0x3FF
// RESET OFFSET
#define CS1262_FIFO_RST_OFFSET 1
#define CS1262_TE_RST_OFFSET 2
#define CS1262_ADC_RST_OFFSET 3
/******************************************* REGS *******************************************/
// SYS_BA
#define CS1262_SYS_BA (0x0000)
#define CS1262_WRPROT_REG (CS1262_SYS_BA + 0x00)
#define CS1262_CLOCK_REG (CS1262_SYS_BA + 0x01)
#define CS1262_RSTCON_REG (CS1262_SYS_BA + 0x02)
#define CS1262_IER_REG (CS1262_SYS_BA + 0x03)
#define CS1262_IFR_REG (CS1262_SYS_BA + 0x04)
#define CS1262_SYS_STATE_REG (CS1262_SYS_BA + 0x05)
// TL_BA
#define CS1262_TL_BA (0x0010)
// TX_BA
#define CS1262_TX_BA (0x0050)
// RX_BA
#define CS1262_RX_BA (0x0070)
// TE_BA
#define CS1262_TE_BA (0x00E0)
#define CS1262_TE_CTRL_REG (CS1262_TE_BA + 0x00)
// TE_BA
#define CS1262_WEAR_BA (0x0120)
// FIFO & ADC
#define CS1262_ADC_BA (0x140)
#define CS1262_FIFO_DATA_REG (CS1262_ADC_BA + 0x00)
#define CS1262_FIFO_STATE_REG (CS1262_ADC_BA + 0x01)
#define CS1262_FIFO_OFFSET_REG (CS1262_ADC_BA + 0x02)
// ID_BA
#define CS1262_ID_BA (0x1F0)
#define CS1262_CHIP_ID1_REG (CS1262_ID_BA + 0x01)
#define CS1262_ENTER_DEEPSLEEP_CMD 0x08C4
#define CS1262_EXIT_DEEPSLEEP_CMD 0x08C1
#define CS1262_CHIP_REST_CMD 0xC2
/****************************************** DEFINE ******************************************/
#define CS1262_SPI_NOCHECK_SINGLEWRITE 0x95
#define CS1262_SPI_CKSUNCHECK_SINGLEWRITE 0x96 // checksum for single write
#define CS1262_SPI_NOCHECK_CONTINUOUSWRITE 0x99
#define CS1262_SPI_CKSUNCHECK_CONTINUOUSWRITE 0x9A // checksum for continuous write
#define CS1262_SPI_NOCHECK_SINGLEREAD 0x65
#define CS1262_SPI_CKSUNCHECK_SINGLEREAD 0x66 // checksum for single read
#define CS1262_SPI_NOCHECK_CONTINUOUSREAD 0x69
#define CS1262_SPI_CKSUNCHECK_CONTINUOUSREAD 0x6A // checksum for continuous read
#define CS1262_SPI_ACK_ADDR 0xA3
#define CS1262_SPI_NACK_ADDR 0xA3
#define CS1262_SPI_ACK_DATA 0xA5
#define CS1262_SPI_NACK_DATA 0xAA
#define CS1262_SPI_DUMMY_DATA 0x00
/*********************************** function prototypes *************************************/
int32_t Cs1262ReadRegs(uint16_t regAddr, uint16_t *dataBuf, uint16_t dataLen);
int32_t Cs1262WriteReg(uint16_t regAddr, uint16_t data);
int32_t Cs1262WriteRegs(uint16_t regAddr, uint16_t *dataBuf, uint16_t dataLen);
int32_t Cs1262WriteRegbit(uint16_t regAddr, uint16_t setbit, Cs1262BitStatus bitval);
int32_t Cs1262WriteData(uint8_t *data, uint16_t dataLen);
int32_t Cs1262WriteGroup(Cs1262RegGroup *regGroup, uint16_t groupLen);
int32_t Cs1262ReadFifoReg(Cs1262FifoVal *fifoBuf, uint16_t fifoLen);
int32_t Cs1262InitSpi(struct SensorBusCfg *busCfg);
void Cs1262ReleaseSpi(struct SensorBusCfg *busCfg);
#endif /* CS1262_SPI_H */

View File

@ -1,205 +0,0 @@
/*
* Copyright (c) 2021 xu
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "proximity_apds9960.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"
#include "sensor_proximity_driver.h"
#define HDF_LOG_TAG hdf_sensor_proximity
#define PROXIMITY_STATE_FAR 5
#define PROXIMITY_STATE_NEAR 0
static struct Apds9960DrvData *g_apds9960DrvData = NULL;
struct Apds9960DrvData *Apds9960GetDrvData(void)
{
return g_apds9960DrvData;
}
/* IO config for int-pin and I2C-pin */
#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
#define SENSOR_I2C6_CLK_REG_ADDR 0x114f0048
#define SENSOR_I2C_REG_CFG 0x403
static int32_t ReadApds9960RawData(struct SensorCfgData *data, struct ProximityData *rawData, int64_t *timestamp)
{
OsalTimespec time;
(void)memset_s(&time, sizeof(time), 0, sizeof(time));
CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
if (OsalGetTime(&time) != HDF_SUCCESS) {
HDF_LOGE("%s: Get time failed", __func__);
return HDF_FAILURE;
}
*timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
int32_t ret = ReadSensor(&data->busCfg, APDS9960_PROXIMITY_DATA_ADDR, &rawData->stateFlag, sizeof(uint8_t));
CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
return ret;
}
int32_t ReadApds9960Data(struct SensorCfgData *data)
{
int32_t ret;
int32_t tmp;
struct ProximityData rawData = { 5 };
struct SensorReportEvent event;
(void)memset_s(&event, sizeof(event), 0, sizeof(event));
ret = ReadApds9960RawData(data, &rawData, &event.timestamp);
if (ret != HDF_SUCCESS) {
return HDF_FAILURE;
}
event.sensorId = SENSOR_TAG_PROXIMITY;
event.option = 0;
event.mode = SENSOR_WORK_MODE_ON_CHANGE;
if (rawData.stateFlag <= APDS9960_PROXIMITY_THRESHOLD) {
tmp = PROXIMITY_STATE_FAR;
} else {
tmp = PROXIMITY_STATE_NEAR;
}
event.dataLen = sizeof(tmp);
event.data = (uint8_t *)&tmp;
ret = ReportSensorEvent(&event);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: APDS9960 report data failed", __func__);
}
return ret;
}
static int32_t InitApda9960(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: APDS9960 sensor init config failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t InitProximityPreConfig(void)
{
if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Data write mux pin failed", __func__);
return HDF_FAILURE;
}
if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
HDF_LOGE("%s: Clk write mux pin failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t DispatchApds9960(struct HdfDeviceIoClient *client,
int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)client;
(void)cmd;
(void)data;
(void)reply;
return HDF_SUCCESS;
}
int32_t Apds9960BindDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)OsalMemCalloc(sizeof(*drvData));
if (drvData == NULL) {
HDF_LOGE("%s: Malloc Apds9960 drv data fail", __func__);
return HDF_ERR_MALLOC_FAIL;
}
drvData->ioService.Dispatch = DispatchApds9960;
drvData->device = device;
device->service = &drvData->ioService;
g_apds9960DrvData = drvData;
return HDF_SUCCESS;
}
int32_t Apds996InitDriver(struct HdfDeviceObject *device)
{
int32_t ret;
struct ProximityOpsCall ops;
CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)device->service;
CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
ret = InitProximityPreConfig();
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init APDS9960 bus mux config", __func__);
return HDF_FAILURE;
}
drvData->sensorCfg = ProximityCreateCfgData(device->property);
if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
HDF_LOGD("%s: Creating proximitycfg failed because detection failed", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ops.Init = NULL;
ops.ReadData = ReadApds9960Data;
ret = ProximityRegisterChipOps(&ops);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Register APDS9960 proximity failed", __func__);
return HDF_FAILURE;
}
ret = InitApda9960(drvData->sensorCfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Init APDS9960 proximity failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
void Apds996ReleaseDriver(struct HdfDeviceObject *device)
{
CHECK_NULL_PTR_RETURN(device);
struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)device->service;
CHECK_NULL_PTR_RETURN(drvData);
if (drvData->sensorCfg != NULL) {
ProximityReleaseCfgData(drvData->sensorCfg);
drvData->sensorCfg = NULL;
}
OsalMemFree(drvData);
}
struct HdfDriverEntry g_proximityApds9960DevEntry = {
.moduleVersion = 1,
.moduleName = "HDF_SENSOR_PROXIMITY_APDS9960",
.Bind = Apds9960BindDriver,
.Init = Apds996InitDriver,
.Release = Apds996ReleaseDriver,
};
HDF_INIT(g_proximityApds9960DevEntry);

View File

@ -1,28 +0,0 @@
/*
* Copyright (c) 2021 xu
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef PROXIMITY_APDS9960_H
#define PROXIMITY_APDS9960_H
#include "sensor_config_parser.h"
#include "sensor_proximity_driver.h"
#define APDS9960_PROXIMITY_DATA_ADDR 0X9C // Proximity Data
#define APDS9960_PROXIMITY_THRESHOLD 7 // threshold
int32_t DetectProximityApds9960Chip(struct SensorCfgData *data);
int32_t ReadApds9960Data(struct SensorCfgData *data);
struct Apds9960DrvData {
struct IDeviceIoService ioService;
struct HdfDeviceObject *device;
struct SensorCfgData *sensorCfg;
};
#endif /* PROXIMITY_APDS9960_H */

View File

@ -9,7 +9,6 @@
#include "sensor_hall_driver.h"
#include <securec.h>
#include "gpio_if.h"
#include "hall_ak8789.h"
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "osal_irq.h"