-
Notifications
You must be signed in to change notification settings - Fork 19
/
vl53l1x_class.cpp
1228 lines (1024 loc) · 34.1 KB
/
vl53l1x_class.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
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
/**
******************************************************************************
* @file vl53l0x_class.cpp
* @author IMG
* @version V0.0.1
* @date 14-December-2018
* @brief Implementation file for the VL53L1X driver class
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes */
#include <stdlib.h>
#include "Arduino.h"
#include "vl53l1x_class.h"
#define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E
#define MM_CONFIG__INNER_OFFSET_MM 0x0020
#define MM_CONFIG__OUTER_OFFSET_MM 0x0022
//#define DEBUG_MODE
const uint8_t VL51L1X_DEFAULT_CONFIGURATION[] =
{
0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */
0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */
0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */
0x00, /* 0x32 : not user-modifiable */
0x02, /* 0x33 : not user-modifiable */
0x08, /* 0x34 : not user-modifiable */
0x00, /* 0x35 : not user-modifiable */
0x08, /* 0x36 : not user-modifiable */
0x10, /* 0x37 : not user-modifiable */
0x01, /* 0x38 : not user-modifiable */
0x01, /* 0x39 : not user-modifiable */
0x00, /* 0x3a : not user-modifiable */
0x00, /* 0x3b : not user-modifiable */
0x00, /* 0x3c : not user-modifiable */
0x00, /* 0x3d : not user-modifiable */
0xff, /* 0x3e : not user-modifiable */
0x00, /* 0x3f : not user-modifiable */
0x0F, /* 0x40 : not user-modifiable */
0x00, /* 0x41 : not user-modifiable */
0x00, /* 0x42 : not user-modifiable */
0x00, /* 0x43 : not user-modifiable */
0x00, /* 0x44 : not user-modifiable */
0x00, /* 0x45 : not user-modifiable */
0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */
0x0b, /* 0x47 : not user-modifiable */
0x00, /* 0x48 : not user-modifiable */
0x00, /* 0x49 : not user-modifiable */
0x02, /* 0x4a : not user-modifiable */
0x0a, /* 0x4b : not user-modifiable */
0x21, /* 0x4c : not user-modifiable */
0x00, /* 0x4d : not user-modifiable */
0x00, /* 0x4e : not user-modifiable */
0x05, /* 0x4f : not user-modifiable */
0x00, /* 0x50 : not user-modifiable */
0x00, /* 0x51 : not user-modifiable */
0x00, /* 0x52 : not user-modifiable */
0x00, /* 0x53 : not user-modifiable */
0xc8, /* 0x54 : not user-modifiable */
0x00, /* 0x55 : not user-modifiable */
0x00, /* 0x56 : not user-modifiable */
0x38, /* 0x57 : not user-modifiable */
0xff, /* 0x58 : not user-modifiable */
0x01, /* 0x59 : not user-modifiable */
0x00, /* 0x5a : not user-modifiable */
0x08, /* 0x5b : not user-modifiable */
0x00, /* 0x5c : not user-modifiable */
0x00, /* 0x5d : not user-modifiable */
0x01, /* 0x5e : not user-modifiable */
0xcc, /* 0x5f : not user-modifiable */
0x0f, /* 0x60 : not user-modifiable */
0x01, /* 0x61 : not user-modifiable */
0xf1, /* 0x62 : not user-modifiable */
0x0d, /* 0x63 : not user-modifiable */
0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */
0x68, /* 0x65 : Sigma threshold LSB */
0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */
0x80, /* 0x67 : Min count Rate LSB */
0x08, /* 0x68 : not user-modifiable */
0xb8, /* 0x69 : not user-modifiable */
0x00, /* 0x6a : not user-modifiable */
0x00, /* 0x6b : not user-modifiable */
0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */
0x00, /* 0x6d : Intermeasurement period */
0x0f, /* 0x6e : Intermeasurement period */
0x89, /* 0x6f : Intermeasurement period LSB */
0x00, /* 0x70 : not user-modifiable */
0x00, /* 0x71 : not user-modifiable */
0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */
0x00, /* 0x73 : distance threshold high LSB */
0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */
0x00, /* 0x75 : distance threshold low LSB */
0x00, /* 0x76 : not user-modifiable */
0x01, /* 0x77 : not user-modifiable */
0x0f, /* 0x78 : not user-modifiable */
0x0d, /* 0x79 : not user-modifiable */
0x0e, /* 0x7a : not user-modifiable */
0x0e, /* 0x7b : not user-modifiable */
0x00, /* 0x7c : not user-modifiable */
0x00, /* 0x7d : not user-modifiable */
0x02, /* 0x7e : not user-modifiable */
0xc7, /* 0x7f : ROI center, use SetROI() */
0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */
0x9B, /* 0x81 : not user-modifiable */
0x00, /* 0x82 : not user-modifiable */
0x00, /* 0x83 : not user-modifiable */
0x00, /* 0x84 : not user-modifiable */
0x01, /* 0x85 : not user-modifiable */
0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */
0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
};
/* VL53L1X_api.h functions */
VL53L1X_ERROR VL53L1X::VL53L1X_GetSWVersion(VL53L1X_Version_t *pVersion)
{
VL53L1X_ERROR Status = 0;
pVersion->major = VL53L1X_IMPLEMENTATION_VER_MAJOR;
pVersion->minor = VL53L1X_IMPLEMENTATION_VER_MINOR;
pVersion->build = VL53L1X_IMPLEMENTATION_VER_SUB;
pVersion->revision = VL53L1X_IMPLEMENTATION_VER_REVISION;
return Status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetI2CAddress(uint8_t new_address)
{
VL53L1X_ERROR status = 0;
status = VL53L1X_WrByte(Device, VL53L1X_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
Device->I2cDevAddr = new_address;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit()
{
VL53L1X_ERROR status = 0;
uint8_t Addr = 0x00, tmp=0;
for (Addr = 0x2D; Addr <= 0x87; Addr++)
{
status = VL53L1X_WrByte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
}
status = VL53L1X_StartRanging();
while(tmp==0)
{
status = VL53L1X_CheckForDataReady(&tmp);
}
tmp = 0;
status = VL53L1X_ClearInterrupt();
status = VL53L1X_StopRanging();
status = VL53L1X_WrByte(Device, VL53L1X_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
status = VL53L1X_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_ClearInterrupt()
{
VL53L1X_ERROR status = 0;
status = VL53L1X_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetInterruptPolarity(uint8_t NewPolarity)
{
uint8_t Temp;
VL53L1X_ERROR status = 0;
status = VL53L1X_RdByte(Device, GPIO_HV_MUX__CTRL, &Temp);
Temp = Temp & 0xEF;
status = VL53L1X_WrByte(Device, GPIO_HV_MUX__CTRL, Temp | (!(NewPolarity & 1)) << 4);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity)
{
uint8_t Temp;
VL53L1X_ERROR status = 0;
status = VL53L1X_RdByte(Device, GPIO_HV_MUX__CTRL, &Temp);
Temp = Temp & 0x10;
*pInterruptPolarity = !(Temp>>4);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging()
{
VL53L1X_ERROR status = 0;
status = VL53L1X_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging()
{
VL53L1X_ERROR status = 0;
status = VL53L1X_WrByte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady)
{
uint8_t Temp;
uint8_t IntPol;
VL53L1X_ERROR status = 0;
status = VL53L1X_GetInterruptPolarity(&IntPol);
status = VL53L1X_RdByte(Device, GPIO__TIO_HV_STATUS, &Temp);
/* Read in the register to check if a new value is available */
if (status == 0)
{
if ((Temp & 1) == IntPol)
*isDataReady = 1;
else
*isDataReady = 0;
}
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetTimingBudgetInMs(uint16_t TimingBudgetInMs)
{
uint16_t DM;
VL53L1X_ERROR status=0;
status = VL53L1X_GetDistanceMode(&DM);
if (DM == 0)
return 1;
else if (DM == 1) /* Short DistanceMode */
{
switch (TimingBudgetInMs)
{
case 15: /* only available in short distance mode */
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x01D);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x0027);
break;
case 20:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x0051);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x006E);
break;
case 33:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x00D6);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x006E);
break;
case 50:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x1AE);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x01E8);
break;
case 100:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x02E1);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x0388);
break;
case 200:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x03E1);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x0496);
break;
case 500:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x0591);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x05C1);
break;
default:
status = 1;
break;
}
}
else
{
switch (TimingBudgetInMs)
{
case 20:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x001E);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x0022);
break;
case 33:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x0060);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x006E);
break;
case 50:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x00AD);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x00C6);
break;
case 100:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x01CC);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x01EA);
break;
case 200:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x02D9);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x02F8);
break;
case 500:
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
0x048F);
VL53L1X_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
0x04A4);
break;
default:
status = 1;
break;
}
}
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetTimingBudgetInMs(uint16_t *pTimingBudget)
{
uint16_t Temp;
VL53L1X_ERROR status = 0;
status = VL53L1X_RdWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp);
switch (Temp)
{
case 0x001D :
*pTimingBudget = 15;
break;
case 0x0051 :
case 0x001E :
*pTimingBudget = 20;
break;
case 0x00D6 :
case 0x0060 :
*pTimingBudget = 33;
break;
case 0x1AE :
case 0x00AD :
*pTimingBudget = 50;
break;
case 0x02E1 :
case 0x01CC :
*pTimingBudget = 100;
break;
case 0x03E1 :
case 0x02D9 :
*pTimingBudget = 200;
break;
case 0x0591 :
case 0x048F :
*pTimingBudget = 500;
break;
default:
*pTimingBudget = 0;
break;
}
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceMode(uint16_t DM)
{
uint16_t TB;
VL53L1X_ERROR status = 0;
status = VL53L1X_GetTimingBudgetInMs(&TB);
switch (DM)
{
case 1:
status = VL53L1X_WrByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);
status = VL53L1X_WrWord(Device, SD_CONFIG__WOI_SD0, 0x0705);
status = VL53L1X_WrWord(Device, SD_CONFIG__INITIAL_PHASE_SD0, 0x0606);
break;
case 2:
status = VL53L1X_WrByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x0A);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
status = VL53L1X_WrByte(Device, RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);
status = VL53L1X_WrWord(Device, SD_CONFIG__WOI_SD0, 0x0F0D);
status = VL53L1X_WrWord(Device, SD_CONFIG__INITIAL_PHASE_SD0, 0x0E0E);
break;
default:
break;
}
status = VL53L1X_SetTimingBudgetInMs(TB);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceMode(uint16_t *DM)
{
uint8_t TempDM, status=0;
status = VL53L1X_RdByte(Device,PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
if (TempDM == 0x14)
*DM=1;
if(TempDM == 0x0A)
*DM=2;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetInterMeasurementInMs(uint16_t InterMeasMs)
{
uint16_t ClockPLL;
VL53L1X_ERROR status = 0;
status = VL53L1X_RdWord(Device, VL53L1X_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
ClockPLL = ClockPLL&0x3FF;
VL53L1X_WrDWord(Device, VL53L1X_SYSTEM__INTERMEASUREMENT_PERIOD,
(uint32_t)(ClockPLL * InterMeasMs * 1.075));
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetInterMeasurementInMs(uint16_t *pIM)
{
uint16_t ClockPLL;
VL53L1X_ERROR status = 0;
uint32_t tmp;
status = VL53L1X_RdDWord(Device,VL53L1X_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp);
*pIM = (uint16_t)tmp;
status = VL53L1X_RdWord(Device, VL53L1X_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
ClockPLL = ClockPLL&0x3FF;
*pIM= (uint16_t)(*pIM/(ClockPLL*1.065));
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_BootState(uint8_t *state)
{
VL53L1X_ERROR status = 0;
uint8_t tmp = 0;
status = VL53L1X_RdByte(Device,VL53L1X_FIRMWARE__SYSTEM_STATUS, &tmp);
*state = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSensorId(uint16_t *sensorId)
{
VL53L1X_ERROR status = 0;
uint16_t tmp = 0;
status = VL53L1X_RdWord(Device, VL53L1X_IDENTIFICATION__MODEL_ID, &tmp);
*sensorId = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistance(uint16_t *distance)
{
VL53L1X_ERROR status = 0;
uint16_t tmp = 0;
status = (VL53L1X_RdWord(Device,
VL53L1X_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp));
*distance = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalPerSpad(uint16_t *signalRate)
{
VL53L1X_ERROR status = 0;
uint16_t SpNb=1, signal;
status = VL53L1X_RdWord(Device,
VL53L1X_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal);
status = VL53L1X_RdWord(Device,
VL53L1X_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
*signalRate = (uint16_t) (2000.0*signal/SpNb);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientPerSpad(uint16_t *ambPerSp)
{
VL53L1X_ERROR status=0;
uint16_t AmbientRate, SpNb=1;
status = VL53L1X_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate);
status = VL53L1X_RdWord(Device, VL53L1X_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
*ambPerSp=(uint16_t) (2000.0 * AmbientRate / SpNb);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalRate(uint16_t *signal)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,
VL53L1X_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp);
*signal = tmp*8;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSpadNb(uint16_t *spNb)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,
VL53L1X_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp);
*spNb = tmp >> 8;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientRate(uint16_t *ambRate)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &tmp);
*ambRate = tmp*8;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetRangeStatus(uint8_t *rangeStatus)
{
VL53L1X_ERROR status = 0;
uint8_t RgSt;
status = VL53L1X_RdByte(Device, VL53L1X_RESULT__RANGE_STATUS, &RgSt);
RgSt = RgSt&0x1F;
switch (RgSt)
{
case 9:
RgSt = 0;
break;
case 6:
RgSt = 1;
break;
case 4:
RgSt = 2;
break;
case 8:
RgSt = 3;
break;
case 5:
RgSt = 4;
break;
case 3:
RgSt = 5;
break;
case 19:
RgSt = 6;
break;
case 7:
RgSt = 7;
break;
case 12:
RgSt = 9;
break;
case 18:
RgSt = 10;
break;
case 22:
RgSt = 11;
break;
case 23:
RgSt = 12;
break;
case 13:
RgSt = 13;
break;
default:
RgSt = 255;
break;
}
*rangeStatus = RgSt;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetOffset(int16_t OffsetValue)
{
VL53L1X_ERROR status = 0;
int16_t Temp;
Temp = (OffsetValue*4);
VL53L1X_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM,
(uint16_t)Temp);
VL53L1X_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
VL53L1X_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetOffset(int16_t *offset)
{
VL53L1X_ERROR status = 0;
uint16_t Temp;
status = VL53L1X_RdWord(Device,ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp);
Temp = Temp<<3;
*offset = (int16_t)(Temp);
*offset = *offset / 32;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetXtalk(uint16_t XtalkValue)
{
/* XTalkValue in count per second to avoid float type */
VL53L1X_ERROR status = 0;
status = VL53L1X_WrWord(Device,
ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS,
0x0000);
status = VL53L1X_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS,
0x0000);
status = VL53L1X_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS,
(XtalkValue<<9)/1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetXtalk(uint16_t *xtalk )
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp);
*xtalk = (tmp*1000)>>9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceThreshold(uint16_t ThreshLow,
uint16_t ThreshHigh, uint8_t Window,
uint8_t IntOnNoTarget)
{
VL53L1X_ERROR status = 0;
uint8_t Temp = 0;
status = VL53L1X_RdByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp);
Temp = Temp & 0x47;
if (IntOnNoTarget == 0)
{
status = VL53L1X_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
(Temp | (Window & 0x07)));
}
else
{
status = VL53L1X_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
((Temp | (Window & 0x07)) | 0x40));
}
status = VL53L1X_WrWord(Device, SYSTEM__THRESH_HIGH, ThreshHigh);
status = VL53L1X_WrWord(Device, SYSTEM__THRESH_LOW, ThreshLow);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdWindow(uint16_t *window)
{
VL53L1X_ERROR status = 0;
uint8_t tmp;
status = VL53L1X_RdByte(Device,SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
*window = (uint16_t)(tmp & 0x7);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdLow(uint16_t *low)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,SYSTEM__THRESH_LOW, &tmp);
*low = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdHigh(uint16_t *high)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,SYSTEM__THRESH_HIGH, &tmp);
*high = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetROI(uint16_t X, uint16_t Y)
{
uint8_t OpticalCenter;
VL53L1X_ERROR status = 0;
status =VL53L1X_RdByte(Device, VL53L1X_ROI_CONFIG__MODE_ROI_CENTRE_SPAD, &OpticalCenter);
if (X > 16)
X = 16;
if (Y > 16)
Y = 16;
if (X > 10 || Y > 10)
{
OpticalCenter = 199;
}
status = VL53L1X_WrByte(Device, ROI_CONFIG__USER_ROI_CENTRE_SPAD, OpticalCenter);
status = VL53L1X_WrByte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
(Y - 1) << 4 | (X - 1));
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetROI_XY(uint16_t *ROI_X, uint16_t *ROI_Y)
{
VL53L1X_ERROR status = 0;
uint8_t tmp;
status = VL53L1X_RdByte(Device,ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp);
*ROI_X = ((uint16_t)tmp & 0x0F) + 1;
*ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetROICenter(uint8_t ROICenter)
{
VL53L1X_ERROR status = 0;
status = VL53L1X_WrByte(Device,ROI_CONFIG__USER_ROI_CENTRE_SPAD,ROICenter);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetROICenter(uint8_t *ROICenter)
{
VL53L1X_ERROR status = 0;
uint8_t tmp;
status = VL53L1X_RdByte(Device,ROI_CONFIG__USER_ROI_CENTRE_SPAD,&tmp);
*ROICenter = tmp;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetSignalThreshold(uint16_t Signal)
{
VL53L1X_ERROR status = 0;
VL53L1X_WrWord(Device,RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS,Signal>>3);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalThreshold(uint16_t *signal)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,
RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp);
*signal = tmp <<3;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetSigmaThreshold(uint16_t Sigma)
{
VL53L1X_ERROR status = 0;
if(Sigma>(0xFFFF>>2))
{
return 1;
}
/* 16 bits register 14.2 format */
status = VL53L1X_WrWord(Device,RANGE_CONFIG__SIGMA_THRESH,Sigma<<2);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_GetSigmaThreshold(uint16_t *sigma)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1X_RdWord(Device,RANGE_CONFIG__SIGMA_THRESH, &tmp);
*sigma = tmp >> 2;
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_StartTemperatureUpdate()
{
VL53L1X_ERROR status = 0;
uint8_t tmp=0;
status = VL53L1X_WrByte(Device,VL53L1X_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND,0x81); /* full VHV */
status = VL53L1X_WrByte(Device,0x0B,0x92);
status = VL53L1X_StartRanging();
while(tmp==0)
{
status = VL53L1X_CheckForDataReady(&tmp);
}
tmp = 0;
status = VL53L1X_ClearInterrupt();
status = VL53L1X_StopRanging();
status = VL53L1X_WrByte(Device, VL53L1X_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
status = VL53L1X_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
return status;
}
/* VL53L1X_calibration.h functions */
int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset)
{
uint8_t i = 0, tmp;
int16_t AverageDistance = 0;
uint16_t distance;
VL53L1X_ERROR status = 0;
status = VL53L1X_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, 0x0);
status = VL53L1X_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
status = VL53L1X_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
status = VL53L1X_StartRanging(); /* Enable VL53L1X sensor */
for (i = 0; i < 50; i++)
{
while (tmp == 0)
{
status = VL53L1X_CheckForDataReady(&tmp);
}
tmp = 0;
status = VL53L1X_GetDistance(&distance);
status = VL53L1X_ClearInterrupt();
AverageDistance = AverageDistance + distance;
}
status = VL53L1X_StopRanging();
AverageDistance = AverageDistance / 50;
*offset = TargetDistInMm - AverageDistance;
status = VL53L1X_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset*4);
return status;
}
int8_t VL53L1X::VL53L1X_CalibrateXtalk(uint16_t TargetDistInMm, uint16_t *xtalk)
{
uint8_t i, tmp= 0;
float AverageSignalRate = 0;
float AverageDistance = 0;
float AverageSpadNb = 0;
uint16_t distance = 0, spadNum;
uint16_t sr;
VL53L1X_ERROR status = 0;
status = VL53L1X_WrWord(Device, 0x0016,0);
status = VL53L1X_StartRanging();
for (i = 0; i < 50; i++)
{
while (tmp == 0)
{
status = VL53L1X_CheckForDataReady(&tmp);
}
tmp=0;
status= VL53L1X_GetSignalRate(&sr);
status= VL53L1X_GetDistance(&distance);
status = VL53L1X_ClearInterrupt();
AverageDistance = AverageDistance + distance;
status = VL53L1X_GetSpadNb(&spadNum);
AverageSpadNb = AverageSpadNb + spadNum;
AverageSignalRate =
AverageSignalRate + sr;
}
status = VL53L1X_StopRanging();
AverageDistance = AverageDistance / 50;
AverageSpadNb = AverageSpadNb / 50;
AverageSignalRate = AverageSignalRate / 50;
/* Calculate Xtalk value */
*xtalk = (uint16_t)(512*(AverageSignalRate*(1-(AverageDistance/TargetDistInMm)))/AverageSpadNb);
status = VL53L1X_WrWord(Device, 0x0016, *xtalk);
return status;
}
/* Write and read functions from I2C */
VL53L1X_ERROR VL53L1X::VL53L1X_WriteMulti(VL53L1X_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
int status;
status = VL53L1X_I2CWrite(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_ReadMulti(VL53L1X_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
int status;
status = VL53L1X_I2CRead(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_WrByte(VL53L1X_DEV Dev, uint16_t index, uint8_t data)
{
int status;
status=VL53L1X_I2CWrite(Dev->I2cDevAddr, index, &data, 1);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_WrWord(VL53L1X_DEV Dev, uint16_t index, uint16_t data)
{
int status;
uint8_t buffer[2];
buffer[0] = data >> 8;
buffer[1] = data & 0x00FF;
status=VL53L1X_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_WrDWord(VL53L1X_DEV Dev, uint16_t index, uint32_t data)
{
int status;
uint8_t buffer[4];
buffer[0] = (data >> 24) & 0xFF;
buffer[1] = (data >> 16) & 0xFF;
buffer[2] = (data >> 8) & 0xFF;
buffer[3] = (data >> 0) & 0xFF;
status=VL53L1X_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_RdByte(VL53L1X_DEV Dev, uint16_t index, uint8_t *data)
{
int status;
status = VL53L1X_I2CRead(Dev->I2cDevAddr, index, data, 1);