1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci *
4094332d3Sopenharmony_ci * HDF is dual licensed: you can use it either under the terms of
5094332d3Sopenharmony_ci * the GPL, or the BSD license, at your option.
6094332d3Sopenharmony_ci * See the LICENSE file in the root of this repository for complete details.
7094332d3Sopenharmony_ci */
8094332d3Sopenharmony_ci
9094332d3Sopenharmony_ci#include "barometer_bmp180.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "osal_mem.h"
12094332d3Sopenharmony_ci#include "osal_time.h"
13094332d3Sopenharmony_ci#include "sensor_barometer_driver.h"
14094332d3Sopenharmony_ci#include "sensor_config_controller.h"
15094332d3Sopenharmony_ci#include "sensor_device_manager.h"
16094332d3Sopenharmony_ci#include "sensor_platform_if.h"
17094332d3Sopenharmony_ci
18094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_barometer_driver
19094332d3Sopenharmony_ci
20094332d3Sopenharmony_cistatic struct Bmp180DrvData *g_bmp180DrvData = NULL;
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ci/* IO config for int-pin and I2C-pin */
23094332d3Sopenharmony_ci#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
24094332d3Sopenharmony_ci#define SENSOR_I2C6_CLK_REG_ADDR  0x114f0048
25094332d3Sopenharmony_ci#define SENSOR_I2C_REG_CFG        0x403
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistatic struct BarometerEepromData g_calibraData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_cistatic int32_t ReadEepromRawData(struct SensorCfgData *data, uint8_t rfg[BAROMETER_EEPROM_SUM])
30094332d3Sopenharmony_ci{
31094332d3Sopenharmony_ci    int32_t ret;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC1_MSB_ADDR, &rfg[BAROMETER_AC1_MSB], sizeof(uint8_t));
34094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC1_LSB_ADDR, &rfg[BAROMETER_AC1_LSB], sizeof(uint8_t));
37094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC2_MSB_ADDR, &rfg[BAROMETER_AC2_MSB], sizeof(uint8_t));
40094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC2_LSB_ADDR, &rfg[BAROMETER_AC2_LSB], sizeof(uint8_t));
43094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC3_MSB_ADDR, &rfg[BAROMETER_AC3_MSB], sizeof(uint8_t));
46094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC3_LSB_ADDR, &rfg[BAROMETER_AC3_LSB], sizeof(uint8_t));
49094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC4_MSB_ADDR, &rfg[BAROMETER_AC4_MSB], sizeof(uint8_t));
52094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC4_LSB_ADDR, &rfg[BAROMETER_AC4_LSB], sizeof(uint8_t));
55094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC5_MSB_ADDR, &rfg[BAROMETER_AC5_MSB], sizeof(uint8_t));
58094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC5_LSB_ADDR, &rfg[BAROMETER_AC5_LSB], sizeof(uint8_t));
61094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC6_MSB_ADDR, &rfg[BAROMETER_AC6_MSB], sizeof(uint8_t));
64094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
65094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_AC6_LSB_ADDR, &rfg[BAROMETER_AC6_LSB], sizeof(uint8_t));
66094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
67094332d3Sopenharmony_ci
68094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_B1_MSB_ADDR, &rfg[BAROMETER_B1_MSB], sizeof(uint8_t));
69094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_B1_LSB_ADDR, &rfg[BAROMETER_B1_LSB], sizeof(uint8_t));
72094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_B2_MSB_ADDR, &rfg[BAROMETER_B2_MSB], sizeof(uint8_t));
75094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_B2_LSB_ADDR, &rfg[BAROMETER_B2_LSB], sizeof(uint8_t));
78094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MB_MSB_ADDR, &rfg[BAROMETER_MB_MSB], sizeof(uint8_t));
81094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MB_LSB_ADDR, &rfg[BAROMETER_MB_LSB], sizeof(uint8_t));
84094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MC_MSB_ADDR, &rfg[BAROMETER_MC_MSB], sizeof(uint8_t));
87094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MC_LSB_ADDR, &rfg[BAROMETER_MC_LSB], sizeof(uint8_t));
90094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MD_MSB_ADDR, &rfg[BAROMETER_MD_MSB], sizeof(uint8_t));
93094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_MD_LSB_ADDR, &rfg[BAROMETER_MD_LSB], sizeof(uint8_t));
96094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci    return ret;
99094332d3Sopenharmony_ci}
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_cistatic int32_t ReadEepromData(struct SensorCfgData *data, struct BarometerEepromData *g_calibraData)
102094332d3Sopenharmony_ci{
103094332d3Sopenharmony_ci    int32_t ret;
104094332d3Sopenharmony_ci    uint8_t reg[BAROMETER_EEPROM_SUM];
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    ret = ReadEepromRawData(data, reg);
111094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
112094332d3Sopenharmony_ci    return HDF_FAILURE;
113094332d3Sopenharmony_ci    }
114094332d3Sopenharmony_ci
115094332d3Sopenharmony_ci    g_calibraData->ac1 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC1_MSB], SENSOR_DATA_WIDTH_8_BIT) |
116094332d3Sopenharmony_ci    reg[BAROMETER_AC1_LSB]);
117094332d3Sopenharmony_ci    g_calibraData->ac2 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC2_MSB], SENSOR_DATA_WIDTH_8_BIT) |
118094332d3Sopenharmony_ci    reg[BAROMETER_AC2_LSB]);
119094332d3Sopenharmony_ci    g_calibraData->ac3 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC3_MSB], SENSOR_DATA_WIDTH_8_BIT) |
120094332d3Sopenharmony_ci    reg[BAROMETER_AC3_LSB]);
121094332d3Sopenharmony_ci    g_calibraData->ac4 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC4_MSB], SENSOR_DATA_WIDTH_8_BIT) |
122094332d3Sopenharmony_ci    reg[BAROMETER_AC4_LSB]);
123094332d3Sopenharmony_ci    g_calibraData->ac5 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC5_MSB], SENSOR_DATA_WIDTH_8_BIT) |
124094332d3Sopenharmony_ci    reg[BAROMETER_AC5_LSB]);
125094332d3Sopenharmony_ci    g_calibraData->ac6 = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_AC6_MSB], SENSOR_DATA_WIDTH_8_BIT) |
126094332d3Sopenharmony_ci    reg[BAROMETER_AC6_LSB]);
127094332d3Sopenharmony_ci    g_calibraData->b1 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_B1_MSB], SENSOR_DATA_WIDTH_8_BIT) |
128094332d3Sopenharmony_ci    reg[BAROMETER_B1_LSB]);
129094332d3Sopenharmony_ci    g_calibraData->b2 = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_B2_MSB], SENSOR_DATA_WIDTH_8_BIT) |
130094332d3Sopenharmony_ci    reg[BAROMETER_B2_LSB]);
131094332d3Sopenharmony_ci    g_calibraData->mb = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MB_MSB], SENSOR_DATA_WIDTH_8_BIT) |
132094332d3Sopenharmony_ci    reg[BAROMETER_MB_LSB]);
133094332d3Sopenharmony_ci    g_calibraData->mc = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MC_MSB], SENSOR_DATA_WIDTH_8_BIT) |
134094332d3Sopenharmony_ci    reg[BAROMETER_MC_LSB]);
135094332d3Sopenharmony_ci    g_calibraData->md = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_MD_MSB], SENSOR_DATA_WIDTH_8_BIT) |
136094332d3Sopenharmony_ci    reg[BAROMETER_MD_LSB]);
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci    return ret;
139094332d3Sopenharmony_ci}
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_cistatic int32_t ReadTempData(struct SensorCfgData *data,  struct BarometerData *Temp)
142094332d3Sopenharmony_ci{
143094332d3Sopenharmony_ci    int32_t ret;
144094332d3Sopenharmony_ci    uint8_t status = 0;
145094332d3Sopenharmony_ci    uint8_t reg[BAROMETER_TEM_SUM];
146094332d3Sopenharmony_ci    uint8_t value[SENSOR_VALUE_BUTT];
147094332d3Sopenharmony_ci    value[SENSOR_ADDR_INDEX] = BMP180_CONTROL_REG_ADDR;
148094332d3Sopenharmony_ci    value[SENSOR_VALUE_INDEX] = BMP180_COVERT_TEMP;
149094332d3Sopenharmony_ci
150094332d3Sopenharmony_ci    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
151094332d3Sopenharmony_ci
152094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_COVERT_PRES_3, &status, sizeof(uint8_t));
155094332d3Sopenharmony_ci    if ((status & BMP180_STATUS_ADDR) == BMP180_STATUS_JUDGE) {
156094332d3Sopenharmony_ci        WriteSensor(&data->busCfg, value, sizeof(value));
157094332d3Sopenharmony_ci        OsalMDelay(DELAY_0);
158094332d3Sopenharmony_ci        ret = ReadSensor(&data->busCfg, BMP180_OUT_MSB_ADDR, &reg[BAROMETER_TEM_MSB], sizeof(uint8_t));
159094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
160094332d3Sopenharmony_ci
161094332d3Sopenharmony_ci        ret = ReadSensor(&data->busCfg, BMP180_OUT_LSB_ADDR, &reg[BAROMETER_TEM_LSB], sizeof(uint8_t));
162094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
163094332d3Sopenharmony_ci
164094332d3Sopenharmony_ci        Temp->unpensateTemp = (int32_t)(SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_TEM_MSB], SENSOR_DATA_WIDTH_8_BIT) |
165094332d3Sopenharmony_ci            reg[BAROMETER_TEM_LSB]);
166094332d3Sopenharmony_ci    }
167094332d3Sopenharmony_ci    return ret;
168094332d3Sopenharmony_ci}
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_cistatic int32_t ReadBarometerData(struct SensorCfgData *data, struct BarometerData *Barom)
171094332d3Sopenharmony_ci{
172094332d3Sopenharmony_ci    int32_t ret;
173094332d3Sopenharmony_ci    uint8_t status = 0;
174094332d3Sopenharmony_ci    uint8_t reg[BAROMETER_BAR_SUM];
175094332d3Sopenharmony_ci    uint8_t value[SENSOR_VALUE_BUTT];
176094332d3Sopenharmony_ci    value[SENSOR_ADDR_INDEX] = BMP180_CONTROL_REG_ADDR;
177094332d3Sopenharmony_ci    value[SENSOR_VALUE_INDEX] = BMP180_COVERT_PRES_1;
178094332d3Sopenharmony_ci
179094332d3Sopenharmony_ci    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
180094332d3Sopenharmony_ci
181094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
182094332d3Sopenharmony_ci
183094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_COVERT_PRES_3, &status, sizeof(uint8_t));
184094332d3Sopenharmony_ci    if ((status & BMP180_STATUS_ADDR) == BMP180_STATUS_JUDGE) {
185094332d3Sopenharmony_ci    WriteSensor(&data->busCfg, value, sizeof(value));
186094332d3Sopenharmony_ci    OsalMDelay(DELAY_1);
187094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_OUT_MSB_ADDR, &reg[BAROMETER_BAR_MSB], sizeof(uint8_t));
188094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
189094332d3Sopenharmony_ci
190094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_OUT_LSB_ADDR, &reg[BAROMETER_BAR_LSB], sizeof(uint8_t));
191094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
192094332d3Sopenharmony_ci
193094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMP180_OUT_XLSB_ADDR, &reg[BAROMETER_BAR_XLSB], sizeof(uint8_t));
194094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
195094332d3Sopenharmony_ci
196094332d3Sopenharmony_ci    Barom->unpensatePre = (int32_t)(SENSOR_DATA_SHIFT_RIGHT(
197094332d3Sopenharmony_ci        (SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_BAR_MSB], SENSOR_DATA_WIDTH_16_BIT) |
198094332d3Sopenharmony_ci            SENSOR_DATA_SHIFT_LEFT(reg[BAROMETER_BAR_LSB], SENSOR_DATA_WIDTH_8_BIT) | reg[BAROMETER_BAR_XLSB]),
199094332d3Sopenharmony_ci        (BMP180_CONSTANT_4 - OSSETTING)));
200094332d3Sopenharmony_ci    }
201094332d3Sopenharmony_ci    return ret;
202094332d3Sopenharmony_ci}
203094332d3Sopenharmony_ci
204094332d3Sopenharmony_cistatic int32_t CalcBarometerData(struct  BarometerData *barometerData, int32_t tnp[BAROMETER_SUM])
205094332d3Sopenharmony_ci{
206094332d3Sopenharmony_ci    struct Coefficient coefficientData = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
207094332d3Sopenharmony_ci
208094332d3Sopenharmony_ci    // Calculated temperature
209094332d3Sopenharmony_ci
210094332d3Sopenharmony_ci    coefficientData.x1 = ((barometerData->unpensateTemp - g_calibraData.ac6) * (g_calibraData.ac5))
211094332d3Sopenharmony_ci        >> BMP180_CONSTANT_8;
212094332d3Sopenharmony_ci    coefficientData.x2 = (g_calibraData.mc << BMP180_CONSTANT_5) / (coefficientData.x1 + g_calibraData.md);
213094332d3Sopenharmony_ci    coefficientData.b5 = coefficientData.x1 + coefficientData.x2;
214094332d3Sopenharmony_ci    tnp[BAROMETER_TEMPERATURE] = (coefficientData.b5 + BMP180_CONSTANT_4) >> BMP180_CONSTANT_3;
215094332d3Sopenharmony_ci
216094332d3Sopenharmony_ci    // Calculated pressure
217094332d3Sopenharmony_ci
218094332d3Sopenharmony_ci    coefficientData.b6 = coefficientData.b5 - BMP180_CONSTANT_12;
219094332d3Sopenharmony_ci    coefficientData.x1 = (g_calibraData.b2 * ((coefficientData.b6 * coefficientData.b6) >> BMP180_CONSTANT_6))
220094332d3Sopenharmony_ci        >> BMP180_CONSTANT_5;
221094332d3Sopenharmony_ci    coefficientData.x2 = (g_calibraData.ac2 * coefficientData.b6) >> BMP180_CONSTANT_5;
222094332d3Sopenharmony_ci    coefficientData.x3 = coefficientData.x1 + coefficientData.x2;
223094332d3Sopenharmony_ci    coefficientData.b3 = (((((int32_t)g_calibraData.ac1) * BMP180_CONSTANT_3 + coefficientData.x3) << OSSETTING)
224094332d3Sopenharmony_ci        + BMP180_CONSTANT_2) >> BMP180_CONSTANT_2;
225094332d3Sopenharmony_ci    coefficientData.x1 = (g_calibraData.ac3 * coefficientData.b6) >> BMP180_CONSTANT_7;
226094332d3Sopenharmony_ci    coefficientData.x2 = (g_calibraData.b1 * ((coefficientData.b6 * coefficientData.b6) >> BMP180_CONSTANT_6))
227094332d3Sopenharmony_ci        >> BMP180_CONSTANT_9;
228094332d3Sopenharmony_ci    coefficientData.x3 = ((coefficientData.x1 + coefficientData.x2) + BMP180_CONSTANT_2) >> BMP180_CONSTANT_2;
229094332d3Sopenharmony_ci    coefficientData.b4 = (g_calibraData.ac4 * (uint32_t)(coefficientData.x3 + BMP180_CONSTANT_13))
230094332d3Sopenharmony_ci        >> BMP180_CONSTANT_8;
231094332d3Sopenharmony_ci    coefficientData.b7 = ((uint32_t)(barometerData->unpensatePre) - (uint32_t)coefficientData.b3)
232094332d3Sopenharmony_ci        * (BMP180_CONSTANT_14 >> OSSETTING);
233094332d3Sopenharmony_ci    if (coefficientData.b7 < BMP180_CONSTANT_15) {
234094332d3Sopenharmony_ci        coefficientData.p = (coefficientData.b7 << BMP180_CONSTANT_1) / coefficientData.b4;
235094332d3Sopenharmony_ci    } else {
236094332d3Sopenharmony_ci        coefficientData.p = (coefficientData.b7 / coefficientData.b4) << BMP180_CONSTANT_1;
237094332d3Sopenharmony_ci    }
238094332d3Sopenharmony_ci    coefficientData.x1 = (coefficientData.p >> BMP180_CONSTANT_4) * (coefficientData.p >> BMP180_CONSTANT_4);
239094332d3Sopenharmony_ci    coefficientData.x1 = (coefficientData.x1 * BMP180_CONSTANT_10) >> BMP180_CONSTANT_9;
240094332d3Sopenharmony_ci    coefficientData.x2 = (BMP180_CONSTANT_0 * coefficientData.p) >> BMP180_CONSTANT_9;
241094332d3Sopenharmony_ci    tnp[BAROMETER_BAROMETER] = coefficientData.p + ((coefficientData.x1 + coefficientData.x2
242094332d3Sopenharmony_ci        + BMP180_CONSTANT_11) >> BMP180_CONSTANT_3);
243094332d3Sopenharmony_ci
244094332d3Sopenharmony_ci    return HDF_SUCCESS;
245094332d3Sopenharmony_ci}
246094332d3Sopenharmony_ci
247094332d3Sopenharmony_cistatic int32_t ReadBmp180Data(struct SensorCfgData *data)
248094332d3Sopenharmony_ci{
249094332d3Sopenharmony_ci    int32_t ret;
250094332d3Sopenharmony_ci    int32_t tmp[BAROMETER_SUM];
251094332d3Sopenharmony_ci    struct  BarometerData barometerData = {0, 0};
252094332d3Sopenharmony_ci    OsalTimespec time;
253094332d3Sopenharmony_ci    struct SensorReportEvent event;
254094332d3Sopenharmony_ci
255094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
256094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
257094332d3Sopenharmony_ci
258094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
259094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
260094332d3Sopenharmony_ci        return HDF_FAILURE;
261094332d3Sopenharmony_ci    }
262094332d3Sopenharmony_ci    event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;
263094332d3Sopenharmony_ci
264094332d3Sopenharmony_ci    ret = ReadTempData(data, &barometerData);
265094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
266094332d3Sopenharmony_ci    return HDF_FAILURE;
267094332d3Sopenharmony_ci    }
268094332d3Sopenharmony_ci
269094332d3Sopenharmony_ci    ret = ReadBarometerData(data, &barometerData);
270094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
271094332d3Sopenharmony_ci    return HDF_FAILURE;
272094332d3Sopenharmony_ci    }
273094332d3Sopenharmony_ci
274094332d3Sopenharmony_ci    ret = CalcBarometerData(&barometerData, tmp);
275094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
276094332d3Sopenharmony_ci    return HDF_FAILURE;
277094332d3Sopenharmony_ci    }
278094332d3Sopenharmony_ci
279094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_BAROMETER;
280094332d3Sopenharmony_ci    event.option = 0;
281094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_REALTIME;
282094332d3Sopenharmony_ci    event.dataLen = sizeof(tmp);
283094332d3Sopenharmony_ci    event.data = (uint8_t *)&tmp;
284094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
285094332d3Sopenharmony_ci    return ret;
286094332d3Sopenharmony_ci}
287094332d3Sopenharmony_ci
288094332d3Sopenharmony_cistatic int32_t InitBmp180(struct SensorCfgData *data)
289094332d3Sopenharmony_ci{
290094332d3Sopenharmony_ci    int32_t ret;
291094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
292094332d3Sopenharmony_ci    struct SensorReportEvent event;
293094332d3Sopenharmony_ci
294094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
295094332d3Sopenharmony_ci
296094332d3Sopenharmony_ci    ret = ReadEepromData(data, &g_calibraData);
297094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
298094332d3Sopenharmony_ci        return HDF_FAILURE;
299094332d3Sopenharmony_ci    }
300094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
301094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
302094332d3Sopenharmony_ci        HDF_LOGE("%s: BMP180 sensor init config failed", __func__);
303094332d3Sopenharmony_ci        return HDF_FAILURE;
304094332d3Sopenharmony_ci    }
305094332d3Sopenharmony_ci    return HDF_SUCCESS;
306094332d3Sopenharmony_ci}
307094332d3Sopenharmony_ci
308094332d3Sopenharmony_cistatic int32_t InitBarometerPreConfig(void)
309094332d3Sopenharmony_ci{
310094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
311094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
312094332d3Sopenharmony_ci        return HDF_FAILURE;
313094332d3Sopenharmony_ci    }
314094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
315094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
316094332d3Sopenharmony_ci        return HDF_FAILURE;
317094332d3Sopenharmony_ci    }
318094332d3Sopenharmony_ci
319094332d3Sopenharmony_ci    return HDF_SUCCESS;
320094332d3Sopenharmony_ci}
321094332d3Sopenharmony_ci
322094332d3Sopenharmony_cistatic int32_t DispatchBMP180(struct HdfDeviceIoClient *client,
323094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
324094332d3Sopenharmony_ci{
325094332d3Sopenharmony_ci    (void)client;
326094332d3Sopenharmony_ci    (void)cmd;
327094332d3Sopenharmony_ci    (void)data;
328094332d3Sopenharmony_ci    (void)reply;
329094332d3Sopenharmony_ci
330094332d3Sopenharmony_ci    return HDF_SUCCESS;
331094332d3Sopenharmony_ci}
332094332d3Sopenharmony_ci
333094332d3Sopenharmony_cistatic int32_t Bmp180BindDriver(struct HdfDeviceObject *device)
334094332d3Sopenharmony_ci{
335094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
336094332d3Sopenharmony_ci
337094332d3Sopenharmony_ci    struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)OsalMemCalloc(sizeof(*drvData));
338094332d3Sopenharmony_ci    if (drvData == NULL) {
339094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
340094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
341094332d3Sopenharmony_ci    }
342094332d3Sopenharmony_ci
343094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchBMP180;
344094332d3Sopenharmony_ci    drvData->device = device;
345094332d3Sopenharmony_ci    device->service = &drvData->ioService;
346094332d3Sopenharmony_ci    g_bmp180DrvData = drvData;
347094332d3Sopenharmony_ci
348094332d3Sopenharmony_ci    return HDF_SUCCESS;
349094332d3Sopenharmony_ci}
350094332d3Sopenharmony_ci
351094332d3Sopenharmony_cistatic int32_t Bmp180InitDriver(struct HdfDeviceObject *device)
352094332d3Sopenharmony_ci{
353094332d3Sopenharmony_ci    int32_t ret;
354094332d3Sopenharmony_ci    struct BarometerOpsCall ops;
355094332d3Sopenharmony_ci
356094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
357094332d3Sopenharmony_ci    struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)device->service;
358094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
359094332d3Sopenharmony_ci
360094332d3Sopenharmony_ci    ret = InitBarometerPreConfig();
361094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
362094332d3Sopenharmony_ci        HDF_LOGE("%s: Init  BMp180 bus mux config", __func__);
363094332d3Sopenharmony_ci        return HDF_FAILURE;
364094332d3Sopenharmony_ci    }
365094332d3Sopenharmony_ci
366094332d3Sopenharmony_ci    drvData->sensorCfg = BarometerCreateCfgData(device->property);
367094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
368094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating barometercfg failed because detection failed", __func__);
369094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
370094332d3Sopenharmony_ci    }
371094332d3Sopenharmony_ci
372094332d3Sopenharmony_ci    ops.Init = NULL;
373094332d3Sopenharmony_ci    ops.ReadData = ReadBmp180Data;
374094332d3Sopenharmony_ci    ret = BarometerRegisterChipOps(&ops);
375094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
376094332d3Sopenharmony_ci        HDF_LOGE("%s: Register BMp180 barometer failed", __func__);
377094332d3Sopenharmony_ci        return HDF_FAILURE;
378094332d3Sopenharmony_ci    }
379094332d3Sopenharmony_ci
380094332d3Sopenharmony_ci    ret = InitBmp180(drvData->sensorCfg);
381094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
382094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BMP180 barometer failed", __func__);
383094332d3Sopenharmony_ci        return HDF_FAILURE;
384094332d3Sopenharmony_ci    }
385094332d3Sopenharmony_ci
386094332d3Sopenharmony_ci    return HDF_SUCCESS;
387094332d3Sopenharmony_ci}
388094332d3Sopenharmony_ci
389094332d3Sopenharmony_cistatic void Bmp180ReleaseDriver(struct HdfDeviceObject *device)
390094332d3Sopenharmony_ci{
391094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
392094332d3Sopenharmony_ci
393094332d3Sopenharmony_ci    struct Bmp180DrvData *drvData = (struct Bmp180DrvData *)device->service;
394094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
395094332d3Sopenharmony_ci
396094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
397094332d3Sopenharmony_ci        BarometerReleaseCfgData(drvData->sensorCfg);
398094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
399094332d3Sopenharmony_ci    }
400094332d3Sopenharmony_ci    OsalMemFree(drvData);
401094332d3Sopenharmony_ci}
402094332d3Sopenharmony_ci
403094332d3Sopenharmony_cistruct HdfDriverEntry g_barometerBmp180DevEntry = {
404094332d3Sopenharmony_ci    .moduleVersion = 1,
405094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_BAROMETER_BMP180",
406094332d3Sopenharmony_ci    .Bind = Bmp180BindDriver,
407094332d3Sopenharmony_ci    .Init = Bmp180InitDriver,
408094332d3Sopenharmony_ci    .Release = Bmp180ReleaseDriver,
409094332d3Sopenharmony_ci};
410094332d3Sopenharmony_ci
411094332d3Sopenharmony_ciHDF_INIT(g_barometerBmp180DevEntry);