-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtof.c
More file actions
933 lines (774 loc) · 27 KB
/
tof.c
File metadata and controls
933 lines (774 loc) · 27 KB
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
//
// VL53L0X time of flight range sensor
// Library to read the distance
// from the I2C bus
//
// by Larry Bank
//
// This code is based on Pololu's Arduino library
// https://github.com/pololu/vl53l0x-arduino
// (see LICENSE.txt for more info)
//
// My version is an attempt to simplify that code and
// create a generic C library for Linux
//
// version modify by Daniel Perron to run on Rasberry Pi pico with the sdk
// march 30 2023
#ifndef PICO_BOARD
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
static int file_i2c = 0;
#else
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
uint8_t i2cAddress= 0x29;
#define usleep(A) sleep_us(A)
#endif
static unsigned char stop_variable;
static uint32_t measurement_timing_budget_us;
static unsigned char readReg(unsigned char ucAddr);
static unsigned short readReg16(unsigned char ucAddr);
static void writeReg16(unsigned char ucAddr, unsigned short usValue);
static void writeReg(unsigned char ucAddr, unsigned char ucValue);
static void writeRegList(unsigned char *ucList);
static int initSensor(int);
static int performSingleRefCalibration(uint8_t vhv_init_byte);
static int setMeasurementTimingBudget(uint32_t budget_us);
#define calcMacroPeriod(vcsel_period_pclks) ((((uint32_t)2304 * (vcsel_period_pclks) * 1655) + 500) / 1000)
// Encode VCSEL pulse period register value from period in PCLKs
// based on VL53L0X_encode_vcsel_period()
#define encodeVcselPeriod(period_pclks) (((period_pclks) >> 1) - 1)
#define SEQUENCE_ENABLE_FINAL_RANGE 0x80
#define SEQUENCE_ENABLE_PRE_RANGE 0x40
#define SEQUENCE_ENABLE_TCC 0x10
#define SEQUENCE_ENABLE_DSS 0x08
#define SEQUENCE_ENABLE_MSRC 0x04
typedef enum vcselperiodtype { VcselPeriodPreRange, VcselPeriodFinalRange } vcselPeriodType;
static int setVcselPulsePeriod(vcselPeriodType type, uint8_t period_pclks);
typedef struct tagSequenceStepTimeouts
{
uint16_t pre_range_vcsel_period_pclks, final_range_vcsel_period_pclks;
uint16_t msrc_dss_tcc_mclks, pre_range_mclks, final_range_mclks;
uint32_t msrc_dss_tcc_us, pre_range_us, final_range_us;
} SequenceStepTimeouts;
// VL53L0X internal registers
#define REG_IDENTIFICATION_MODEL_ID 0xc0
#define REG_IDENTIFICATION_REVISION_ID 0xc2
#define REG_SYSRANGE_START 0x00
#define REG_RESULT_INTERRUPT_STATUS 0x13
#define RESULT_RANGE_STATUS 0x14
#define ALGO_PHASECAL_LIM 0x30
#define ALGO_PHASECAL_CONFIG_TIMEOUT 0x30
#define GLOBAL_CONFIG_VCSEL_WIDTH 0x32
#define FINAL_RANGE_CONFIG_VALID_PHASE_LOW 0x47
#define FINAL_RANGE_CONFIG_VALID_PHASE_HIGH 0x48
#define PRE_RANGE_CONFIG_VCSEL_PERIOD 0x50
#define PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI 0x51
#define PRE_RANGE_CONFIG_VALID_PHASE_LOW 0x56
#define PRE_RANGE_CONFIG_VALID_PHASE_HIGH 0x57
#define REG_MSRC_CONFIG_CONTROL 0x60
#define FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
#define FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI 0x71
#define MSRC_CONFIG_TIMEOUT_MACROP 0x46
#define FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT 0x44
#define SYSRANGE_START 0x00
#define SYSTEM_SEQUENCE_CONFIG 0x01
#define SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A
#define RESULT_INTERRUPT_STATUS 0x13
#define VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV 0x89
#define GLOBAL_CONFIG_SPAD_ENABLES_REF_0 0xB0
#define GPIO_HV_MUX_ACTIVE_HIGH 0x84
#define SYSTEM_INTERRUPT_CLEAR 0x0B
//
// Opens a file system handle to the I2C device
// reads the calibration data and sets the device
// into auto sensing mode
//
int tofInit(int iChan, int iAddr, int bLongRange)
{
#ifndef PICO_BOARD
char filename[32];
sprintf(filename,"/dev/i2c-%d", iChan);
if ((file_i2c = open(filename, O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus; need to run as sudo?\n");
return 0;
}
if (ioctl(file_i2c, I2C_SLAVE, iAddr) < 0)
{
fprintf(stderr, "Failed to acquire bus access or talk to slave\n");
close(file_i2c);
file_i2c = -1;
return 0;
}
#else
i2cAddress=iAddr;
return initSensor(bLongRange); // finally, initialize the magic numbers in the sensor
#endif
return initSensor(bLongRange); // finally, initialize the magic numbers in the sensor
} /* tofInit() */
//
// Read a pair of registers as a 16-bit value
//
static unsigned short readReg16(unsigned char ucAddr)
{
unsigned char ucTemp[2];
#ifndef PICO_BOARD
int rc;
rc = write(file_i2c, &ucAddr, 1);
if (rc == 1)
{
rc = read(file_i2c, ucTemp, 2);
}
#else
i2c_write_blocking(i2c_default,i2cAddress,&ucAddr,1,true);
i2c_read_blocking(i2c_default,i2cAddress,ucTemp,2,false);
#endif
return (unsigned short)((ucTemp[0]<<8) + ucTemp[1]);
} /* readReg16() */
//
// Read a single register value from I2C device
//
static unsigned char readReg(unsigned char ucAddr)
{
unsigned char ucTemp;
#ifndef PICO_BOARD
int rc;
ucTemp = ucAddr;
rc = write(file_i2c, &ucTemp, 1);
if (rc == 1)
{
rc = read(file_i2c, &ucTemp, 1);
if (rc != 1) {};
}
#else
i2c_write_blocking(i2c_default,i2cAddress,&ucAddr,1,true);
i2c_read_blocking(i2c_default,i2cAddress,&ucTemp,1,false);
#endif
return ucTemp;
} /* ReadReg() */
static void readMulti(unsigned char ucAddr, unsigned char *pBuf, int iCount)
{
#ifndef PICO_BOARD
int rc;
rc = write(file_i2c, &ucAddr, 1);
if (rc == 1)
{
rc = read(file_i2c, pBuf, iCount);
if (rc != iCount) {};
}
#else
i2c_write_blocking(i2c_default,i2cAddress,&ucAddr,1,true);
i2c_read_blocking(i2c_default,i2cAddress,pBuf,iCount,false);
#endif
} /* readMulti() */
static void writeMulti(unsigned char ucAddr, unsigned char *pBuf, int iCount)
{
unsigned char ucTemp[16];
int rc;
ucTemp[0] = ucAddr;
memcpy(&ucTemp[1], pBuf, iCount);
#ifndef PICO_BOARD
rc = write(file_i2c, ucTemp, iCount+1);
if (rc != iCount+1) {};
#else
i2c_write_blocking(i2c_default,i2cAddress,ucTemp,iCount+1,false);
#endif
} /* writeMulti() */
//
// Write a 16-bit value to a register
//
static void writeReg16(unsigned char ucAddr, unsigned short usValue)
{
unsigned char ucTemp[4];
int rc;
ucTemp[0] = ucAddr;
ucTemp[1] = (unsigned char)(usValue >> 8); // MSB first
ucTemp[2] = (unsigned char)usValue;
#ifndef PICO_BOARD
rc = write(file_i2c, ucTemp, 3);
if (rc != 3) {}; // suppress warning
#else
i2c_write_blocking(i2c_default,i2cAddress,ucTemp,3,false);
#endif
} /* writeReg16() */
//
// Write a single register/value pair
//
static void writeReg(unsigned char ucAddr, unsigned char ucValue)
{
unsigned char ucTemp[2];
int rc;
ucTemp[0] = ucAddr;
ucTemp[1] = ucValue;
#ifndef PICO_BOARD
rc = write(file_i2c, ucTemp, 2);
if (rc != 2) {}; // suppress warning
#else
i2c_write_blocking(i2c_default,i2cAddress,ucTemp,2,false);
#endif
} /* writeReg() */
//
// Write a list of register/value pairs to the I2C device
//
static void writeRegList(unsigned char *ucList)
{
unsigned char ucCount = *ucList++; // count is the first element in the list
int rc;
while (ucCount)
{
#ifndef PICO_BOARD
rc = write(file_i2c, ucList, 2);
if (rc != 2) {};
#else
i2c_write_blocking(i2c_default,i2cAddress,ucList,2,false);
#endif
ucList += 2;
ucCount--;
}
} /* writeRegList() */
//
// Register init lists consist of the count followed by register/value pairs
//
unsigned char ucI2CMode[] = {4, 0x88,0x00, 0x80,0x01, 0xff,0x01, 0x00,0x00};
unsigned char ucI2CMode2[] = {3, 0x00,0x01, 0xff,0x00, 0x80,0x00};
unsigned char ucSPAD0[] = {4, 0x80,0x01, 0xff,0x01, 0x00,0x00, 0xff,0x06};
unsigned char ucSPAD1[] = {5, 0xff,0x07, 0x81,0x01, 0x80,0x01, 0x94,0x6b, 0x83,0x00};
unsigned char ucSPAD2[] = {4, 0xff,0x01, 0x00,0x01, 0xff,0x00, 0x80,0x00};
unsigned char ucSPAD[] = {5, 0xff,0x01, 0x4f,0x00, 0x4e,0x2c, 0xff,0x00, 0xb6,0xb4};
unsigned char ucDefTuning[] = {80, 0xff,0x01, 0x00,0x00, 0xff,0x00, 0x09,0x00,
0x10,0x00, 0x11,0x00, 0x24,0x01, 0x25,0xff, 0x75,0x00, 0xff,0x01, 0x4e,0x2c,
0x48,0x00, 0x30,0x20, 0xff,0x00, 0x30,0x09, 0x54,0x00, 0x31,0x04, 0x32,0x03,
0x40,0x83, 0x46,0x25, 0x60,0x00, 0x27,0x00, 0x50,0x06, 0x51,0x00, 0x52,0x96,
0x56,0x08, 0x57,0x30, 0x61,0x00, 0x62,0x00, 0x64,0x00, 0x65,0x00, 0x66,0xa0,
0xff,0x01, 0x22,0x32, 0x47,0x14, 0x49,0xff, 0x4a,0x00, 0xff,0x00, 0x7a,0x0a,
0x7b,0x00, 0x78,0x21, 0xff,0x01, 0x23,0x34, 0x42,0x00, 0x44,0xff, 0x45,0x26,
0x46,0x05, 0x40,0x40, 0x0e,0x06, 0x20,0x1a, 0x43,0x40, 0xff,0x00, 0x34,0x03,
0x35,0x44, 0xff,0x01, 0x31,0x04, 0x4b,0x09, 0x4c,0x05, 0x4d,0x04, 0xff,0x00,
0x44,0x00, 0x45,0x20, 0x47,0x08, 0x48,0x28, 0x67,0x00, 0x70,0x04, 0x71,0x01,
0x72,0xfe, 0x76,0x00, 0x77,0x00, 0xff,0x01, 0x0d,0x01, 0xff,0x00, 0x80,0x01,
0x01,0xf8, 0xff,0x01, 0x8e,0x01, 0x00,0x01, 0xff,0x00, 0x80,0x00};
static int getSpadInfo(unsigned char *pCount, unsigned char *pTypeIsAperture)
{
int iTimeout;
unsigned char ucTemp;
#define MAX_TIMEOUT 50
writeRegList(ucSPAD0);
writeReg(0x83, readReg(0x83) | 0x04);
writeRegList(ucSPAD1);
iTimeout = 0;
while(iTimeout < MAX_TIMEOUT)
{
if (readReg(0x83) != 0x00) break;
iTimeout++;
usleep(5000);
}
if (iTimeout == MAX_TIMEOUT)
{
fprintf(stderr, "Timeout while waiting for SPAD info\n");
return 0;
}
writeReg(0x83,0x01);
ucTemp = readReg(0x92);
*pCount = (ucTemp & 0x7f);
*pTypeIsAperture = (ucTemp & 0x80);
writeReg(0x81,0x00);
writeReg(0xff,0x06);
writeReg(0x83, readReg(0x83) & ~0x04);
writeRegList(ucSPAD2);
return 1;
} /* getSpadInfo() */
// Decode sequence step timeout in MCLKs from register value
// based on VL53L0X_decode_timeout()
// Note: the original function returned a uint32_t, but the return value is
// always stored in a uint16_t.
static uint16_t decodeTimeout(uint16_t reg_val)
{
// format: "(LSByte * 2^MSByte) + 1"
return (uint16_t)((reg_val & 0x00FF) <<
(uint16_t)((reg_val & 0xFF00) >> 8)) + 1;
}
// Convert sequence step timeout from MCLKs to microseconds with given VCSEL period in PCLKs
// based on VL53L0X_calc_timeout_us()
static uint32_t timeoutMclksToMicroseconds(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks)
{
uint32_t macro_period_ns = calcMacroPeriod(vcsel_period_pclks);
return ((timeout_period_mclks * macro_period_ns) + (macro_period_ns / 2)) / 1000;
}
// Convert sequence step timeout from microseconds to MCLKs with given VCSEL period in PCLKs
// based on VL53L0X_calc_timeout_mclks()
static uint32_t timeoutMicrosecondsToMclks(uint32_t timeout_period_us, uint8_t vcsel_period_pclks)
{
uint32_t macro_period_ns = calcMacroPeriod(vcsel_period_pclks);
return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);
}
// Encode sequence step timeout register value from timeout in MCLKs
// based on VL53L0X_encode_timeout()
// Note: the original function took a uint16_t, but the argument passed to it
// is always a uint16_t.
static uint16_t encodeTimeout(uint16_t timeout_mclks)
{
// format: "(LSByte * 2^MSByte) + 1"
uint32_t ls_byte = 0;
uint16_t ms_byte = 0;
if (timeout_mclks > 0)
{
ls_byte = timeout_mclks - 1;
while ((ls_byte & 0xFFFFFF00) > 0)
{
ls_byte >>= 1;
ms_byte++;
}
return (ms_byte << 8) | (ls_byte & 0xFF);
}
else { return 0; }
}
static void getSequenceStepTimeouts(uint8_t enables, SequenceStepTimeouts * timeouts)
{
timeouts->pre_range_vcsel_period_pclks = ((readReg(PRE_RANGE_CONFIG_VCSEL_PERIOD) +1) << 1);
timeouts->msrc_dss_tcc_mclks = readReg(MSRC_CONFIG_TIMEOUT_MACROP) + 1;
timeouts->msrc_dss_tcc_us =
timeoutMclksToMicroseconds(timeouts->msrc_dss_tcc_mclks,
timeouts->pre_range_vcsel_period_pclks);
timeouts->pre_range_mclks =
decodeTimeout(readReg16(PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI));
timeouts->pre_range_us =
timeoutMclksToMicroseconds(timeouts->pre_range_mclks,
timeouts->pre_range_vcsel_period_pclks);
timeouts->final_range_vcsel_period_pclks = ((readReg(FINAL_RANGE_CONFIG_VCSEL_PERIOD) +1) << 1);
timeouts->final_range_mclks =
decodeTimeout(readReg16(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI));
if (enables & SEQUENCE_ENABLE_PRE_RANGE)
{
timeouts->final_range_mclks -= timeouts->pre_range_mclks;
}
timeouts->final_range_us =
timeoutMclksToMicroseconds(timeouts->final_range_mclks,
timeouts->final_range_vcsel_period_pclks);
} /* getSequenceStepTimeouts() */
// Set the VCSEL (vertical cavity surface emitting laser) pulse period for the
// given period type (pre-range or final range) to the given value in PCLKs.
// Longer periods seem to increase the potential range of the sensor.
// Valid values are (even numbers only):
// pre: 12 to 18 (initialized default: 14)
// final: 8 to 14 (initialized default: 10)
// based on VL53L0X_set_vcsel_pulse_period()
static int setVcselPulsePeriod(vcselPeriodType type, uint8_t period_pclks)
{
uint8_t vcsel_period_reg = encodeVcselPeriod(period_pclks);
uint8_t enables;
SequenceStepTimeouts timeouts;
enables = readReg(SYSTEM_SEQUENCE_CONFIG);
getSequenceStepTimeouts(enables, &timeouts);
// "Apply specific settings for the requested clock period"
// "Re-calculate and apply timeouts, in macro periods"
// "When the VCSEL period for the pre or final range is changed,
// the corresponding timeout must be read from the device using
// the current VCSEL period, then the new VCSEL period can be
// applied. The timeout then must be written back to the device
// using the new VCSEL period.
//
// For the MSRC timeout, the same applies - this timeout being
// dependant on the pre-range vcsel period."
if (type == VcselPeriodPreRange)
{
// "Set phase check limits"
switch (period_pclks)
{
case 12:
writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x18);
break;
case 14:
writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x30);
break;
case 16:
writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x40);
break;
case 18:
writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x50);
break;
default:
// invalid period
return 0;
}
writeReg(PRE_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);
// apply new VCSEL period
writeReg(PRE_RANGE_CONFIG_VCSEL_PERIOD, vcsel_period_reg);
// update timeouts
// set_sequence_step_timeout() begin
// (SequenceStepId == VL53L0X_SEQUENCESTEP_PRE_RANGE)
uint16_t new_pre_range_timeout_mclks =
timeoutMicrosecondsToMclks(timeouts.pre_range_us, period_pclks);
writeReg16(PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI,
encodeTimeout(new_pre_range_timeout_mclks));
// set_sequence_step_timeout() end
// set_sequence_step_timeout() begin
// (SequenceStepId == VL53L0X_SEQUENCESTEP_MSRC)
uint16_t new_msrc_timeout_mclks =
timeoutMicrosecondsToMclks(timeouts.msrc_dss_tcc_us, period_pclks);
writeReg(MSRC_CONFIG_TIMEOUT_MACROP,
(new_msrc_timeout_mclks > 256) ? 255 : (new_msrc_timeout_mclks - 1));
// set_sequence_step_timeout() end
}
else if (type == VcselPeriodFinalRange)
{
switch (period_pclks)
{
case 8:
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x10);
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);
writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x02);
writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x0C);
writeReg(0xFF, 0x01);
writeReg(ALGO_PHASECAL_LIM, 0x30);
writeReg(0xFF, 0x00);
break;
case 10:
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x28);
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);
writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x09);
writeReg(0xFF, 0x01);
writeReg(ALGO_PHASECAL_LIM, 0x20);
writeReg(0xFF, 0x00);
break;
case 12:
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x38);
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);
writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x08);
writeReg(0xFF, 0x01);
writeReg(ALGO_PHASECAL_LIM, 0x20);
writeReg(0xFF, 0x00);
break;
case 14:
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x48);
writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);
writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x07);
writeReg(0xFF, 0x01);
writeReg(ALGO_PHASECAL_LIM, 0x20);
writeReg(0xFF, 0x00);
break;
default:
// invalid period
return 0;
}
// apply new VCSEL period
writeReg(FINAL_RANGE_CONFIG_VCSEL_PERIOD, vcsel_period_reg);
// update timeouts
// set_sequence_step_timeout() begin
// (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)
// "For the final range timeout, the pre-range timeout
// must be added. To do this both final and pre-range
// timeouts must be expressed in macro periods MClks
// because they have different vcsel periods."
uint16_t new_final_range_timeout_mclks =
timeoutMicrosecondsToMclks(timeouts.final_range_us, period_pclks);
if (enables & SEQUENCE_ENABLE_PRE_RANGE)
{
new_final_range_timeout_mclks += timeouts.pre_range_mclks;
}
writeReg16(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI,
encodeTimeout(new_final_range_timeout_mclks));
// set_sequence_step_timeout end
}
else
{
// invalid type
return 0;
}
// "Finally, the timing budget must be re-applied"
setMeasurementTimingBudget(measurement_timing_budget_us);
// "Perform the phase calibration. This is needed after changing on vcsel period."
// VL53L0X_perform_phase_calibration() begin
uint8_t sequence_config = readReg(SYSTEM_SEQUENCE_CONFIG);
writeReg(SYSTEM_SEQUENCE_CONFIG, 0x02);
performSingleRefCalibration(0x0);
writeReg(SYSTEM_SEQUENCE_CONFIG, sequence_config);
// VL53L0X_perform_phase_calibration() end
return 1;
}
// Set the measurement timing budget in microseconds, which is the time allowed
// for one measurement; the ST API and this library take care of splitting the
// timing budget among the sub-steps in the ranging sequence. A longer timing
// budget allows for more accurate measurements. Increasing the budget by a
// factor of N decreases the range measurement standard deviation by a factor of
// sqrt(N). Defaults to about 33 milliseconds; the minimum is 20 ms.
// based on VL53L0X_set_measurement_timing_budget_micro_seconds()
static int setMeasurementTimingBudget(uint32_t budget_us)
{
uint32_t used_budget_us;
uint32_t final_range_timeout_us;
uint16_t final_range_timeout_mclks;
uint8_t enables;
SequenceStepTimeouts timeouts;
uint16_t const StartOverhead = 1320; // note that this is different than the value in get_
uint16_t const EndOverhead = 960;
uint16_t const MsrcOverhead = 660;
uint16_t const TccOverhead = 590;
uint16_t const DssOverhead = 690;
uint16_t const PreRangeOverhead = 660;
uint16_t const FinalRangeOverhead = 550;
uint32_t const MinTimingBudget = 20000;
if (budget_us < MinTimingBudget) { return 0; }
used_budget_us = StartOverhead + EndOverhead;
enables = readReg(SYSTEM_SEQUENCE_CONFIG);
getSequenceStepTimeouts(enables, &timeouts);
if (enables & SEQUENCE_ENABLE_TCC)
{
used_budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
}
if (enables & SEQUENCE_ENABLE_DSS)
{
used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
}
else if (enables & SEQUENCE_ENABLE_MSRC)
{
used_budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
}
if (enables & SEQUENCE_ENABLE_PRE_RANGE)
{
used_budget_us += (timeouts.pre_range_us + PreRangeOverhead);
}
if (enables & SEQUENCE_ENABLE_FINAL_RANGE)
{
used_budget_us += FinalRangeOverhead;
// "Note that the final range timeout is determined by the timing
// budget and the sum of all other timeouts within the sequence.
// If there is no room for the final range timeout, then an error
// will be set. Otherwise the remaining time will be applied to
// the final range."
if (used_budget_us > budget_us)
{
// "Requested timeout too big."
return 0;
}
final_range_timeout_us = budget_us - used_budget_us;
// set_sequence_step_timeout() begin
// (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)
// "For the final range timeout, the pre-range timeout
// must be added. To do this both final and pre-range
// timeouts must be expressed in macro periods MClks
// because they have different vcsel periods."
final_range_timeout_mclks =
timeoutMicrosecondsToMclks(final_range_timeout_us,
timeouts.final_range_vcsel_period_pclks);
if (enables & SEQUENCE_ENABLE_PRE_RANGE)
{
final_range_timeout_mclks += timeouts.pre_range_mclks;
}
writeReg16(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI,
encodeTimeout(final_range_timeout_mclks));
// set_sequence_step_timeout() end
measurement_timing_budget_us = budget_us; // store for internal reuse
}
return 1;
}
static uint32_t getMeasurementTimingBudget(void)
{
uint8_t enables;
SequenceStepTimeouts timeouts;
uint16_t const StartOverhead = 1910; // note that this is different than the value in set_
uint16_t const EndOverhead = 960;
uint16_t const MsrcOverhead = 660;
uint16_t const TccOverhead = 590;
uint16_t const DssOverhead = 690;
uint16_t const PreRangeOverhead = 660;
uint16_t const FinalRangeOverhead = 550;
// "Start and end overhead times always present"
uint32_t budget_us = StartOverhead + EndOverhead;
enables = readReg(SYSTEM_SEQUENCE_CONFIG);
getSequenceStepTimeouts(enables, &timeouts);
if (enables & SEQUENCE_ENABLE_TCC)
{
budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
}
if (enables & SEQUENCE_ENABLE_DSS)
{
budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
}
else if (enables & SEQUENCE_ENABLE_MSRC)
{
budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
}
if (enables & SEQUENCE_ENABLE_PRE_RANGE)
{
budget_us += (timeouts.pre_range_us + PreRangeOverhead);
}
if (enables & SEQUENCE_ENABLE_FINAL_RANGE)
{
budget_us += (timeouts.final_range_us + FinalRangeOverhead);
}
measurement_timing_budget_us = budget_us; // store for internal reuse
return budget_us;
}
static int performSingleRefCalibration(uint8_t vhv_init_byte)
{
int iTimeout;
writeReg(SYSRANGE_START, 0x01 | vhv_init_byte); // VL53L0X_REG_SYSRANGE_MODE_START_STOP
iTimeout = 0;
while ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0)
{
iTimeout++;
usleep(5000);
if (iTimeout > 100) { return 0; }
}
writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);
writeReg(SYSRANGE_START, 0x00);
return 1;
} /* performSingleRefCalibration() */
//
// Initialize the vl53l0x
//
static int initSensor(int bLongRangeMode)
{
unsigned char spad_count=0, spad_type_is_aperture=0, ref_spad_map[6];
unsigned char ucFirstSPAD, ucSPADsEnabled;
int i;
// set 2.8V mode
writeReg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
readReg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01); // set bit 0
// Set I2C standard mode
writeRegList(ucI2CMode);
stop_variable = readReg(0x91);
writeRegList(ucI2CMode2);
// disable SIGNAL_RATE_MSRC (bit 1) and SIGNAL_RATE_PRE_RANGE (bit 4) limit checks
writeReg(REG_MSRC_CONFIG_CONTROL, readReg(REG_MSRC_CONFIG_CONTROL) | 0x12);
// Q9.7 fixed point format (9 integer bits, 7 fractional bits)
writeReg16(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, 32); // 0.25
writeReg(SYSTEM_SEQUENCE_CONFIG, 0xFF);
getSpadInfo(&spad_count, &spad_type_is_aperture);
readMulti(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
//printf("initial spad map: %02x,%02x,%02x,%02x,%02x,%02x\n", ref_spad_map[0], ref_spad_map[1], ref_spad_map[2], ref_spad_map[3], ref_spad_map[4], ref_spad_map[5]);
writeRegList(ucSPAD);
ucFirstSPAD = (spad_type_is_aperture) ? 12: 0;
ucSPADsEnabled = 0;
// clear bits for unused SPADs
for (i=0; i<48; i++)
{
if (i < ucFirstSPAD || ucSPADsEnabled == spad_count)
{
ref_spad_map[i>>3] &= ~(1<<(i & 7));
}
else if (ref_spad_map[i>>3] & (1<< (i & 7)))
{
ucSPADsEnabled++;
}
} // for i
writeMulti(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
//printf("final spad map: %02x,%02x,%02x,%02x,%02x,%02x\n", ref_spad_map[0],
//ref_spad_map[1], ref_spad_map[2], ref_spad_map[3], ref_spad_map[4], ref_spad_map[5]);
// load default tuning settings
writeRegList(ucDefTuning); // long list of magic numbers
// change some settings for long range mode
if (bLongRangeMode)
{
writeReg16(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, 13); // 0.1
setVcselPulsePeriod(VcselPeriodPreRange, 18);
setVcselPulsePeriod(VcselPeriodFinalRange, 14);
}
// set interrupt configuration to "new sample ready"
writeReg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04);
writeReg(GPIO_HV_MUX_ACTIVE_HIGH, readReg(GPIO_HV_MUX_ACTIVE_HIGH) & ~0x10); // active low
writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);
measurement_timing_budget_us = getMeasurementTimingBudget();
writeReg(SYSTEM_SEQUENCE_CONFIG, 0xe8);
setMeasurementTimingBudget(measurement_timing_budget_us);
writeReg(SYSTEM_SEQUENCE_CONFIG, 0x01);
if (!performSingleRefCalibration(0x40)) { return 0; }
writeReg(SYSTEM_SEQUENCE_CONFIG, 0x02);
if (!performSingleRefCalibration(0x00)) { return 0; }
writeReg(SYSTEM_SEQUENCE_CONFIG, 0xe8);
return 1;
} /* initSensor() */
uint16_t readRangeContinuousMillimeters(void)
{
int iTimeout = 0;
uint16_t range;
while ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0)
{
iTimeout++;
usleep(5000);
if (iTimeout > 50)
{
return -1;
}
}
// assumptions: Linearity Corrective Gain is 1000 (default);
// fractional ranging is not enabled
range = readReg16(RESULT_RANGE_STATUS + 10);
writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);
return range;
}
//
// Read the current distance in mm
//
int tofReadDistance(void)
{
int iTimeout;
writeReg(0x80, 0x01);
writeReg(0xFF, 0x01);
writeReg(0x00, 0x00);
writeReg(0x91, stop_variable);
writeReg(0x00, 0x01);
writeReg(0xFF, 0x00);
writeReg(0x80, 0x00);
writeReg(SYSRANGE_START, 0x01);
// "Wait until start bit has been cleared"
iTimeout = 0;
while (readReg(SYSRANGE_START) & 0x01)
{
iTimeout++;
usleep(5000);
if (iTimeout > 50)
{
return -1;
}
}
return readRangeContinuousMillimeters();
} /* tofReadDistance() */
int tofGetModel(int *model, int *revision)
{
unsigned char ucTemp[2];
int i;
#ifndef PICO_BOARD
if (file_i2c == -1)
return 0;
#endif
if (model)
{
#ifndef PICO_BOARD
ucTemp[0] = REG_IDENTIFICATION_MODEL_ID;
i = write(file_i2c, ucTemp, 1); // write address of register to read
i = read(file_i2c, ucTemp, 1);
if (i == 1)
*model = ucTemp[0];
#else
*model = readReg(REG_IDENTIFICATION_MODEL_ID);
#endif
}
if (revision)
{
#ifndef PICO_BOARD
ucTemp[0] = REG_IDENTIFICATION_REVISION_ID;
i = write(file_i2c, ucTemp, 1);
i = read(file_i2c, ucTemp, 1);
if (i == 1)
*revision = ucTemp[0];
#else
*revision = readReg(REG_IDENTIFICATION_REVISION_ID);
#endif
}
return 1;
} /* tofGetModel() */