-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbmp5_selftest.c
More file actions
284 lines (239 loc) · 8.51 KB
/
Copy pathbmp5_selftest.c
File metadata and controls
284 lines (239 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* Copyright (c) 2026 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bmp5_selftest.c
* @date 2026-05-29
* @version v1.5.0
*
*/
#include "bmp5_selftest.h"
/******************************************************************************/
/*! Global Declaration */
#define SAMPLE_COUNT UINT8_C(100)
/******************************************************************************/
/*! Static Function Declaration */
/*!
* @brief This internal API is used to set configurations of the sensor.
*
* @param[in,out] osr_odr_press_cfg : Structure instance of bmp5_osr_odr_press_config
* @param[in] dev : Structure instance of bmp5_dev.
*
* @return Status of execution.
*/
static int8_t set_config(struct bmp5_osr_odr_press_config *osr_odr_press_cfg, struct bmp5_dev *dev);
/*!
* @brief This internal API is used to analyze the sensor temperature and pressure data.
*
* @param[in] data : Structure holding sensor data.
*
* @return Status of execution.
*/
static int8_t analyze_sensor_data(const struct bmp5_sensor_data *data);
/*!
* @brief This internal API is used to get sensor data.
*
* @param[in] data : Structure holding sensor data.
* @param[in] osr_odr_press_cfg : Structure instance of bmp5_osr_odr_press_config
* @param[in] dev : Structure instance of bmp5_dev.
*
* @return Status of execution.
*/
static int8_t get_sensor_data(struct bmp5_sensor_data *data,
const struct bmp5_osr_odr_press_config *osr_odr_press_cfg,
struct bmp5_dev *dev);
/*!
* @brief This API is used to do self test.
*/
int8_t bmp5_selftest_check(struct bmp5_dev *dev)
{
int8_t rslt;
struct bmp5_sensor_data sensor_data[SAMPLE_COUNT] = { { 0 } };
struct bmp5_osr_odr_press_config osr_odr_press_cfg = { 0 };
/* Initialize the BMP5 dev instance */
rslt = bmp5_init(dev);
if (rslt == BMP5_E_COM_FAIL || rslt == BMP5_E_DEV_NOT_FOUND)
{
rslt = BMP5_COMMUNICATION_ERROR_OR_WRONG_DEVICE;
}
if (rslt == BMP5_OK)
{
/* sensor configuration*/
rslt = set_config(&osr_odr_press_cfg, dev);
}
if (rslt == BMP5_OK)
{
/* getting data from sensor and store it to struct bmp5_sensor_data */
rslt = get_sensor_data(sensor_data, &osr_odr_press_cfg, dev);
}
if (rslt == BMP5_OK)
{
/* bmp5 temperature and pressure validation*/
rslt = analyze_sensor_data(sensor_data);
}
if (rslt == BMP5_OK)
{
/* Bring device to stand-by mode */
rslt = bmp5_set_power_mode(BMP5_POWERMODE_STANDBY, dev);
}
return rslt;
}
/*!
* @brief This API is used to analyze the data read during the Seft Test procedure.
*/
static int8_t analyze_sensor_data(const struct bmp5_sensor_data *data)
{
#ifdef BMP5_USE_FIXED_POINT
int64_t avg_p = 0, avg_t = 0;
#else
float avg_p = 0.0, avg_t = 0.0;
#endif
int8_t rslt = BMP5_OK;
uint8_t i;
for (i = 0; i < SAMPLE_COUNT; i++)
{
avg_p = avg_p + data[i].pressure;
avg_t = avg_t + data[i].temperature;
}
#ifdef BMP5_USE_FIXED_POINT
avg_t = div_s64(avg_t, SAMPLE_COUNT);
if ((avg_t < BMP5_MIN_TEMPERATURE) || (avg_t > BMP5_MAX_TEMPERATURE))
{
rslt = BMP5_IMPLAUSIBLE_TEMPERATURE;
}
avg_p = div_u64(div_u64(avg_p, 100), SAMPLE_COUNT);
if ((avg_p < BMP5_MIN_PRESSURE) || (avg_p > BMP5_MAX_PRESSURE))
{
rslt = BMP5_IMPLAUSIBLE_PRESSURE;
}
#else
avg_t = avg_t / SAMPLE_COUNT;
if ((avg_t < BMP5_MIN_TEMPERATURE) || (avg_t > BMP5_MAX_TEMPERATURE))
{
rslt = BMP5_IMPLAUSIBLE_TEMPERATURE;
}
avg_p = (avg_p / 100.0f) / SAMPLE_COUNT;
if ((avg_p < BMP5_MIN_PRESSURE) || (avg_p > BMP5_MAX_PRESSURE))
{
rslt = BMP5_IMPLAUSIBLE_PRESSURE;
}
#endif
return rslt;
}
/*!
* @brief This API is used to set sensor configuration for Pressue and Temperature values.
*/
static int8_t set_config(struct bmp5_osr_odr_press_config *osr_odr_press_cfg, struct bmp5_dev *dev)
{
int8_t rslt;
struct bmp5_iir_config set_iir_cfg;
struct bmp5_int_source_select int_source_select;
rslt = bmp5_set_power_mode(BMP5_POWERMODE_STANDBY, dev);
if (rslt == BMP5_OK)
{
/* Get default odr */
rslt = bmp5_get_osr_odr_press_config(osr_odr_press_cfg, dev);
if (rslt == BMP5_OK)
{
/* Set ODR as 50Hz */
osr_odr_press_cfg->odr = BMP5_ODR_50_HZ;
/* Enable pressure */
osr_odr_press_cfg->press_en = BMP5_ENABLE;
/* Set Over-sampling rate with respect to odr */
osr_odr_press_cfg->osr_t = BMP5_OVERSAMPLING_64X;
osr_odr_press_cfg->osr_p = BMP5_OVERSAMPLING_4X;
rslt = bmp5_set_osr_odr_press_config(osr_odr_press_cfg, dev);
}
if (rslt == BMP5_OK)
{
/* Set IIR Configuration */
set_iir_cfg.set_iir_t = BMP5_IIR_FILTER_COEFF_1;
set_iir_cfg.set_iir_p = BMP5_IIR_FILTER_COEFF_1;
set_iir_cfg.shdw_set_iir_t = BMP5_ENABLE;
set_iir_cfg.shdw_set_iir_p = BMP5_ENABLE;
rslt = bmp5_set_iir_config(&set_iir_cfg, dev);
}
if (rslt == BMP5_OK)
{
/* Configure interrupt */
rslt = bmp5_configure_interrupt(BMP5_PULSED, BMP5_ACTIVE_HIGH, BMP5_INTR_PUSH_PULL, BMP5_INTR_ENABLE, dev);
if (rslt == BMP5_OK)
{
/* Note : Select INT_SOURCE after configuring interrupt */
int_source_select.drdy_en = BMP5_ENABLE;
rslt = bmp5_int_source_select(&int_source_select, dev);
}
}
if (rslt == BMP5_OK)
{
/* Set powermode as normal */
rslt = bmp5_set_power_mode(BMP5_POWERMODE_NORMAL, dev);
}
}
return rslt;
}
/*!
* @brief This API is used to get sensor data
*/
static int8_t get_sensor_data(struct bmp5_sensor_data *data,
const struct bmp5_osr_odr_press_config *osr_odr_press_cfg,
struct bmp5_dev *dev)
{
int8_t rslt = 0;
uint8_t idx = 0;
uint8_t int_status;
uint16_t try_count = 0;
while (idx < SAMPLE_COUNT)
{
rslt = bmp5_get_interrupt_status(&int_status, dev);
if (rslt == BMP5_OK)
{
if (int_status & BMP5_INT_ASSERTED_DRDY)
{
rslt = bmp5_get_sensor_data(&data[idx], osr_odr_press_cfg, dev);
idx++;
}
else
{
/* Data not ready yet (or status read failed): wait 1 ms and
* check whether the overall timeout has been exceeded. */
dev->delay_us(BMP5_SELFTEST_POLL_PERIOD_US, dev->intf_ptr);
try_count++;
if (try_count >= BMP5_SELFTEST_TIMEOUT_MS)
{
rslt = BMP5_E_SELFTEST_TIMEOUT;
break;
}
}
}
}
return rslt;
}