Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / VL53L0X / v1 / alld_VL53L0X_v1.c @ c368a765

History | View | Annotate | Download (23.975 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
void print_pal_error(VL53L0X_Error Status){
59
    char buf[VL53L0X_MAX_STRING_LENGTH];
60
    VL53L0X_GetPalErrorString(Status, buf);
61
    apalDbgPrintf("\t\tAPI Status: %i : %s\n", Status, buf);
62
}
63

    
64

    
65

    
66
void print_range_status(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData){
67
    char buf[VL53L0X_MAX_STRING_LENGTH];
68
    uint8_t RangeStatus;
69

    
70
    /*
71
     * New Range Status: data is valid when pRangingMeasurementData->RangeStatus = 0
72
     */
73

    
74
    RangeStatus = pRangingMeasurementData->RangeStatus;
75

    
76
    VL53L0X_GetRangeStatusString(RangeStatus, buf);
77
    apalDbgPrintf("Range Status: %i : %s\n", RangeStatus, buf);
78

    
79
}
80

    
81

    
82
/******************************************************************************/
83
/* EXPORTED FUNCTIONS                                                         */
84
/******************************************************************************/
85

    
86
/* general single register access */
87

    
88

    
89

    
90

    
91

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

    
100

    
101
apalExitStatus_t vl53l0x_lld_init(VL53L0XDriver* vl53l0x)
102
{
103

    
104
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
105
    apalExitStatus_t status_driver = APAL_STATUS_OK;
106

    
107
    VL53L0X_DeviceInfo_t DeviceInfo;
108

    
109

    
110
    if(status == VL53L0X_ERROR_NONE)
111
        {
112
            apalDbgPrintf("Call of VL53L0X_DataInit\n");
113
            status = VL53L0X_DataInit(&(vl53l0x->vl53l0x_dev)); // Data initialization
114

    
115
            print_pal_error(status);
116
        }else{
117
        status_driver = APAL_STATUS_ERROR;
118
    }
119

    
120

    
121
    if(status == VL53L0X_ERROR_NONE)
122
       {
123
           status = VL53L0X_GetDeviceInfo(&(vl53l0x->vl53l0x_dev), &DeviceInfo);
124
           if(status == VL53L0X_ERROR_NONE)
125
           {
126
               apalDbgPrintf("VL53L0X_GetDeviceInfo:\n");
127
               apalDbgPrintf("\tDevice Name : %s\n", DeviceInfo.Name);
128
               apalDbgPrintf("\tDevice Type : %s\n", DeviceInfo.Type);
129
               apalDbgPrintf("\tDevice ID : %s\n", DeviceInfo.ProductId);
130
               apalDbgPrintf("\tProductRevisionMajor : %d\n", DeviceInfo.ProductRevisionMajor);
131
           apalDbgPrintf("\tProductRevisionMinor : %d\n", DeviceInfo.ProductRevisionMinor);
132

    
133
           if ((DeviceInfo.ProductRevisionMinor != 1) && (DeviceInfo.ProductRevisionMinor != 1)) {
134
               apalDbgPrintf("Error expected cut 1.1 but found cut %d.%d\n",
135
                          DeviceInfo.ProductRevisionMajor, DeviceInfo.ProductRevisionMinor);
136
                   status = VL53L0X_ERROR_NOT_SUPPORTED;
137
               }
138
           }
139
           print_pal_error(status);
140
    }else{
141
        status_driver = APAL_STATUS_ERROR;
142
    }
143

    
144

    
145
    /* static init and tests*/
146
    uint32_t refSpadCount;
147
    uint8_t isApertureSpads;
148
    uint8_t VhvSettings;
149
    uint8_t PhaseCal;
150

    
151
    if(status == VL53L0X_ERROR_NONE)
152
    {
153
        apalDbgPrintf("Call of VL53L0X_StaticInit\n");
154
        status = VL53L0X_StaticInit(&(vl53l0x->vl53l0x_dev)); // Device Initialization
155
       print_pal_error(status);
156
    }else{
157
        status_driver = APAL_STATUS_ERROR;
158
    }
159

    
160

    
161
    /* START Load calibration data*/
162

    
163

    
164
    // need to be done after VL53L0X_StaticInit and before VL53L0X_PerformRefCalibration. In order to optimize the
165
    // dynamic of the system, the reference SPADs have to be calibrated
166
    // there are three different possibilities to set the Parameter
167
    /*
168
            - calibration has not been performed -> VL53L0X_PerformRefSpadManagement
169
            - Host has not programmed the number and type of SPADs -> VL53L0X_SetReferenceSpads
170
            - get the number and type of reference SPADs programmed into the device NVM -> VL53L0X_GetReferenceSpads
171

172
     */
173
    if(status == VL53L0X_ERROR_NONE)
174
    {
175
        // only test
176
       status = VL53L0X_GetReferenceSpads(&(vl53l0x->vl53l0x_dev), &refSpadCount, &isApertureSpads);
177
       apalDbgPrintf("Call of VL53L0X_GetReferenceSpads with refSpadCount:%lu and isApertureSpads:%u \n", refSpadCount, isApertureSpads);
178

    
179
        apalDbgPrintf("Call of VL53L0X_PerformRefSpadManagement\n");
180
        status |= VL53L0X_PerformRefSpadManagement(&(vl53l0x->vl53l0x_dev),
181
                &refSpadCount, &isApertureSpads); // Device Initialization
182
        apalDbgPrintf("\trefSpadCount = %d, isApertureSpads = %d\n", refSpadCount, isApertureSpads);
183
        print_pal_error(status);
184
        if(status == -6){
185
            status_driver = APAL_STATUS_ERROR;
186
            status  = VL53L0X_ERROR_NONE;
187
        }
188
    }else{
189
        status_driver = APAL_STATUS_ERROR;
190
    }
191

    
192

    
193

    
194

    
195
    // calibration of two parameters (VHV and phase cal) which are temperature dependent,  it should be
196
    // performed again when temperature varies more than 8degC compared to the initial calibration temperature
197
    // if the user has previously performed a calibration and stored the two parameters in the Host memory we can use VL53L0X_SetRefCalibration
198
    if(status == VL53L0X_ERROR_NONE)
199
    {
200
        apalDbgPrintf("Call of VL53L0X_PerformRefCalibration\n");
201
        status = VL53L0X_PerformRefCalibration(&(vl53l0x->vl53l0x_dev),
202
                &VhvSettings, &PhaseCal); // Device Initialization
203
        print_pal_error(status);
204
        if(status == -6){
205
            status  = VL53L0X_ERROR_NONE;
206
            status_driver = APAL_STATUS_ERROR;
207
        }
208
    }else{
209
        status_driver = APAL_STATUS_ERROR;
210
    }
211

    
212

    
213
    //Offset is get via VL53L0X_GetDeviceParameters in VL53L0X_DataInit
214
    //VL53L0X_SetOffsetCalibrationDataMicroMeter();
215

    
216
    // ross-talk correction is set in VL53L0X_DataInit to 0.
217
    // the Host will have to program the cross-talk correction factor and enable the cross-talk correction
218

    
219
    /* End Load calibration data*/
220

    
221

    
222

    
223
    // Enable/Disable Sigma and Signal check
224
    // why are no values set?? with VL53L0X_SetLimitCheckValue
225
    if (status == VL53L0X_ERROR_NONE) {
226
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
227
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
228
    }else{
229
        status_driver = APAL_STATUS_ERROR;
230
    }
231

    
232
    // why are no values seet?? with VL53L0X_SetLimitCheckValue
233
    if (status == VL53L0X_ERROR_NONE) {
234
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
235
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
236
    }else{
237
        status_driver = APAL_STATUS_ERROR;
238
    }
239

    
240

    
241

    
242
    // Signal rate minimum threshold. Measurements with signal rates below this value are ignored.
243
    //This ensures that false measurements are not made due to reflection from the housing.
244
    if (status == VL53L0X_ERROR_NONE) {
245
        status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
246
                VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 1);
247
    }else{
248
        status_driver = APAL_STATUS_ERROR;
249
    }
250

    
251
    if (status == VL53L0X_ERROR_NONE) {
252
        status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
253
                VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
254
                (FixPoint1616_t)(1.5*0.023*65536));
255
    }else{
256
        status_driver = APAL_STATUS_ERROR;
257
    }
258
    /* end static init and some tests */
259

    
260

    
261
    return status_driver;
262

    
263

    
264
}
265

    
266

    
267
/**
268
 * @brief vl53l0x_lld_set_mode set one of the thre possible modes. Same as VL53L0X_SetDeviceMode and VL53L0X_SetGpioConfig.
269
 * @param vl53l0x[in]
270
 * @param mode[in] one of the VL53L0X_DeviceModes, VL53L0X_DEVICEMODE_SINGLE_RANGING == 0, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING == 1,
271
 *                  VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING == 3
272
 * @param interrupt[in] set one of the different interrupt modes: VL53L0X_GPIOFUNCTIONALITY_OFF == 0 no interrupt,
273
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW == 1 Level Low (value < thresh_low),
274
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH == 2 Level High (value > thresh_high),
275
 *                      VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT == 3 value < thresh_low OR value > thresh_high,
276
 *                      VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY == 4 New Sample Ready.
277
 *                      The Interruptpolarity is set to low, an interrupt is created if HIGH changes to LOW.
278
 * @return
279
 */
280
apalExitStatus_t vl53l0x_lld_set_mode(VL53L0XDriver* vl53l0x, VL53L0X_DeviceModes mode, VL53L0X_GpioFunctionality interrupt,
281
                                      FixPoint1616_t low, FixPoint1616_t high)
282
{
283

    
284
    uint32_t ThresholdLow;
285
    uint32_t ThresholdHigh;
286
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
287
    apalExitStatus_t status_driver = APAL_STATUS_OK;
288

    
289
    if(status == VL53L0X_ERROR_NONE)
290
    {
291

    
292
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
293
        apalDbgPrintf("Call of VL53L0X_SetDeviceMode\n");
294
        status = VL53L0X_SetDeviceMode(&(vl53l0x->vl53l0x_dev), mode); // Setup in single ranging mode
295
        print_pal_error(status);
296
    }else{
297
        status_driver = APAL_STATUS_ERROR;
298
    }
299

    
300

    
301
    if(status == VL53L0X_ERROR_NONE)
302
    {
303
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
304
        apalDbgPrintf("Call of VL53L0X_SetGpioConfig\n");
305
        status = VL53L0X_SetGpioConfig(&(vl53l0x->vl53l0x_dev), 0, mode, interrupt, VL53L0X_INTERRUPTPOLARITY_HIGH);
306
        print_pal_error(status);
307
    }else{
308
        status_driver = APAL_STATUS_ERROR;
309
    }
310

    
311

    
312
    if(status == VL53L0X_ERROR_NONE && interrupt != VL53L0X_GPIOFUNCTIONALITY_OFF)
313
    {
314

    
315
        // multiplication with 65536 is imported, no clue why!
316
        // 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!
317
        if(mode == VL53L0X_DEVICEMODE_SINGLE_RANGING && (low > 254 || high > 254)){
318
            apalDbgPrintf("Device mode is VL53L0X_DEVICEMODE_SINGLE_RANGING and therefore we get no Interrupt if the interrupt "
319
                                                 "threshold is bigger than 254 mm\n");
320
        }
321

    
322
        ThresholdLow = low * 65536;
323
        ThresholdHigh = high * 65536;
324

    
325

    
326

    
327
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
328
        apalDbgPrintf("Call of VL53L0X_SetInterruptThresholds\n");
329
        status = VL53L0X_SetInterruptThresholds(&(vl53l0x->vl53l0x_dev),interrupt,ThresholdLow ,ThresholdHigh);
330
        print_pal_error(status);
331
        if(status != VL53L0X_ERROR_NONE)
332
        {
333
            status_driver = APAL_STATUS_ERROR;
334
        }
335
    }
336

    
337
    return status_driver;
338

    
339

    
340

    
341

    
342
    /*High Accurancy
343

344
    // Enable/Disable Sigma and Signal check
345
    if (Status == VL53L0X_ERROR_NONE) {
346
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
347
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
348
    }
349
    if (Status == VL53L0X_ERROR_NONE) {
350
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
351
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
352
    }
353

354
    if (Status == VL53L0X_ERROR_NONE) {
355
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
356
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
357
                (FixPoint1616_t)(0.25*65536));
358
    }
359
    if (Status == VL53L0X_ERROR_NONE) {
360
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
361
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
362
                (FixPoint1616_t)(18*65536));
363
    }
364
    if (Status == VL53L0X_ERROR_NONE) {
365
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
366
                200000);
367
    }
368

369
    */
370

    
371
    /*High Speed
372

373
    // Enable/Disable Sigma and Signal check
374
    if (Status == VL53L0X_ERROR_NONE) {
375
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
376
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
377
    }
378
    if (Status == VL53L0X_ERROR_NONE) {
379
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
380
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
381
    }
382

383
    if (Status == VL53L0X_ERROR_NONE) {
384
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
385
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
386
                (FixPoint1616_t)(0.25*65536));
387
    }
388
    if (Status == VL53L0X_ERROR_NONE) {
389
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
390
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
391
                (FixPoint1616_t)(32*65536));
392
    }
393
    if (Status == VL53L0X_ERROR_NONE) {
394
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
395
                30000);
396
    }
397

398

399
    */
400

    
401
    /*Long Range
402

403

404
    if (Status == VL53L0X_ERROR_NONE) {
405
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
406
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
407
    }
408
    if (Status == VL53L0X_ERROR_NONE) {
409
        Status = VL53L0X_SetLimitCheckEnable(&(vl53l0x->vl53l0x_dev),
410
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
411
    }
412

413
    if (Status == VL53L0X_ERROR_NONE) {
414
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
415
                VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
416
                (FixPoint1616_t)(0.1*65536));
417
    }
418
    if (Status == VL53L0X_ERROR_NONE) {
419
        Status = VL53L0X_SetLimitCheckValue(&(vl53l0x->vl53l0x_dev),
420
                VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
421
                (FixPoint1616_t)(60*65536));
422
    }
423
    if (Status == VL53L0X_ERROR_NONE) {
424
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&(vl53l0x->vl53l0x_dev),
425
                33000);
426
    }
427

428
    if (Status == VL53L0X_ERROR_NONE) {
429
        Status = VL53L0X_SetVcselPulsePeriod(&(vl53l0x->vl53l0x_dev),
430
                VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18);
431
    }
432
    if (Status == VL53L0X_ERROR_NONE) {
433
        Status = VL53L0X_SetVcselPulsePeriod(&(vl53l0x->vl53l0x_dev),
434
                VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 14);
435
    }
436

437
    */
438

    
439

    
440
}
441

    
442
/**
443
 * @brief vl53l0x_lld_start_measurement, use this method for interrupt mode. You must call vl53l0x_lld_set_mode beforehand.
444
 * @param vl53l0x
445
 * @return
446
 */
447
apalExitStatus_t vl53l0x_lld_start_measurement(VL53L0XDriver* vl53l0x)
448
{
449
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
450
    apalExitStatus_t status_driver = APAL_STATUS_OK;
451
    status = VL53L0X_StartMeasurement(&(vl53l0x->vl53l0x_dev));
452

    
453
    if(status == VL53L0X_ERROR_NONE)
454
    {
455
        // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
456
        apalDbgPrintf("Successful startet measurement\n");
457
        print_pal_error(status);
458
    }else{
459
        apalDbgPrintf("Error in startet measurement\n");
460
        print_pal_error(status);
461
        status_driver = APAL_STATUS_ERROR;
462
    }
463

    
464
    return status_driver;
465

    
466
}
467

    
468

    
469

    
470
/**
471
 * @brief vl53l0x_lld_stop_measurement, stops a measurement when the measurement were started with vl53l0x_lld_start_measurement
472
 * @param vl53l0x[in]
473
 * @return
474
 */
475
apalExitStatus_t vl53l0x_lld_stop_measurement(VL53L0XDriver* vl53l0x)
476
{
477

    
478
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
479
    apalExitStatus_t status_driver = APAL_STATUS_OK;
480
    status = VL53L0X_StopMeasurement(&(vl53l0x->vl53l0x_dev));
481
    if(status != VL53L0X_ERROR_NONE)
482
    {
483
        print_pal_error(status);
484
        status_driver = APAL_STATUS_ERROR;
485
    }
486

    
487
    return status_driver;
488
}
489

    
490
/**
491
 * @brief vl53l0x_lld_perform_mesaurement with delay and not interrupts ! No need to call vl53l0x_lld_set_mode.
492
 *
493
 * write 16 Byte to 80, FF, 00, 91, 00, FF 80, 00
494
 *  -> read 00 and get 00 back
495
 * listen on 13 till we get 44 back
496
 * write the data from sensor 14 -> 12 B , 12 single BYTES_PER_DWORD
497
 * clear things: 0B -> 01 and 0B -> 00
498
 *
499
 * @param vl53l0x
500
 * @param data
501
 * @param timeout
502
 * @return
503
 */
504
apalExitStatus_t vl53l0x_lld_perform_mesaurement(VL53L0XDriver* vl53l0x, uint16_t* data)
505
{
506
    //the information of the measurement is saved in although in vl53l0x->vl53l0x_dev.Data.LastRangeMeasure, but only after VL53L0X_PerformSingleRangingMeasurement
507
    // is called
508
    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
509
    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
510
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
511
    apalExitStatus_t status_driver = APAL_STATUS_OK;
512

    
513
    status = VL53L0X_PerformSingleMeasurement(&(vl53l0x->vl53l0x_dev));
514

    
515
    if (status == VL53L0X_ERROR_NONE)
516
        status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev),
517
            pRangingMeasurementData);
518
    else
519
        status_driver = APAL_STATUS_ERROR;
520

    
521

    
522
    if (status == VL53L0X_ERROR_NONE)
523
        status = VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), 0);
524
    else
525
        status_driver = APAL_STATUS_ERROR;
526

    
527

    
528

    
529
    data[0] = RangingMeasurementData.RangeMilliMeter;
530
    //apalDbgPrintf( "Measured distance: %i\n\n", RangingMeasurementData.RangeMilliMeter);
531

    
532

    
533
    //TODO write millimeter to data
534
    //data = &RangingMeasurementData.RangeMilliMeter;
535

    
536
    return status_driver;
537
}
538

    
539
/**
540
 * @brief vl53l0x_lld_CheckRangingDataReady custom method, because in api was a strange phenonom
541
 * @param vl53l0x[in]
542
 * @param pMeasurementDataReady[in]
543
 * @return
544
 */
545
apalExitStatus_t vl53l0x_lld_CheckRangingDataReady(VL53L0XDriver* vl53l0x, uint8_t *pMeasurementDataReady)
546
{
547
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
548
    apalExitStatus_t status_driver = APAL_STATUS_OK;
549

    
550
    uint8_t sysRangeStatusRegister;
551
    uint8_t interruptConfig;
552
    uint32_t interruptMask;
553
    //LOG_FUNCTION_START("");
554
   interruptConfig = vl53l0x->vl53l0x_dev.Data.DeviceSpecificParameters.Pin0GpioFunctionality;
555
    if (interruptConfig !=
556
        VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_DISABLED) {
557
        status = VL53L0X_GetInterruptMaskStatus(&(vl53l0x->vl53l0x_dev), &interruptMask);
558
        if (interruptMask == interruptConfig)
559
            *pMeasurementDataReady = 1;
560
        else
561
            *pMeasurementDataReady = 0;
562

    
563
    } else {
564
        status = VL53L0X_RdByte(&(vl53l0x->vl53l0x_dev), VL53L0X_REG_RESULT_RANGE_STATUS,
565
            &sysRangeStatusRegister);
566
        if (status == VL53L0X_ERROR_NONE) {
567
            if (sysRangeStatusRegister & 0x01)
568
                *pMeasurementDataReady = 1;
569
            else
570
                *pMeasurementDataReady = 0;
571
        }
572
    }
573

    
574
    if(status != VL53L0X_ERROR_NONE)
575
    {
576
        print_pal_error(status);
577
        status_driver = APAL_STATUS_ERROR;
578
    }
579

    
580

    
581
    //LOG_FUNCTION_END(status);
582
    return status_driver;
583
}
584

    
585
/**
586
 * @brief vl53l0x_lld_getRangingData
587
 * @param vl53l0x
588
 * @param pRangingMeasurementData
589
 * @return
590
 */
591
apalExitStatus_t vl53l0x_lld_getRangingData(VL53L0XDriver* vl53l0x, VL53L0X_RangingMeasurementData_t *pRangingMeasurementData)
592
{
593
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
594
    apalExitStatus_t status_driver = APAL_STATUS_OK;
595
    status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev),
596
        pRangingMeasurementData);
597

    
598
    status |= VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), 0);
599

    
600
    if(status != VL53L0X_ERROR_NONE)
601
    {
602
        print_pal_error(status);
603
        status_driver = APAL_STATUS_ERROR;
604
    }
605

    
606
    return status_driver;
607
}
608

    
609

    
610

    
611
/**
612
 * @brief vl53l0x_lld_set_address
613
 * @param vl53l0x
614
 * @param new_address
615
 * @return
616
 */
617
apalExitStatus_t vl53l0x_lld_set_address(VL53L0XDriver* vl53l0x, uint8_t new_address)
618
{
619

    
620
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
621
    apalExitStatus_t status_driver = APAL_STATUS_OK;
622
    status = VL53L0X_SetDeviceAddress(&(vl53l0x->vl53l0x_dev), new_address );
623
    if(status != VL53L0X_ERROR_NONE)
624
    {
625
        print_pal_error(status);
626
        status_driver = APAL_STATUS_ERROR;
627
    }
628

    
629
    return status_driver;
630
}
631

    
632

    
633
/*
634

635

636
  examples for continous getting examples without interrupt
637

638

639

640

641
    uint32_t measurement;
642
    uint32_t no_of_measurements = 32;
643

644
    uint16_t* pResults = (uint16_t*)malloc(sizeof(uint16_t) * no_of_measurements);
645

646
    for(measurement=0; measurement<no_of_measurements; measurement++)
647
    {
648

649
        Status = WaitMeasurementDataReady(&(vl53l0x->vl53l0x_dev));
650

651
        if(Status == VL53L0X_ERROR_NONE)
652
        {
653
            Status = VL53L0X_GetRangingMeasurementData(&(vl53l0x->vl53l0x_dev), pRangingMeasurementData);
654

655
            *(pResults + measurement) = pRangingMeasurementData->RangeMilliMeter;
656
            printf("In loop measurement %ld: %d\n", measurement, pRangingMeasurementData->RangeMilliMeter);
657

658
            // Clear the interrupt
659
            VL53L0X_ClearInterruptMask(&(vl53l0x->vl53l0x_dev), VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
660
            usleep(20);
661
            //VL53L0X_PollingDelay(&(vl53l0x->vl53l0x_dev);
662
        } else {
663
            break;
664
        }
665
    }
666

667
    if(Status == VL53L0X_ERROR_NONE)
668
    {
669
        for(measurement=0; measurement<no_of_measurements; measurement++)
670
        {
671
            printf("measurement %ld: %d\n", measurement, *(pResults + measurement));
672
        }
673
    }
674
    data = &pResults[0];
675

676
    free(pResults);
677

678

679

680
    example for measurement with interrupt single
681

682

683
    example for measurement with interrupt continious
684

685
*/
686

    
687

    
688
/**
689
 * @brief WaitMeasurementDataReady.
690
 * @param Dev
691
 * @return
692
 */
693
VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
694
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
695
    uint8_t NewDatReady=0;
696
    uint32_t LoopNb;
697

    
698
    // Wait until it finished
699
    // use timeout to avoid deadlock
700
    if (Status == VL53L0X_ERROR_NONE) {
701
        LoopNb = 0;
702
        do {
703
            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
704
            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
705
                break;
706
            }
707
            LoopNb = LoopNb + 1;
708
            // sleep for 50 ms
709
            usleep(50);
710
        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
711

    
712
        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
713
            Status = VL53L0X_ERROR_TIME_OUT;
714
        }
715
    }
716

    
717
    return Status;
718
}
719

    
720

    
721

    
722
/*============================================================================*/
723
/* threshold register access                                                  */
724
/*============================================================================*/
725

    
726

    
727
#endif /* defined(AMIROLLD_CFG_VL53L0X) && (AMIROLLD_CFG_VL53L0X == 1) */
728

    
729
/** @} */
730

    
731

    
732

    
733