-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme68xLibrary.cpp
612 lines (517 loc) · 13.4 KB
/
bme68xLibrary.cpp
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/**
* Copyright (c) 2021 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 bme68xLibrary.cpp
* @date 11 Aug 2021
* @version 1.1.40406
*
*/
#include <string.h>
#include "esp_log.h"
#include "esp_err.h"
#include "bme68xLibrary.h"
/* Maximum transaction size. Field size 17 x 3 */
#define BME68X_MAX_READ_LENGTH 51
#define BME68X_I2C_BUFFER_SIZE 128
#define NOP() asm volatile ("nop")
#define I2C_SDA_GPIO 40
#define I2C_SCL_GPIO 39
#define I2C_PORT 0
#define I2C_BME68x_ADDR 0x76
#define I2C_FREQ_HZ 400000
static const char *TAG = "bme68xLibrary";
uint8_t rxBuffer[BME68X_I2C_BUFFER_SIZE];
size_t rxIndex;
size_t rxLength;
uint8_t txBuffer[BME68X_I2C_BUFFER_SIZE];
static int i2c_buff_available(void)
{
int result = rxLength - rxIndex;
return result;
}
static int i2c_buff_read(void)
{
int value = -1;
if(rxIndex < rxLength) {
value = rxBuffer[rxIndex++];
}
return value;
}
static unsigned long IRAM_ATTR micros()
{
return (unsigned long) (esp_timer_get_time());
}
void IRAM_ATTR delayMicroseconds(uint32_t us)
{
uint32_t m = micros();
if(us){
uint32_t e = (m + us);
if(m > e){ //overflow
while(micros() > e){
NOP();
}
}
while(micros() < e){
NOP();
}
}
}
Bme68x::Bme68x(void)
{
status = BME68X_OK;
memset(&bme6, 0, sizeof(bme6));
memset(&conf, 0, sizeof(conf));
memset(&heatrConf, 0, sizeof(heatrConf));
memset(sensorData, 0, sizeof(sensorData));
bme6.amb_temp = 25; /* Typical room temperature in Celsius */
nFields = 0;
iFields = 0;
lastOpMode = BME68X_SLEEP_MODE;
}
/**
* @brief Function to initialize the sensor based on the Wire library
*/
void Bme68x::begin(i2c_dev_t *dev, bme68x_delay_us_fptr_t idleTask)
{
esp_err_t err = ESP_OK;
dev->port = I2C_PORT;
dev->addr = I2C_BME68x_ADDR;
dev->cfg.sda_io_num = I2C_SDA_GPIO;
dev->cfg.scl_io_num = I2C_SCL_GPIO;
dev->cfg.master.clk_speed = I2C_FREQ_HZ;
err = i2c_dev_create_mutex(dev);
if(err != ESP_OK || status != BME68X_OK)
{
ESP_LOGE(TAG, "begin: i2c_dev_create_mutex() failed");
}
comm.i2c.i2cAddr = I2C_BME68x_ADDR;
comm.i2c.i2c_conf = dev;
bme6.intf = BME68X_I2C_INTF;
bme6.read = bme68xI2cRead;
bme6.write = bme68xI2cWrite;
bme6.delay_us = idleTask;
bme6.intf_ptr = &comm;
bme6.amb_temp = 25;
status = bme68x_init(&bme6);
if(err != ESP_OK || status != BME68X_OK)
{
ESP_LOGE(TAG, "begin: bme68x_init() failed: %d", status);
}
ESP_LOGI(TAG, "bme68x_init: Init Success, chip ID: %X, variant ID: %X", bme6.chip_id, bme6.variant_id);
}
/**
* @brief Function to read a register
*/
uint8_t Bme68x::readReg(uint8_t regAddr)
{
uint8_t regData;
readReg(regAddr, ®Data, 1);
return regData;
}
/**
* @brief Function to read multiple registers
*/
void Bme68x::readReg(uint8_t regAddr, uint8_t *regData, uint32_t length)
{
status = bme68x_get_regs(regAddr, regData, length, &bme6);
}
/**
* @brief Function to write data to a register
*/
void Bme68x::writeReg(uint8_t regAddr, uint8_t regData)
{
status = bme68x_set_regs(®Addr, ®Data, 1, &bme6);
}
/**
* @brief Function to write multiple registers
*/
void Bme68x::writeReg(uint8_t *regAddr, const uint8_t *regData, uint32_t length)
{
status = bme68x_set_regs(regAddr, regData, length, &bme6);
}
/**
* @brief Function to trigger a soft reset
*/
void Bme68x::softReset(void)
{
status = bme68x_soft_reset(&bme6);
}
/**
* @brief Function to set the ambient temperature for better configuration
*/
void Bme68x::setAmbientTemp(int8_t temp)
{
bme6.amb_temp = temp;
}
/**
* @brief Function to get the measurement duration in microseconds
*/
uint32_t Bme68x::getMeasDur(uint8_t opMode)
{
if (opMode == BME68X_SLEEP_MODE)
opMode = lastOpMode;
return bme68x_get_meas_dur(opMode, &conf, &bme6);
}
/**
* @brief Function to set the operation mode
*/
void Bme68x::setOpMode(uint8_t opMode)
{
status = bme68x_set_op_mode(opMode, &bme6);
if ((status == BME68X_OK) && (opMode != BME68X_SLEEP_MODE))
lastOpMode = opMode;
}
/**
* @brief Function to get the operation mode
*/
uint8_t Bme68x::getOpMode(void)
{
uint8_t opMode;
status = bme68x_get_op_mode(&opMode, &bme6);
return opMode;
}
/**
* @brief Function to get the Temperature, Pressure and Humidity over-sampling
*/
void Bme68x::getTPH(uint8_t &osHum, uint8_t &osTemp, uint8_t &osPres)
{
status = bme68x_get_conf(&conf, &bme6);
if (status == BME68X_OK)
{
osHum = conf.os_hum;
osTemp = conf.os_temp;
osPres = conf.os_pres;
}
}
/**
* @brief Function to set the Temperature, Pressure and Humidity over-sampling
*/
void Bme68x::setTPH(uint8_t osTemp, uint8_t osPres, uint8_t osHum)
{
status = bme68x_get_conf(&conf, &bme6);
if (status == BME68X_OK)
{
conf.os_hum = osHum;
conf.os_temp = osTemp;
conf.os_pres = osPres;
status = bme68x_set_conf(&conf, &bme6);
}
}
/**
* @brief Function to get the filter configuration
*/
uint8_t Bme68x::getFilter(void)
{
status = bme68x_get_conf(&conf, &bme6);
return conf.filter;
}
/**
* @brief Function to set the filter configuration
*/
void Bme68x::setFilter(uint8_t filter)
{
status = bme68x_get_conf(&conf, &bme6);
if (status == BME68X_OK)
{
conf.filter = filter;
status = bme68x_set_conf(&conf, &bme6);
}
}
/**
* @brief Function to get the sleep duration during Sequential mode
*/
uint8_t Bme68x::getSeqSleep(void)
{
status = bme68x_get_conf(&conf, &bme6);
return conf.odr;
}
/**
* @brief Function to set the sleep duration during Sequential mode
*/
void Bme68x::setSeqSleep(uint8_t odr)
{
status = bme68x_get_conf(&conf, &bme6);
if (status == BME68X_OK)
{
conf.odr = odr;
status = bme68x_set_conf(&conf, &bme6);
}
}
/**
* @brief Function to set the heater profile for Forced mode
*/
void Bme68x::setHeaterProf(uint16_t temp, uint16_t dur)
{
heatrConf.enable = BME68X_ENABLE;
heatrConf.heatr_temp = temp;
heatrConf.heatr_dur = dur;
status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &heatrConf, &bme6);
}
/**
* @brief Function to set the heater profile for Sequential mode
*/
void Bme68x::setHeaterProf(uint16_t *temp, uint16_t *dur, uint8_t profileLen)
{
heatrConf.enable = BME68X_ENABLE;
heatrConf.heatr_temp_prof = temp;
heatrConf.heatr_dur_prof = dur;
heatrConf.profile_len = profileLen;
status = bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &heatrConf, &bme6);
}
/**
* @brief Function to set the heater profile for Parallel mode
*/
void Bme68x::setHeaterProf(uint16_t *temp, uint16_t *mul, uint16_t sharedHeatrDur, uint8_t profileLen)
{
heatrConf.enable = BME68X_ENABLE;
heatrConf.heatr_temp_prof = temp;
heatrConf.heatr_dur_prof = mul;
heatrConf.shared_heatr_dur = sharedHeatrDur;
heatrConf.profile_len = profileLen;
status = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, &heatrConf, &bme6);
}
/**
* @brief Function to fetch data from the sensor into the local buffer
*/
uint8_t Bme68x::fetchData(void)
{
nFields = 0;
status = bme68x_get_data(lastOpMode, sensorData, &nFields, &bme6);
iFields = 0;
return nFields;
}
/**
* @brief Function to get a single data field
*/
uint8_t Bme68x::getData(bme68xData &data)
{
if (lastOpMode == BME68X_FORCED_MODE)
{
data = sensorData[0];
} else
{
if (nFields)
{
/* iFields spans from 0-2 while nFields spans from
* 0-3, where 0 means that there is no new data
*/
data = sensorData[iFields];
iFields++;
/* Limit reading continuously to the last fields read */
if (iFields >= nFields)
{
iFields = nFields - 1;
return 0;
}
/* Indicate if there is something left to read */
return nFields - iFields;
}
}
return 0;
}
/**
* @brief Function to get whole sensor data
*/
bme68xData* Bme68x::getAllData(void)
{
return sensorData;
}
/**
* @brief Function to get the BME68x heater configuration
*/
const bme68xHeatrConf& Bme68x::getHeaterConfiguration(void)
{
return heatrConf;
}
/**
* @brief Function to retrieve the sensor's unique ID
*/
uint32_t Bme68x::getUniqueId(void)
{
uint8_t id_regs[4];
uint32_t uid;
readReg(BME68X_REG_UNIQUE_ID, id_regs, 4);
uint32_t id1 = ((uint32_t) id_regs[3] + ((uint32_t) id_regs[2] << 8)) & 0x7fff;
uid = (id1 << 16) + (((uint32_t) id_regs[1]) << 8) + (uint32_t) id_regs[0];
return uid;
}
/**
* @brief Function to get the error code of the interface functions
*/
BME68X_INTF_RET_TYPE Bme68x::intfError(void)
{
return bme6.intf_rslt;
}
/**
* @brief Function to check if an error / warning has occurred
*/
int8_t Bme68x::checkStatus(void)
{
if (status < BME68X_OK)
{
return BME68X_ERROR;
}
else if(status > BME68X_OK)
{
return BME68X_WARNING;
}
else
{
return BME68X_OK;
}
}
/**
* @brief Function to get a brief text description of the error
*/
char* Bme68x::statusString(void)
{
char *ret = "";
switch (status)
{
case BME68X_OK:
/* Don't return a text for OK */
break;
case BME68X_E_NULL_PTR:
ret = "Null pointer";
break;
case BME68X_E_COM_FAIL:
ret = "Communication failure";
break;
case BME68X_E_DEV_NOT_FOUND:
ret = "Sensor not found";
break;
case BME68X_E_INVALID_LENGTH:
ret = "Invalid length";
break;
case BME68X_W_DEFINE_OP_MODE:
ret = "Set the operation mode";
break;
case BME68X_W_NO_NEW_DATA:
ret = "No new data";
break;
case BME68X_W_DEFINE_SHD_HEATR_DUR:
ret = "Set the shared heater duration";
break;
default:
ret = "Undefined error code";
}
return ret;
}
/**
* @brief Function that implements the default microsecond delay callback
*/
void bme68xDelayUs(uint32_t periodUs, void *intfPtr) {
(void) intfPtr;
delayMicroseconds(periodUs);
}
/**
* @brief Function that implements the default I2C write transaction
*/
int8_t bme68xI2cWrite(uint8_t regAddr, const uint8_t *regData,
uint32_t length, void *intfPtr) {
int8_t rslt = BME68X_OK;
bme68xScommT *comm = NULL;
ESP_LOGD(TAG, "bme68xI2cWrite requested.");
#ifdef BME68X_I2C_BUFFER_SIZE
if (length + 1 > BME68X_I2C_BUFFER_SIZE)
return BME68X_E_COM_FAIL;
#endif
if (intfPtr) {
comm = (bme68xScommT *) intfPtr;
esp_err_t err = ESP_OK;
ESP_LOGD(TAG, "bme68xI2cWrite: Writing:");
for(int i=0; i<length; i++)
{
ESP_LOGD(TAG,"%X", regData[i]);
}
if(comm->i2c.i2c_conf)
{
I2C_DEV_TAKE_MUTEX(comm->i2c.i2c_conf);
err = i2c_dev_write_reg(comm->i2c.i2c_conf, regAddr, regData, length);
I2C_DEV_GIVE_MUTEX(comm->i2c.i2c_conf);
if(err != ESP_OK)
{
return BME68X_E_COM_FAIL;
}
}
else
{
rslt = BME68X_E_NULL_PTR;
}
} else {
rslt = BME68X_E_NULL_PTR;
}
return rslt;
}
/**
* @brief Function that implements the default I2C read transaction
*/
int8_t bme68xI2cRead(uint8_t regAddr, uint8_t *regData, uint32_t length,
void *intfPtr) {
uint32_t i;
int8_t rslt = BME68X_OK;
bme68xScommT *comm = NULL;
ESP_LOGD(TAG, "bme68xI2cRead requested.");
#ifdef BME68X_I2C_BUFFER_SIZE
if (length > BME68X_I2C_BUFFER_SIZE)
return BME68X_E_COM_FAIL;
#endif
if (intfPtr) {
comm = (bme68xScommT *) intfPtr;
esp_err_t err = ESP_OK;
ESP_LOGD(TAG, "bme68xI2cRead: Reading data at %X :", regAddr);
rxIndex = 0;
rxLength = 0;
if(comm->i2c.i2c_conf)
{
I2C_DEV_TAKE_MUTEX(comm->i2c.i2c_conf);
err = i2cWriteReadNonStop(comm->i2c.i2c_conf, comm->i2c.i2cAddr, ®Addr, 1, rxBuffer, length, 50, &rxLength);
I2C_DEV_GIVE_MUTEX(comm->i2c.i2c_conf);
if(err != ESP_OK)
{
return BME68X_E_COM_FAIL;
}
for (i = 0; (i < length) && i2c_buff_available(); i++)
{
regData[i] = i2c_buff_read();
ESP_LOGV(TAG, "bme68xI2cRead: %X", regData[i]);
}
}
else
{
rslt = BME68X_E_NULL_PTR;
}
} else {
rslt = BME68X_E_NULL_PTR;
}
return rslt;
}