-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme680fs.c
1445 lines (1143 loc) · 32.9 KB
/
bme680fs.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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
/* mas definitions */
#define MASK_CTRL_MEAS_MODE 0x03
#define MASK_CTRL_REG_70 0x08
#define MASK_CTRL_REG_71 0x1F
#define MASK_CTRL_REG_72 0x47
#define MASK_CTRL_REG_73 0x10
#define MASK_CTRL_REG_74 0xFF
#define MASK_CTRL_REG_75 0x1D
/* 9p filesystem functions */
typedef struct Devfile Devfile;
void initfs(char *dirname);
void fsstart(Srv *);
void fsread(Req *r);
void fswrite(Req *r);
void fsend(Srv *);
/* I2C device functions */
void openi2cdev(void);
void initi2cdev(void);
void deiniti2cdev(void);
void closei2cdev(void);
void openi2cdevmain(void);
void closei2cdevmain(void);
/* program logic functions, called from fs functions using I2C functions */
char* fsreadctl(Req *r);
char* fswritectl(Req *r);
char* fsreadtemp(Req *r);
char* fswritetemp(Req *r);
char* fsreadpress(Req *r);
char* fswritepress(Req *r);
char* fsreadhum(Req *r);
char* fswritehum(Req *r);
char* fsreadgas(Req *r);
char* fswritegas(Req *r);
char* fsreadall(Req *r);
char* fswriteall(Req *r);
/* I2C device logic functions, implemented calls and processing */
typedef struct CalibrationData CalibrationData;
typedef struct AllMeasurments AllMeasurments;
typedef struct AllParameters AllParameters;
uchar bme680getchipid(void);
uchar bme680softreset(void);
void bme680setundocumented(void);
void bme680resetparameters(void);
void bme680readcalibrationdata(void);
float bme680gettemp(void);
float bme680getpress(void);
float bme680gethum(void);
void bme680readall(int);
struct CalibrationData {
/* data types are not recognizible from the official datasheet */
/* referenced from BSEC Arduino library - https://github.com/boschsensortec/BSEC-Arduino-library/blob/master/src/bme68x/bme68x.c */
/* temperature calibration parameters */
unsigned short par_t1;
short par_t2;
char par_t3;
/* pressure calibration parameters */
unsigned short par_p1;
short par_p2;
char par_p3;
short par_p4;
short par_p5;
char par_p6;
char par_p7;
short par_p8;
short par_p9;
unsigned char par_p10;
/* humidity calibration parameters */
unsigned short par_h1;
unsigned short par_h2;
char par_h3;
char par_h4;
char par_h5;
unsigned char par_h6;
char par_h7;
/* gas calibration parameters */
char par_g1;
short par_g2;
char par_g3;
char res_heat_range;
char res_heat_val;
char range_sw_error;
};
struct AllMeasurments {
/* calculated measuirment results */
float temp;
float pres;
float hum;
float gas;
long time;
};
struct AllParameters {
/* parameters controling measurments */
char filter;
char temp;
char pres;
char hum;
char gastime;
short gastemp;
int looptime;
};
struct Devfile {
char *name;
char* (*fsread)(Req*);
char* (*fswrite)(Req*);
int mode;
};
Srv fs = {
.start = fsstart,
.read = fsread,
.write = fswrite,
.end = fsend,
};
Devfile files[] = {
{ "ctl", fsreadctl, fswritectl, DMEXCL|0666 },
{ "temp", fsreadtemp, fswritetemp, DMEXCL|0444 },
{ "press", fsreadpress, fswritepress, DMEXCL|0444 },
{ "hum", fsreadhum, fswritehum, DMEXCL|0444 },
{ "gas", fsreadgas, fswritegas, DMEXCL|0444 },
{ "all", fsreadall, fswriteall, DMEXCL|0444 },
};
const float gas_range_const_array1_f[16] = {
/* as per the datasheet */
1.0,
1.0,
1.0,
1.0,
1.0,
0.99,
1.0,
0.992,
1.0,
1.0,
0.998,
0.995,
1.0,
0.99,
1.0,
1.0,
};
const float gas_range_const_array2_f[16] = {
/* as per the datasheet */
8000000.0,
4000000.0,
2000000.0,
1000000.0,
499500.4995,
248262.1648,
125000.0,
63004.03226,
31281.28128,
15625.0,
7812.5,
3906.25,
1953.125,
976.5625,
488.28125,
244.140625,
};
static int i2cfdfs;
static int i2cfdmain;
static CalibrationData caldat;
static AllMeasurments mesdat;
static AllParameters pardat;
enum
{
osrs_t,
osrs_p,
osrs_h,
filter,
gas_wait,
gas_temp,
loop_time,
reset,
};
static char *cmds[] = {
[osrs_t] = "osrs_t",
[osrs_p] = "osrs_p",
[osrs_h] = "osrs_h",
[filter] = "filter",
[gas_wait] = "gas_wait",
[gas_temp] = "gas_temp",
[loop_time] = "loop_time",
[reset] = "reset",
nil
};
/* logic state tracking variables */
static int stu; /* undocumented features - set parameters as observed from BSEC library instead of documentation */
static int std; /* threaded deamo - sample all sensor readings continiously instead of on demand (usefull for AQI) */
void
initfs(char *dirname)
{
File *devdir;
int i;
fs.tree = alloctree(nil, nil, 0555, nil);
if(fs.tree == nil){
sysfatal("initfs: alloctree: %r");
}
if((devdir = createfile(fs.tree->root, dirname, nil, DMDIR|0555, nil)) == nil){
sysfatal("initfs: createfile: %s: %r", dirname);
}
for(i = 0; i < nelem(files); i++){
if(createfile(devdir, files[i].name, nil, files[i].mode, files + i) == nil){
sysfatal("initfs: createfile: %s: %r", files[i].name);
}
}
}
void
fsstart(Srv *)
{
openi2cdev();
initi2cdev();
}
void
fsread(Req *r)
{
Devfile *f;
r->ofcall.count = 0;
f = r->fid->file->aux;
respond(r, f->fsread(r));
}
void
fswrite(Req *r)
{
Devfile *f;
r->ofcall.count = 0;
f = r->fid->file->aux;
respond(r, f->fswrite(r));
}
void
fsend(Srv *)
{
deiniti2cdev();
closei2cdev();
}
void
openi2cdev(void)
{
i2cfdfs = -1;
/* default location of bme680 device is 0x77*/
if(access("/dev/i2c1/i2c.77.data", 0) != 0){
if(bind("#J77", "/dev", MBEFORE) < 0){
sysfatal("no J77 device");
}
}
i2cfdfs = open("/dev/i2c1/i2c.77.data", ORDWR);
if(i2cfdfs < 0){
sysfatal("cannot open i2c.77.data file");
}
}
void
initi2cdev(void)
{
uchar res;
/* check device id */
res = bme680getchipid();
if(res != 0x61){
/* value 0x61 is the id of bme680 device */
sysfatal("device responded with wrong chip ID for bme680");
}
/* perform soft reset */
res = bme680softreset();
if(res != 0x0){
/* value 0x61 is the id of bme680 device */
sysfatal("bme680 device cannot be reset");
}
/* read calibration data */
bme680readcalibrationdata();
/* set parameters */
bme680resetparameters();
/* set undocumented features as decoded from BSEC lib */
if (stu) {
bme680setundocumented();
}
/*
fprint(1, "par_t1 val: %d\n", caldat.par_t1);
fprint(1, "par_t2 val: %d\n", caldat.par_t2);
fprint(1, "par_t3 val: %d\n", caldat.par_t3);
fprint(1, "par_p1 val: %d\n", caldat.par_p1);
fprint(1, "par_p2 val: %d\n", caldat.par_p2);
fprint(1, "par_p3 val: %d\n", caldat.par_p3);
fprint(1, "par_p4 val: %d\n", caldat.par_p4);
fprint(1, "par_p5 val: %d\n", caldat.par_p5);
fprint(1, "par_p6 val: %d\n", caldat.par_p6);
fprint(1, "par_p7 val: %d\n", caldat.par_p7);
fprint(1, "par_p8 val: %d\n", caldat.par_p8);
fprint(1, "par_p9 val: %d\n", caldat.par_p9);
fprint(1, "par_p10 val: %d\n", caldat.par_p10);
fprint(1, "par_h1 val: %d\n", caldat.par_h1);
fprint(1, "par_h2 val: %d\n", caldat.par_h2);
fprint(1, "par_h3 val: %d\n", caldat.par_h3);
fprint(1, "par_h4 val: %d\n", caldat.par_h4);
fprint(1, "par_h5 val: %d\n", caldat.par_h5);
fprint(1, "par_h6 val: %d\n", caldat.par_h6);
fprint(1, "par_h7 val: %d\n", caldat.par_h7);
fprint(1, "par_g1 val: %d\n", caldat.par_g1);
fprint(1, "par_g2 val: %d\n", caldat.par_g2);
fprint(1, "par_g3 val: %d\n", caldat.par_g3);
fprint(1, "res_heat_val val: %d\n", caldat.res_heat_val);
fprint(1, "res_heat_range val: %d\n", caldat.res_heat_range);
fprint(1, "ragenge_sw_error val: %d\n", caldat.range_sw_error);
*/
}
void
deiniti2cdev(void)
{
/* nothing really to do wuth this device */
return;
}
void
closei2cdev(void)
{
close(i2cfdfs);
unmount("#J77", "/dev");
}
void
openi2cdevmain(void)
{
i2cfdmain = -1;
/* default location of bme680 device is 0x77*/
if(access("/dev/i2c1/i2c.77.data", 0) != 0){
if(bind("#J77", "/dev", MBEFORE) < 0){
sysfatal("no J77 device");
}
}
i2cfdmain = open("/dev/i2c1/i2c.77.data", ORDWR);
if(i2cfdmain < 0){
sysfatal("cannot open i2c.77.data file");
}
}
void
closei2cdevmain(void)
{
close(i2cfdmain);
unmount("#J77", "/dev");
}
uchar
bme680getchipid(void)
{
uchar cmd;
uchar res;
cmd = 0xD0; /* location of id register */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &res, 1, 0);
/* bme680 device id is 0x61 */
return res;
}
uchar
bme680softreset(void)
{
uchar cmd[2];
uchar res;
cmd[0] = 0xE0; /* location of reset register */
cmd[1] = 0xB6; /* reset command */
pwrite(i2cfdfs, &cmd[0], 2, 0);
sleep(5);
pread(i2cfdfs, &res, 1, 0);
return res;
}
void
bme680setundocumented(void)
{
uchar cmd[2];
cmd[0] = 0x71; /* location of ctrl_gas_1 register */
cmd[1] = 0x80; /* set bit 7 (undocumented, do not change) from 0 to 1 */
pwrite(i2cfdfs, &cmd[0], 2, 0);
}
void
bme680resetparameters(void)
{
/* set default parameters for measurments */
pardat.filter = 0x00; /* disable low pass filter */
pardat.pres = 0x04; /* 8x oversampling */
pardat.temp = 0x03; /* 4x oversampling */
pardat.hum = 0x03; /* 4x oversampling */
if (stu) {
/* set gas temperature and wait time as decoded from BSEC library */
pardat.gastime = 0x71; /* 196ms = 4 * 49ms */
pardat.gastemp = 260.0; /* 260 deg C */
} else {
/* set gas temperature and wait time as in documentation example */
pardat.gastime = 0x59; /* 100ms = 4 * 25ms */
pardat.gastemp = 300.0; /* 300 deg C */
}
pardat.looptime = 3000; /* timeout between automatic samples in ms */
/* init all measurments structure */
mesdat.temp = 0.0;
mesdat.pres = 0.0;
mesdat.hum = 0.0;
mesdat.gas = 0.0;
}
void
bme680readcalibrationdata(void)
{
uchar cmd;
uchar buf;
/* read temperature calibration data */
cmd = 0xE9; /* par_t1 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_t1, 2, 0);
cmd = 0x8A; /* par_t2 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_t2, 2, 0);
cmd = 0x8C; /* par_t3 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_t3, 1, 0);
/* read pressure calibration data */
cmd = 0x8E; /* par_p1 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p1, 2, 0);
cmd = 0x90; /* par_p2 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p2, 2, 0);
cmd = 0x92; /* par_p3 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p3, 1, 0);
cmd = 0x94; /* par_p4 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p4, 2, 0);
cmd = 0x96; /* par_p5 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p5, 2, 0);
cmd = 0x99; /* par_p6 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p6, 1, 0);
cmd = 0x98; /* par_p7 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p7, 1, 0);
cmd = 0x9C; /* par_p8 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p8, 2, 0);
cmd = 0x9E; /* par_p9 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p9, 2, 0);
cmd = 0xA0; /* par_p10 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_p10, 1, 0);
/* read humidity calibration data */
/* par_h1 (lsb 4bit <3:0>), par_h2 (lsb 4bit <7:4>) */
cmd = 0xE2;
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &buf, 1, 0);
cmd = 0xE3; /* par_h1 (msb) - 12bit */
caldat.par_h1 = 0; /* clear since we read in only 1 byte but the var is 2 bytes long */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h1, 1, 0);
caldat.par_h1 = (caldat.par_h1<<4) | ((unsigned short)(buf & 0x0F)); /* msb + lsb */
cmd = 0xE1; /* par_h2 (msb) - 12bit */
caldat.par_h2 = 0; /* clear since we read in only 1 byte but the var is 2 bytes long */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h2, 1, 0);
caldat.par_h2 = (caldat.par_h2<<4) | (((unsigned short)(buf & 0xF0))>>4); /* msb + lsb */
cmd = 0xE4; /* par_h3 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h3, 1, 0);
cmd = 0xE5; /* par_h4 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h4, 1, 0);
cmd = 0xE6; /* par_h5 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h5, 1, 0);
cmd = 0xE7; /* par_h6 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h6, 1, 0);
cmd = 0xE8; /* par_h7 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_h7, 1, 0);
/* gas humidity calibration data */
cmd = 0xED; /* par_g1 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_g1, 1, 0);
cmd = 0xEB; /* par_g2 address - 16bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_g2, 2, 0);
cmd = 0xEE; /* par_g3 address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.par_g3, 1, 0);
cmd = 0x02; /* res_heat_range (lsb) - 2bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.res_heat_range, 1, 0);
caldat.res_heat_range = (((char)(caldat.res_heat_range & 0x30))>>4);
cmd = 0x00; /* res_heat_val address - 8bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.res_heat_val, 1, 0);
cmd = 0x04; /* range_sw_error address - 4bit */
pwrite(i2cfdfs, &cmd, 1, 0);
pread(i2cfdfs, &caldat.range_sw_error, 1, 0);
caldat.range_sw_error = (((char)(caldat.range_sw_error & 0xF0))>>4);
}
float
bme680gettemp(void)
{
int temp_raw;
float temp_comp;
float var1;
float var2;
float t_fine;
uchar cmd[2];
uchar buf[3];
uchar reg_val[6]; // read regs 0x70-0x75 to ONLY change their value
/* get sensor control registry state ... */
/* wait if any measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read control registries */
cmd[0] = 0x70;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, ®_val[0], 6, 0);
/* set humidity settings */
/* 0x72 - ctrl_hum - osrs_h<2:0> - skip humidity measurment */
cmd[0] = 0x72;
cmd[1] = (reg_val[2] & ~MASK_CTRL_REG_72) | (MASK_CTRL_REG_72 & 0x00);
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* 0x75 - config - filter<4:2> - low pass filter for temp and pressure */
cmd[0] = 0x75;
cmd[1] = (reg_val[5] & ~MASK_CTRL_REG_75) | (MASK_CTRL_REG_75 & (pardat.filter<<2));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set gas settings */
/* 0x71 - ctrl_gas_1 - run_gass<4> - skip gass measurment */
cmd[0] = 0x71;
cmd[1] = (reg_val[1] & ~MASK_CTRL_REG_71) | (MASK_CTRL_REG_71 & (0x00<<4));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set temperature and pressure settings settings + set forced mode - execute measurment */
/* 0x74 - ctrl_meas - osrs_t<7:5> osrs_p<4:2> mode<1:0> - set temp, skip pressure measurment, FORCED MODE */
cmd[0] = 0x74;
cmd[1] = (reg_val[4] & ~MASK_CTRL_REG_74) | (MASK_CTRL_REG_74 & ((pardat.temp<<5) + (0x00<<2) + 0x01));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* wait while measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read temperature from adc */
cmd[0] = 0x22;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 3, 0);
/* calculate values */
temp_raw = ((int)((int)buf[0]<<16) | (int)((int)buf[1]<<8) | (int)buf[2])>>4;
var1 = (((float)temp_raw / 16384.0f) - ((float)caldat.par_t1 / 1024.0f)) * ((float)caldat.par_t2);
var2 = ((((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f)) *
(((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f))) * ((float)caldat.par_t3 * 16.0f);
t_fine = var1 + var2;
temp_comp = t_fine / 5120.0;
return temp_comp;
}
float
bme680getpress(void)
{
int temp_raw;
int pres_raw;
float pres_comp;
float var1;
float var2;
float var3;
float t_fine;
uchar cmd[2];
uchar buf[3];
uchar reg_val[6]; // read regs 0x70-0x75 to ONLY change their value
/* get sensor control registry state ... */
/* wait if any measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read control registries */
cmd[0] = 0x70;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, ®_val[0], 6, 0);
/* set humidity settings */
/* 0x72 - ctrl_hum - osrs_h<2:0> - skip humidity measurment */
cmd[0] = 0x72;
cmd[1] = (reg_val[2] & ~MASK_CTRL_REG_72) | (MASK_CTRL_REG_72 & 0x00);
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* 0x75 - config - filter<4:2> - low pass filter for temp and pressure */
cmd[0] = 0x75;
cmd[1] = (reg_val[5] & ~MASK_CTRL_REG_75) | (MASK_CTRL_REG_75 & (pardat.filter<<2));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set gas settings */
/* 0x71 - ctrl_gas_1 - run_gass<4> - skip gass measurment */
cmd[0] = 0x71;
cmd[1] = (reg_val[1] & ~MASK_CTRL_REG_71) | (MASK_CTRL_REG_71 & (0x00<<4));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set temperature and pressure settings settings + set forced mode - execute measurment */
/* 0x74 - ctrl_meas - osrs_t<7:5> osrs_p<4:2> mode<1:0> - set temp, set pressure, FORCED MODE */
cmd[0] = 0x74;
cmd[1] = (reg_val[4] & ~MASK_CTRL_REG_74) | (MASK_CTRL_REG_74 & ((pardat.temp<<5) + (pardat.pres<<2) + 0x01));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* wait while measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read temperature from adc */
cmd[0] = 0x22;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 3, 0);
/* calculate values */
temp_raw = ((int)((int)buf[0]<<16) | (int)((int)buf[1]<<8) | (int)buf[2])>>4;
var1 = (((float)temp_raw / 16384.0f) - ((float)caldat.par_t1 / 1024.0f)) * ((float)caldat.par_t2);
var2 = ((((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f)) *
(((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f))) * ((float)caldat.par_t3 * 16.0f);
t_fine = var1 + var2;
/* read pressure from adc */
cmd[0] = 0x1F;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 3, 0);
/* calculate values */
pres_raw = ((int)((int)buf[0]<<16) | (int)((int)buf[1]<<8) | (int)buf[2])>>4;
var1 = ((float)t_fine / 2.0f) - 64000.0f;
var2 = var1 * var1 * ((float)caldat.par_p6 / 131072.0f);
var2 = var2 + (var1 * (float)caldat.par_p5 * 2.0f);
var2 = (var2 / 4.0) + ((float)caldat.par_p4 * 65536.0f);
var1 = ((((float)caldat.par_p3 * var1 * var1) / 16384.0f) + ((float)caldat.par_p2 * var1)) / 524288.0f;
var1 = (1.0f + (var1 / 32768.0f)) * (float)caldat.par_p1;
pres_comp = 1048576.0f - (float)pres_raw;
pres_comp = ((pres_comp - (var2 / 4096.0f)) * 6250.0f) / var1;
var1 = ((float)caldat.par_p9 * pres_comp * pres_comp) / 2147483648.0f;
var2 = pres_comp * ((float)caldat.par_p8 / 32768.0f);
var3 = (pres_comp / 256.0f) * (pres_comp / 256.0f) * (pres_comp / 256.0f) * (caldat.par_p10 / 131072.0f);
pres_comp = pres_comp + (var1 + var2 + var3 + ((float)caldat.par_p7 * 128.0f)) / 16.0f;
return pres_comp;
}
float
bme680gethum(void)
{
int hum_raw;
int temp_raw;
float hum_comp;
float temp_comp;
float var1;
float var2;
float var3;
float var4;
float t_fine;
uchar cmd[2];
uchar buf[3];
uchar reg_val[6]; // read regs 0x70-0x75 to ONLY change their value
/* get sensor control registry state ... */
/* wait if any measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read control registries */
cmd[0] = 0x70;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, ®_val[0], 6, 0);
/* set humidity settings */
/* 0x72 - ctrl_hum - osrs_h<2:0> - set humidity */
cmd[0] = 0x72;
cmd[1] = (reg_val[2] & ~MASK_CTRL_REG_72) | (MASK_CTRL_REG_72 & pardat.hum);
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* 0x75 - config - filter<4:2> - low pass filter for temp and pressure */
cmd[0] = 0x75;
cmd[1] = (reg_val[5] & ~MASK_CTRL_REG_75) | (MASK_CTRL_REG_75 & (pardat.filter<<2));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set gas settings */
/* 0x71 - ctrl_gas_1 - run_gass<4> - skip gass measurment */
cmd[0] = 0x71;
cmd[1] = (reg_val[1] & ~MASK_CTRL_REG_71) | (MASK_CTRL_REG_71 & (0x00<<4));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* set temperature and pressure settings settings + set forced mode - execute measurment */
/* 0x74 - ctrl_meas - osrs_t<7:5> osrs_p<4:2> mode<1:0> - set temp, skip pressure measurment, FORCED MODE */
cmd[0] = 0x74;
cmd[1] = (reg_val[4] & ~MASK_CTRL_REG_74) | (MASK_CTRL_REG_74 & ((pardat.temp<<5) + (0x00<<2) + 0x01));
pwrite(i2cfdfs, &cmd[0], 2, 0);
/* wait while measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read temperature from adc */
cmd[0] = 0x22;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 3, 0);
/* calculate values */
temp_raw = ((int)((int)buf[0]<<16) | (int)((int)buf[1]<<8) | (int)buf[2])>>4;
var1 = (((float)temp_raw / 16384.0f) - ((float)caldat.par_t1 / 1024.0f)) * ((float)caldat.par_t2);
var2 = ((((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f)) *
(((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f))) * ((float)caldat.par_t3 * 16.0f);
t_fine = var1 + var2;
temp_comp = t_fine / 5120.0;
/* read humidity from adc */
cmd[0] = 0x25;
pwrite(i2cfdfs, &cmd[0], 1, 0);
pread(i2cfdfs, &buf[0], 2, 0);
/* calculate values */
hum_raw = ((int)(((int)buf[0])<<8) | (int)buf[1]);
var1 = (float)((float)hum_raw) - (((float)caldat.par_h1 * 16.0f) + (((float)caldat.par_h3 / 2.0f) * temp_comp));
var2 = var1 * ((float)(((float)caldat.par_h2 / 262144.0f) *
(1.0f + (((float)caldat.par_h4 / 16384.0f) * temp_comp) + (((float)caldat.par_h5 / 1048576.0f) * temp_comp * temp_comp))));
var3 = (float)caldat.par_h6 / 16384.0f;
var4 = (float)caldat.par_h7 / 2097152.0f;
hum_comp = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2);
return hum_comp;
}
void
bme680readall(int i2cfd)
{
int temp_raw;
int pres_raw;
int hum_raw;
int gas_raw;
int gas_range;
float temp_comp;
float pres_comp;
float hum_comp;
float gas_comp;
float var1;
float var2;
float var3;
float var4;
float var5;
float t_fine;
unsigned char res_heat_x;
uchar cmd[2];
uchar buf[3];
uchar reg_val[6]; // read regs 0x70-0x75 to ONLY change their value
/* STEP 1 - get temperature for ambient temperature refenrence*/
/* get sensor control registry state ... */
/* wait if any measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfd, &cmd[0], 1, 0);
pread(i2cfd, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read control registries */
cmd[0] = 0x70;
pwrite(i2cfd, &cmd[0], 1, 0);
pread(i2cfd, ®_val[0], 6, 0);
/* set humidity settings */
/* 0x72 - ctrl_hum - osrs_h<2:0> - skipp humidity measurment */
cmd[0] = 0x72;
cmd[1] = (reg_val[2] & ~MASK_CTRL_REG_72) | (MASK_CTRL_REG_72 & 0x00);
pwrite(i2cfd, &cmd[0], 2, 0);
/* 0x75 - config - filter<4:2> - low pass filter for temp and pressure */
cmd[0] = 0x75;
cmd[1] = (reg_val[5] & ~MASK_CTRL_REG_75) | (MASK_CTRL_REG_75 & (pardat.filter<<2));
pwrite(i2cfd, &cmd[0], 2, 0);
/* set gas settings */
/* 0x71 - ctrl_gas_1 - run_gass<4> - skip gass measurment */
cmd[0] = 0x71;
cmd[1] = (reg_val[1] & ~MASK_CTRL_REG_71) | (MASK_CTRL_REG_71 & (0x00<<4));
pwrite(i2cfd, &cmd[0], 2, 0);
/* set temperature and pressure settings settings + set forced mode - execute measurment */
/* 0x74 - ctrl_meas - osrs_t<7:5> osrs_p<4:2> mode<1:0> - set temp, skip pressure measurment, FORCED MODE */
cmd[0] = 0x74;
cmd[1] = (reg_val[4] & ~MASK_CTRL_REG_74) | (MASK_CTRL_REG_74 & ((pardat.temp<<5) + (0x00<<2) + 0x01));
pwrite(i2cfd, &cmd[0], 2, 0);
/* wait while measurment is in progress */
cmd[0] = 0x74;
do {
pwrite(i2cfd, &cmd[0], 1, 0);
pread(i2cfd, &buf[0], 1, 0);
} while ((buf[0] & MASK_CTRL_MEAS_MODE) > 0);
/* read temperature from adc */
cmd[0] = 0x22;
pwrite(i2cfd, &cmd[0], 1, 0);
pread(i2cfd, &buf[0], 3, 0);
/* calculate values */
temp_raw = ((int)((int)buf[0]<<16) | (int)((int)buf[1]<<8) | (int)buf[2])>>4;
var1 = (((float)temp_raw / 16384.0f) - ((float)caldat.par_t1 / 1024.0f)) * ((float)caldat.par_t2);
var2 = ((((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f)) *
(((float)temp_raw / 131072.0f) - ((float)caldat.par_t1 / 8192.0f))) * ((float)caldat.par_t3 * 16.0f);
t_fine = var1 + var2;
temp_comp = t_fine / 5120.0;
/* STEP 2 - mesure gas with known ambient temperature */
/* get sensor control registry state ... read control registries */
cmd[0] = 0x70;
pwrite(i2cfd, &cmd[0], 1, 0);
pread(i2cfd, ®_val[0], 6, 0);
/* calculate heater resistance*/
var1 = ((float)caldat.par_g1 / 16.0f) + 49.0f;
var2 = (((float)caldat.par_g2 / 32768.0f) * 0.0005f) + 0.00235f;
var3 = (float)caldat.par_g3 / 1024.0f;
var4 = var1 * (1.0 + (var2 * (float)pardat.gastemp));
var5 = var4 + (var3 * (float)temp_comp);