IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
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 
60 /**
61  * @file vl53l1x_api.c
62  * @brief Functions implementation
63  */
64 
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 
207 {
208  VL53L1X_ERROR Status = 0;
209 
214  return Status;
215 }
216 
218 {
219  VL53L1X_ERROR status = 0;
220 
221  status = VL53L1_WrByte(dev, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
222  return status;
223 }
224 
226 {
227  VL53L1X_ERROR status = 0;
228  uint8_t Addr = 0x00, tmp;
229 
230  for (Addr = 0x2D; Addr <= 0x87; Addr++){
231  status = VL53L1_WrByte(dev, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
232  }
233  status = VL53L1X_StartRanging(dev);
234  tmp = 0;
235  while(tmp==0){
236  status = VL53L1X_CheckForDataReady(dev, &tmp);
237  }
238  status = VL53L1X_ClearInterrupt(dev);
239  status = VL53L1X_StopRanging(dev);
240  status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
241  status = VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
242  return status;
243 }
244 
246 {
247  VL53L1X_ERROR status = 0;
248 
249  status = VL53L1_WrByte(dev, SYSTEM__INTERRUPT_CLEAR, 0x01);
250  return status;
251 }
252 
254 {
255  uint8_t Temp;
256  VL53L1X_ERROR status = 0;
257 
258  status = VL53L1_RdByte(dev, GPIO_HV_MUX__CTRL, &Temp);
259  Temp = Temp & 0xEF;
260  status = VL53L1_WrByte(dev, GPIO_HV_MUX__CTRL, Temp | (!(NewPolarity & 1)) << 4);
261  return status;
262 }
263 
265 {
266  uint8_t Temp;
267  VL53L1X_ERROR status = 0;
268 
269  status = VL53L1_RdByte(dev, GPIO_HV_MUX__CTRL, &Temp);
270  Temp = Temp & 0x10;
271  *pInterruptPolarity = !(Temp>>4);
272  return status;
273 }
274 
276 {
277  VL53L1X_ERROR status = 0;
278 
279  status = VL53L1_WrByte(dev, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
280  return status;
281 }
282 
284 {
285  VL53L1X_ERROR status = 0;
286 
287  status = VL53L1_WrByte(dev, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
288  return status;
289 }
290 
292 {
293  uint8_t Temp;
294  uint8_t IntPol;
295  VL53L1X_ERROR status = 0;
296 
297  status = VL53L1X_GetInterruptPolarity(dev, &IntPol);
298  status = VL53L1_RdByte(dev, GPIO__TIO_HV_STATUS, &Temp);
299  /* Read in the register to check if a new value is available */
300  if (status == 0){
301  if ((Temp & 1) == IntPol)
302  *isDataReady = 1;
303  else
304  *isDataReady = 0;
305  }
306  return status;
307 }
308 
310 {
311  uint16_t DM;
312  VL53L1X_ERROR status=0;
313 
314  status = VL53L1X_GetDistanceMode(dev, &DM);
315  if (DM == 0)
316  return 1;
317  else if (DM == 1) { /* Short DistanceMode */
318  switch (TimingBudgetInMs) {
319  case 15: /* only available in short distance mode */
321  0x01D);
323  0x0027);
324  break;
325  case 20:
327  0x0051);
329  0x006E);
330  break;
331  case 33:
333  0x00D6);
335  0x006E);
336  break;
337  case 50:
339  0x1AE);
341  0x01E8);
342  break;
343  case 100:
345  0x02E1);
347  0x0388);
348  break;
349  case 200:
351  0x03E1);
353  0x0496);
354  break;
355  case 500:
357  0x0591);
359  0x05C1);
360  break;
361  default:
362  status = 1;
363  break;
364  }
365  } else {
366  switch (TimingBudgetInMs) {
367  case 20:
369  0x001E);
371  0x0022);
372  break;
373  case 33:
375  0x0060);
377  0x006E);
378  break;
379  case 50:
381  0x00AD);
383  0x00C6);
384  break;
385  case 100:
387  0x01CC);
389  0x01EA);
390  break;
391  case 200:
393  0x02D9);
395  0x02F8);
396  break;
397  case 500:
399  0x048F);
401  0x04A4);
402  break;
403  default:
404  status = 1;
405  break;
406  }
407  }
408  return status;
409 }
410 
412 {
413  uint16_t Temp;
414  VL53L1X_ERROR status = 0;
415 
416  status = VL53L1_RdWord(dev, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp);
417  switch (Temp) {
418  case 0x001D :
419  *pTimingBudget = 15;
420  break;
421  case 0x0051 :
422  case 0x001E :
423  *pTimingBudget = 20;
424  break;
425  case 0x00D6 :
426  case 0x0060 :
427  *pTimingBudget = 33;
428  break;
429  case 0x1AE :
430  case 0x00AD :
431  *pTimingBudget = 50;
432  break;
433  case 0x02E1 :
434  case 0x01CC :
435  *pTimingBudget = 100;
436  break;
437  case 0x03E1 :
438  case 0x02D9 :
439  *pTimingBudget = 200;
440  break;
441  case 0x0591 :
442  case 0x048F :
443  *pTimingBudget = 500;
444  break;
445  default:
446  status = 1;
447  *pTimingBudget = 0;
448  }
449  return status;
450 }
451 
453 {
454  uint16_t TB;
455  VL53L1X_ERROR status = 0;
456 
457  status = VL53L1X_GetTimingBudgetInMs(dev, &TB);
458  if (status != 0)
459  return 1;
460  switch (DM) {
461  case 1:
462  status = VL53L1_WrByte(dev, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14);
463  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
464  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
465  status = VL53L1_WrByte(dev, RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);
466  status = VL53L1_WrWord(dev, SD_CONFIG__WOI_SD0, 0x0705);
467  status = VL53L1_WrWord(dev, SD_CONFIG__INITIAL_PHASE_SD0, 0x0606);
468  break;
469  case 2:
470  status = VL53L1_WrByte(dev, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x0A);
471  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
472  status = VL53L1_WrByte(dev, RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
473  status = VL53L1_WrByte(dev, RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);
474  status = VL53L1_WrWord(dev, SD_CONFIG__WOI_SD0, 0x0F0D);
475  status = VL53L1_WrWord(dev, SD_CONFIG__INITIAL_PHASE_SD0, 0x0E0E);
476  break;
477  default:
478  status = 1;
479  break;
480  }
481 
482  if (status == 0)
483  status = VL53L1X_SetTimingBudgetInMs(dev, TB);
484  return status;
485 }
486 
488 {
489  uint8_t TempDM, status=0;
490 
491  status = VL53L1_RdByte(dev,PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
492  if (TempDM == 0x14)
493  *DM=1;
494  if(TempDM == 0x0A)
495  *DM=2;
496  return status;
497 }
498 
500 {
501  uint16_t ClockPLL;
502  VL53L1X_ERROR status = 0;
503 
504  status = VL53L1_RdWord(dev, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
505  ClockPLL = ClockPLL&0x3FF;
507  (uint32_t)(ClockPLL * InterMeasMs * 1.075));
508  return status;
509 
510 }
511 
513 {
514  uint16_t ClockPLL;
515  VL53L1X_ERROR status = 0;
516  uint32_t tmp;
517 
519  *pIM = (uint16_t)tmp;
520  status = VL53L1_RdWord(dev, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
521  ClockPLL = ClockPLL&0x3FF;
522  *pIM= (uint16_t)(*pIM/(ClockPLL*1.065));
523  return status;
524 }
525 
527 {
528  VL53L1X_ERROR status = 0;
529  uint8_t tmp = 0;
530 
531  status = VL53L1_RdByte(dev,VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
532  *state = tmp;
533  return status;
534 }
535 
537 {
538  VL53L1X_ERROR status = 0;
539  uint16_t tmp = 0;
540 
541  status = VL53L1_RdWord(dev, VL53L1_IDENTIFICATION__MODEL_ID, &tmp);
542  *sensorId = tmp;
543  return status;
544 }
545 
547 {
548  VL53L1X_ERROR status = 0;
549  uint16_t tmp;
550 
551  status = (VL53L1_RdWord(dev,
553  *distance = tmp;
554  return status;
555 }
556 
558 {
559  VL53L1X_ERROR status = 0;
560  uint16_t SpNb=1, signal;
561 
562  status = VL53L1_RdWord(dev,
564  status = VL53L1_RdWord(dev,
566  *signalRate = (uint16_t) (2000.0*signal/SpNb);
567  return status;
568 }
569 
571 {
572  VL53L1X_ERROR status = 0;
573  uint16_t AmbientRate, SpNb = 1;
574 
575  status = VL53L1_RdWord(dev, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate);
577  *ambPerSp=(uint16_t) (2000.0 * AmbientRate / SpNb);
578  return status;
579 }
580 
582 {
583  VL53L1X_ERROR status = 0;
584  uint16_t tmp;
585 
586  status = VL53L1_RdWord(dev,
588  *signal = tmp*8;
589  return status;
590 }
591 
593 {
594  VL53L1X_ERROR status = 0;
595  uint16_t tmp;
596 
597  status = VL53L1_RdWord(dev,
599  *spNb = tmp >> 8;
600  return status;
601 }
602 
604 {
605  VL53L1X_ERROR status = 0;
606  uint16_t tmp;
607 
609  *ambRate = tmp*8;
610  return status;
611 }
612 
614 {
615  VL53L1X_ERROR status = 0;
616  uint8_t RgSt;
617 
618  *rangeStatus = 255;
619  status = VL53L1_RdByte(dev, VL53L1_RESULT__RANGE_STATUS, &RgSt);
620  RgSt = RgSt & 0x1F;
621  if (RgSt < 24)
622  *rangeStatus = status_rtn[RgSt];
623  return status;
624 }
625 
627 {
628  VL53L1X_ERROR status = 0;
629  uint8_t Temp[17];
630  uint8_t RgSt = 255;
631 
632  status = VL53L1_ReadMulti(dev, VL53L1_RESULT__RANGE_STATUS, Temp, 17);
633  RgSt = Temp[0] & 0x1F;
634  if (RgSt < 24)
635  RgSt = status_rtn[RgSt];
636  pResult->Status = RgSt;
637  pResult->Ambient = (Temp[7] << 8 | Temp[8]) * 8;
638  pResult->NumSPADs = Temp[3];
639  pResult->SigPerSPAD = (Temp[15] << 8 | Temp[16]) * 8;
640  pResult->Distance = Temp[13] << 8 | Temp[14];
641 
642  return status;
643 }
644 
646 {
647  VL53L1X_ERROR status = 0;
648  int16_t Temp;
649 
650  Temp = (OffsetValue*4);
652  (uint16_t)Temp);
655  return status;
656 }
657 
659 {
660  VL53L1X_ERROR status = 0;
661  uint16_t Temp;
662 
664  Temp = Temp<<3;
665  Temp = Temp>>5;
666  *offset = (int16_t)(Temp);
667  return status;
668 }
669 
671 {
672 /* XTalkValue in count per second to avoid float type */
673  VL53L1X_ERROR status = 0;
674 
675  status = VL53L1_WrWord(dev,
677  0x0000);
679  0x0000);
681  (XtalkValue<<9)/1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
682  return status;
683 }
684 
686 {
687  VL53L1X_ERROR status = 0;
688  uint32_t tmp;
689 
691  *xtalk = (uint16_t)(tmp*1000)>>9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
692  return status;
693 }
694 
696  uint16_t ThreshHigh, uint8_t Window,
697  uint8_t IntOnNoTarget)
698 {
699  VL53L1X_ERROR status = 0;
700  uint8_t Temp = 0;
701 
702  status = VL53L1_RdByte(dev, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp);
703  Temp = Temp & 0x47;
704  if (IntOnNoTarget == 0) {
706  (Temp | (Window & 0x07)));
707  } else {
709  ((Temp | (Window & 0x07)) | 0x40));
710  }
711  status = VL53L1_WrWord(dev, SYSTEM__THRESH_HIGH, ThreshHigh);
712  status = VL53L1_WrWord(dev, SYSTEM__THRESH_LOW, ThreshLow);
713  return status;
714 }
715 
717 {
718  VL53L1X_ERROR status = 0;
719  uint8_t tmp;
720  status = VL53L1_RdByte(dev,SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
721  *window = (uint16_t)(tmp & 0x7);
722  return status;
723 }
724 
726 {
727  VL53L1X_ERROR status = 0;
728  uint16_t tmp;
729 
730  status = VL53L1_RdWord(dev,SYSTEM__THRESH_LOW, &tmp);
731  *low = tmp;
732  return status;
733 }
734 
736 {
737  VL53L1X_ERROR status = 0;
738  uint16_t tmp;
739 
740  status = VL53L1_RdWord(dev,SYSTEM__THRESH_HIGH, &tmp);
741  *high = tmp;
742  return status;
743 }
744 
746 {
747  VL53L1X_ERROR status = 0;
748  status = VL53L1_WrByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, ROICenter);
749  return status;
750 }
751 
753 {
754  VL53L1X_ERROR status = 0;
755  uint8_t tmp;
756  status = VL53L1_RdByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, &tmp);
757  *ROICenter = tmp;
758  return status;
759 }
760 
762 {
763  uint8_t OpticalCenter;
764  VL53L1X_ERROR status = 0;
765 
766  status =VL53L1_RdByte(dev, VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD, &OpticalCenter);
767  if (X > 16)
768  X = 16;
769  if (Y > 16)
770  Y = 16;
771  if (X > 10 || Y > 10){
772  OpticalCenter = 199;
773  }
774  status = VL53L1_WrByte(dev, ROI_CONFIG__USER_ROI_CENTRE_SPAD, OpticalCenter);
776  (Y - 1) << 4 | (X - 1));
777  return status;
778 }
779 
781 {
782  VL53L1X_ERROR status = 0;
783  uint8_t tmp;
784 
786  *ROI_X = ((uint16_t)tmp & 0x0F) + 1;
787  *ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1;
788  return status;
789 }
790 
792 {
793  VL53L1X_ERROR status = 0;
794 
796  return status;
797 }
798 
800 {
801  VL53L1X_ERROR status = 0;
802  uint16_t tmp;
803 
804  status = VL53L1_RdWord(dev,
806  *signal = tmp <<3;
807  return status;
808 }
809 
811 {
812  VL53L1X_ERROR status = 0;
813 
814  if(Sigma>(0xFFFF>>2)){
815  return 1;
816  }
817  /* 16 bits register 14.2 format */
818  status = VL53L1_WrWord(dev,RANGE_CONFIG__SIGMA_THRESH,Sigma<<2);
819  return status;
820 }
821 
823 {
824  VL53L1X_ERROR status = 0;
825  uint16_t tmp;
826 
827  status = VL53L1_RdWord(dev,RANGE_CONFIG__SIGMA_THRESH, &tmp);
828  *sigma = tmp >> 2;
829  return status;
830 
831 }
832 
834 {
835  VL53L1X_ERROR status = 0;
836  uint8_t tmp=0;
837 
838  status = VL53L1_WrByte(dev,VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND,0x81); /* full VHV */
839  status = VL53L1_WrByte(dev,0x0B,0x92);
840  status = VL53L1X_StartRanging(dev);
841  while(tmp==0){
842  status = VL53L1X_CheckForDataReady(dev, &tmp);
843  }
844  tmp = 0;
845  status = VL53L1X_ClearInterrupt(dev);
846  status = VL53L1X_StopRanging(dev);
847  status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
848  status = VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
849  return status;
850 }
int8_t VL53L1_WrWord(uint16_t dev, uint16_t index, uint16_t data)
Write a word (16 bits) to the I2C bus.
int8_t VL53L1X_ERROR
Definition: VL53L1X_api.h:78
#define VL53L1_FIRMWARE__SYSTEM_STATUS
Definition: VL53L1X_api.h:118
VL53L1X_ERROR VL53L1X_GetDistanceThresholdHigh(uint16_t dev, uint16_t *high)
This function returns the high threshold in mm.
Definition: VL53L1X_api.c:735
VL53L1X_ERROR VL53L1X_GetSWVersion(VL53L1X_Version_t *pVersion)
This function returns the SW driver version.
Definition: VL53L1X_api.c:206
VL53L1X_ERROR VL53L1X_StopRanging(uint16_t dev)
This function stops the ranging.
Definition: VL53L1X_api.c:283
#define SYSTEM__THRESH_HIGH
Definition: VL53L1X_api.h:102
VL53L1X_ERROR VL53L1X_SetInterruptPolarity(uint16_t dev, uint8_t NewPolarity)
This function programs the interrupt polarity 1=active high (default), 0=active low.
Definition: VL53L1X_api.c:253
#define VL53L1_RESULT__RANGE_STATUS
Definition: VL53L1X_api.h:112
#define VL53L1_RESULT__OSC_CALIBRATE_VAL
Definition: VL53L1X_api.h:117
#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 VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0
Definition: VL53L1X_api.h:113
VL53L1X_ERROR VL53L1X_GetAmbientPerSpad(uint16_t dev, uint16_t *ambPerSp)
This function returns the ambient per SPAD in kcps/SPAD.
Definition: VL53L1X_api.c:570
VL53L1X_ERROR VL53L1X_StartRanging(uint16_t dev)
This function starts the ranging distance operation The ranging operation is continuous. The clear interrupt has to be done after each get data to allow the interrupt to raise when the next data is ready 1=active high (default), 0=active low, use SetInterruptPolarity() to change the interrupt polarity if required.
Definition: VL53L1X_api.c:275
VL53L1X_ERROR VL53L1X_SetROI(uint16_t 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:761
int8_t VL53L1_WrDWord(uint16_t dev, uint16_t index, uint32_t data)
Write a double word (32 bits) to the I2C bus.
VL53L1X_ERROR VL53L1X_GetInterruptPolarity(uint16_t dev, uint8_t *pInterruptPolarity)
This function returns the current interrupt polarity 1=active high (default), 0=active low...
Definition: VL53L1X_api.c:264
VL53L1X_ERROR VL53L1X_GetDistanceThresholdLow(uint16_t dev, uint16_t *low)
This function returns the low threshold in mm.
Definition: VL53L1X_api.c:725
VL53L1X_ERROR VL53L1X_GetSigmaThreshold(uint16_t dev, uint16_t *sigma)
This function returns the current sigma threshold in mm.
Definition: VL53L1X_api.c:822
VL53L1X_ERROR VL53L1X_GetInterMeasurementInMs(uint16_t dev, uint16_t *pIM)
This function returns the Intermeasurement period in ms.
Definition: VL53L1X_api.c:512
#define RANGE_CONFIG__VCSEL_PERIOD_B
Definition: VL53L1X_api.h:95
#define RANGE_CONFIG__SIGMA_THRESH
Definition: VL53L1X_api.h:98
uint16_t Ambient
Definition: VL53L1X_api.h:142
int16_t offset
Definition: VL43L1X.c:52
short int16_t
Typedef defining 16 bit short type. The developer should modify this to suit the platform being deplo...
Definition: vl53l1_types.h:93
defines SW Version
Definition: VL53L1X_api.h:129
uint16_t xtalk
Definition: VL43L1X.c:53
#define ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS
Definition: VL53L1X_api.h:83
#define VL53L1X_IMPLEMENTATION_VER_MINOR
Definition: VL53L1X_api.h:74
#define RANGE_CONFIG__VCSEL_PERIOD_A
Definition: VL53L1X_api.h:94
VL53L1X_ERROR VL53L1X_SetROICenter(uint16_t 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:745
VL53L1X_ERROR VL53L1X_GetXtalk(uint16_t dev, uint16_t *xtalk)
This function returns the current programmed xtalk correction value in cps.
Definition: VL53L1X_api.c:685
VL53L1X_ERROR VL53L1X_SetOffset(uint16_t dev, int16_t OffsetValue)
This function programs the offset correction in mm.
Definition: VL53L1X_api.c:645
#define VL53L1_I2C_SLAVE__DEVICE_ADDRESS
Definition: VL53L1X_api.h:81
#define ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS
Definition: VL53L1X_api.h:84
int8_t VL53L1_RdByte(uint16_t dev, uint16_t index, uint8_t *data)
Read a byte (8 bits) from the I2C bus.
#define VL53L1X_IMPLEMENTATION_VER_SUB
Definition: VL53L1X_api.h:75
VL53L1X_ERROR VL53L1X_SetDistanceMode(uint16_t dev, uint16_t DM)
This function programs the distance mode (1=short, 2=long(default)). Short mode max distance is limit...
Definition: VL53L1X_api.c:452
VL53L1X_ERROR VL53L1X_SetSignalThreshold(uint16_t dev, uint16_t Signal)
This function programs a new signal threshold in kcps (default=1024 kcps .
Definition: VL53L1X_api.c:791
VL53L1X_ERROR VL53L1X_SetSigmaThreshold(uint16_t dev, uint16_t Sigma)
This function programs a new sigma threshold in mm (default=15 mm)
Definition: VL53L1X_api.c:810
VL53L1X_ERROR VL53L1X_StartTemperatureUpdate(uint16_t dev)
This function performs the temperature calibration. It is recommended to call this function any time ...
Definition: VL53L1X_api.c:833
VL53L1X_ERROR VL53L1X_GetSignalRate(uint16_t dev, uint16_t *signal)
This function returns the returned signal in kcps.
Definition: VL53L1X_api.c:581
int8_t VL53L1_RdWord(uint16_t dev, uint16_t index, uint16_t *data)
Read a word (16 bits) from the I2C bus.
#define SYSTEM__THRESH_LOW
Definition: VL53L1X_api.h:103
VL53L1X_ERROR VL53L1X_SetI2CAddress(uint16_t 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:217
#define RANGE_CONFIG__TIMEOUT_MACROP_B_HI
Definition: VL53L1X_api.h:96
VL53L1X_ERROR VL53L1X_GetTimingBudgetInMs(uint16_t dev, uint16_t *pTimingBudget)
This function returns the current timing budget in ms.
Definition: VL53L1X_api.c:411
int8_t VL53L1_RdDWord(uint16_t dev, uint16_t index, uint32_t *data)
Read a double word (32 bits) from the I2C bus.
int8_t VL53L1_WrByte(uint16_t dev, uint16_t index, uint8_t data)
Write a byte (8 bits) to the I2C bus.
VL53L1X_ERROR VL53L1X_GetDistance(uint16_t dev, uint16_t *distance)
This function returns the distance measured by the sensor in mm.
Definition: VL53L1X_api.c:546
#define SYSTEM__INTERRUPT_CONFIG_GPIO
Definition: VL53L1X_api.h:91
VL53L1X_ERROR VL53L1X_GetAmbientRate(uint16_t dev, uint16_t *ambRate)
This function returns the ambient rate in kcps.
Definition: VL53L1X_api.c:603
VL53L1X_ERROR VL53L1X_SetXtalk(uint16_t dev, uint16_t XtalkValue)
This function programs the xtalk correction value in cps (Count Per Second). This is the number of ph...
Definition: VL53L1X_api.c:670
#define SYSTEM__INTERRUPT_CLEAR
Definition: VL53L1X_api.h:110
VL53L1X_ERROR VL53L1X_GetDistanceMode(uint16_t dev, uint16_t *DM)
This function returns the current distance mode (1=short, 2=long).
Definition: VL53L1X_api.c:487
VL53L1X_ERROR VL53L1X_GetROICenter(uint16_t dev, uint8_t *ROICenter)
This function returns the current user ROI center.
Definition: VL53L1X_api.c:752
VL53L1X_ERROR VL53L1X_GetOffset(uint16_t dev, int16_t *offset)
This function returns the programmed offset correction value in mm.
Definition: VL53L1X_api.c:658
#define GPIO__TIO_HV_STATUS
Definition: VL53L1X_api.h:90
VL53L1X_ERROR VL53L1X_GetRangeStatus(uint16_t dev, uint8_t *rangeStatus)
This function returns the ranging status error (0:no error, 1:sigma failed, 2:signal failed...
Definition: VL53L1X_api.c:613
#define SD_CONFIG__WOI_SD0
Definition: VL53L1X_api.h:104
#define VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0
Definition: VL53L1X_api.h:115
VL53L1X_ERROR VL53L1X_GetSpadNb(uint16_t dev, uint16_t *spNb)
This function returns the current number of enabled SPADs.
Definition: VL53L1X_api.c:592
#define RANGE_CONFIG__TIMEOUT_MACROP_A_HI
Definition: VL53L1X_api.h:93
#define ROI_CONFIG__USER_ROI_CENTRE_SPAD
Definition: VL53L1X_api.h:106
#define VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD
Definition: VL53L1X_api.h:120
uint16_t SigPerSPAD
Definition: VL53L1X_api.h:143
#define PHASECAL_CONFIG__TIMEOUT_MACROP
Definition: VL53L1X_api.h:92
#define VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND
Definition: VL53L1X_api.h:82
#define VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD
Definition: VL53L1X_api.h:101
VL53L1X_ERROR VL53L1X_GetSensorId(uint16_t dev, uint16_t *sensorId)
This function returns the sensor id, sensor Id must be 0xEEAC.
Definition: VL53L1X_api.c:536
uint16_t Distance
Definition: VL53L1X_api.h:141
VL53L1X_ERROR VL53L1X_GetSignalThreshold(uint16_t dev, uint16_t *signal)
This function returns the current signal threshold in kcps.
Definition: VL53L1X_api.c:799
VL53L1X_ERROR VL53L1X_GetROI_XY(uint16_t dev, uint16_t *ROI_X, uint16_t *ROI_Y)
This function returns width X and height Y.
Definition: VL53L1X_api.c:780
#define SYSTEM__MODE_START
Definition: VL53L1X_api.h:111
#define SD_CONFIG__INITIAL_PHASE_SD0
Definition: VL53L1X_api.h:105
VL53L1X_ERROR VL53L1X_GetSignalPerSpad(uint16_t dev, uint16_t *signalRate)
This function returns the returned signal per SPAD in kcps/SPAD. With kcps stands for Kilo Count Per ...
Definition: VL53L1X_api.c:557
#define RANGE_CONFIG__VALID_PHASE_HIGH
Definition: VL53L1X_api.h:100
#define MM_CONFIG__INNER_OFFSET_MM
Definition: VL53L1X_api.h:87
#define VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0
Definition: VL53L1X_api.h:116
defines packed reading results type
Definition: VL53L1X_api.h:139
VL53L1X_ERROR VL53L1X_SensorInit(uint16_t dev)
This function loads the 135 bytes default values to initialize the sensor.
Definition: VL53L1X_api.c:225
#define RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS
Definition: VL53L1X_api.h:99
VL53L1X_ERROR VL53L1X_GetResult(uint16_t dev, VL53L1X_Result_t *pResult)
This function returns measurements and the range status in a single read access.
Definition: VL53L1X_api.c:626
VL53L1X_ERROR VL53L1X_SetTimingBudgetInMs(uint16_t dev, uint16_t TimingBudgetInMs)
This function programs the timing budget in ms. Predefined values = 15, 20, 33, 50, 100(default), 200, 500.
Definition: VL53L1X_api.c:309
VL53L1X_ERROR VL53L1X_SetInterMeasurementInMs(uint16_t dev, uint32_t InterMeasMs)
This function programs the Intermeasurement period in ms Intermeasurement period must be >/= timing b...
Definition: VL53L1X_api.c:499
#define VL53L1_IDENTIFICATION__MODEL_ID
Definition: VL53L1X_api.h:119
#define VL53L1X_IMPLEMENTATION_VER_MAJOR
Definition: VL53L1X_api.h:73
#define ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS
Definition: VL53L1X_api.h:85
#define MM_CONFIG__OUTER_OFFSET_MM
Definition: VL53L1X_api.h:88
VL53L1X_ERROR VL53L1X_CheckForDataReady(uint16_t dev, uint8_t *isDataReady)
This function checks if the new ranging data is available by polling the dedicated register...
Definition: VL53L1X_api.c:291
#define RESULT__AMBIENT_COUNT_RATE_MCPS_SD
Definition: VL53L1X_api.h:114
#define GPIO_HV_MUX__CTRL
Definition: VL53L1X_api.h:89
VL53L1X_ERROR VL53L1X_GetDistanceThresholdWindow(uint16_t dev, uint16_t *window)
This function returns the window detection mode (0=below; 1=above; 2=out; 3=in)
Definition: VL53L1X_api.c:716
VL53L1X_ERROR VL53L1X_ClearInterrupt(uint16_t dev)
This function clears the interrupt, to be called after a ranging data reading to arm the interrupt fo...
Definition: VL53L1X_api.c:245
#define ALGO__PART_TO_PART_RANGE_OFFSET_MM
Definition: VL53L1X_api.h:86
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
static const uint8_t status_rtn[24]
Definition: VL53L1X_api.c:201
int8_t VL53L1_ReadMulti(uint16_t dev, uint16_t index, uint8_t *pdata, uint32_t count)
TODO.
const uint8_t VL51L1X_DEFAULT_CONFIGURATION[]
Definition: VL53L1X_api.c:107
VL53L1X_ERROR VL53L1X_SetDistanceThreshold(uint16_t 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:695
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
VL53L1X_ERROR VL53L1X_BootState(uint16_t dev, uint8_t *state)
This function returns the boot state of the device (1:booted, 0:not booted)
Definition: VL53L1X_api.c:526
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:78
uint16_t NumSPADs
Definition: VL53L1X_api.h:144