-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathbmp280_support.c
442 lines (416 loc) · 18.4 KB
/
bmp280_support.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
****************************************************************************
* Copyright (C) 2014 Bosch Sensortec GmbH
*
* bmp280_support.c
* Date: 2014/12/12
* Revision: 1.0.4
*
* Usage: Sensor Driver support file for BMP280 sensor
*
****************************************************************************
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of the copyright holder nor the names of the
* 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 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
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/
/*---------------------------------------------------------------------------*/
/* Includes*/
/*---------------------------------------------------------------------------*/
#include "bmp280.h"
/*----------------------------------------------------------------------------*
* The following functions are used for reading and writing of
* sensor data using I2C or SPI communication
*----------------------------------------------------------------------------*/
#ifdef BMP280_API
/* \Brief: The function is used as I2C bus read
* \Return : Status of the I2C read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of byte of data to be read
*/
s8 BMP280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as I2C bus write
* \Return : Status of the I2C write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BMP280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as SPI bus write
* \Return : Status of the SPI write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BMP280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as SPI bus read
* \Return : Status of the SPI read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of byte of data to be read */
s8 BMP280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/*
* \Brief: SPI/I2C init routine
*/
s8 I2C_routine(void);
s8 SPI_routine(void);
#endif
/********************End of I2C/SPI function declarations***********************/
/* Brief : The delay routine
* \param : delay in ms
*/
void BMP280_delay_msek(u32 msek);
/* This function is an example for reading sensor data
* \param: None
* \return: communication result
*/
s32 bmp280_data_readout_template(void);
/*----------------------------------------------------------------------------*
* struct bmp280_t parameters can be accessed by using bmp280
* bmp280_t having the following parameters
* Bus write function pointer: BMP280_WR_FUNC_PTR
* Bus read function pointer: BMP280_RD_FUNC_PTR
* Delay function pointer: delay_msec
* I2C address: dev_addr
* Chip id of the sensor: chip_id
*---------------------------------------------------------------------------*/
struct bmp280_t bmp280;
/* This function is an example for reading sensor data
* \param: None
* \return: communication result
*/
s32 bmp280_data_readout_template(void)
{
/* The variable used to assign the standby time*/
u8 v_standby_time_u8 = BMP280_ZERO_U8X;
/* The variable used to read uncompensated temperature*/
s32 v_data_uncomp_tem_s32 = BMP280_ZERO_U8X;
/* The variable used to read uncompensated pressure*/
s32 v_data_uncomp_pres_s32 = BMP280_ZERO_U8X;
/* The variable used to read real temperature*/
s32 v_actual_temp_s32 = BMP280_ZERO_U8X;
/* The variable used to read real pressure*/
u32 v_actual_press_u32 = BMP280_ZERO_U8X;
s32 v_actual_press_data_s32 = BMP280_ZERO_U8X;
/* result of communication results*/
s32 com_rslt = ERROR;
/*********************** START INITIALIZATION ************************/
/* Based on the user need configure I2C or SPI interface.
* It is example code to explain how to use the bma2x2 API*/
#ifdef BMP280
I2C_routine();
/*SPI_routine(); */
#endif
/*--------------------------------------------------------------------------*
* This function used to assign the value/reference of
* the following parameters
* I2C address
* Bus Write
* Bus read
* Chip id
*-------------------------------------------------------------------------*/
com_rslt = bmp280_init(&bmp280);
/* For initialization it is required to set the mode of
* the sensor as "NORMAL"
* data acquisition/read/write is possible in this mode
* by using the below API able to set the power mode as NORMAL*/
/* Set the power mode as NORMAL*/
com_rslt += bmp280_set_power_mode(BMP280_NORMAL_MODE);
/* For reading the pressure and temperature data it is required to
* set the work mode
* The measurement period in the Normal mode is depends on the setting of
* over sampling setting of pressure, temperature and standby time
*
* OSS pressure OSS temperature OSS
* ultra low power x1 x1
* low power x2 x1
* standard resolution x4 x1
* high resolution x8 x2
* ultra high resolution x16 x2
*/
/* The oversampling settings are set by using the following API*/
com_rslt += bmp280_set_work_mode(BMP280_ULTRA_LOW_POWER_MODE);
/*------------------------------------------------------------------------*
************************* START GET and SET FUNCTIONS DATA ****************
*---------------------------------------------------------------------------*/
/* This API used to Write the standby time of the sensor input
* value have to be given*/
/* Normal mode comprises an automated perpetual cycling between an (active)
* Measurement period and an (inactive) standby period.
* The standby time is determined by the contents of the register t_sb.
* Standby time can be set using BMP280_STANDBYTIME_125_MS.
* Usage Hint : BMP280_set_standbydur(BMP280_STANDBYTIME_125_MS)*/
com_rslt += bmp280_set_standby_durn(BMP280_STANDBY_TIME_1_MS);
/* This API used to read back the written value of standby time*/
com_rslt += bmp280_get_standby_durn(&v_standby_time_u8);
/*-----------------------------------------------------------------*
************************* END GET and SET FUNCTIONS ****************
*------------------------------------------------------------------*/
/************************* END INITIALIZATION *************************/
/*------------------------------------------------------------------*
************ START READ UNCOMPENSATED PRESSURE AND TEMPERATURE********
*---------------------------------------------------------------------*/
/* API is used to read the uncompensated temperature*/
com_rslt += bmp280_read_uncomp_temperature(&v_data_uncomp_tem_s32);
/* API is used to read the uncompensated pressure*/
com_rslt += bmp280_read_uncomp_pressure(&v_data_uncomp_pres_s32);
/* API is used to read the uncompensated temperature and pressure*/
com_rslt += bmp280_read_uncomp_pressure_temperature(&v_actual_press_data_s32,
&v_actual_temp_s32);
/*--------------------------------------------------------------------*
************ END READ UNCOMPENSATED PRESSURE AND TEMPERATURE********
*-------------------------------------------------------------------------*/
/*------------------------------------------------------------------*
************ START READ TRUE PRESSURE AND TEMPERATURE********
*---------------------------------------------------------------------*/
/* API is used to read the true temperature*/
/* Input value as uncompensated temperature*/
com_rslt += bmp280_compensate_T_int32(v_actual_temp_s32);
/* API is used to read the true pressure*/
/* Input value as uncompensated pressure*/
com_rslt += bmp280_compensate_P_int32(v_actual_press_u32);
/* API is used to read the true temperature and pressure*/
/* Input value as uncompensated pressure and temperature*/
com_rslt += bmp280_read_pressure_temperature(&v_actual_press_u32,
&v_actual_temp_s32);
/*--------------------------------------------------------------------*
************ END READ TRUE PRESSURE AND TEMPERATURE********
*-------------------------------------------------------------------------*/
/************************* START DE-INITIALIZATION ***********************/
/* For de-initialization it is required to set the mode of
* the sensor as "SLEEP"
* the device reaches the lowest power consumption only
* In SLEEP mode no measurements are performed
* All registers are accessible
* by using the below API able to set the power mode as SLEEP*/
/* Set the power mode as SLEEP*/
com_rslt += bmp280_set_power_mode(BMP280_SLEEP_MODE);
return com_rslt;
/************************* END DE-INITIALIZATION **********************/
}
#ifdef BMP280_API
/*--------------------------------------------------------------------------*
* The following function is used to map the I2C bus read, write, delay and
* device address with global structure bmp280_t
*-------------------------------------------------------------------------*/
s8 I2C_routine(void) {
/*--------------------------------------------------------------------------*
* By using bmp280 the following structure parameter can be accessed
* Bus write function pointer: BMP280_WR_FUNC_PTR
* Bus read function pointer: BMP280_RD_FUNC_PTR
* Delay function pointer: delay_msec
* I2C address: dev_addr
*--------------------------------------------------------------------------*/
bmp280.bus_write = BMP280_I2C_bus_write;
bmp280.bus_read = BMP280_I2C_bus_read;
bmp280.dev_addr = BMP280_I2C_ADDRESS2;
bmp280.delay_msec = BMP280_delay_msek;
return BMP280_ZERO_U8X;
}
/*---------------------------------------------------------------------------*
* The following function is used to map the SPI bus read, write and delay
* with global structure bmp280_t
*--------------------------------------------------------------------------*/
s8 SPI_routine(void) {
/*--------------------------------------------------------------------------*
* By using bmp280 the following structure parameter can be accessed
* Bus write function pointer: BMP280_WR_FUNC_PTR
* Bus read function pointer: BMP280_RD_FUNC_PTR
* Delay function pointer: delay_msec
*--------------------------------------------------------------------------*/
bmp280.bus_write = BMP280_SPI_bus_write;
bmp280.bus_read = BMP280_SPI_bus_read;
bmp280.delay_msec = BMP280_delay_msek;
return BMP280_ZERO_U8X;
}
/************** I2C/SPI buffer length ******/
#define I2C_BUFFER_LEN 8
#define SPI_BUFFER_LEN 5
#define BUFFER_LENGTH 0xFF
#define MASK_DATA 0x80
#define REGISTER_MASK 0x7F
/*-------------------------------------------------------------------*
* This is a sample code for read and write the data by using I2C/SPI
* Use either I2C or SPI based on your need
* The device address defined in the bmp180.c
*
*-----------------------------------------------------------------------*/
/* \Brief: The function is used as I2C bus write
* \Return : Status of the I2C write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BMP280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BMP280_ZERO_U8X;
u8 array[I2C_BUFFER_LEN];
u8 stringpos = BMP280_ZERO_U8X;
array[BMP280_ZERO_U8X] = reg_addr;
for (stringpos = BMP280_ZERO_U8X; stringpos < cnt; stringpos++) {
array[stringpos + BMP280_ONE_U8X] = *(reg_data + stringpos);
}
/*
* Please take the below function as your reference for
* write the data using I2C communication
* "IERROR = I2C_WRITE_STRING(DEV_ADDR, ARRAY, CNT+1)"
* add your I2C write function here
* iError is an return value of I2C read function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_ZERO_U8X
* and FAILURE defined as -1
* Note :
* This is a full duplex operation,
* The first read data is discarded, for that extra write operation
* have to be initiated. For that cnt+1 operation done in the I2C write string function
* For more information please refer data sheet SPI communication:
*/
return (s8)iError;
}
/* \Brief: The function is used as I2C bus read
* \Return : Status of the I2C read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of data to be read
*/
s8 BMP280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BMP280_ZERO_U8X;
u8 array[I2C_BUFFER_LEN] = {BMP280_ZERO_U8X};
u8 stringpos = BMP280_ZERO_U8X;
array[BMP280_ZERO_U8X] = reg_addr;
/* Please take the below function as your reference
* for read the data using I2C communication
* add your I2C rad function here.
* "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
* iError is an return value of SPI write function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_ZERO_U8X
* and FAILURE defined as -1
*/
for (stringpos = BMP280_ZERO_U8X; stringpos < cnt; stringpos++) {
*(reg_data + stringpos) = array[stringpos];
}
return (s8)iError;
}
/* \Brief: The function is used as SPI bus read
* \Return : Status of the SPI read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of data to be read
*/
s8 BMP280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError=BMP280_ZERO_U8X;
u8 array[SPI_BUFFER_LEN]={BUFFER_LENGTH};
u8 stringpos;
/* For the SPI mode only 7 bits of register addresses are used.
The MSB of register address is declared the bit what functionality it is
read/write (read as 1/write as BMP280_ZERO_U8X)*/
array[BMP280_ZERO_U8X] = reg_addr|MASK_DATA;/*read routine is initiated register address is mask with 0x80*/
/*
* Please take the below function as your reference for
* read the data using SPI communication
* " IERROR = SPI_READ_WRITE_STRING(ARRAY, ARRAY, CNT+1)"
* add your SPI read function here
* iError is an return value of SPI read function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_ZERO_U8X
* and FAILURE defined as -1
* Note :
* This is a full duplex operation,
* The first read data is discarded, for that extra write operation
* have to be initiated. For that cnt+1 operation done in the SPI read
* and write string function
* For more information please refer data sheet SPI communication:
*/
for (stringpos = BMP280_ZERO_U8X; stringpos < cnt; stringpos++) {
*(reg_data + stringpos) = array[stringpos+BMP280_ONE_U8X];
}
return (s8)iError;
}
/* \Brief: The function is used as SPI bus write
* \Return : Status of the SPI write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BMP280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BMP280_ZERO_U8X;
u8 array[SPI_BUFFER_LEN * BMP280_TWO_U8X];
u8 stringpos = BMP280_ZERO_U8X;
for (stringpos = BMP280_ZERO_U8X; stringpos < cnt; stringpos++) {
/* the operation of (reg_addr++)&0x7F done: because it ensure the
BMP280_ZERO_U8X and 1 of the given value
It is done only for 8bit operation*/
array[stringpos * BMP280_TWO_U8X] = (reg_addr++) & REGISTER_MASK;
array[stringpos * BMP280_TWO_U8X + BMP280_ONE_U8X] = *(reg_data + stringpos);
}
/* Please take the below function as your reference
* for write the data using SPI communication
* add your SPI write function here.
* "IERROR = SPI_WRITE_STRING(ARRAY, CNT*2)"
* iError is an return value of SPI write function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_ZERO_U8X
* and FAILURE defined as -1
*/
return (s8)iError;
}
/* Brief : The delay routine
* \param : delay in ms
*/
void BMP280_delay_msek(u32 msek)
{
/*Here you can write your own delay routine*/
}
#endif