-
Notifications
You must be signed in to change notification settings - Fork 3
/
BME688.cpp
534 lines (447 loc) · 15.6 KB
/
BME688.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
#include "BME688.h"
#include "Defer.h"
static int8_t ReadRegister(uint8_t reg_addr, uint8_t *regdata, uint32_t length, void *intf_ptr);
static int8_t WriteRegister(uint8_t reg_addr,const uint8_t *reg_data, uint32_t length,void *intf_ptr);
static void delay(uint32_t period, void *intf_ptr);
BME688::BME688( PinName i2c_sda, PinName i2c_scl, uint32_t bme688_addr )
{
this->new_data_available = 0;
this->i2c_sda = i2c_sda;
this->i2c_scl = i2c_scl;
this->bme688_addr = bme688_addr;
this->bme688_addr_8bit = ( bme688_addr << 1 );
this->bsec_conf.requested_virtual_sensors_number = BSEC_REQUESTED_VIRTUAL_SENSORS_NUMBER;
this->bsec_conf.temperature_offset = BSEC_TEMPERATURE_OFFSET;
this->bsec_conf.temp_profile[0] = 320;
this->bsec_conf.temp_profile[1] = 320;
this->bsec_conf.dur_profile[0] = 150;
this->bsec_conf.dur_profile[1] = 150;
}
BME688::ReturnCode BME688::Initialise()
{
BME688::ReturnCode result;
// enable I2C
I2C i2c_temp( i2c_sda, i2c_scl ); // will be freed after function return
this->i2c_local = &i2c_temp; // set i2c object for bsec to use
Defer<I2C*> i2c_defer( this->i2c_local, nullptr ); // defer setting i2c_local to nullptr at Initialise() return
// TODO: check chip ID
result = this->InitialiseSensor();
if( result != ReturnCode::kOk )
{
return result;
}
// this is not mentiond in the doc
result = this->InitialiseSensorFilterSettings();
if( result != ReturnCode::kOk )
{
return result;
}
result = this->InitialiseSensorHeaterSettings();
if( result != ReturnCode::kOk )
{
return result;
}
result = this->SetSequentialMode();
if( result != ReturnCode::kOk )
{
return result;
}
// this is not mentiond in the doc
result = this->InitialiseBsec();
if( result != ReturnCode::kOk )
{
return result;
}
result = this->UpdateSubscription();
if( result != ReturnCode::kOk )
{
return result;
}
return ReturnCode::kOk;
}
void BME688::DoMeasurements()
{
// enable I2C
I2C i2c_temp( i2c_sda, i2c_scl );
this->i2c_local = &i2c_temp; // set i2c object for bsec to use
Defer<I2C*> i2c_defer( this->i2c_local, nullptr ); // defer setting i2c_local to nullptr at Initialise() return
this->ProcessData();
this->BsecProcessing();
}
BME688::ReturnCode BME688::InitialiseSensor()
{
bme688.dev.intf_ptr = this;
bme688.dev.intf = BME68X_I2C_INTF;
bme688.dev.read = ReadRegister;
bme688.dev.write = WriteRegister;
bme688.dev.delay_us = delay;
bme688.dev.amb_temp = 25; // used in arduino example
if( bme68x_init(&bme688.dev) != 0 )
{
return BME688::ReturnCode::kSensorStructureFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::InitialiseSensorFilterSettings()
{
bme68x_get_conf(&bme688.conf, &bme688.dev);
bme688.conf.filter = BME68X_FILTER_OFF;
bme688.conf.odr = BME68X_ODR_NONE;
bme688.conf.os_hum = BME68X_OS_16X;
bme688.conf.os_pres = BME68X_OS_1X;
bme688.conf.os_temp = BME68X_OS_2X;
if( bme68x_set_conf(&bme688.conf, &bme688.dev) != 0 )
{
return BME688::ReturnCode::kSensorConfigFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::InitialiseSensorHeaterSettings()
{
bsec_conf.sensor_heater_config.enable = BME68X_ENABLE;
bsec_conf.sensor_heater_config.heatr_temp_prof = bsec_conf.temp_profile;
bsec_conf.sensor_heater_config.heatr_dur_prof = bsec_conf.dur_profile;
bsec_conf.sensor_heater_config.profile_len = 1;
if( bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &bsec_conf.sensor_heater_config, &bme688.dev) != 0 )
{
return BME688::ReturnCode::kSensorHeaterFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::SetSequentialMode()
{
if( bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &bme688.dev) != 0 )
{
return BME688::ReturnCode::kSensorOperationSeqFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::InitialiseBsec()
{
if( bsec_init() != 0 )
{
return BME688::ReturnCode::kSensorBsecFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::UpdateSubscription()
{
bsec_conf.requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_IAQ;
bsec_conf.requested_virtual_sensors[0].sample_rate = BSEC_SAMPLE_RATE_LP;
bsec_conf.requested_virtual_sensors[1].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT;
bsec_conf.requested_virtual_sensors[1].sample_rate = BSEC_SAMPLE_RATE_LP;
bsec_conf.requested_virtual_sensors[2].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE;
bsec_conf.requested_virtual_sensors[2].sample_rate = BSEC_SAMPLE_RATE_LP;
bsec_conf.requested_virtual_sensors[3].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY;
bsec_conf.requested_virtual_sensors[3].sample_rate = BSEC_SAMPLE_RATE_LP;
bsec_sensor_configuration_t requiredSensorSettings[BSEC_MAX_PHYSICAL_SENSOR];
bsec_conf.number_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
if( bsec_update_subscription( bsec_conf.requested_virtual_sensors,
bsec_conf.requested_virtual_sensors_number,
requiredSensorSettings,
&bsec_conf.number_required_sensor_settings ) != 0 )
{
return BME688::ReturnCode::kSensorBsecSubscriptionFail;
}
return BME688::ReturnCode::kOk;
}
BME688::ReturnCode BME688::ProcessData()
{
uint32_t delayInUs = bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bme688.conf, &bme688.dev) + (bsec_conf.sensor_heater_config.heatr_dur_prof[0] * 1000);
delay(delayInUs,NULL);
if( bme68x_get_data(BME68X_SEQUENTIAL_MODE, bsec_conf.sensor_data, &bsec_conf.data_fields, &bme688.dev) != 0 )
{
return BME688::ReturnCode::kSensorGetDataFail;
}
return BME688::ReturnCode::kOk;
}
void BME688::BsecProcessing()
{
if (!(bsec_conf.sensor_data[bsec_conf.data_fields - 1].status & BME68X_NEW_DATA_MSK))
return;
bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR];
uint8_t nInputs = 0;;
bsec_conf.current_time_ns = Kernel::get_ms_count() * INT64_C(1000000);
inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE;
inputs[nInputs].signal = bsec_conf.sensor_data[bsec_conf.data_fields - 1].temperature;
inputs[nInputs].time_stamp = bsec_conf.current_time_ns;
nInputs++;
inputs[nInputs].sensor_id = BSEC_INPUT_HUMIDITY;
inputs[nInputs].signal = bsec_conf.sensor_data[bsec_conf.data_fields - 1].humidity;
inputs[nInputs].time_stamp = bsec_conf.current_time_ns;
nInputs++;
inputs[nInputs].sensor_id = BSEC_INPUT_PRESSURE;
inputs[nInputs].signal = bsec_conf.sensor_data[bsec_conf.data_fields - 1].pressure;
inputs[nInputs].time_stamp = bsec_conf.current_time_ns;
nInputs++;
inputs[nInputs].sensor_id = BSEC_INPUT_GASRESISTOR;
inputs[nInputs].signal = bsec_conf.sensor_data[bsec_conf.data_fields - 1].gas_resistance;
inputs[nInputs].time_stamp = bsec_conf.current_time_ns;
nInputs++;
inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE;
inputs[nInputs].signal = bsec_conf.temperature_offset;
inputs[nInputs].time_stamp = bsec_conf.current_time_ns;
uint8_t nOutputs = 0;
nOutputs = BSEC_NUMBER_OUTPUTS;
bsec_output_t outputStorage[BSEC_NUMBER_OUTPUTS];
auto status = bsec_do_steps(inputs, nInputs, outputStorage, &nOutputs);
if (status != BSEC_OK)
{
return;
}
if (nOutputs > 0)
{
new_data_available = true;
for (uint8_t i = 0; i < nOutputs; i++)
{
switch (outputStorage[i].sensor_id)
{
case BSEC_OUTPUT_IAQ:
sensor_data.iaq = outputStorage[i].signal;
sensor_data.iaq_accuracy = outputStorage[i].accuracy;
break;
case BSEC_OUTPUT_CO2_EQUIVALENT:
sensor_data.co2 = outputStorage[i].signal;
sensor_data.co2_accuracy = outputStorage[i].accuracy;
break;
case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE:
sensor_data.temperature = outputStorage[i].signal;
break;
case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY:
sensor_data.humidity = outputStorage[i].signal;
break;
default:
break;
}
}
}
else
{
new_data_available = false;
}
}
void BME688::DumpData()
{
printf("Temperature:%2.1f *C\n\r",sensor_data.temperature);
printf("Humidity:%2.1f\n\r",sensor_data.humidity);
printf("CO2:%2.1f\n\r",sensor_data.co2);
printf("CO2 Accuracy:%d\n\r",sensor_data.co2_accuracy);
printf("IAQ:%2.1f\n\r",sensor_data.iaq);
printf("IAQ Accuracy:%d",sensor_data.iaq_accuracy);
}
bool BME688::IsNewDataAvailable()
{
return new_data_available;
}
BME688::SensorData BME688::GetLatest()
{
return sensor_data;
}
/**
* Below is the first effort to re-implement BSEC2 Arduino library
*
*/
/**
* @brief Set the BME68X sensor configuration to forced mode
*/
BME688::ReturnCode BME688::SetBme68xConfigForced( const bsec_bme_settings_t &bsec_bme_conf )
{
ReturnCode status = ReturnCode::kError;
// Set the filter, odr, temperature, pressure and humidity settings */
status = SetTphOverSampling( bsec_bme_conf.temperature_oversampling,
bsec_bme_conf.pressure_oversampling,
bsec_bme_conf.humidity_oversampling );
if( status != ReturnCode::kOk )
{
return status;
}
// set heater profile
status = SetHeaterProfile( bsec_bme_conf.heater_temperature,
bsec_bme_conf.heater_duration );
if( status != ReturnCode::kOk )
{
return status;
}
// set operation mode
status = SetOperationMode( BME68X_FORCED_MODE );
if( status != ReturnCode::kOk )
{
return status;
}
this->last_op_mode = BME68X_FORCED_MODE;
return status;
}
/**
* @brief Set the BME68X sensor configuration to parallel mode
*/
BME688::ReturnCode BME688::SetBme68xConfigParallel( const bsec_bme_settings_t& bsec_bme_conf )
{
uint16_t shared_heater_dur = 0;
ReturnCode status = ReturnCode::kError;
// Set the filter, odr, temperature, pressure and humidity settings */
status = SetTphOverSampling( bsec_bme_conf.temperature_oversampling,
bsec_bme_conf.pressure_oversampling,
bsec_bme_conf.humidity_oversampling );
if( status != ReturnCode::kOk )
{
return status;
}
// calculate heater duration
shared_heater_dur = BSEC_TOTAL_HEAT_DUR - (GetMeasurementDuration(BME68X_PARALLEL_MODE) / INT64_C(1000));
// set heater profile
status = SetHeaterProfile( (uint16_t*) bsec_bme_conf.heater_temperature_profile,
(uint16_t*) bsec_bme_conf.heater_duration_profile,
shared_heater_dur,
bsec_bme_conf.heater_profile_len );
if( status != ReturnCode::kOk )
{
return status;
}
// set bme688 operation mode
status = SetOperationMode( BME68X_PARALLEL_MODE );
if( status != ReturnCode::kOk )
{
return status;
}
this->last_op_mode = BME68X_PARALLEL_MODE;
return status;
}
/**
* @brief Function to set the Temperature, Pressure and Humidity over-sampling
*/
BME688::ReturnCode BME688::SetTphOverSampling( const uint8_t os_temp,
const uint8_t os_pres,
const uint8_t os_hum )
{
if( bme68x_get_conf(&bme688.conf, &bme688.dev) == BME68X_OK )
{
bme688.conf.os_hum = os_hum;
bme688.conf.os_pres = os_pres;
bme688.conf.os_temp = os_temp;
if( bme68x_set_conf(&bme688.conf, &bme688.dev) == BME68X_OK )
{
return ReturnCode::kOk;
}
}
return ReturnCode::kSensorConfigFail;
}
/**
* @brief Function to set the heater profile for Forced mode
*/
BME688::ReturnCode BME688::SetHeaterProfile( const uint16_t temp,
const uint16_t dur )
{
bme68x_heatr_conf heater_conf
{
.enable = BME68X_ENABLE,
.heatr_temp = temp,
.heatr_dur = dur
};
if( bme68x_set_heatr_conf(BME68X_FORCED_MODE, &heater_conf, &bme688.dev) == BME68X_OK )
{
return ReturnCode::kOk;
}
return ReturnCode::kSensorHeaterFail;
}
/**
* @brief Function to set the heater profile for Parallel mode
*/
BME688::ReturnCode BME688::SetHeaterProfile( uint16_t* const temp,
uint16_t* const mul,
const uint16_t shared_heater_dur,
const uint8_t profile_len )
{
bme68x_heatr_conf heater_conf
{
.enable = BME68X_ENABLE,
.heatr_temp_prof = temp,
.heatr_dur_prof = mul,
.profile_len = profile_len,
.shared_heatr_dur = shared_heater_dur
};
if( bme68x_set_heatr_conf( BME68X_PARALLEL_MODE, &heater_conf, &bme688.dev) == BME68X_OK )
{
return ReturnCode::kOk;
}
return ReturnCode::kSensorHeaterFail;
}
/**
* @brief Function to set the operation mode
*/
BME688::ReturnCode BME688::SetOperationMode(const uint8_t op_mode)
{
int8_t status = bme68x_set_op_mode(op_mode, &bme688.dev);
if( status == BME68X_OK &&
op_mode != BME68X_SLEEP_MODE )
{
this->last_op_mode = op_mode;
}
if( status == BME68X_OK )
{
return ReturnCode::kOk;
}
return ReturnCode::kSensorSetOperationFail;
}
/**
* @brief Function to get the measurement duration in microseconds
*/
uint32_t BME688::GetMeasurementDuration(const uint8_t op_mode)
{
uint8_t target_op_mode = op_mode;
if (target_op_mode == BME68X_SLEEP_MODE)
{
target_op_mode = this->last_op_mode;
}
return bme68x_get_meas_dur(target_op_mode, &bme688.conf, &bme688.dev);
}
// Static function
int8_t ReadRegister(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
BME688* bme688 = (BME688*) intf_ptr;
if( bme688->i2c_local == nullptr )
{
// return error
return 0xFF;
}
// local variable definition
int8_t rslt = 0; // Return 0 for Success, non-zero for failure
uint32_t aux = 0;
aux = bme688->i2c_local->write ( bme688->bme688_addr_8bit, (char*)®_addr, 1, true );
if ( aux != 0 ) {
return 0xFF; // return error
}
aux = bme688->i2c_local->read ( bme688->bme688_addr_8bit, (char*)®_data[0], length );
if ( aux != 0 ) {
return 0xFF; // return error
}
return rslt;
}
static int8_t WriteRegister(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
// check callback obeject pointer
BME688* bme688 = (BME688*) intf_ptr;
if( bme688->i2c_local == nullptr )
{
return 0xFF; // return error
}
// local variable definition
int8_t rslt = 0; // Return 0 for Success, non-zero for failure
uint32_t aux = 0;
char cmd[16] = {0};
uint32_t i = 0;
// Prepare the data to be sent
cmd[0] = reg_addr;
for ( i = 1; i <= length; i++ ) {
cmd[i] = reg_data[i - 1];
}
// Write data
aux = bme688->i2c_local->write( bme688->bme688_addr_8bit, &cmd[0], length + 1, false );
if ( aux != 0 ) {
return 0xFF; // return error
}
return rslt;
}
static void delay(uint32_t period, void *intf_ptr)
{
wait_us ( period );
}