-
Notifications
You must be signed in to change notification settings - Fork 9
/
bmx280.c
548 lines (464 loc) · 16.2 KB
/
bmx280.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
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
/**
* BMX280 - BME280 & BMP280 Driver for Esspressif ESP-32.
*
* MIT License
*
* Copyright (C) 2020 Halit Utku Maden
* Please contact at <utkumaden@hotmail.com>
*/
// LEGAL NOTE:
// Any code between below the caption "// HERE BE DRAGONS" and above the caption
// "// END OF DRAGONS" contains modified versions of code owned by Bosch
// Sensortec GmbH and it is not clearly licensed, therefore this code is not
// covered by the MIT of this repository. Use at your own risk.
#include "bmx280.h"
#include "esp_log.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// [BME280] Register address of humidity least significant byte.
#define BMX280_REG_HUMI_LSB 0xFE
// [BME280] Register address of humidity most significant byte.
#define BMX280_REG_HUMI_MSB 0xFD
// Register address of temperature fraction significant byte.
#define BMX280_REG_TEMP_XSB 0xFC
// Register address of temperature least significant byte.
#define BMX280_REG_TEMP_LSB 0xFB
// Register address of temperature most significant byte.
#define BMX280_REG_TEMP_MSB 0xFA
// Register address of pressure fraction significant byte.
#define BMX280_REG_PRES_XSB 0xF9
// Register address of pressure least significant byte.
#define BMX280_REG_PRES_LSB 0xF8
// Register address of pressure most significant byte.
#define BMX280_REG_PRES_MSB 0xF7
// Register address of sensor configuration.
#define BMX280_REG_CONFIG 0xF5
// Register address of sensor measurement control.
#define BMX280_REG_MESCTL 0xF4
// Register address of sensor status.
#define BMX280_REG_STATUS 0xF3
// [BME280] Register address of humidity control.
#define BMX280_REG_HUMCTL 0xF2
// [BME280] Register address of calibration constants. (high bank)
#define BMX280_REG_CAL_HI 0xE1
// Register address of calibration constants. (low bank)
#define BMX280_REG_CAL_LO 0x88
// Register address for sensor reset.
#define BMX280_REG_RESET 0xE0
// Chip reset vector.
#define BMX280_RESET_VEC 0xB6
// Register address for chip identification number.
#define BMX280_REG_CHPID 0xD0
// Value of REG_CHPID for BME280
#define BME280_ID 0x60
// Value of REG_CHPID for BMP280 (Engineering Sample 1)
#define BMP280_ID0 0x56
// Value of REG_CHPID for BMP280 (Engineering Sample 2)
#define BMP280_ID1 0x57
// Value of REG_CHPID for BMP280 (Production)
#define BMP280_ID2 0x58
struct bmx280_t{
// I2C port.
i2c_port_t i2c_port;
// Slave Address of sensor.
uint8_t slave;
// Chip ID of sensor
uint8_t chip_id;
// Compensation data
struct {
uint16_t T1;
int16_t T2;
int16_t T3;
uint16_t P1;
int16_t P2;
int16_t P3;
int16_t P4;
int16_t P5;
int16_t P6;
int16_t P7;
int16_t P8;
int16_t P9;
#if !(CONFIG_BMX280_EXPECT_BMP280)
uint8_t H1;
int16_t H2;
uint8_t H3;
int16_t H4;
int16_t H5;
int8_t H6;
#endif
} cmps;
// Storage for a variable proportional to temperature.
int32_t t_fine;
};
/**
* Macro that identifies a chip id as BME280 or BMP280
* @note Only use when the chip is verified to be either a BME280 or BMP280.
* @see bmx280_verify
* @param chip_id The chip id.
*/
#define bmx280_isBME(chip_id) ((chip_id) == BME280_ID)
/**
* Macro to verify a the chip id matches with the expected values.
* @note Use when the chip needs to be verified as a BME280 or BME280.
* @see bmx280_isBME
* @param chip_id The chip id.
*/
#define bmx280_verify(chip_id) (((chip_id) == BME280_ID) || ((chip_id) == BMP280_ID2) || ((chip_id) == BMP280_ID1) || ((chip_id) == BMP280_ID0))
/**
* Returns false if the sensor was not found.
* @param bmx280 The driver structure.
*/
#define bmx280_validate(bmx280) (!(bmx280->slave == 0xDE && bmx280->chip_id == 0xAD))
/**
* Read from sensor.
* @param bmx280 Driver Sturcture.
* @param addr Register address.
* @param dout Data to read.
* @param size The number of bytes to read.
* @returns Error codes.
*/
static esp_err_t bmx280_read(bmx280_t *bmx280, uint8_t addr, uint8_t *dout, size_t size)
{
esp_err_t err;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
if (cmd)
{
// Write register address
i2c_master_start(cmd);
i2c_master_write_byte(cmd, bmx280->slave | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, addr, true);
// Read Registers
i2c_master_start(cmd);
i2c_master_write_byte(cmd, bmx280->slave | I2C_MASTER_READ, true);
i2c_master_read(cmd, dout, size, I2C_MASTER_LAST_NACK);
i2c_master_stop(cmd);
err = i2c_master_cmd_begin(bmx280->i2c_port, cmd, CONFIG_BMX280_TIMEOUT);
i2c_cmd_link_delete(cmd);
return err;
}
else
{
return ESP_ERR_NO_MEM;
}
}
static esp_err_t bmx280_write(bmx280_t* bmx280, uint8_t addr, const uint8_t *din, size_t size)
{
esp_err_t err;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
if (cmd)
{
for (int i = 0; i < size; i++)
{
i2c_master_start(cmd);
i2c_master_write_byte(cmd, bmx280->slave | I2C_MASTER_WRITE, true);
// Register
i2c_master_write_byte(cmd, addr + i, true);
//Data
i2c_master_write_byte(cmd, din[i], true);
}
i2c_master_stop(cmd);
err = i2c_master_cmd_begin(bmx280->i2c_port, cmd, CONFIG_BMX280_TIMEOUT);
i2c_cmd_link_delete(cmd);
return err;
}
else
{
return ESP_ERR_NO_MEM;
}
}
static esp_err_t bmx280_probe_address(bmx280_t *bmx280)
{
esp_err_t err = bmx280_read(bmx280, BMX280_REG_CHPID, &bmx280->chip_id, sizeof bmx280->chip_id);
if (err == ESP_OK)
{
if (
#if CONFIG_BMX280_EXPECT_BME280
bmx280->chip_id == BME280_ID
#elif CONFIG_BMX280_EXPECT_BMP280
bmx280->chip_id == BMP280_ID2 || bmx280->chip_id == BMP280_ID1 || bmx280->chip_id == BMP280_ID0
#else
bmx280_verify(bmx280->chip_id)
#endif
)
{
ESP_LOGI("bmx280", "Probe success: address=%hhx, id=%hhx", bmx280->slave, bmx280->chip_id);
return ESP_OK;
}
else
{
err = ESP_ERR_NOT_FOUND;
}
}
ESP_LOGW("bmx280", "Probe failure: address=%hhx, id=%hhx, reason=%s", bmx280->slave, bmx280->chip_id, esp_err_to_name(err));
return err;
}
static esp_err_t bmx280_probe(bmx280_t *bmx280)
{
ESP_LOGI("bmx280", "Probing for BMP280/BME280 sensors on I2C %d", bmx280->i2c_port);
#if CONFIG_BMX280_ADDRESS_HI
bmx280->slave = 0xEE;
return bmx280_probe_address(bmx280);
#elif CONFIG_BMX280_ADDRESS_LO
bmx280->slave = 0xEC;
return bmx280_probe_address(bmx280);
#else
esp_err_t err;
bmx280->slave = 0xEC;
if ((err = bmx280_probe_address(bmx280)) != ESP_OK)
{
bmx280->slave = 0xEE;
if ((err = bmx280_probe_address(bmx280)) != ESP_OK)
{
ESP_LOGE("bmx280", "Sensor not found.");
bmx280->slave = 0xDE;
bmx280->chip_id = 0xAD;
}
}
return err;
#endif
}
static esp_err_t bmx280_reset(bmx280_t *bmx280)
{
const static uint8_t din[] = { BMX280_RESET_VEC };
return bmx280_write(bmx280, BMX280_REG_RESET, din, sizeof din);
}
static esp_err_t bmx280_calibrate(bmx280_t *bmx280)
{
// Honestly, the best course of action is to read the high and low banks
// into a buffer, then put them in the calibration values. Makes code
// endian agnostic, and overcomes struct packing issues.
// Also the BME280 high bank is weird.
//
// Write and pray to optimizations is my new motto.
ESP_LOGI("bmx280", "Reading out calibration values...");
esp_err_t err;
uint8_t buf[26];
// Low Bank
err = bmx280_read(bmx280, BMX280_REG_CAL_LO, buf, sizeof buf);
if (err != ESP_OK) return err;
ESP_LOGI("bmx280", "Read Low Bank.");
bmx280->cmps.T1 = buf[0] | (buf[1] << 8);
bmx280->cmps.T2 = buf[2] | (buf[3] << 8);
bmx280->cmps.T3 = buf[4] | (buf[5] << 8);
bmx280->cmps.P1 = buf[6] | (buf[7] << 8);
bmx280->cmps.P2 = buf[8] | (buf[9] << 8);
bmx280->cmps.P3 = buf[10] | (buf[11] << 8);
bmx280->cmps.P4 = buf[12] | (buf[13] << 8);
bmx280->cmps.P5 = buf[14] | (buf[15] << 8);
bmx280->cmps.P6 = buf[16] | (buf[17] << 8);
bmx280->cmps.P7 = buf[18] | (buf[19] << 8);
bmx280->cmps.P8 = buf[20] | (buf[21] << 8);
bmx280->cmps.P9 = buf[22] | (buf[23] << 8);
#if !(CONFIG_BMX280_EXPECT_BMP280)
#if CONFIG_BMX280_EXPECT_DETECT
if (bmx280_isBME(bmx280->chip_id)) // Only conditional for detect scenario.
#endif
{
// First get H1 out of the way.
bmx280->cmps.H1 = buf[23];
err = bmx280_read(bmx280, BMX280_REG_CAL_HI, buf, 7);
if (err != ESP_OK) return err;
ESP_LOGI("bmx280", "Read High Bank.");
bmx280->cmps.H2 = buf[0] | (buf[1] << 8);
bmx280->cmps.H3 = buf[2];
bmx280->cmps.H4 = (buf[3] << 4) | (buf[4] & 0x0F);
bmx280->cmps.H5 = (buf[4] >> 4) | (buf[5] << 4);
bmx280->cmps.H6 = buf[6];
}
#endif
return ESP_OK;
}
bmx280_t* bmx280_create(i2c_port_t port)
{
bmx280_t* bmx280 = malloc(sizeof(bmx280_t));
if (bmx280)
{
memset(bmx280, 0, sizeof(bmx280_t));
bmx280->i2c_port = port;
bmx280->slave = 0xDE;
bmx280->chip_id = 0xAD;
}
return bmx280;
}
void bmx280_close(bmx280_t *bmx280)
{
free(bmx280);
}
esp_err_t bmx280_init(bmx280_t* bmx280)
{
if (bmx280 == NULL) return ESP_ERR_INVALID_ARG;
esp_err_t error = bmx280_probe(bmx280) || bmx280_reset(bmx280);
if (error == ESP_OK)
{
// Give the sensor 10 ms delay to reset.
vTaskDelay(pdMS_TO_TICKS(10));
// Read calibration data.
bmx280_calibrate(bmx280);
ESP_LOGI("bmx280", "Dumping calibration...");
ESP_LOG_BUFFER_HEX("bmx280", &bmx280->cmps, sizeof(bmx280->cmps));
}
return error;
}
esp_err_t bmx280_configure(bmx280_t* bmx280, bmx280_config_t *cfg)
{
if (bmx280 == NULL || cfg == NULL) return ESP_ERR_INVALID_ARG;
if (!bmx280_validate(bmx280)) return ESP_ERR_INVALID_STATE;
// Always set ctrl_meas first.
uint8_t num = (cfg->t_sampling << 5) | (cfg->p_sampling << 2) | BMX280_MODE_SLEEP;
esp_err_t err = bmx280_write(bmx280, BMX280_REG_MESCTL, &num, sizeof num);
if (err) return err;
// We can set cfg now.
num = (cfg->t_standby << 5) | (cfg->iir_filter << 2);
err = bmx280_write(bmx280, BMX280_REG_CONFIG, &num, sizeof num);
if (err) return err;
#if !(CONFIG_BMX280_EXPECT_BMP280)
#if CONFIG_BMX280_EXPECT_DETECT
if (bmx280_isBME(bmx280->chip_id))
#elif CONFIG_BMX280_EXPECT_BME280
#endif
{
num = cfg->h_sampling;
err = bmx280_write(bmx280, BMX280_REG_HUMCTL, &num, sizeof(num));
if (err) return err;
}
#endif
// f = 0;
return ESP_OK;
}
esp_err_t bmx280_setMode(bmx280_t* bmx280, bmx280_mode_t mode)
{
uint8_t ctrl_mes;
esp_err_t err;
if ((err = bmx280_read(bmx280, BMX280_REG_MESCTL, &ctrl_mes, 1)) != ESP_OK)
return err;
ctrl_mes = (ctrl_mes & (~3)) | mode;
return bmx280_write(bmx280, BMX280_REG_MESCTL, &ctrl_mes, 1);
}
esp_err_t bmx280_getMode(bmx280_t* bmx280, bmx280_mode_t* mode)
{
uint8_t ctrl_mes;
esp_err_t err;
if ((err = bmx280_read(bmx280, BMX280_REG_MESCTL, &ctrl_mes, 1)) != ESP_OK)
return err;
ctrl_mes &= 3;
switch (ctrl_mes)
{
default:
*mode = ctrl_mes; break;
case (BMX280_MODE_FORCE + 1):
*mode = BMX280_MODE_FORCE; break;
}
return ESP_OK;
}
bool bmx280_isSampling(bmx280_t* bmx280)
{
uint8_t status;
if (bmx280_read(bmx280, BMX280_REG_STATUS, &status, 1) == ESP_OK)
return (status & (1 << 3)) != 0;
else
return false;
}
// LEGAL NOTE:
// Any code between below the caption "// HERE BE DRAGONS" and above the caption
// "// END OF DRAGONS" contains modified versions of code owned by Bosch
// Sensortec GmbH and it is not clearly licensed, therefore this code is not
// covered by the MIT of this repository. Use at your own risk.
// HERE BE DRAGONS
// This code is revised from the Bosch code within the datasheet of the BME280.
// I do not understand it enough to tell you what it does.
// No touchies.
// Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC.
// t_fine carries fine temperature as global value
int32_t BME280_compensate_T_int32(bmx280_t *bmx280, int32_t adc_T)
{
int32_t var1, var2, T;
var1 = ((((adc_T>>3) -((int32_t)bmx280->cmps.T1<<1))) * ((int32_t)bmx280->cmps.T2)) >> 11;
var2 =(((((adc_T>>4) -((int32_t)bmx280->cmps.T1)) * ((adc_T>>4) -((int32_t)bmx280->cmps.T1))) >> 12) * ((int32_t)bmx280->cmps.T3)) >> 14;
bmx280->t_fine = var1 + var2;
T = (bmx280->t_fine * 5 + 128) >> 8;
return T;
}
// Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
// Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa
uint32_t BME280_compensate_P_int64(bmx280_t *bmx280, int32_t adc_P)
{
int64_t var1, var2, p;
var1 = ((int64_t)bmx280->t_fine) -128000;
var2 = var1 * var1 * (int64_t)bmx280->cmps.P6;
var2 = var2 + ((var1*(int64_t)bmx280->cmps.P5)<<17);
var2 = var2 + (((int64_t)bmx280->cmps.P4)<<35);
var1 = ((var1 * var1 * (int64_t)bmx280->cmps.P3)>>8) + ((var1 * (int64_t)bmx280->cmps.P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)bmx280->cmps.P1)>>33;
if(var1 == 0){
return 0; // avoid exception caused by division by zero
}
p = 1048576-adc_P;
p = (((p<<31)-var2)*3125)/var1;
var1 = (((int64_t)bmx280->cmps.P9) * (p>>13) * (p>>13)) >> 25;
var2 =(((int64_t)bmx280->cmps.P8) * p) >> 19;
p = ((p + var1 + var2) >> 8) + (((int64_t)bmx280->cmps.P7)<<4);
return (uint32_t)p;
}
#if !CONFIG_BMX280_EXPECT_BMP280
// Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits).
// Output value of “47445” represents 47445/1024 = 46.333 %RH
uint32_t bme280_compensate_H_int32(bmx280_t *bmx280, int32_t adc_H)
{
int32_t v_x1_u32r;
v_x1_u32r = (bmx280->t_fine -((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) -(((int32_t)bmx280->cmps.H4) << 20) -(((int32_t)bmx280->cmps.H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) * (((((((v_x1_u32r * ((int32_t)bmx280->cmps.H6)) >> 10) * (((v_x1_u32r * ((int32_t)bmx280->cmps.H3)) >> 11) + ((int32_t)32768))) >> 10) + ((int32_t)2097152)) * ((int32_t)bmx280->cmps.H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r -(((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)bmx280->cmps.H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
v_x1_u32r = (v_x1_u32r > 419430400? 419430400: v_x1_u32r);
return(uint32_t)(v_x1_u32r>>12);
}
#endif
// END OF DRAGONS
esp_err_t bmx280_readout(bmx280_t *bmx280, int32_t *temperature, uint32_t *pressure, uint32_t *humidity)
{
if (bmx280 == NULL) return ESP_ERR_INVALID_ARG;
if (!bmx280_validate(bmx280)) return ESP_ERR_INVALID_STATE;
uint8_t buffer[3];
esp_err_t error;
if (temperature)
{
if ((error = bmx280_read(bmx280, BMX280_REG_TEMP_MSB, buffer, 3)) != ESP_OK)
return error;
*temperature = BME280_compensate_T_int32(bmx280,
(buffer[0] << 12) | (buffer[1] << 4) | (buffer[0] >> 4)
);
}
if (pressure)
{
if ((error = bmx280_read(bmx280, BMX280_REG_PRES_MSB, buffer, 3)) != ESP_OK)
return error;
*pressure = BME280_compensate_P_int64(bmx280,
(buffer[0] << 12) | (buffer[1] << 4) | (buffer[0] >> 4)
);
}
#if !(CONFIG_BMX280_EXPECT_BMP280)
#if CONFIG_BMX280_EXPECT_DETECT
if (bmx280_isBME(bmx280->chip_id))
#elif CONFIG_BMX280_EXPECT_BME280
#endif
{
if (humidity)
{
if ((error = bmx280_read(bmx280, BMX280_REG_HUMI_MSB, buffer, 2)) != ESP_OK)
return error;
*humidity = bme280_compensate_H_int32(bmx280,
(buffer[0] << 8) | buffer[1]
);
}
}
#if CONFIG_BMX280_EXPECT_DETECT
else if (humidity)
*humidity = UINT32_MAX;
#endif
#else
if (humidity)
*humidity = UINT32_MAX;
#endif
return ESP_OK;
}