forked from eriksl/esp8266-universal-io-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_sensor.c
1229 lines (936 loc) · 28.3 KB
/
i2c_sensor.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 "i2c_sensor.h"
#include "util.h"
#include "config.h"
typedef struct
{
double raw;
double cooked;
} value_t;
typedef struct
{
unsigned int detected:1;
} device_data_t;
typedef struct device_table_entry_T
{
i2c_sensor_t id;
uint8_t address;
const char *name;
const char *type;
const char *unity;
uint8_t precision;
i2c_error_t (* const init_fn)(const struct device_table_entry_T *);
i2c_error_t (* const read_fn)(const struct device_table_entry_T *, value_t *);
} device_table_entry_t;
device_data_t device_data[i2c_sensor_size];
irom void i2c_sensor_config_init(i2c_sensor_config_t *dst)
{
int current;
for(current = 0; current < i2c_sensor_size; current++)
{
dst->sensor[current].calibration.factor = 1;
dst->sensor[current].calibration.offset = 0;
}
}
irom static i2c_error_t sensor_digipicco_temp_init(const device_table_entry_t *entry)
{
i2c_error_t error;
uint8_t i2cbuffer[4] = { 0, 0, 0, 0 };
if((error = i2c_send(entry->address, 1, i2cbuffer)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 4, i2cbuffer)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_digipicco_temp_read(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[4];
if((error = i2c_receive(entry->address, 4, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = ((uint16_t)i2cbuffer[2] << 8) | (uint16_t)i2cbuffer[3];
value->cooked = ((value->raw * 165.0) / 32767.0) - 40.5;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_digipicco_hum_init(const device_table_entry_t *entry)
{
if(!i2c_sensor_detected(i2c_sensor_digipicco_temperature))
return(i2c_error_address_nak);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_digipicco_hum_read(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[4];
if((error = i2c_receive(entry->address, 4, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = ((uint16_t)i2cbuffer[0] << 8) | (uint16_t)i2cbuffer[1];
value->cooked = (value->raw * 100.0) / 32768.0;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_ds1631_init(const device_table_entry_t *entry)
{
i2c_error_t error;
// 0xac select config register
// 0x0c r0=r1=1, max resolution, other bits zero
if((error = i2c_send_2(entry->address, 0xac, 0x0c)) != i2c_error_ok)
return(error);
// start conversions
if((error = i2c_send_1(entry->address, 0x51)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_ds1631_read(const device_table_entry_t *entry, value_t *value)
{
uint8_t i2cbuffer[2];
i2c_error_t error;
int raw;
if((entry->id == i2c_sensor_ds1631_0) && i2c_sensor_detected(i2c_sensor_lm75_0))
return(i2c_error_device_error_1);
// read temperature
if((error = i2c_send_1(entry->address, 0xaa)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = raw = ((unsigned int)(i2cbuffer[0] << 8)) | i2cbuffer[1];
if(raw & 0x8000)
{
raw &= ~(uint32_t)0x8000;
value->cooked = (double)raw / -256;
}
else
value->cooked = (double)raw / 256;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_lm75_init(const device_table_entry_t *entry)
{
uint8_t i2cbuffer[4];
i2c_error_t error;
if((entry->id == i2c_sensor_lm75_0) && i2c_sensor_detected(i2c_sensor_ds1631_0))
return(i2c_error_device_error_1);
// 0x01 select config register
// 0x60 set all defaults, operation is not shutdown
// specific for tmp275 variant, select high-res operation
if((error = i2c_send_2(entry->address, 0x01, 0x60)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 1, i2cbuffer)) != i2c_error_ok)
return(error);
if(i2cbuffer[0] != 0x60)
return(i2c_error_device_error_1);
// 0x03 select overtemperature register
if((error = i2c_send_3(entry->address, 0x03, 0xff, 0xff)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
if((i2cbuffer[0] != 0xff) || ((i2cbuffer[1] & 0x0f) != 0x00))
return(i2c_error_device_error_2);
// 0x03 select overtemperature register
if((error = i2c_send_3(entry->address, 0x03, 0x00, 0x00)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
if((i2cbuffer[0] != 0x00) || (i2cbuffer[1] != 0x00))
return(i2c_error_device_error_3);
// select temperature register
if((error = i2c_send_1(entry->address, 0x00)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_lm75_read(const device_table_entry_t *entry, value_t *value)
{
uint8_t i2cbuffer[2];
i2c_error_t error;
int raw;
if((entry->id == i2c_sensor_lm75_0) && i2c_sensor_detected(i2c_sensor_ds1631_0))
return(i2c_error_device_error_1);
if((error = i2c_receive(entry->address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = raw = (i2cbuffer[0] << 8) | i2cbuffer[1];
if(raw & 0x8000)
{
raw &= ~0x8000;
value->cooked = (double)raw / -256;
}
else
value->cooked = (double)raw / 256;
return(i2c_error_ok);
}
irom static i2c_error_t bmp085_write(int address, int reg, int value)
{
i2c_error_t error;
if((error = i2c_send_2(address, reg, value)) != i2c_error_ok)
return(error);
return(0);
}
irom static i2c_error_t bmp085_read(int address, int reg, uint16_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[2];
if((error = i2c_send_1(address, reg)) != i2c_error_ok)
return(error);
if((error = i2c_receive(address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
*value = ((uint16_t)(i2cbuffer[0] << 8)) | (uint16_t)i2cbuffer[1];
return(0);
}
irom static i2c_error_t bmp085_read_long(int address, int reg, uint32_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[4];
if((error = i2c_send_1(address, reg)) != i2c_error_ok)
return(error);
if((error = i2c_receive(address, 3, i2cbuffer)) != i2c_error_ok)
return(error);
*value = ((uint32_t)i2cbuffer[0] << 16) | ((uint32_t)i2cbuffer[1] << 8) | (uint32_t)i2cbuffer[2];
return(0);
}
irom static i2c_error_t sensor_read_bmp085(int address, double *temp, double *temp_raw, double *pressure, double *pressure_raw)
{
int16_t ac1, ac2, ac3;
uint16_t ac4, ac5, ac6;
int16_t b1, b2;
int16_t mc, md;
uint16_t ut;
uint32_t up;
int32_t b3, b4, b5, b6;
uint32_t b7;
int32_t x1, x2, x3, p;
uint8_t oss = 3;
i2c_error_t error;
if((error = bmp085_read(address, 0xaa, (uint16_t *)&ac1)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xac, (uint16_t *)&ac2)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xae, (uint16_t *)&ac3)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xb0, &ac4)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xb2, &ac5)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xb4, &ac6)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xb6, (uint16_t *)&b1)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xb8, (uint16_t *)&b2)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xbc, (uint16_t *)&mc)) != i2c_error_ok)
return(error);
if((error = bmp085_read(address, 0xbe, (uint16_t *)&md)) != i2c_error_ok)
return(error);
if((error = bmp085_write(address, 0xf4, 0x2e)) != i2c_error_ok) // set cmd = 0x2e = start temperature measurement
return(error);
msleep(5);
if((error = bmp085_read(address, 0xf6, &ut)) != i2c_error_ok) // select result 0xf6+0xf7
return(error);
#if 0
ac1 = 408;
ac2 = -72;
ac3 = -14383;
ac4 = 32741;
ac5 = 32757;
ac6 = 23153;
b1 = 6190;
b2 = 4;
mc = -8711;
md = 2868;
ut = 27898;
#endif
x1 = (((uint32_t)ut - (uint32_t)ac6) * (uint32_t)ac5) >> 15;
x2 = ((int32_t)mc << 11) / (x1 + (int32_t)md);
b5 = x1 + x2;
*temp_raw = ut;
*temp = ((((double)b5 + 8) / 16) / 10);
if((error = bmp085_write(address, 0xf4, 0x34 | (oss << 6))) != i2c_error_ok) // set cmd = 0x34 = start air pressure measurement
return(error);
msleep(20);
up = 0;
if((error = bmp085_read_long(address, 0xf6, &up)) != i2c_error_ok) // select result 0xf6+0xf7+f8
return(error);
#if 0
up = 23843;
#endif
up = up >> (8 - oss);
b6 = b5 - 4000;
x1 = ((int32_t)b2 * ((b6 * b6) >> 12)) >> 11;
x2 = ((int32_t)ac2 * b6) >> 11;
x3 = x1 + x2;
b3 = ((((int32_t)ac1 * 4 + x3) << oss) + 2) / 4;
x1 = ((int32_t)ac3 * b6) >> 13;
x2 = ((int32_t)b1 * ((b6 * b6) >> 12)) >> 16;
x3 = (x1 + x2 + 2) >> 2;
b4 = ((uint32_t)ac4 * (uint32_t)(x3 + 32768)) >> 15;
b7 = (uint32_t)(((uint32_t)up - b3) * (50000 >> oss));
if(b7 & 0x80000000)
p = (b7 / b4) << 1;
else
p = (b7 << 1) / b4;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038UL) >> 16;
x2 = (p * -7357) >> 16;
p = p + ((x1 + x2 + 3791L) >> 4);
*pressure_raw = up;
*pressure = p / 100.0;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_bmp085_read_temp(const device_table_entry_t *entry, value_t *value)
{
double temp, temp_raw, pressure, pressure_raw;
i2c_error_t error;
if((error = sensor_read_bmp085(entry->address, &temp, &temp_raw, &pressure, &pressure_raw)) != i2c_error_ok)
return(error);
value->raw = temp_raw;
value->cooked = temp;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_bmp085_read_pressure(const device_table_entry_t *entry, value_t *value)
{
double temp, temp_raw, pressure, pressure_raw;
i2c_error_t error;
if((error = sensor_read_bmp085(entry->address, &temp, &temp_raw, &pressure, &pressure_raw)) != i2c_error_ok)
return(error);
value->raw = pressure_raw;
value->cooked = pressure;
return(i2c_error_ok);
}
typedef struct
{
const double ratio_top;
const double ch0_factor;
const double ch1_factor;
} tsl2560_lookup_t;
static const tsl2560_lookup_t tsl2560_lookup[] =
{
{ 0.125, 0.03040, 0.02720 },
{ 0.250, 0.03250, 0.04400 },
{ 0.375, 0.03510, 0.05440 },
{ 0.500, 0.03810, 0.06240 },
{ 0.610, 0.02240, 0.03100 },
{ 0.800, 0.01280, 0.01530 },
{ 1.300, 0.00146, 0.00112 },
{ 0.000, 0.00000, 0.00000 }
};
irom static i2c_error_t tsl2560_write(int address, int reg, int value)
{
i2c_error_t error;
// 0xc0 write byte
if((error = i2c_send_2(address, 0xc0 | reg, value)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t tsl2560_read(int address, int reg, uint8_t *byte)
{
i2c_error_t error;
// 0xc0 read byte
if((error = i2c_send_1(address, 0xc0 | reg)) != i2c_error_ok)
return(error);
if((error = i2c_receive(address, 1, byte)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t tsl2560_write_check(int address, int reg, int value)
{
i2c_error_t error;
uint8_t rv;
if((error = tsl2560_write(address, reg, value)) != i2c_error_ok)
return(error);
if((error = tsl2560_read(address, reg, &rv)) != i2c_error_ok)
return(error);
if(value != rv)
return(i2c_error_device_error_1);
return(i2c_error_ok);
}
irom static i2c_error_t tsl2560_read_block(int address, int reg, uint8_t *values)
{
i2c_error_t error;
// 0xd0 read block
if((error = i2c_send_1(address, 0xd0 | reg)) != i2c_error_ok)
return(error);
if((error = i2c_receive(address , 4, values)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_tsl2560_init(const device_table_entry_t *entry)
{
i2c_error_t error;
uint8_t regval;
if(i2c_sensor_detected(i2c_sensor_tsl2550))
return(i2c_error_device_error_1);
if((error = tsl2560_write_check(entry->address, 0x00, 0x00)) != i2c_error_ok) // power down
return(error);
if((error = tsl2560_write(entry->address, 0x00, 0x03)) != i2c_error_ok) // power up
return(error);
if((error = tsl2560_read(entry->address, 0x00, ®val)) != i2c_error_ok)
return(error);
if((regval & 0x0f) != 0x03)
return(i2c_error_device_error_2);
if((error = tsl2560_write_check(entry->address, 0x06, 0x00)) != i2c_error_ok) // disable interrupts
return(error);
if((error = tsl2560_write(entry->address, 0x0a, 0x00)) != i2c_error_ok) // id register 1
return(error);
if((error = tsl2560_read(entry->address, 0x0a, ®val)) != i2c_error_ok) // read id register 1
return(error);
if(regval != 0x50)
return(i2c_error_device_error_3);
if((error = tsl2560_write(entry->address, 0x0b, 0x00)) != i2c_error_ok) // id register 2
return(error);
if((error = tsl2560_read(entry->address, 0x0b, ®val)) != i2c_error_ok) // read id register 2
return(error);
if(regval != 0x04)
return(i2c_error_device_error_3);
if(config_get_flag(config_flag_tsl_high_sens))
regval = 0b00010010; // 400 ms sampling window, gain is high, 16x
else
regval = 0b00000010; // 400 ms sampling window, gain is high, 1x
if((error = tsl2560_write_check(entry->address, 0x01, regval)) != i2c_error_ok) // start continuous sampling
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_tsl2560_read(const device_table_entry_t *entry, value_t *value)
{
uint8_t i2cbuffer[4];
i2c_error_t error;
unsigned int ch0r, ch1r;
double ratio, ch0, ch1;
const tsl2560_lookup_t *tsl2560_entry;
int current;
if(i2c_sensor_detected(i2c_sensor_tsl2550))
return(i2c_error_device_error_1);
if((error = tsl2560_read_block(entry->address, 0x0c, i2cbuffer)) != i2c_error_ok)
return(error);
ch0r = i2cbuffer[0] | (i2cbuffer[1] << 8);
ch1r = i2cbuffer[2] | (i2cbuffer[3] << 8);
value->raw = ((double)ch1r * 1000000) + (double)ch0r;
if((ch0r == 65535) || (ch1r == 65535))
{
value->cooked = -1;
return(i2c_error_ok);
}
if(config_get_flag(config_flag_tsl_high_sens))
{
// high sensitivity = 400 ms integration time, scaling factor = 1
// analogue amplification = 16x, scaling factor = 1
ch0 = (double)ch0r * 1.0;
ch1 = (double)ch1r * 1.0;
}
else
{
// low sensitivity = 400 ms integration time, scaling factor = 1
// analogue amplification = 1x, scaling factor = 16
ch0 = (double)ch0r * 1.0 * 16.0;
ch1 = (double)ch1r * 1.0 * 16.0;
}
if((unsigned int)ch0 != 0)
ratio = ch1 / ch0;
else
ratio = 0;
for(current = 0;; current++)
{
tsl2560_entry = &tsl2560_lookup[current];
if(((unsigned int)tsl2560_entry->ratio_top == 0) || ((unsigned int)tsl2560_entry->ch0_factor == 0) || ((unsigned int)tsl2560_entry->ch1_factor == 0))
break;
if(ratio <= tsl2560_entry->ratio_top)
break;
}
value->cooked = (ch0 * tsl2560_entry->ch0_factor) - (ch1 * tsl2560_entry->ch1_factor);
if(value->cooked < 0)
value->cooked = 0;
return(i2c_error_ok);
}
static const uint16_t tsl2550_count[128] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 26,
28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 49, 53, 57, 61, 65, 69, 73, 77, 81,
85, 89, 93, 97, 101, 105, 109, 115, 123, 131, 139, 147, 155, 163, 171, 179,
187, 195, 203, 211, 219, 227, 235, 247, 263, 279, 295, 311, 327, 343, 359,
375, 391, 407, 423, 439, 455, 471, 487, 511, 543, 575, 607, 639, 671, 703,
735, 767, 799, 831, 863, 895, 927, 959, 991,
1039,1103,1167,1231,1295,1359,1423,1487,
1551,1615,1679,1743,1807,1871,1935,1999,
2095,2223,2351,2479,2607,2735,2863,2991,
3119,3247,3375,3503,3631,3759,3887,4015
};
static const uint8_t tsl2550_ratio[129] =
{
100,100,100,100,100,100,100,100,
100,100,100,100,100,100,99,99,
99,99,99,99,99,99,99,99,
99,99,99,98,98,98,98,98,
98,98,97,97,97,97,97,96,
96,96,96,95,95,95,94,94,
93,93,93,92,92,91,91,90,
89,89,88,87,87,86,85,84,
83,82,81,80,79,78,77,75,
74,73,71,69,68,66,64,62,
60,58,56,54,52,49,47,44,
42,41,40,40,39,39,38,38,
37,37,37,36,36,36,35,35,
35,35,34,34,34,34,33,33,
33,33,32,32,32,32,32,31,
31,31,31,31,30,30,30,30,
30
};
irom static i2c_error_t sensor_tsl2550_rw(int address, int in, uint8_t *out)
{
i2c_error_t error;
if((error = i2c_send_1(address, in)) != i2c_error_ok)
return(error);
if((error = i2c_receive(address, 1, out)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_tsl2550_write_check(int address, int in, int compare)
{
i2c_error_t error;
uint8_t out;
if((error = sensor_tsl2550_rw(address, in, &out)) != i2c_error_ok)
return(error);
if(out != compare)
return(i2c_error_device_error_2);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_tsl2550_init(const device_table_entry_t *entry)
{
i2c_error_t error;
int sens_command;
if(i2c_sensor_detected(i2c_sensor_tsl2560))
return(i2c_error_device_error_1);
// tsl2550 power up
if((error = sensor_tsl2550_write_check(entry->address, 0x03, 0x03)) != i2c_error_ok)
return(error);
// standard range / extended range
if(config_get_flag(config_flag_tsl_high_sens))
sens_command = 0x18;
else
sens_command = 0x1d;
if((error = sensor_tsl2550_write_check(entry->address, sens_command, 0x1b)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_tsl2550_read(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t ch0, ch1;
int attempt, ratio;
if(i2c_sensor_detected(i2c_sensor_tsl2560))
return(i2c_error_device_error_1);
error = i2c_error_ok;
for(attempt = 16; attempt > 0; attempt--)
{
// read from channel 0
if((error = sensor_tsl2550_rw(entry->address, 0x43, &ch0)) != i2c_error_ok)
goto error;
// read from channel 1
if((error = sensor_tsl2550_rw(entry->address, 0x83, &ch1)) != i2c_error_ok)
goto error;
if((ch0 & 0x80) && (ch1 & 0x80))
break;
error:
msleep(10);
}
if(error != i2c_error_ok)
return(error);
ch0 &= 0x7f;
ch1 &= 0x7f;
value->raw = (ch0 * 10000.0) + ch1;
if((tsl2550_count[ch1] <= tsl2550_count[ch0]) && (tsl2550_count[ch0] > 0))
ratio = (tsl2550_count[ch1] * 128) / tsl2550_count[ch0];
else
ratio = 128;
if(ratio > 128)
ratio = 128;
value->cooked = ((tsl2550_count[ch0] - tsl2550_count[ch1]) * tsl2550_ratio[ratio]) / 2560.0;
if(value->cooked < 0)
value->cooked = 0;
if(!config_get_flag(config_flag_tsl_high_sens))
value->cooked *= 5;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_bh1750_init(const device_table_entry_t *entry)
{
i2c_error_t error;
int timing;
uint8_t regval[2];
// there is no "read register" command on this device, so assume
// a device at 0x23 is actually a bh1750, there is no way to be sure.
// power on
if((error = i2c_send_1(entry->address, 0b00000001)) != i2c_error_ok)
return(error);
// reset
if((error = i2c_send_1(entry->address, 0b00000111)) != i2c_error_ok)
return(error);
// set sensitivity
// "window" can be set between 31 and 254
// lux-per-count is 0.93 for low sensibility mode (window = 31)
// lux-per-count is 0.11 for high sensibility mode (window = 254)
if(config_get_flag(config_flag_bh_high_sens))
timing = 254;
else
timing = 31;
regval[0] = 0b01000000 | ((timing >> 5) & 0b00000111);
regval[1] = 0b01100000 | ((timing >> 0) & 0b00011111);
if((error = i2c_send_1(entry->address, regval[0])) != i2c_error_ok)
return(error);
if((error = i2c_send_1(entry->address, regval[1])) != i2c_error_ok)
return(error);
// start continuous sampling every 120 ms, high resolution = 0.42 Lx
if((error = i2c_send_1(entry->address, 0b00010001)) != i2c_error_ok)
return(error);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_bh1750_read(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[2];
double luxpercount;
if((error = i2c_receive(entry->address, 2, i2cbuffer)) != i2c_error_ok)
return(error);
if(config_get_flag(config_flag_bh_high_sens))
luxpercount = 0.11;
else
luxpercount = 0.93;
value->raw = (double)((i2cbuffer[0] << 8) | i2cbuffer[1]);
value->cooked = value->raw * luxpercount;
return(i2c_error_ok);
}
irom attr_pure static uint8_t htu21_crc(int length, const uint8_t *data)
{
uint8_t outer, inner, testbit, crc;
crc = 0;
for(outer = 0; (int)outer < length; outer++)
{
crc ^= data[outer];
for(inner = 0; inner < 8; inner++)
{
testbit = !!(crc & 0x80);
crc <<= 1;
if(testbit)
crc ^= 0x31;
}
}
return(crc);
}
irom static i2c_error_t sensor_htu21_read_temp(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[4];
uint8_t crc1, crc2;
// temperature measurement "hold master" mode
if((error = i2c_send_1(entry->address, 0xe3)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 4, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = ((uint16_t)i2cbuffer[0] << 8) | (uint16_t)i2cbuffer[1];
crc1 = i2cbuffer[2];
crc2 = htu21_crc(2, &i2cbuffer[0]);
if(crc1 != crc2)
{
value->cooked = -256;
return(i2c_error_ok);
}
value->cooked = ((value->raw * 175.72) / 65536) - 46.85;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_htu21_read_hum(const device_table_entry_t *entry, value_t *value)
{
i2c_error_t error;
uint8_t i2cbuffer[4];
uint8_t crc1, crc2;
// humidity measurement "hold master" mode
if((error = i2c_send_1(entry->address, 0xe5)) != i2c_error_ok)
return(error);
if((error = i2c_receive(entry->address, 4, i2cbuffer)) != i2c_error_ok)
return(error);
value->raw = ((uint16_t)i2cbuffer[0] << 8) | (uint16_t)i2cbuffer[1];
crc1 = i2cbuffer[2];
crc2 = htu21_crc(2, &i2cbuffer[0]);
if(crc1 != crc2)
{
value->cooked = -1;
return(i2c_error_ok);
}
value->cooked = ((value->raw * 125) / 65536) - 6;
return(i2c_error_ok);
}
irom attr_pure static uint16_t am2321_crc(int length, const uint8_t *data)
{
uint8_t outer, inner, testbit;
uint16_t crc;
crc = 0xffff;
for(outer = 0; outer < length; outer++)
{
crc ^= data[outer];
for(inner = 0; inner < 8; inner++)
{
testbit = !!(crc & 0x01);
crc >>= 1;
if(testbit)
crc ^= 0xa001;
}
}
return(crc);
}
irom static i2c_error_t sensor_am2321_read_registers(int address, int offset, int length, uint8_t *values)
{
i2c_error_t error;
uint8_t i2cbuffer[32];
uint16_t crc1, crc2;
// wake the device
i2c_send_1(address, 0);
if((error = i2c_reset()) != i2c_error_ok)
return(i2c_error_device_error_1);
msleep(1);
if((error = i2c_send_3(address, 0x03, offset, length)) != i2c_error_ok)
return(error);
msleep(1);
if((error = i2c_receive(address, length + 4, i2cbuffer)) != i2c_error_ok)
return(error);
if((i2cbuffer[0] != 0x03) || (i2cbuffer[1] != length))
return(i2c_error_device_error_2);
crc1 = i2cbuffer[length + 2] | (i2cbuffer[length + 3] << 8);
crc2 = am2321_crc(length + 2, i2cbuffer);
if(crc1 != crc2)
return(i2c_error_device_error_3);
ets_memcpy(values, &i2cbuffer[2], length);
return(i2c_error_ok);
}
static value_t sensor_am2321_cached_temperature;
static value_t sensor_am2321_cached_humidity;
irom static i2c_error_t sensor_am2321_read(int address, value_t *value, bool_t request_humidity)
{
i2c_error_t error;
uint8_t values[4];
// 0x00 start address: humidity (16 bits), temperature (16 bits)
// 0x04 length
if((error = sensor_am2321_read_registers(address, 0x00, 0x04, values)) == i2c_error_ok)
{
sensor_am2321_cached_humidity.raw = (values[0] << 8) | values[1];
sensor_am2321_cached_humidity.cooked = sensor_am2321_cached_humidity.raw / 10.0;
sensor_am2321_cached_temperature.raw = (values[2] << 8) | values[3];
sensor_am2321_cached_temperature.cooked = sensor_am2321_cached_temperature.raw / 10.0;
}
else
i2c_reset();
if(request_humidity)
*value = sensor_am2321_cached_humidity;
else
*value = sensor_am2321_cached_temperature;
return(i2c_error_ok);
}
irom static i2c_error_t sensor_am2321_temp_init(const device_table_entry_t *entry)
{
i2c_error_t error;
uint8_t values[2];
// 0x08 start address: device id
// 0x02 length
if((error = sensor_am2321_read_registers(entry->address, 0x08, 0x02, values)) != i2c_error_ok)
return(i2c_error_address_nak);
//if((values[0] != 0x32) || (values[1] != 0x31))
//return(i2c_error_address_nak);
return(i2c_error_ok);
}
irom static i2c_error_t sensor_am2321_temp_read(const device_table_entry_t *entry, value_t *value)
{
return(sensor_am2321_read(entry->address, value, false));