Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
vl53l1x_api.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2017, STMicroelectronics - All Rights Reserved
3 
4  This file : part of VL53L1 Core and : dual licensed,
5  either 'STMicroelectronics
6  Proprietary license'
7  or 'BSD 3-clause "New" or "Revised" License' , at your option.
8 
9 *******************************************************************************
10 
11  'STMicroelectronics Proprietary license'
12 
13 *******************************************************************************
14 
15  License terms: STMicroelectronics Proprietary in accordance with licensing
16  terms at www.st.com/sla0081
17 
18  STMicroelectronics confidential
19  Reproduction and Communication of this document : strictly prohibited unless
20  specifically authorized in writing by STMicroelectronics.
21 
22 
23 *******************************************************************************
24 
25  Alternatively, VL53L1 Core may be distributed under the terms of
26  'BSD 3-clause "New" or "Revised" License', in which case the following
27  provisions apply instead of the ones mentioned above :
28 
29 *******************************************************************************
30 
31  License terms: BSD 3-clause "New" or "Revised" License.
32 
33  Redistribution and use in source and binary forms, with or without
34  modification, are permitted provided that the following conditions are met:
35 
36  1. Redistributions of source code must retain the above copyright notice, this
37  list of conditions and the following disclaimer.
38 
39  2. Redistributions in binary form must reproduce the above copyright notice,
40  this list of conditions and the following disclaimer in the documentation
41  and/or other materials provided with the distribution.
42 
43  3. Neither the name of the copyright holder nor the names of its contributors
44  may be used to endorse or promote products derived from this software
45  without specific prior written permission.
46 
47  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *******************************************************************************
58 */
59 
65 #include "vl53l1x_api.h"
66 #include <string.h>
67 
68 #if 0
69 uint8_t VL51L1X_NVM_CONFIGURATION[] = {
70  0x00, /* 0x00 : not user-modifiable */
71  0x29, /* 0x01 : 7 bits I2C address (default=0x29), use SetI2CAddress(). Warning: after changing the register value to a new I2C address, the device will only answer to the new address */
72  0x00, /* 0x02 : not user-modifiable */
73  0x00, /* 0x03 : not user-modifiable */
74  0x00, /* 0x04 : not user-modifiable */
75  0x00, /* 0x05 : not user-modifiable */
76  0x00, /* 0x06 : not user-modifiable */
77  0x00, /* 0x07 : not user-modifiable */
78  0x00, /* 0x08 : not user-modifiable */
79  0x50, /* 0x09 : not user-modifiable */
80  0x00, /* 0x0A : not user-modifiable */
81  0x00, /* 0x0B : not user-modifiable */
82  0x00, /* 0x0C : not user-modifiable */
83  0x00, /* 0x0D : not user-modifiable */
84  0x0a, /* 0x0E : not user-modifiable */
85  0x00, /* 0x0F : not user-modifiable */
86  0x00, /* 0x10 : not user-modifiable */
87  0x00, /* 0x11 : not user-modifiable */
88  0x00, /* 0x12 : not user-modifiable */
89  0x00, /* 0x13 : not user-modifiable */
90  0x00, /* 0x14 : not user-modifiable */
91  0x00, /* 0x15 : not user-modifiable */
92  0x00, /* 0x16 : Xtalk calibration value MSB (7.9 format in kcps), use SetXtalk() */
93  0x00, /* 0x17 : Xtalk calibration value LSB */
94  0x00, /* 0x18 : not user-modifiable */
95  0x00, /* 0x19 : not user-modifiable */
96  0x00, /* 0x1a : not user-modifiable */
97  0x00, /* 0x1b : not user-modifiable */
98  0x00, /* 0x1e : Part to Part offset x4 MSB (in mm), use SetOffset() */
99  0x50, /* 0x1f : Part to Part offset x4 LSB */
100  0x00, /* 0x20 : not user-modifiable */
101  0x00, /* 0x21 : not user-modifiable */
102  0x00, /* 0x22 : not user-modifiable */
103  0x00, /* 0x23 : not user-modifiable */
104 }
105 #endif
106 
108  0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */
109  0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
110  0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
111  0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */
112  0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */
113  0x00, /* 0x32 : not user-modifiable */
114  0x02, /* 0x33 : not user-modifiable */
115  0x08, /* 0x34 : not user-modifiable */
116  0x00, /* 0x35 : not user-modifiable */
117  0x08, /* 0x36 : not user-modifiable */
118  0x10, /* 0x37 : not user-modifiable */
119  0x01, /* 0x38 : not user-modifiable */
120  0x01, /* 0x39 : not user-modifiable */
121  0x00, /* 0x3a : not user-modifiable */
122  0x00, /* 0x3b : not user-modifiable */
123  0x00, /* 0x3c : not user-modifiable */
124  0x00, /* 0x3d : not user-modifiable */
125  0xff, /* 0x3e : not user-modifiable */
126  0x00, /* 0x3f : not user-modifiable */
127  0x0F, /* 0x40 : not user-modifiable */
128  0x00, /* 0x41 : not user-modifiable */
129  0x00, /* 0x42 : not user-modifiable */
130  0x00, /* 0x43 : not user-modifiable */
131  0x00, /* 0x44 : not user-modifiable */
132  0x00, /* 0x45 : not user-modifiable */
133  0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */
134  0x0b, /* 0x47 : not user-modifiable */
135  0x00, /* 0x48 : not user-modifiable */
136  0x00, /* 0x49 : not user-modifiable */
137  0x02, /* 0x4a : not user-modifiable */
138  0x0a, /* 0x4b : not user-modifiable */
139  0x21, /* 0x4c : not user-modifiable */
140  0x00, /* 0x4d : not user-modifiable */
141  0x00, /* 0x4e : not user-modifiable */
142  0x05, /* 0x4f : not user-modifiable */
143  0x00, /* 0x50 : not user-modifiable */
144  0x00, /* 0x51 : not user-modifiable */
145  0x00, /* 0x52 : not user-modifiable */
146  0x00, /* 0x53 : not user-modifiable */
147  0xc8, /* 0x54 : not user-modifiable */
148  0x00, /* 0x55 : not user-modifiable */
149  0x00, /* 0x56 : not user-modifiable */
150  0x38, /* 0x57 : not user-modifiable */
151  0xff, /* 0x58 : not user-modifiable */
152  0x01, /* 0x59 : not user-modifiable */
153  0x00, /* 0x5a : not user-modifiable */
154  0x08, /* 0x5b : not user-modifiable */
155  0x00, /* 0x5c : not user-modifiable */
156  0x00, /* 0x5d : not user-modifiable */
157  0x01, /* 0x5e : not user-modifiable */
158  0xcc, /* 0x5f : not user-modifiable */
159  0x0f, /* 0x60 : not user-modifiable */
160  0x01, /* 0x61 : not user-modifiable */
161  0xf1, /* 0x62 : not user-modifiable */
162  0x0d, /* 0x63 : not user-modifiable */
163  0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */
164  0x68, /* 0x65 : Sigma threshold LSB */
165  0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */
166  0x80, /* 0x67 : Min count Rate LSB */
167  0x08, /* 0x68 : not user-modifiable */
168  0xb8, /* 0x69 : not user-modifiable */
169  0x00, /* 0x6a : not user-modifiable */
170  0x00, /* 0x6b : not user-modifiable */
171  0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */
172  0x00, /* 0x6d : Intermeasurement period */
173  0x0f, /* 0x6e : Intermeasurement period */
174  0x89, /* 0x6f : Intermeasurement period LSB */
175  0x00, /* 0x70 : not user-modifiable */
176  0x00, /* 0x71 : not user-modifiable */
177  0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */
178  0x00, /* 0x73 : distance threshold high LSB */
179  0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */
180  0x00, /* 0x75 : distance threshold low LSB */
181  0x00, /* 0x76 : not user-modifiable */
182  0x01, /* 0x77 : not user-modifiable */
183  0x0f, /* 0x78 : not user-modifiable */
184  0x0d, /* 0x79 : not user-modifiable */
185  0x0e, /* 0x7a : not user-modifiable */
186  0x0e, /* 0x7b : not user-modifiable */
187  0x00, /* 0x7c : not user-modifiable */
188  0x00, /* 0x7d : not user-modifiable */
189  0x02, /* 0x7e : not user-modifiable */
190  0xc7, /* 0x7f : ROI center, use SetROI() */
191  0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */
192  0x9B, /* 0x81 : not user-modifiable */
193  0x00, /* 0x82 : not user-modifiable */
194  0x00, /* 0x83 : not user-modifiable */
195  0x00, /* 0x84 : not user-modifiable */
196  0x01, /* 0x85 : not user-modifiable */
197  0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */
198  0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
199 };
200 
201 static const uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, 0,
202  255, 255, 9, 13, 255, 255, 255, 255, 10, 6,
203  255, 255, 11, 12
204  };
205 
206 #if VL53L1X_AUTO_INCR_ADDR
207 // start after default address
208 static uint8_t auto_incr_addr = VL53L1_DEFAULT_ADDRESS + 2;
209 #endif
210 
212 {
213  VL53L1X_ERROR Status = 0;
214 
219  return Status;
220 }
221 
223 {
224  VL53L1X_ERROR status = 0;
225 
226  status = VL53L1_WrByte(dev, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
227  if (!status) { dev->i2c_trans.slave_addr = new_address; }
228  return status;
229 }
230 
232 {
233  VL53L1X_ERROR status = 0;
234  uint8_t Addr = 0x00, tmp;
235 
236  for (Addr = 0x2D; Addr <= 0x87; Addr++) {
237  status = VL53L1_WrByte(dev, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
238  }
239  status = VL53L1X_StartRanging(dev);
240  tmp = 0;
241  while (tmp == 0) {
242  status = VL53L1X_CheckForDataReady(dev, &tmp);
243  }
244  status = VL53L1X_ClearInterrupt(dev);
245  status = VL53L1X_StopRanging(dev);
246  status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
247  status = VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
248 #if VL53L1X_AUTO_INCR_ADDR
249  status = VL53L1X_SetI2CAddress(dev, auto_incr_addr);
250  auto_incr_addr += 2; // auto increment by 2 (+1 on 7 bits address)
251 #endif
252  return status;
253 }
254 
255 void VL53L1X_BootDevice(VL53L1_DEV dev, uint16_t TimingBudgetInMs, uint16_t DistanceMode, uint32_t InterMeasurementInMs)
256 {
257  uint8_t state;
258  do {
259  VL53L1X_BootState(dev, &state);
260  } while (!state);
261  VL53L1X_SensorInit(dev);
262  /* Configure sensor */
263  VL53L1X_SetTimingBudgetInMs(dev, TimingBudgetInMs);
264  VL53L1X_SetDistanceMode(dev, DistanceMode);
265  VL53L1X_SetInterMeasurementInMs(dev, InterMeasurementInMs);
266  /* Start measurement */
268 }
269 
270 
272 {
273  VL53L1X_ERROR status = 0;
274 
275  status = VL53L1_WrByte(dev, SYSTEM__INTERRUPT_CLEAR, 0x01);
276  return status;
277 }
278 
280 {
281  uint8_t Temp;
282  VL53L1X_ERROR status = 0;
283 
284  status = VL53L1_RdByte(dev, GPIO_HV_MUX__CTRL, &Temp);
285  Temp = Temp & 0xEF;
286  status = VL53L1_WrByte(dev, GPIO_HV_MUX__CTRL, Temp | (!(NewPolarity & 1)) << 4);
287  return status;
288 }
289 
291 {
292  uint8_t Temp;
293  VL53L1X_ERROR status = 0;
294 
295  status = VL53L1_RdByte(dev, GPIO_HV_MUX__CTRL, &Temp);
296  Temp = Temp & 0x10;
297  *pInterruptPolarity = !(Temp >> 4);
298  return status;
299 }
300 
302 {
303  VL53L1X_ERROR status = 0;
304 
305  status = VL53L1_WrByte(dev, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
306  return status;
307 }
308 
310 {
311  VL53L1X_ERROR status = 0;
312 
313  status = VL53L1_WrByte(dev, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
314  return status;
315 }
316 
318 {
319  uint8_t Temp;
320  uint8_t IntPol;
321  VL53L1X_ERROR status = 0;
322 
323  status = VL53L1X_GetInterruptPolarity(dev, &IntPol);
324  status = VL53L1_RdByte(dev, GPIO__TIO_HV_STATUS, &Temp);
325  /* Read in the register to check if a new value is available */
326  if (status == 0) {
327  if ((Temp & 1) == IntPol) {
328  *isDataReady = 1;
329  } else {
330  *isDataReady = 0;
331  }
332  }
333  return status;
334 }
335 
337 {
338  uint16_t DM;
339  VL53L1X_ERROR status = 0;
340 
341  status = VL53L1X_GetDistanceMode(dev, &DM);
342  if (DM == 0) {
343  return 1;
344  } else if (DM == 1) { /* Short DistanceMode */
345  switch (TimingBudgetInMs) {
346  case 15: /* only available in short distance mode */
348  0x01D);
350  0x0027);
351  break;
352  case 20:
354  0x0051);
356  0x006E);
357  break;
358  case 33:
360  0x00D6);
362  0x006E);
363  break;
364  case 50:
366  0x1AE);
368  0x01E8);
369  break;
370  case 100:
372  0x02E1);
374  0x0388);
375  break;
376  case 200:
378  0x03E1);
380  0x0496);
381  break;
382  case 500:
384  0x0591);
386  0x05C1);
387  break;
388  default:
389  status = 1;
390  break;
391  }
392  } else {
393  switch (TimingBudgetInMs) {
394  case 20:
396  0x001E);
398  0x0022);
399  break;
400  case 33:
402  0x0060);
404  0x006E);
405  break;
406  case 50:
408  0x00AD);
410  0x00C6);
411  break;
412  case 100:
414  0x01CC);
416  0x01EA);
417  break;
418  case 200:
420  0x02D9);
422  0x02F8);
423  break;
424  case 500:
426  0x048F);
428  0x04A4);
429  break;
430  default:
431  status = 1;
432  break;
433  }
434  }
435  return status;
436 }
437 
439 {
440  uint16_t Temp;
441  VL53L1X_ERROR status = 0;
442 
443  status = VL53L1_RdWord(dev, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp);
444  switch (Temp) {
445  case 0x001D :
446  *pTimingBudget = 15;
447  break;
448  case 0x0051 :
449  case 0x001E :
450  *pTimingBudget = 20;
451  break;
452  case 0x00D6 :
453  case 0x0060 :
454  *pTimingBudget = 33;
455  break;
456  case 0x1AE :
457  case 0x00AD :
458  *pTimingBudget = 50;
459  break;
460  case 0x02E1 :
461  case 0x01CC :
462  *pTimingBudget = 100;
463  break;
464  case 0x03E1 :
465  case 0x02D9 :
466  *pTimingBudget = 200;
467  break;
468  case 0x0591 :
469  case 0x048F :
470  *pTimingBudget = 500;
471  break;
472  default:
473  status = 1;
474  *pTimingBudget = 0;
475  }
476  return status;
477 }
478 
480 {
481  uint16_t TB;
482  VL53L1X_ERROR status = 0;
483 
484  status = VL53L1X_GetTimingBudgetInMs(dev, &TB);
485  if (status != 0) {
486  return 1;
487  }
488  switch (DM) {
489  case 1:
490  status = VL53L1_WrByte(dev, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14);
491  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
492  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
493  status = VL53L1_WrByte(dev, RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);
494  status = VL53L1_WrWord(dev, SD_CONFIG__WOI_SD0, 0x0705);
495  status = VL53L1_WrWord(dev, SD_CONFIG__INITIAL_PHASE_SD0, 0x0606);
496  break;
497  case 2:
498  status = VL53L1_WrByte(dev, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x0A);
499  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
500  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
501  status = VL53L1_WrByte(dev, RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);
502  status = VL53L1_WrWord(dev, SD_CONFIG__WOI_SD0, 0x0F0D);
503  status = VL53L1_WrWord(dev, SD_CONFIG__INITIAL_PHASE_SD0, 0x0E0E);
504  break;
505  default:
506  status = 1;
507  break;
508  }
509 
510  if (status == 0) {
511  status = VL53L1X_SetTimingBudgetInMs(dev, TB);
512  }
513  return status;
514 }
515 
517 {
518  uint8_t TempDM, status = 0;
519 
520  status = VL53L1_RdByte(dev, PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
521  if (TempDM == 0x14) {
522  *DM = 1;
523  }
524  if (TempDM == 0x0A) {
525  *DM = 2;
526  }
527  return status;
528 }
529 
531 {
532  uint16_t ClockPLL;
533  VL53L1X_ERROR status = 0;
534 
535  status = VL53L1_RdWord(dev, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
536  ClockPLL = ClockPLL & 0x3FF;
538  (uint32_t)(ClockPLL * InterMeasMs * 1.075));
539  return status;
540 
541 }
542 
544 {
545  uint16_t ClockPLL;
546  VL53L1X_ERROR status = 0;
547  uint32_t tmp;
548 
550  *pIM = (uint16_t)tmp;
551  status = VL53L1_RdWord(dev, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
552  ClockPLL = ClockPLL & 0x3FF;
553  *pIM = (uint16_t)(*pIM / (ClockPLL * 1.065));
554  return status;
555 }
556 
558 {
559  VL53L1X_ERROR status = 0;
560  uint8_t tmp = 0;
561 
562  status = VL53L1_RdByte(dev, VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
563  *state = tmp;
564  return status;
565 }
566 
568 {
569  VL53L1X_ERROR status = 0;
570  uint16_t tmp = 0;
571 
572  status = VL53L1_RdWord(dev, VL53L1_IDENTIFICATION__MODEL_ID, &tmp);
573  *sensorId = tmp;
574  return status;
575 }
576 
578 {
579  VL53L1X_ERROR status = 0;
580  uint16_t tmp;
581 
582  status = (VL53L1_RdWord(dev,
584  *distance = tmp;
585  return status;
586 }
587 
589 {
590  VL53L1X_ERROR status = 0;
591  uint16_t SpNb = 1, signal;
592 
593  status = VL53L1_RdWord(dev,
595  status = VL53L1_RdWord(dev,
597  *signalRate = (uint16_t)(2000.0 * signal / SpNb);
598  return status;
599 }
600 
602 {
603  VL53L1X_ERROR status = 0;
604  uint16_t AmbientRate, SpNb = 1;
605 
606  status = VL53L1_RdWord(dev, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate);
608  *ambPerSp = (uint16_t)(2000.0 * AmbientRate / SpNb);
609  return status;
610 }
611 
613 {
614  VL53L1X_ERROR status = 0;
615  uint16_t tmp;
616 
617  status = VL53L1_RdWord(dev,
619  *signal = tmp * 8;
620  return status;
621 }
622 
624 {
625  VL53L1X_ERROR status = 0;
626  uint16_t tmp;
627 
628  status = VL53L1_RdWord(dev,
630  *spNb = tmp >> 8;
631  return status;
632 }
633 
635 {
636  VL53L1X_ERROR status = 0;
637  uint16_t tmp;
638 
640  *ambRate = tmp * 8;
641  return status;
642 }
643 
645 {
646  VL53L1X_ERROR status = 0;
647  uint8_t RgSt;
648 
649  *rangeStatus = 255;
650  status = VL53L1_RdByte(dev, VL53L1_RESULT__RANGE_STATUS, &RgSt);
651  RgSt = RgSt & 0x1F;
652  if (RgSt < 24) {
653  *rangeStatus = status_rtn[RgSt];
654  }
655  return status;
656 }
657 
659 {
660  VL53L1X_ERROR status = 0;
661  uint8_t Temp[17];
662  uint8_t RgSt = 255;
663 
664  status = VL53L1_ReadMulti(dev, VL53L1_RESULT__RANGE_STATUS, Temp, 17);
665  RgSt = Temp[0] & 0x1F;
666  if (RgSt < 24) {
667  RgSt = status_rtn[RgSt];
668  }
669  pResult->Status = RgSt;
670  pResult->Ambient = (Temp[7] << 8 | Temp[8]) * 8;
671  pResult->NumSPADs = Temp[3];
672  pResult->SigPerSPAD = (Temp[15] << 8 | Temp[16]) * 8;
673  pResult->Distance = Temp[13] << 8 | Temp[14];
674 
675  return status;
676 }
677 
679 {
680  VL53L1X_ERROR status = 0;
681  int16_t Temp;
682 
683  Temp = (OffsetValue * 4);
685  (uint16_t)Temp);
688  return status;
689 }
690 
692 {
693  VL53L1X_ERROR status = 0;
694  uint16_t Temp;
695 
697  Temp = Temp << 3;
698  Temp = Temp >> 5;
699  *offset = (int16_t)(Temp);
700  return status;
701 }
702 
704 {
705  /* XTalkValue in count per second to avoid float type */
706  VL53L1X_ERROR status = 0;
707 
708  status = VL53L1_WrWord(dev,
710  0x0000);
712  0x0000);
714  (XtalkValue << 9) / 1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
715  return status;
716 }
717 
719 {
720  VL53L1X_ERROR status = 0;
721  uint32_t tmp;
722 
724  *xtalk = (uint16_t)(tmp * 1000) >> 9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
725  return status;
726 }
727 
729  uint16_t ThreshHigh, uint8_t Window,
730  uint8_t IntOnNoTarget)
731 {
732  VL53L1X_ERROR status = 0;
733  uint8_t Temp = 0;
734 
735  status = VL53L1_RdByte(dev, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp);
736  Temp = Temp & 0x47;
737  if (IntOnNoTarget == 0) {
739  (Temp | (Window & 0x07)));
740  } else {
742  ((Temp | (Window & 0x07)) | 0x40));
743  }
744  status = VL53L1_WrWord(dev, SYSTEM__THRESH_HIGH, ThreshHigh);
745  status = VL53L1_WrWord(dev, SYSTEM__THRESH_LOW, ThreshLow);
746  return status;
747 }
748 
750 {
751  VL53L1X_ERROR status = 0;
752  uint8_t tmp;
753  status = VL53L1_RdByte(dev, SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
754  *window = (uint16_t)(tmp & 0x7);
755  return status;
756 }
757 
759 {
760  VL53L1X_ERROR status = 0;
761  uint16_t tmp;
762 
763  status = VL53L1_RdWord(dev, SYSTEM__THRESH_LOW, &tmp);
764  *low = tmp;
765  return status;
766 }
767 
769 {
770  VL53L1X_ERROR status = 0;
771  uint16_t tmp;
772 
773  status = VL53L1_RdWord(dev, SYSTEM__THRESH_HIGH, &tmp);
774  *high = tmp;
775  return status;
776 }
777 
779 {
780  VL53L1X_ERROR status = 0;
781  status = VL53L1_WrByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, ROICenter);
782  return status;
783 }
784 
786 {
787  VL53L1X_ERROR status = 0;
788  uint8_t tmp;
789  status = VL53L1_RdByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, &tmp);
790  *ROICenter = tmp;
791  return status;
792 }
793 
795 {
796  uint8_t OpticalCenter;
797  VL53L1X_ERROR status = 0;
798 
799  status = VL53L1_RdByte(dev, VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD, &OpticalCenter);
800  if (X > 16) {
801  X = 16;
802  }
803  if (Y > 16) {
804  Y = 16;
805  }
806  if (X > 10 || Y > 10) {
807  OpticalCenter = 199;
808  }
809  status = VL53L1_WrByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, OpticalCenter);
811  (Y - 1) << 4 | (X - 1));
812  return status;
813 }
814 
816 {
817  VL53L1X_ERROR status = 0;
818  uint8_t tmp;
819 
821  *ROI_X = ((uint16_t)tmp & 0x0F) + 1;
822  *ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1;
823  return status;
824 }
825 
827 {
828  VL53L1X_ERROR status = 0;
829 
831  return status;
832 }
833 
835 {
836  VL53L1X_ERROR status = 0;
837  uint16_t tmp;
838 
839  status = VL53L1_RdWord(dev,
841  *signal = tmp << 3;
842  return status;
843 }
844 
846 {
847  VL53L1X_ERROR status = 0;
848 
849  if (Sigma > (0xFFFF >> 2)) {
850  return 1;
851  }
852  /* 16 bits register 14.2 format */
853  status = VL53L1_WrWord(dev, RANGE_CONFIG__SIGMA_THRESH, Sigma << 2);
854  return status;
855 }
856 
858 {
859  VL53L1X_ERROR status = 0;
860  uint16_t tmp;
861 
862  status = VL53L1_RdWord(dev, RANGE_CONFIG__SIGMA_THRESH, &tmp);
863  *sigma = tmp >> 2;
864  return status;
865 
866 }
867 
869 {
870  VL53L1X_ERROR status = 0;
871  uint8_t tmp = 0;
872 
873  status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x81); /* full VHV */
874  status = VL53L1_WrByte(dev, 0x0B, 0x92);
875  status = VL53L1X_StartRanging(dev);
876  while (tmp == 0) {
877  status = VL53L1X_CheckForDataReady(dev, &tmp);
878  }
879  tmp = 0;
880  status = VL53L1X_ClearInterrupt(dev);
881  status = VL53L1X_StopRanging(dev);
882  status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
883  status = VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
884  return status;
885 }
#define RESULT__AMBIENT_COUNT_RATE_MCPS_SD
Definition: vl53l1x_api.h:114
VL53L1X_ERROR VL53L1X_SetI2CAddress(VL53L1_DEV dev, uint8_t new_address)
This function sets the sensor I2C address used in case multiple devices application, default address 0x52.
Definition: vl53l1x_api.c:222
unsigned short uint16_t
Definition: types.h:16
VL53L1X_ERROR VL53L1X_GetROI_XY(VL53L1_DEV dev, uint16_t *ROI_X, uint16_t *ROI_Y)
This function returns width X and height Y.
Definition: vl53l1x_api.c:815
VL53L1X_ERROR VL53L1X_GetSignalPerSpad(VL53L1_DEV dev, uint16_t *signalRate)
This function returns the returned signal per SPAD in kcps/SPAD.
Definition: vl53l1x_api.c:588
uint16_t Distance
Definition: vl53l1x_api.h:141
VL53L1X_ERROR VL53L1X_SetSigmaThreshold(VL53L1_DEV dev, uint16_t Sigma)
This function programs a new sigma threshold in mm (default=15 mm)
Definition: vl53l1x_api.c:845
VL53L1X_ERROR VL53L1X_GetDistance(VL53L1_DEV dev, uint16_t *distance)
This function returns the distance measured by the sensor in mm.
Definition: vl53l1x_api.c:577
#define SYSTEM__INTERRUPT_CLEAR
Definition: vl53l1x_api.h:110
VL53L1X_ERROR VL53L1X_CheckForDataReady(VL53L1_DEV dev, uint8_t *isDataReady)
This function checks if the new ranging data is available by polling the dedicated register...
Definition: vl53l1x_api.c:317
int8_t VL53L1_WrByte(VL53L1_DEV dev, uint16_t index, uint8_t data)
VL53L1_WrByte() definition.
uint16_t Ambient
Definition: vl53l1x_api.h:142
#define RANGE_CONFIG__SIGMA_THRESH
Definition: vl53l1x_api.h:98
const uint8_t VL51L1X_DEFAULT_CONFIGURATION[]
Definition: vl53l1x_api.c:107
#define GPIO_HV_MUX__CTRL
Definition: vl53l1x_api.h:89
VL53L1X_ERROR VL53L1X_GetAmbientPerSpad(VL53L1_DEV dev, uint16_t *ambPerSp)
This function returns the ambient per SPAD in kcps/SPAD.
Definition: vl53l1x_api.c:601
#define SYSTEM__MODE_START
Definition: vl53l1x_api.h:111
#define VL53L1_FIRMWARE__SYSTEM_STATUS
Definition: vl53l1x_api.h:118
VL53L1X_ERROR VL53L1X_GetSWVersion(VL53L1X_Version_t *pVersion)
This function returns the SW driver version.
Definition: vl53l1x_api.c:211
#define VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND
Definition: vl53l1x_api.h:82
VL53L1X_ERROR VL53L1X_GetRangeStatus(VL53L1_DEV dev, uint8_t *rangeStatus)
This function returns the ranging status error (0:no error, 1:sigma failed, 2:signal failed...
Definition: vl53l1x_api.c:644
#define VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD
Definition: vl53l1x_api.h:120
VL53L1X_ERROR VL53L1X_StartRanging(VL53L1_DEV dev)
This function starts the ranging distance operation The ranging operation is continuous.
Definition: vl53l1x_api.c:301
VL53L1X_ERROR VL53L1X_GetDistanceThresholdLow(VL53L1_DEV dev, uint16_t *low)
This function returns the low threshold in mm.
Definition: vl53l1x_api.c:758
VL53L1X_ERROR VL53L1X_GetSigmaThreshold(VL53L1_DEV dev, uint16_t *sigma)
This function returns the current sigma threshold in mm.
Definition: vl53l1x_api.c:857
#define VL53L1_RESULT__RANGE_STATUS
Definition: vl53l1x_api.h:112
VL53L1X_ERROR VL53L1X_GetSensorId(VL53L1_DEV dev, uint16_t *sensorId)
This function returns the sensor id, sensor Id must be 0xEEAC.
Definition: vl53l1x_api.c:567
#define VL53L1X_IMPLEMENTATION_VER_MAJOR
Definition: vl53l1x_api.h:73
VL53L1X_ERROR VL53L1X_SetROI(VL53L1_DEV dev, uint16_t X, uint16_t Y)
This function programs the ROI (Region of Interest) The ROI position is centered, only the ROI size c...
Definition: vl53l1x_api.c:794
int8_t VL53L1_ReadMulti(VL53L1_DEV dev, uint16_t index, uint8_t *pdata, uint32_t count)
VL53L1_ReadMulti() definition.
#define RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS
Definition: vl53l1x_api.h:99
#define MM_CONFIG__INNER_OFFSET_MM
Definition: vl53l1x_api.h:87
uint16_t SigPerSPAD
Definition: vl53l1x_api.h:143
#define VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD
Definition: vl53l1x_api.h:101
#define ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE
Definition: vl53l1x_api.h:107
#define VL53L1X_IMPLEMENTATION_VER_REVISION
Definition: vl53l1x_api.h:76
#define VL53L1X_IMPLEMENTATION_VER_MINOR
Definition: vl53l1x_api.h:74
#define RANGE_CONFIG__VCSEL_PERIOD_B
Definition: vl53l1x_api.h:95
#define ROI_CONFIG__USER_ROI_CENTRE_SPAD
Definition: vl53l1x_api.h:106
VL53L1X_ERROR VL53L1X_SetSignalThreshold(VL53L1_DEV dev, uint16_t Signal)
This function programs a new signal threshold in kcps (default=1024 kcps .
Definition: vl53l1x_api.c:826
VL53L1X_ERROR VL53L1X_GetXtalk(VL53L1_DEV dev, uint16_t *xtalk)
This function returns the current programmed xtalk correction value in cps.
Definition: vl53l1x_api.c:718
VL53L1X_ERROR VL53L1X_SetDistanceThreshold(VL53L1_DEV dev, uint16_t ThreshLow, uint16_t ThreshHigh, uint8_t Window, uint8_t IntOnNoTarget)
This function programs the threshold detection mode Example: VL53L1X_SetDistanceThreshold(dev,100,300,0,1): Below 100 VL53L1X_SetDistanceThreshold(dev,100,300,1,1): Above 300 VL53L1X_SetDistanceThreshold(dev,100,300,2,1): Out of window VL53L1X_SetDistanceThreshold(dev,100,300,3,1): In window .
Definition: vl53l1x_api.c:728
int8_t VL53L1X_ERROR
Definition: vl53l1x_api.h:78
#define ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS
Definition: vl53l1x_api.h:85
VL53L1X_ERROR VL53L1X_StartTemperatureUpdate(VL53L1_DEV dev)
This function performs the temperature calibration.
Definition: vl53l1x_api.c:868
#define VL53L1_DEFAULT_ADDRESS
#define VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0
Definition: vl53l1x_api.h:116
static const float offset[]
defines packed reading results type
Definition: vl53l1x_api.h:139
#define SD_CONFIG__INITIAL_PHASE_SD0
Definition: vl53l1x_api.h:105
VL53L1X_ERROR VL53L1X_GetResult(VL53L1_DEV dev, VL53L1X_Result_t *pResult)
This function returns measurements and the range status in a single read access.
Definition: vl53l1x_api.c:658
VL53L1X_ERROR VL53L1X_GetROICenter(VL53L1_DEV dev, uint8_t *ROICenter)
This function returns the current user ROI center.
Definition: vl53l1x_api.c:785
VL53L1X_ERROR VL53L1X_GetInterMeasurementInMs(VL53L1_DEV dev, uint16_t *pIM)
This function returns the Intermeasurement period in ms.
Definition: vl53l1x_api.c:543
uint8_t status
VL53L1X_ERROR VL53L1X_SetROICenter(VL53L1_DEV dev, uint8_t ROICenter)
This function programs the new user ROI center, please to be aware that there is no check in this fun...
Definition: vl53l1x_api.c:778
VL53L1X_ERROR VL53L1X_GetDistanceThresholdWindow(VL53L1_DEV dev, uint16_t *window)
This function returns the window detection mode (0=below; 1=above; 2=out; 3=in)
Definition: vl53l1x_api.c:749
VL53L1X_ERROR VL53L1X_GetDistanceThresholdHigh(VL53L1_DEV dev, uint16_t *high)
This function returns the high threshold in mm.
Definition: vl53l1x_api.c:768
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
#define MM_CONFIG__OUTER_OFFSET_MM
Definition: vl53l1x_api.h:88
#define VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0
Definition: vl53l1x_api.h:115
uint16_t NumSPADs
Definition: vl53l1x_api.h:144
#define GPIO__TIO_HV_STATUS
Definition: vl53l1x_api.h:90
VL53L1X_ERROR VL53L1X_GetOffset(VL53L1_DEV dev, int16_t *offset)
This function returns the programmed offset correction value in mm.
Definition: vl53l1x_api.c:691
struct i2c_transaction i2c_trans
#define RANGE_CONFIG__TIMEOUT_MACROP_A_HI
Definition: vl53l1x_api.h:93
#define SYSTEM__THRESH_HIGH
Definition: vl53l1x_api.h:102
#define ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS
Definition: vl53l1x_api.h:84
#define VL53L1X_IMPLEMENTATION_VER_SUB
Definition: vl53l1x_api.h:75
int8_t VL53L1_RdWord(VL53L1_DEV dev, uint16_t index, uint16_t *data)
VL53L1_RdWord() definition.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define PHASECAL_CONFIG__TIMEOUT_MACROP
Definition: vl53l1x_api.h:92
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define RANGE_CONFIG__VALID_PHASE_HIGH
Definition: vl53l1x_api.h:100
VL53L1X_ERROR VL53L1X_BootState(VL53L1_DEV dev, uint8_t *state)
This function returns the boot state of the device (1:booted, 0:not booted)
Definition: vl53l1x_api.c:557
#define ALGO__PART_TO_PART_RANGE_OFFSET_MM
Definition: vl53l1x_api.h:86
#define SYSTEM__THRESH_LOW
Definition: vl53l1x_api.h:103
VL53L1X_ERROR VL53L1X_GetSpadNb(VL53L1_DEV dev, uint16_t *spNb)
This function returns the current number of enabled SPADs.
Definition: vl53l1x_api.c:623
static const uint8_t status_rtn[24]
Definition: vl53l1x_api.c:201
unsigned char uint8_t
Definition: types.h:14
VL53L1X_ERROR VL53L1X_GetSignalRate(VL53L1_DEV dev, uint16_t *signal)
This function returns the returned signal in kcps.
Definition: vl53l1x_api.c:612
defines SW Version
Definition: vl53l1x_api.h:129
int8_t VL53L1_WrDWord(VL53L1_DEV dev, uint16_t index, uint32_t data)
VL53L1_WrDWord() definition.
VL53L1X_ERROR VL53L1X_SetOffset(VL53L1_DEV dev, int16_t OffsetValue)
This function programs the offset correction in mm.
Definition: vl53l1x_api.c:678
VL53L1X_ERROR VL53L1X_GetInterruptPolarity(VL53L1_DEV dev, uint8_t *pInterruptPolarity)
This function returns the current interrupt polarity 1=active high (default), 0=active low...
Definition: vl53l1x_api.c:290
#define VL53L1_RESULT__OSC_CALIBRATE_VAL
Definition: vl53l1x_api.h:117
Functions definition.
#define RANGE_CONFIG__VCSEL_PERIOD_A
Definition: vl53l1x_api.h:94
VL53L1X_ERROR VL53L1X_GetSignalThreshold(VL53L1_DEV dev, uint16_t *signal)
This function returns the current signal threshold in kcps.
Definition: vl53l1x_api.c:834
int8_t VL53L1_RdDWord(VL53L1_DEV dev, uint16_t index, uint32_t *data)
VL53L1_RdDWord() definition.
int8_t VL53L1_RdByte(VL53L1_DEV dev, uint16_t index, uint8_t *data)
VL53L1_RdByte() definition.
VL53L1X_ERROR VL53L1X_SetXtalk(VL53L1_DEV dev, uint16_t XtalkValue)
This function programs the xtalk correction value in cps (Count Per Second).
Definition: vl53l1x_api.c:703
VL53L1X_ERROR VL53L1X_SetInterMeasurementInMs(VL53L1_DEV dev, uint32_t InterMeasMs)
This function programs the Intermeasurement period in ms Intermeasurement period must be >/= timing b...
Definition: vl53l1x_api.c:530
VL53L1X_ERROR VL53L1X_SensorInit(VL53L1_DEV dev)
This function loads the 135 bytes default values to initialize the sensor.
Definition: vl53l1x_api.c:231
VL53L1X_ERROR VL53L1X_ClearInterrupt(VL53L1_DEV dev)
This function clears the interrupt, to be called after a ranging data reading to arm the interrupt fo...
Definition: vl53l1x_api.c:271
void VL53L1X_BootDevice(VL53L1_DEV dev, uint16_t TimingBudgetInMs, uint16_t DistanceMode, uint32_t InterMeasurementInMs)
Implement boot sequence of VL53L1 device as described in documentation See VL53L1X_SetTimingBudgetInM...
Definition: vl53l1x_api.c:255
#define SD_CONFIG__WOI_SD0
Definition: vl53l1x_api.h:104
VL53L1X_ERROR VL53L1X_StopRanging(VL53L1_DEV dev)
This function stops the ranging.
Definition: vl53l1x_api.c:309
VL53L1X_ERROR VL53L1X_SetTimingBudgetInMs(VL53L1_DEV dev, uint16_t TimingBudgetInMs)
This function programs the timing budget in ms.
Definition: vl53l1x_api.c:336
#define RANGE_CONFIG__TIMEOUT_MACROP_B_HI
Definition: vl53l1x_api.h:96
VL53L1X_ERROR VL53L1X_GetDistanceMode(VL53L1_DEV dev, uint16_t *DM)
This function returns the current distance mode (1=short, 2=long).
Definition: vl53l1x_api.c:516
#define VL53L1_IDENTIFICATION__MODEL_ID
Definition: vl53l1x_api.h:119
#define VL53L1_I2C_SLAVE__DEVICE_ADDRESS
Definition: vl53l1x_api.h:81
#define SYSTEM__INTERRUPT_CONFIG_GPIO
Definition: vl53l1x_api.h:91
#define ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS
Definition: vl53l1x_api.h:83
#define VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0
Definition: vl53l1x_api.h:113
VL53L1X_ERROR VL53L1X_SetInterruptPolarity(VL53L1_DEV dev, uint8_t NewPolarity)
This function programs the interrupt polarity 1=active high (default), 0=active low.
Definition: vl53l1x_api.c:279
VL53L1X_ERROR VL53L1X_SetDistanceMode(VL53L1_DEV dev, uint16_t DM)
This function programs the distance mode (1=short, 2=long(default)).
Definition: vl53l1x_api.c:479
struct State state
Definition: state.c:36
VL53L1X_ERROR VL53L1X_GetTimingBudgetInMs(VL53L1_DEV dev, uint16_t *pTimingBudget)
This function returns the current timing budget in ms.
Definition: vl53l1x_api.c:438
VL53L1X_ERROR VL53L1X_GetAmbientRate(VL53L1_DEV dev, uint16_t *ambRate)
This function returns the ambient rate in kcps.
Definition: vl53l1x_api.c:634
int8_t VL53L1_WrWord(VL53L1_DEV dev, uint16_t index, uint16_t data)
VL53L1_WrWord() definition.