Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / VL53L0X / v1 / alld_VL53L0X_v1.c @ 6ebebd4d

History | View | Annotate | Download (22.7 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_vl53l0x.c
21
 * @brief   Proximity Sensor function implementations.
22
 *
23
 * @addtogroup lld_proximity
24
 * @{
25
 */
26

    
27

    
28
#include <alld_VL53L0X.h>
29
//#include <string.h>
30
//debugging
31
//#include <aos_thread.h>
32

    
33

    
34
#if (defined(AMIROLLD_CFG_VL53L0X) && (AMIROLLD_CFG_VL53L0X == 1)) || defined(__DOXYGEN__)
35

    
36

    
37

    
38
/******************************************************************************/
39
/* LOCAL DEFINITIONS                                                          */
40
/******************************************************************************/
41

    
42
/******************************************************************************/
43
/* EXPORTED VARIABLES                                                         */
44
/******************************************************************************/
45

    
46
/******************************************************************************/
47
/* LOCAL TYPES                                                                */
48
/******************************************************************************/
49

    
50
/******************************************************************************/
51
/* LOCAL VARIABLES                                                            */
52
/******************************************************************************/
53

    
54
/******************************************************************************/
55
/* LOCAL FUNCTIONS                                                            */
56
/******************************************************************************/
57

    
58
/******************************************************************************/
59
/* EXPORTED FUNCTIONS                                                         */
60
/******************************************************************************/
61

    
62
/* general single register access */
63

    
64

    
65
void print_pal_error(VL53L0X_Error Status){
66
    char buf[VL53L0X_MAX_STRING_LENGTH];
67
    VL53L0X_GetPalErrorString(Status, buf);
68
    chprintf((BaseSequentialStream*)&SD1,"\t\tAPI Status: %i : %s\n", Status, buf);
69
}
70

    
71

    
72

    
73
void print_range_status(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData){
74
    char buf[VL53L0X_MAX_STRING_LENGTH];
75
    uint8_t RangeStatus;
76

    
77
    /*
78
     * New Range Status: data is valid when pRangingMeasurementData->RangeStatus = 0
79
     */
80

    
81
    RangeStatus = pRangingMeasurementData->RangeStatus;
82

    
83
    VL53L0X_GetRangeStatusString(RangeStatus, buf);
84
    chprintf((BaseSequentialStream*)&SD1,"Range Status: %i : %s\n", RangeStatus, buf);
85

    
86
}
87

    
88

    
89
/**
90
 * @brief vl53l0x_lld_init useses the Vl53L0X functions so init the sensor. This function are VL53L0X_DataInit (performs the device initialization)
91
 * and VL53L0X_StaticInit (allows to load device settings specific for a given use case). These two function need 40 ms to perform.
92
 *
93
 * @param[in] vl53l0x
94
 * @return
95
 */
96

    
97

    
98
apalExitStatus_t vl53l0x_lld_init(VL53L0XDriver* vl53l0x)
99
{
100

    
101
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
102

    
103
    VL53L0X_DeviceInfo_t DeviceInfo;
104

    
105

    
106
    if(status == VL53L0X_ERROR_NONE)
107
        {
108
            chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_DataInit\n");
109
            status = VL53L0X_DataInit(&(vl53l0x->vl53l0x_dev)); // Data initialization
110
            print_pal_error(status);
111
        }
112

    
113

    
114
    if(status == VL53L0X_ERROR_NONE)
115
       {
116
           status = VL53L0X_GetDeviceInfo(&(vl53l0x->vl53l0x_dev), &DeviceInfo);
117
           if(status == VL53L0X_ERROR_NONE)
118
           {
119
               chprintf((BaseSequentialStream*)&SD1,"VL53L0X_GetDeviceInfo:\n");
120
               chprintf((BaseSequentialStream*)&SD1,"\tDevice Name : %s\n", DeviceInfo.Name);
121
               chprintf((BaseSequentialStream*)&SD1,"\tDevice Type : %s\n", DeviceInfo.Type);
122
               chprintf((BaseSequentialStream*)&SD1,"\tDevice ID : %s\n", DeviceInfo.ProductId);
123
               chprintf((BaseSequentialStream*)&SD1,"\tProductRevisionMajor : %d\n", DeviceInfo.ProductRevisionMajor);
124
           chprintf((BaseSequentialStream*)&SD1,"\tProductRevisionMinor : %d\n", DeviceInfo.ProductRevisionMinor);
125

    
126
           if ((DeviceInfo.ProductRevisionMinor != 1) && (DeviceInfo.ProductRevisionMinor != 1)) {
127
               chprintf((BaseSequentialStream*)&SD1,"Error expected cut 1.1 but found cut %d.%d\n",
128
                          DeviceInfo.ProductRevisionMajor, DeviceInfo.ProductRevisionMinor);
129
                   status = VL53L0X_ERROR_NOT_SUPPORTED;
130
               }
131
           }
132
           print_pal_error(status);
133
    }
134

    
135

    
136
    /* static init and tests*/
137
    uint32_t refSpadCount;
138
    uint8_t isApertureSpads;
139
    uint8_t VhvSettings;
140
    uint8_t PhaseCal;
141

    
142
    if(status == VL53L0X_ERROR_NONE)
143
    {
144
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_StaticInit\n");
145
        status = VL53L0X_StaticInit(&(vl53l0x->vl53l0x_dev)); // Device Initialization
146
       print_pal_error(status);
147
    }
148

    
149

    
150
    /* START Load calibration data*/
151

    
152

    
153
    // need to be done after VL53L0X_StaticInit and before VL53L0X_PerformRefCalibration. In order to optimize the
154
    // dynamic of the system, the reference SPADs have to be calibrated
155
    // there are three different possibilities to set the Parameter
156
    /*
157
            - calibration has not been performed -> VL53L0X_PerformRefSpadManagement
158
            - Host has not programmed the number and type of SPADs -> VL53L0X_SetReferenceSpads
159
            - get the number and type of reference SPADs programmed into the device NVM -> VL53L0X_GetReferenceSpads
160

161
     */
162
    if(status == VL53L0X_ERROR_NONE)
163
    {
164
        // only test
165
       status = VL53L0X_GetReferenceSpads(&(vl53l0x->vl53l0x_dev), &refSpadCount, &isApertureSpads);
166
       chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_GetReferenceSpads with refSpadCount:%lu and isApertureSpads:%u \n", refSpadCount, isApertureSpads);
167

    
168
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_PerformRefSpadManagement\n");
169
        status |= VL53L0X_PerformRefSpadManagement(&(vl53l0x->vl53l0x_dev),
170
                &refSpadCount, &isApertureSpads); // Device Initialization
171
        chprintf((BaseSequentialStream*)&SD1,"\trefSpadCount = %d, isApertureSpads = %d\n", refSpadCount, isApertureSpads);
172
        print_pal_error(status);
173
        if(status == -6){
174
            status  = VL53L0X_ERROR_NONE;
175
        }
176
    }
177

    
178

    
179

    
180

    
181
    // calibration of two parameters (VHV and phase cal) which are temperature dependent,  it should be
182
    // performed again when temperature varies more than 8degC compared to the initial calibration temperature
183
    // if the user has previously performed a calibration and stored the two parameters in the Host memory we can use VL53L0X_SetRefCalibration
184
    if(status == VL53L0X_ERROR_NONE)
185
    {
186
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_PerformRefCalibration\n");
187
        status = VL53L0X_PerformRefCalibration(&(vl53l0x->vl53l0x_dev),
188
                &VhvSettings, &PhaseCal); // Device Initialization
189
        print_pal_error(status);
190
        if(status == -6){
191
            status  = VL53L0X_ERROR_NONE;
192
        }
193
    }
194

    
195

    
196
    //Offset is get via VL53L0X_GetDeviceParameters in VL53L0X_DataInit
197
    //VL53L0X_SetOffsetCalibrationDataMicroMeter();
198

    
199
    // ross-talk correction is set in VL53L0X_DataInit to 0.
200
    // the Host will have to program the cross-talk correction factor and enable the cross-talk correction
201

    
202
    /* End Load calibration data*/
203

    
204

    
205

    
206
    // Enable/Disable Sigma and Signal check
207
    // why are no values set?? with VL53L0X_SetLimitCheckValue
208
    if (status == VL53L0X_ERROR_NONE) {
209
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
210
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
211
    }
212

    
213
    // why are no values seet?? with VL53L0X_SetLimitCheckValue
214
    if (status == VL53L0X_ERROR_NONE) {
215
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
216
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
217
    }
218

    
219

    
220

    
221
    // Signal rate minimum threshold. Measurements with signal rates below this value are ignored.
222
    //This ensures that false measurements are not made due to reflection from the housing.
223
    if (status == VL53L0X_ERROR_NONE) {
224
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
225
                VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 1);
226
    }
227

    
228
    if (status == VL53L0X_ERROR_NONE) {
229
        status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
230
                VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
231
                (FixPoint1616_t)(1.5*0.023*65536));
232
    }
233
    /* end static init and some tests */
234

    
235

    
236
    return status;
237

    
238

    
239
}
240

    
241

    
242
/**
243
 * @brief vl53l0x_lld_set_mode set one of the thre possible modes.
244
 * @param vl53l0x[in]
245
 * @param mode[in] one of the VL53L0X_DeviceModes, VL53L0X_DEVICEMODE_SINGLE_RANGING == 0, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING == 1,
246
 *                  VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING == 3
247
 * @param interrupt[in] set one of the different interrupt modes: VL53L0X_GPIOFUNCTIONALITY_OFF == 0 no interrupt,
248
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW == 1 Level Low (value < thresh_low),
249
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH == 2 Level High (value > thresh_high),
250
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT == 3 value < thresh_low OR value > thresh_high,
251
 *                      VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY == 4 New Sample Ready.
252
 *                      The Interruptpolarity is set to low, an interrupt is created if HIGH changes to LOW.
253
 * @return
254
 */
255
apalExitStatus_t vl53l0x_lld_set_mode(VL53L0XDriver* vl53l0x, VL53L0X_DeviceModes mode, VL53L0X_GpioFunctionality interrupt,
256
                                      FixPoint1616_t low, FixPoint1616_t high)
257
{
258

    
259
    uint32_t ThresholdLow;
260
    uint32_t ThresholdHigh;
261
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
262

    
263
    if(status == VL53L0X_ERROR_NONE)
264
    {
265

    
266
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
267
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_SetDeviceMode\n");
268
        status = VL53L0X_SetDeviceMode(&(vl53l0x->vl53l0x_dev), mode); // Setup in single ranging mode
269
        print_pal_error(status);
270
    }
271

    
272

    
273
    if(status == VL53L0X_ERROR_NONE)
274
    {
275
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
276
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_SetGpioConfig\n");
277
        status = VL53L0X_SetGpioConfig(&(vl53l0x->vl53l0x_dev), 0, mode, interrupt, VL53L0X_INTERRUPTPOLARITY_LOW);
278
        print_pal_error(status);
279
    }
280

    
281

    
282
    if(status == VL53L0X_ERROR_NONE && interrupt != VL53L0X_GPIOFUNCTIONALITY_OFF)
283
    {
284

    
285
        // multiplication with 65536 is imported, no clue why!
286
        // in single ranging high and low must be below 255, otherwise there will be no interrupt. In the other two modes the ranging is a few ms slower!
287
        if(mode == VL53L0X_DEVICEMODE_SINGLE_RANGING && (low > 254 || high > 254)){
288
            chprintf((BaseSequentialStream*)&SD1,"Device mode is VL53L0X_DEVICEMODE_SINGLE_RANGING and therefore we get no Interrupt if the interrupt "
289
                                                 "threshold is bigger than 254 mm\n");
290
        }
291

    
292
        ThresholdLow = low * 65536;
293
        ThresholdHigh = high * 65536;
294

    
295

    
296

    
297
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
298
        chprintf((BaseSequentialStream*)&SD1,"Call of VL53L0X_SetInterruptThresholds\n");
299
        status = VL53L0X_SetInterruptThresholds(&(vl53l0x->vl53l0x_dev),interrupt,ThresholdLow ,ThresholdHigh);
300
        print_pal_error(status);
301
    }
302

    
303
    return status;
304

    
305

    
306

    
307

    
308
    /*High Accurancy
309

310
    // Enable/Disable Sigma and Signal check
311
    if (Status == VL53L0X_ERROR_NONE) {
312
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
313
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
314
    }
315
    if (Status == VL53L0X_ERROR_NONE) {
316
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
317
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
318
    }
319

320
    if (Status == VL53L0X_ERROR_NONE) {
321
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
322
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
323
                (FixPoint1616_t)(0.25*65536));
324
    }
325
    if (Status == VL53L0X_ERROR_NONE) {
326
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
327
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
328
                (FixPoint1616_t)(18*65536));
329
    }
330
    if (Status == VL53L0X_ERROR_NONE) {
331
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
332
                200000);
333
    }
334

335
    */
336

    
337
    /*High Speed
338

339
    // Enable/Disable Sigma and Signal check
340
    if (Status == VL53L0X_ERROR_NONE) {
341
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
342
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
343
    }
344
    if (Status == VL53L0X_ERROR_NONE) {
345
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
346
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
347
    }
348

349
    if (Status == VL53L0X_ERROR_NONE) {
350
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
351
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
352
                (FixPoint1616_t)(0.25*65536));
353
    }
354
    if (Status == VL53L0X_ERROR_NONE) {
355
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
356
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
357
                (FixPoint1616_t)(32*65536));
358
    }
359
    if (Status == VL53L0X_ERROR_NONE) {
360
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
361
                30000);
362
    }
363

364

365
    */
366

    
367
    /*Long Range
368

369

370
    if (Status == VL53L0X_ERROR_NONE) {
371
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
372
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
373
    }
374
    if (Status == VL53L0X_ERROR_NONE) {
375
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
376
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
377
    }
378

379
    if (Status == VL53L0X_ERROR_NONE) {
380
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
381
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
382
                (FixPoint1616_t)(0.1*65536));
383
    }
384
    if (Status == VL53L0X_ERROR_NONE) {
385
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
386
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
387
                (FixPoint1616_t)(60*65536));
388
    }
389
    if (Status == VL53L0X_ERROR_NONE) {
390
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
391
                33000);
392
    }
393

394
    if (Status == VL53L0X_ERROR_NONE) {
395
        Status = VL53L0X_SetVcselPulsePeriod(&(vl53l0x->vl53l0x_dev),
396
                VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18);
397
    }
398
    if (Status == VL53L0X_ERROR_NONE) {
399
        Status = VL53L0X_SetVcselPulsePeriod(&(vl53l0x->vl53l0x_dev),
400
                VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 14);
401
    }
402

403
    */
404

    
405

    
406
}
407

    
408
/**
409
 * @brief vl53l0x_lld_start_measurement, use this method for interrupt mode. You must call vl53l0x_lld_set_mode beforehand.
410
 * @param vl53l0x
411
 * @return
412
 */
413
apalExitStatus_t vl53l0x_lld_start_measurement(VL53L0XDriver* vl53l0x)
414
{
415
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
416
    status = VL53L0X_StartMeasurement(&(vl53l0x->vl53l0x_dev));
417

    
418
    if(status == VL53L0X_ERROR_NONE)
419
    {
420
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
421
        chprintf((BaseSequentialStream*)&SD1,"Successful startet measurement\n");
422
        print_pal_error(status);
423
    }else{
424
        chprintf((BaseSequentialStream*)&SD1,"Error in startet measurement\n");
425
        print_pal_error(status);
426
    }
427

    
428
    return status;
429

    
430
}
431

    
432

    
433

    
434
apalExitStatus_t vl53l0x_lld_stop_measurement(VL53L0XDriver* vl53l0x)
435
{
436

    
437
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
438
    status = VL53L0X_StopMeasurement(&(vl53l0x->vl53l0x_dev));
439

    
440
    return status;
441
}
442

    
443
/**
444
 * @brief vl53l0x_lld_perform_mesaurement with delay and not interrupts ! No need to call vl53l0x_lld_set_mode.
445
 *
446
 * write 16 Byte to 80, FF, 00, 91, 00, FF 80, 00
447
 *  -> read 00 and get 00 back
448
 * listen on 13 till we get 44 back
449
 * write the data from sensor 14 -> 12 B , 12 single BYTES_PER_DWORD
450
 * clear things: 0B -> 01 and 0B -> 00
451
 *
452
 * @param vl53l0x
453
 * @param data
454
 * @param timeout
455
 * @return
456
 */
457
apalExitStatus_t vl53l0x_lld_perform_mesaurement(VL53L0XDriver* vl53l0x, uint16_t* data)
458
{
459
    //the information of the measurement is saved in although in vl53l0x->vl53l0x_dev.Data.LastRangeMeasure, but only after VL53L0X_PerformSingleRangingMeasurement
460
    // is called
461
    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
462
    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
463
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
464

    
465
    status = VL53L0X_PerformSingleMeasurement(&(vl53l0x->vl53l0x_dev));
466

    
467
    if (status == VL53L0X_ERROR_NONE)
468
        status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev),
469
            pRangingMeasurementData);
470

    
471

    
472
    if (status == VL53L0X_ERROR_NONE)
473
        status = VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), 0);
474

    
475

    
476

    
477
    data[0] = RangingMeasurementData.RangeMilliMeter;
478
    //chprintf((BaseSequentialStream*)&SD1, "Measured distance: %i\n\n", RangingMeasurementData.RangeMilliMeter);
479

    
480

    
481
    //TODO write millimeter to data
482
    //data = &RangingMeasurementData.RangeMilliMeter;
483

    
484
    return status;
485
}
486

    
487
/**
488
 * @brief vl53l0x_lld_CheckRangingDataReady custom method, because in api was a strange phenonom
489
 * @param vl53l0x[in]
490
 * @param pMeasurementDataReady[in]
491
 * @return
492
 */
493
apalExitStatus_t vl53l0x_lld_CheckRangingDataReady(VL53L0XDriver* vl53l0x, uint8_t *pMeasurementDataReady)
494
{
495
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
496

    
497
    uint8_t sysRangeStatusRegister;
498
    uint8_t interruptConfig;
499
    uint32_t interruptMask;
500
    //LOG_FUNCTION_START("");
501
   interruptConfig = vl53l0x->vl53l0x_dev.Data.DeviceSpecificParameters.Pin0GpioFunctionality;
502
    if (interruptConfig !=
503
        VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_DISABLED) {
504
        status = VL53L0X_GetInterruptMaskStatus(&(vl53l0x->vl53l0x_dev), &interruptMask);
505
        if (interruptMask == interruptConfig)
506
            *pMeasurementDataReady = 1;
507
        else
508
            *pMeasurementDataReady = 0;
509

    
510
    } else {
511
        status = VL53L0X_RdByte(&(vl53l0x->vl53l0x_dev), VL53L0X_REG_RESULT_RANGE_STATUS,
512
            &sysRangeStatusRegister);
513
        if (status == VL53L0X_ERROR_NONE) {
514
            if (sysRangeStatusRegister & 0x01)
515
                *pMeasurementDataReady = 1;
516
            else
517
                *pMeasurementDataReady = 0;
518
        }
519
    }
520

    
521
    //LOG_FUNCTION_END(status);
522
    return status;
523
}
524

    
525
/**
526
 * @brief vl53l0x_lld_getRangingData
527
 * @param vl53l0x
528
 * @param pRangingMeasurementData
529
 * @return
530
 */
531
apalExitStatus_t vl53l0x_lld_getRangingData(VL53L0XDriver* vl53l0x, VL53L0X_RangingMeasurementData_t *pRangingMeasurementData)
532
{
533
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
534
    status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev),
535
        pRangingMeasurementData);
536

    
537
    status |= VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), 0);
538

    
539
    return status;
540
}
541

    
542

    
543

    
544
/**
545
 * @brief vl53l0x_lld_set_address
546
 * @param vl53l0x
547
 * @param new_address
548
 * @return
549
 */
550
apalExitStatus_t vl53l0x_lld_set_address(VL53L0XDriver* vl53l0x, uint8_t new_address)
551
{
552

    
553
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
554
    status = VL53L0X_SetDeviceAddress(&(vl53l0x->vl53l0x_dev), new_address );
555
    return status;
556
}
557

    
558

    
559
/*
560

561

562
  examples for continous getting examples without interrupt
563

564

565

566

567
    uint32_t measurement;
568
    uint32_t no_of_measurements = 32;
569

570
    uint16_t* pResults = (uint16_t*)malloc(sizeof(uint16_t) * no_of_measurements);
571

572
    for(measurement=0; measurement<no_of_measurements; measurement++)
573
    {
574

575
        Status = WaitMeasurementDataReady(&(vl53l0x->vl53l0x_dev));
576

577
        if(Status == VL53L0X_ERROR_NONE)
578
        {
579
            Status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev), pRangingMeasurementData);
580

581
            *(pResults + measurement) = pRangingMeasurementData->RangeMilliMeter;
582
            printf("In loop measurement %ld: %d\n", measurement, pRangingMeasurementData->RangeMilliMeter);
583

584
            // Clear the interrupt
585
            VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
586
            usleep(20);
587
            //VL53L0X_PollingDelay(&(vl53l0x->vl53l0x_dev);
588
        } else {
589
            break;
590
        }
591
    }
592

593
    if(Status == VL53L0X_ERROR_NONE)
594
    {
595
        for(measurement=0; measurement<no_of_measurements; measurement++)
596
        {
597
            printf("measurement %ld: %d\n", measurement, *(pResults + measurement));
598
        }
599
    }
600
    data = &pResults[0];
601

602
    free(pResults);
603

604

605

606
    example for measurement with interrupt single
607

608

609
    example for measurement with interrupt continious
610

611
*/
612

    
613

    
614
/**
615
 * @brief WaitMeasurementDataReady.
616
 * @param Dev
617
 * @return
618
 */
619
VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
620
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
621
    uint8_t NewDatReady=0;
622
    uint32_t LoopNb;
623

    
624
    // Wait until it finished
625
    // use timeout to avoid deadlock
626
    if (Status == VL53L0X_ERROR_NONE) {
627
        LoopNb = 0;
628
        do {
629
            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
630
            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
631
                break;
632
            }
633
            LoopNb = LoopNb + 1;
634
            // sleep for 50 ms
635
            usleep(50);
636
        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
637

    
638
        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
639
            Status = VL53L0X_ERROR_TIME_OUT;
640
        }
641
    }
642

    
643
    return Status;
644
}
645

    
646

    
647
//#define VL53L0X_POLLINGDELAY_LOOPNB  250
648
//int32_t VL53L0X_PollingDelay(VL53L0X_DEV Dev){
649
//    int32_t status = VL53L0X_ERROR_NONE;
650
//    //LOG_FUNCTION_START("");
651

    
652
////    const DWORD cTimeout_ms = 1;
653
//    usleep(1000);
654

    
655
////    HANDLE hEvent = CreateEvent(0, TRUE, FALSE, 0);
656
////    if(hEvent != NULL)
657
////    {
658
////        WaitForSingleObject(hEvent,cTimeout_ms);
659
////    }
660

    
661
//    //LOG_FUNCTION_END(status);
662
//    return status;
663
//}
664

    
665

    
666
/*============================================================================*/
667
/* threshold register access                                                  */
668
/*============================================================================*/
669

    
670

    
671
#endif /* defined(AMIROLLD_CFG_VL53L0X) && (AMIROLLD_CFG_VL53L0X == 1) */
672

    
673
/** @} */
674

    
675

    
676

    
677