-
Notifications
You must be signed in to change notification settings - Fork 11
/
i2c_bme280.c
488 lines (393 loc) · 13.8 KB
/
i2c_bme280.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
/*
The driver for the temperature, pressure, & humidity sensor BME280
Official repository: https://github.com/RyAndrew/esp8266_i2c_bme280
Adapted From: https://github.com/CHERTS/esp8266-i2c_bmp180
This driver depends on the I2C driver https://github.com/zarya/esp8266_i2c_driver/
The MIT License (MIT)
Copyright (C) 2015 Andrew Rymarczyk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <math.h>
#include "i2c_bme280.h"
#include "../i2c/i2c.h"
uint16_t calib_dig_T1;
int16_t calib_dig_T2;
int16_t calib_dig_T3;
uint16_t calib_dig_P1;
int16_t calib_dig_P2;
int16_t calib_dig_P3;
int16_t calib_dig_P4;
int16_t calib_dig_P5;
int16_t calib_dig_P6;
int16_t calib_dig_P7;
int16_t calib_dig_P8;
int16_t calib_dig_P9;
int8_t calib_dig_H1;
int16_t calib_dig_H2;
int8_t calib_dig_H3;
int16_t calib_dig_H4;
int16_t calib_dig_H5;
int8_t calib_dig_H6;
uint8_t osrs_t = 1; //Temperature oversampling x 1
uint8_t osrs_p = 1; //Pressure oversampling x 1
uint8_t osrs_h = 1; //Humidity oversampling x 1
uint8_t t_sb = 4; //Tstandby, 5=1000ms, 4=500ms
uint8_t filter = 0; //Filter off
uint8_t spi3w_en = 0; //3-wire SPI Disable
uint8_t BME280_OperationMode = BME280_MODE_NORMAL;
unsigned long int hum_raw, temp_raw, pres_raw;
signed long int t_fine;
signed long int temp_act;
unsigned long int press_act, hum_act;
bool ICACHE_FLASH_ATTR BME280_Init(uint8_t operationMode)
{
i2c_init();
if(!BME280_verifyChipId()){
return 0;
}
BME280_OperationMode = operationMode;
BME280_writeConfigRegisters();
BME280_readCalibrationRegisters();
return 1;
}
bool ICACHE_FLASH_ATTR BME280_sendI2cWriteData(uint8_t writeReg, uint8_t regData){
if(!BME280_startI2cWrite() ){
return 0;
}
i2c_writeByte(writeReg);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_sendI2cWriteData: i2c_writeByte(%X) slave not ack..\r\n", writeReg);
#endif
i2c_stop();
return 0;
}
i2c_writeByte(regData);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_sendI2cWriteData: i2c_writeByte(%X) slave not ack..\r\n", regData);
#endif
i2c_stop();
return 0;
}
i2c_stop();
return 1;
}
bool ICACHE_FLASH_ATTR BME280_startI2cWrite(){
i2c_start();
i2c_writeByte(BME280_W);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_startI2cWrite: i2c_writeByte(BME280_W) slave not ack..\r\n");
#endif
i2c_stop();
return 0;
}
return 1;
}
bool ICACHE_FLASH_ATTR BME280_sendI2cRead(uint8_t readReg){
i2c_start();
i2c_writeByte(BME280_W);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_sendI2cRead: i2c_writeByte(BME280_W) slave not ack..\r\n");
#endif
i2c_stop();
return 0;
}
i2c_writeByte(readReg);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_sendI2cRead: i2c_writeByte(readReg) slave not ack..\r\n");
#endif
i2c_stop();
return 0;
}
i2c_start();
i2c_writeByte(BME280_R);
if(!i2c_check_ack()){
#ifdef BME280_DEBUG
ets_uart_printf("BME280_sendI2cRead: i2c_writeByte(BME280_R) slave not ack..\r\n");
#endif
i2c_stop();
return 1;
}
}
bool ICACHE_FLASH_ATTR BME280_verifyChipId(void){
BME280_sendI2cRead(BME280_CHIP_ID_REG);
uint8_t version = i2c_readByte();
i2c_send_ack(0);
i2c_stop();
if (version != BME280_CHIP_ID ) {
#ifdef BME280_DEBUG
ets_uart_printf("BME280: expected chip id 0x%X, found chip id 0x%X\r\n", BME280_CHIP_ID, version);
#endif
return 0;
}
return 1;
}
void ICACHE_FLASH_ATTR BME280_writeConfigRegisters(void){
uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | BME280_OperationMode;
uint8_t ctrl_hum_reg = osrs_h;
uint8_t config_reg = (t_sb << 5) | (filter << 2) | spi3w_en;
BME280_sendI2cWriteData(BME280_REG_CTRL_HUM, ctrl_hum_reg);
BME280_sendI2cWriteData(BME280_REG_CTRL_MEAS, ctrl_meas_reg);
BME280_sendI2cWriteData(BME280_REG_CONFIG, config_reg);
}
void ICACHE_FLASH_ATTR BME280_readCalibrationRegisters(void){
uint8_t msb, lsb;
//////////////
// Read section 0x88
BME280_sendI2cRead(0x88);
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_T1 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_T1 = %u\r\n", lsb, msb, calib_dig_T1);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_T2 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_T2 = %d\r\n", lsb, msb, calib_dig_T2);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_T3 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_T3 = %d\r\n", lsb, msb, calib_dig_T3);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P1 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P1 = %u\r\n", lsb, msb, calib_dig_P1);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P2 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P2 = %d\r\n", lsb, msb, calib_dig_P2);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P3 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P3 = %d\r\n", lsb, msb, calib_dig_P3);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P4 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P4 = %d\r\n", lsb, msb, calib_dig_P4);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P5 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P5 = %d\r\n", lsb, msb, calib_dig_P5);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P6 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P6 = %d\r\n", lsb, msb, calib_dig_P6);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P7 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P7 = %d\r\n", lsb, msb, calib_dig_P7);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_P8 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P8 = %d\r\n", lsb, msb, calib_dig_P8);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte();
i2c_send_ack(0);
i2c_stop();
calib_dig_P9 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_P9 = %d\r\n", lsb, msb, calib_dig_P9);
//#endif
//////////////
// Read section 0xA1
BME280_sendI2cRead(0xA1);
msb = i2c_readByte(); i2c_send_ack(0); // STOP
i2c_stop();
calib_dig_H1 = msb;
//#ifdef BME280_DEBUG
//ets_uart_printf("msb: 0x%X = calib_dig_H1 = %d\r\n", msb, calib_dig_H1);
//#endif
//////////////
// Read section 0xE1
BME280_sendI2cRead(0xE1);
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_H2 = (msb << 8) | lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_H2 = %d\r\n", lsb, msb, calib_dig_H2);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
calib_dig_H3 = lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X = calib_dig_H3 = %d\r\n", lsb, calib_dig_H3);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
msb = i2c_readByte(); i2c_send_ack(1);
calib_dig_H4 = (lsb << 4) | (0x0f & msb);
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_H4 = %d\r\n", lsb, msb, calib_dig_H4);
//#endif
lsb = i2c_readByte(); i2c_send_ack(1);
calib_dig_H5 = (lsb << 4) | ((msb >> 4) & 0x0F);
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X, msb: 0x%X = calib_dig_H5 = %d\r\n", lsb, msb, calib_dig_H5);
//#endif
lsb = i2c_readByte();
i2c_send_ack(0); // STOP
i2c_stop();
calib_dig_H6 = lsb;
//#ifdef BME280_DEBUG
//ets_uart_printf("lsb 0x%X = calib_dig_H6 = %d\r\n", lsb, calib_dig_H6);
//#endif
}
bool ICACHE_FLASH_ATTR BME280_sendI2cTriggerForcedRead(){
uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | BME280_OperationMode;
BME280_sendI2cWriteData(BME280_REG_CTRL_MEAS, ctrl_meas_reg);
os_delay_us(10000); // wait 10ms for worst case max sensor read time
return 1;
}
bool ICACHE_FLASH_ATTR BME280_sendI2cReadSensorData(){
uint8 msb, lsb, xlsb;
#ifdef BME280_DEBUG
ets_uart_printf("operation mode = %d\r\n", BME280_OperationMode);
#endif
if(BME280_OperationMode == BME280_MODE_FORCED){
if(!BME280_sendI2cTriggerForcedRead()){
return 0;
}
}
if(!BME280_sendI2cRead(0xF7)){
return 0;
}
//0xF7 - pressure
msb = i2c_readByte(); i2c_send_ack(1);
lsb = i2c_readByte(); i2c_send_ack(1);
xlsb = i2c_readByte(); i2c_send_ack(1);
#ifdef BME280_DEBUG
ets_uart_printf("pres_raw 0: %X, pres_raw 1: %X, pres_raw 2: %X\r\n", msb, lsb, xlsb);
#endif
pres_raw = (msb << 12) | (lsb << 4) | (xlsb >> 4);
//0xFA - temp
msb = i2c_readByte(); i2c_send_ack(1);
lsb = i2c_readByte(); i2c_send_ack(1);
xlsb = i2c_readByte(); i2c_send_ack(1);
#ifdef BME280_DEBUG
ets_uart_printf("temp_raw 3: %X, temp_raw 4: %X, temp_raw 5: %X\r\n",msb, lsb, xlsb);
#endif
temp_raw = (msb << 12) | (lsb << 4) | (xlsb >> 4);
//0xFD - humidity
msb = i2c_readByte(); i2c_send_ack(1);
lsb = i2c_readByte(); i2c_send_ack(1);
#ifdef BME280_DEBUG
ets_uart_printf("hum_raw 6: %X, hum_raw 7: %X\r\n", msb, lsb);
#endif
hum_raw = (msb << 8) | lsb;
i2c_stop();
return 1;
}
unsigned long int ICACHE_FLASH_ATTR BME280_GetTemperatureRaw(){
return temp_raw;
}
unsigned long int ICACHE_FLASH_ATTR BME280_GetPressureRaw(){
return pres_raw;
}
unsigned long int ICACHE_FLASH_ATTR BME280_GetHumidityRaw(){
return hum_raw;
}
void ICACHE_FLASH_ATTR BME280_readSensorData(){
BME280_sendI2cReadSensorData();
// test data:
//temp_raw = 529184;
//pres_raw = 282960;
//hum_raw = 28012;
temp_act = BME280_calibration_Temp(temp_raw);
press_act = BME280_calibration_Press(pres_raw);
hum_act = BME280_calibration_Hum(hum_raw);
}
signed long int ICACHE_FLASH_ATTR BME280_GetT_Fine(){
return t_fine;
}
signed long int ICACHE_FLASH_ATTR BME280_GetTemperature(){
return temp_act;
}
unsigned long int ICACHE_FLASH_ATTR BME280_GetPressure(){
return press_act;
}
unsigned long int ICACHE_FLASH_ATTR BME280_GetHumidity(){
return hum_act;
}
signed long int BME280_calibration_Temp(signed long int adc_T)
{
signed long int var1, var2, T;
var1 = ((((adc_T >> 3) - ((signed long int)calib_dig_T1<<1))) * ((signed long int)calib_dig_T2)) >> 11;
var2 = (((((adc_T >> 4) - ((signed long int)calib_dig_T1)) * ((adc_T>>4) - ((signed long int)calib_dig_T1))) >> 12) * ((signed long int)calib_dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
unsigned long int BME280_calibration_Press(signed long int adc_P)
{
signed long int var1, var2;
unsigned long int P;
var1 = (((signed long int)t_fine)>>1) - (signed long int)64000;
var2 = (((var1>>2) * (var1>>2)) >> 11) * ((signed long int)calib_dig_P6);
var2 = var2 + ((var1*((signed long int)calib_dig_P5))<<1);
var2 = (var2>>2)+(((signed long int)calib_dig_P4)<<16);
var1 = (((calib_dig_P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long int)calib_dig_P2) * var1)>>1))>>18;
var1 = ((((32768+var1))*((signed long int)calib_dig_P1))>>15);
if (var1 == 0){
return 0;
}
P = (((unsigned long int)(((signed long int)1048576)-adc_P)-(var2>>12)))*3125;
if(P<0x80000000){
P = (P << 1) / ((unsigned long int) var1);
}else{
P = (P / (unsigned long int)var1) * 2;
}
var1 = (((signed long int)calib_dig_P9) * ((signed long int)(((P>>3) * (P>>3))>>13)))>>12;
var2 = (((signed long int)(P>>2)) * ((signed long int)calib_dig_P8))>>13;
P = (unsigned long int)((signed long int)P + ((var1 + var2 + calib_dig_P7) >> 4));
return P;
}
unsigned long int BME280_calibration_Hum(signed long int adc_H)
{
signed long int v_x1;
v_x1 = (t_fine - ((signed long int)76800));
v_x1 = (((((adc_H << 14) -(((signed long int)calib_dig_H4) << 20) - (((signed long int)calib_dig_H5) * v_x1)) +
((signed long int)16384)) >> 15) * (((((((v_x1 * ((signed long int)calib_dig_H6)) >> 10) *
(((v_x1 * ((signed long int)calib_dig_H3)) >> 11) + ((signed long int) 32768))) >> 10) + (( signed long int)2097152)) *
((signed long int) calib_dig_H2) + 8192) >> 14));
v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * ((signed long int)calib_dig_H1)) >> 4));
v_x1 = (v_x1 < 0 ? 0 : v_x1);
v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
return (unsigned long int)(v_x1 >> 12);
}