Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / VL53L1X / v1 / alld_VL53L1X.c @ f0dd1ac4

History | View | Annotate | Download (24.283 KB)

1 f0dd1ac4 Thomas Schöpping
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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_VL53L1X.c
21
 * @brief   ToF sensor function implementations.
22
 *
23
 * @addtogroup lld_vl53l1x
24
 * @{
25
 */
26
27 4dba9195 galberding
#include <alld_VL53L1X.h>
28
#include <vl53l1_api.h>
29 f0dd1ac4 Thomas Schöpping
#include <string.h>
30 4dba9195 galberding
31 f0dd1ac4 Thomas Schöpping
/******************************************************************************/
32
/* LOCAL DEFINITIONS                                                          */
33
/******************************************************************************/
34 4dba9195 galberding
35 f0dd1ac4 Thomas Schöpping
/**
36
 * @brief   I2C communication timeout (in microseconds).
37
 * @details Cutsom values can be set via alldconf.h file.
38
 */
39
#if !defined(VL53L1X_LLD_I2C_TIMEOUT) || defined(__DOXYGEN__)
40
# define VL53L1X_LLD_I2C_TIMEOUT               ((apalTime_t)1000)
41
#endif
42
43
/******************************************************************************/
44
/* EXPORTED VARIABLES                                                         */
45
/******************************************************************************/
46
47
/******************************************************************************/
48
/* LOCAL TYPES                                                                */
49
/******************************************************************************/
50
51
/******************************************************************************/
52
/* LOCAL VARIABLES                                                            */
53
/******************************************************************************/
54
55
/******************************************************************************/
56
/* LOCAL FUNCTIONS                                                            */
57
/******************************************************************************/
58
59
/******************************************************************************/
60
/* EXPORTED FUNCTIONS                                                         */
61
/******************************************************************************/
62
63
/* interface functions for ST API *********************************************/
64 4dba9195 galberding
65 f0dd1ac4 Thomas Schöpping
/**
66
 * @brief   Initialization of communication interface (I2C).
67
 * @details Function not supported.
68
 *          Initialization must be done by OS or application instead.
69
 *
70
 * @param[in] pdev              VL53L1X device handle.
71
 * @param[in] comms_type        I2C speed class (?).
72
 * @param[in] comms_speed_khz   I2C transmission frequency.
73
 *
74
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
75
 */
76
VL53L1_Error VL53L1_CommsInitialise(VL53L1_Dev_t *pdev, uint8_t comms_type, uint16_t comms_speed_khz)
77
{
78
  apalDbgAssert(pdev != NULL);
79
80
  (void)pdev;
81
  (void)comms_type;
82
  (void)comms_speed_khz;
83
84
  return VL53L1_ERROR_NOT_SUPPORTED;
85 4dba9195 galberding
}
86
87
/**
88 f0dd1ac4 Thomas Schöpping
 * @brief   Deinitialization of communication interface (I2C).
89
 * @details Function not supported.
90
 *          Deinitialization must be done by OS or application instead.
91
 *
92
 * @param[in] pdev  VL53L1X device handle.
93
 *
94
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
95 4dba9195 galberding
 */
96 f0dd1ac4 Thomas Schöpping
VL53L1_Error VL53L1_CommsClose(VL53L1_Dev_t *pdev)
97 4dba9195 galberding
{
98 f0dd1ac4 Thomas Schöpping
  apalDbgAssert(pdev != NULL);
99
100
  (void)pdev;
101
102
  return VL53L1_ERROR_NOT_SUPPORTED;
103 4dba9195 galberding
}
104
105 f0dd1ac4 Thomas Schöpping
/**
106
 * @brief   Transmit multiple bytes of data via I2C.
107
 *
108
 * @param[in] pdev    Device handle.
109
 * @param[in] index   Register address to write to.
110
 * @param[in] pdata   Data buffer to be transmitted.
111
 * @param[in] count   Number of bytes to transmit.
112
 *
113
 * @return  Status indicating whether the call was successful.
114
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
115
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
116
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
117
 */
118
VL53L1_Error VL53L1_WriteMulti(VL53L1_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
119
{
120
  apalDbgAssert(pdev != NULL);
121 4dba9195 galberding
122 f0dd1ac4 Thomas Schöpping
  // prepare transmit buffer with prepended address bytes.
123
  uint8_t txbuf[sizeof(uint16_t) + count];
124
  txbuf[0] = index >> 8;
125
  txbuf[1] = index & 0xFF;
126
  memcpy(&txbuf[2], pdata, count);
127 4dba9195 galberding
128 f0dd1ac4 Thomas Schöpping
  // transmit address and data
129
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), NULL, 0, VL53L1X_LLD_I2C_TIMEOUT);
130 4dba9195 galberding
131 f0dd1ac4 Thomas Schöpping
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
132
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
133
}
134 4dba9195 galberding
135 f0dd1ac4 Thomas Schöpping
/**
136
 * @brief   Read multiple bytes of data via I2C.
137
 *
138
 * @param[in]   pdev    Device handle.
139
 * @param[in]   index   Register address to read from.
140
 * @param[out]  pdata   Data buffer to store read data to.
141
 * @param[in]   count   Number of bytes to read.
142
 *
143
 * @return  Status indicating whether the call was successful.
144
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
145
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
146
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
147
 */
148
VL53L1_Error VL53L1_ReadMulti(VL53L1_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
149
{
150
  apalDbgAssert(pdev != NULL);
151 4dba9195 galberding
152 f0dd1ac4 Thomas Schöpping
  // prepare address buffer
153
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
154 4dba9195 galberding
155 f0dd1ac4 Thomas Schöpping
  // transmit address and recieve data
156
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), pdata, count, VL53L1X_LLD_I2C_TIMEOUT);
157 4dba9195 galberding
158 f0dd1ac4 Thomas Schöpping
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
159
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
160
}
161 4dba9195 galberding
162 f0dd1ac4 Thomas Schöpping
/**
163
 * @brief   Transmit a single byte via I2C.
164
 *
165
 * @param[in] pdev              Device handle.
166
 * @param[in] index             Register address to write to.
167
 * @param[in] VL53L1_PRM_00005  Byte to transmit.
168
 *
169
 * @return  Status indicating whether the call was successful.
170
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
171
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
172
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
173
 */
174
VL53L1_Error VL53L1_WrByte(VL53L1_Dev_t *pdev, uint16_t index, uint8_t VL53L1_PRM_00005)
175
{
176
  apalDbgAssert(pdev != NULL);
177
178
  // prepare transmit buffer with prepended address bytes.
179
  uint8_t txbuf[sizeof(uint16_t) + sizeof(uint8_t)] = {index >> 8, index & 0xFF,
180
                                                       VL53L1_PRM_00005};
181 4dba9195 galberding
182 f0dd1ac4 Thomas Schöpping
  // transmit address and data
183
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), NULL, 0, VL53L1X_LLD_I2C_TIMEOUT);
184 4dba9195 galberding
185 f0dd1ac4 Thomas Schöpping
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
186
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
187
}
188
189
/**
190
 * @brief   Transmit a 16-bit data word via I2C.
191
 *
192
 * @param[in] pdev              Device handle.
193
 * @param[in] index             Register address to write to.
194
 * @param[in] VL53L1_PRM_00005  16-bit data word to transmit.
195
 *
196
 * @return  Status indicating whether the call was successful.
197
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
198
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
199
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
200 4dba9195 galberding
 */
201 f0dd1ac4 Thomas Schöpping
VL53L1_Error VL53L1_WrWord(VL53L1_Dev_t *pdev, uint16_t index, uint16_t VL53L1_PRM_00005)
202
{
203
  apalDbgAssert(pdev != NULL);
204
205
  // prepare transmit buffer with prepended address bytes.
206
  uint8_t txbuf[sizeof(uint16_t) + sizeof(uint16_t)] = {index >> 8, index & 0xFF,
207
                                                        (VL53L1_PRM_00005 >> 8) & 0xFF,
208
                                                        (VL53L1_PRM_00005 >> 0) & 0xFF};
209 4dba9195 galberding
210 f0dd1ac4 Thomas Schöpping
  // transmit address and data
211
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), NULL, 0, VL53L1X_LLD_I2C_TIMEOUT);
212
213
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
214
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
215
}
216
217
/**
218
 * @brief   Transmit a 32-bit data word via I2C.
219
 *
220
 * @param[in] pdev              Device handle.
221
 * @param[in] index             Register address to write to.
222
 * @param[in] VL53L1_PRM_00005  32-bit data word to transmit.
223
 *
224
 * @return  Status indicating whether the call was successful.
225
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
226
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
227
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
228 4dba9195 galberding
 */
229 f0dd1ac4 Thomas Schöpping
VL53L1_Error VL53L1_WrDWord(VL53L1_Dev_t *pdev,uint16_t index, uint32_t VL53L1_PRM_00005)
230
{
231
  apalDbgAssert(pdev != NULL);
232
233
  // prepare transmit buffer with prepended address bytes.
234
  uint8_t txbuf[sizeof(uint16_t) + sizeof(uint32_t)] = {index >> 8, index & 0xFF,
235
                                                        (VL53L1_PRM_00005 >> 24) & 0xFF,
236
                                                        (VL53L1_PRM_00005 >> 16) & 0xFF,
237
                                                        (VL53L1_PRM_00005 >> 8) & 0xFF,
238
                                                        (VL53L1_PRM_00005 >> 0) & 0xFF};
239 4dba9195 galberding
240 f0dd1ac4 Thomas Schöpping
  // transmit address and data
241
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), NULL, 0, VL53L1X_LLD_I2C_TIMEOUT);
242 4dba9195 galberding
243 f0dd1ac4 Thomas Schöpping
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
244
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
245
}
246 4dba9195 galberding
247 f0dd1ac4 Thomas Schöpping
/**
248
 * @brief   Read a single byte via I2C.
249
 *
250
 * @param[in]   pdev    Device handle.
251
 * @param[in]   index   Register address to read from.
252
 * @param[out]  pdata   Single byte buffer to store the read data to.
253
 *
254
 * @return  Status indicating whether the call was successful.
255
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
256
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
257
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
258
 */
259
VL53L1_Error VL53L1_RdByte(VL53L1_Dev_t *pdev, uint16_t index, uint8_t *pdata)
260
{
261
  apalDbgAssert(pdev != NULL);
262 4dba9195 galberding
263 f0dd1ac4 Thomas Schöpping
  // prepare address buffer
264
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
265
266
  // transmit address and recieve data
267
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), pdata, 1, VL53L1X_LLD_I2C_TIMEOUT);
268
269
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
270
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
271 4dba9195 galberding
}
272
273 f0dd1ac4 Thomas Schöpping
/**
274
 * @brief   Read a 16-bit data word via I2C.
275
 *
276
 * @param[in]   pdev    Device handle.
277
 * @param[in]   index   Register address to read from.
278
 * @param[out]  pdata   16-bit buffer to store the read data to.
279
 *
280
 * @return  Status indicating whether the call was successful.
281
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
282
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
283
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
284
 */
285
VL53L1_Error VL53L1_RdWord(VL53L1_Dev_t *pdev,uint16_t index, uint16_t *pdata)
286
{
287
  apalDbgAssert(pdev != NULL);
288
289
  // prepare address buffer
290
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
291
  // prepare receive buffer
292
  uint8_t rxbuf[sizeof(uint16_t)];
293
294
  // transmit address and recieve data
295
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), rxbuf, sizeof(rxbuf), VL53L1X_LLD_I2C_TIMEOUT);
296
297
  // fill return value
298
  *pdata = (((uint16_t)rxbuf[0]) << 8) &
299
           (((uint16_t)rxbuf[1]) << 0);
300 4dba9195 galberding
301 f0dd1ac4 Thomas Schöpping
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
302
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
303 4dba9195 galberding
}
304
305 f0dd1ac4 Thomas Schöpping
/**
306
 * @brief   Read a 32-bit data word via I2C.
307
 *
308
 * @param[in]   pdev    Device handle.
309
 * @param[in]   index   Register address to read from.
310
 * @param[out]  pdata   32-bit buffer to store the read data to.
311
 *
312
 * @return  Status indicating whether the call was successful.
313
 * @retval  VL53L1_ERROR_NONE       Transmission was successful.
314
 * @retval  VL53L1_ERROR_TIME_OUT   Transmission timed out.
315
 * @retval  VL53L1_ERROR_UNDEFINED  Transmission failed for some other reason.
316
 */
317
VL53L1_Error VL53L1_RdDWord(VL53L1_Dev_t *pdev,uint16_t index, uint32_t *pdata)
318
{
319
  apalDbgAssert(pdev != NULL);
320
321
  // prepare address buffer
322
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
323
  // prepare receive buffer
324
  uint8_t rxbuf[sizeof(uint32_t)];
325 4dba9195 galberding
326 f0dd1ac4 Thomas Schöpping
  apalExitStatus_t status = apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), rxbuf, sizeof(rxbuf), VL53L1X_LLD_I2C_TIMEOUT);
327 4dba9195 galberding
328 f0dd1ac4 Thomas Schöpping
  // fill return value
329
  *pdata = (((uint32_t)rxbuf[0]) << 24) &
330
           (((uint32_t)rxbuf[1]) << 16) &
331
           (((uint32_t)rxbuf[2]) << 8) &
332
           (((uint32_t)rxbuf[3]) << 0);
333
334
  return (status == APAL_STATUS_OK || status > 0) ? VL53L1_ERROR_NONE :
335
         (status == APAL_STATUS_TIMEOUT) ? VL53L1_ERROR_TIME_OUT : VL53L1_ERROR_UNDEFINED;
336 4dba9195 galberding
}
337
338 f0dd1ac4 Thomas Schöpping
/**
339
 * @brief   Wait for a specified amount of microseconds.
340
 *
341
 * @param[in] pdev      Device handle.
342
 * @param[in] wait_us   Time to wait in microsecnonds.
343
 *
344
 * @return  Always returns VL53L1_ERROR_NONE.
345
 */
346
VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us)
347
{
348
  (void)pdev;
349
350
  apalSleep(wait_us);
351
352
  return VL53L1_ERROR_NONE;
353 4dba9195 galberding
}
354
355 f0dd1ac4 Thomas Schöpping
/**
356
 * @brief   Wait for a specified amount of milliseconds.
357
 *
358
 * @param[in] pdev      Device handle.
359
 * @param[in] wait_ms   Time to wait in milliseconds.
360
 *
361
 * @return  Always returns VL53L1_ERROR_NONE.
362
 */
363
VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms)
364
{
365
  (void)pdev;
366 4dba9195 galberding
367 f0dd1ac4 Thomas Schöpping
  apalSleep(wait_ms * 1000);
368 4dba9195 galberding
369 f0dd1ac4 Thomas Schöpping
  return VL53L1_ERROR_NONE;
370
}
371 4dba9195 galberding
372 f0dd1ac4 Thomas Schöpping
/**
373
 * @brief   Retrieve the frequency of a timer?
374
 * @details Function not supported since it is not used by the ST API.
375
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
376
 *
377
 * @param[out]  ptimer_freq_hz  Buffer to store the timer frequency to.
378
 *
379
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
380
 */
381
VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz)
382
{
383
  apalDbgAssert(ptimer_freq_hz != NULL);
384
385
  (void)ptimer_freq_hz;
386
387
  return VL53L1_ERROR_NOT_SUPPORTED;
388 4dba9195 galberding
}
389
390 f0dd1ac4 Thomas Schöpping
/**
391
 * @brief   Retrieve the current value of a timer?
392
 * @details Function not supported since it is not used by the ST API.
393
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
394
 *
395
 * @param[out]  ptimer_count  Buffer to store the current timer value to.
396
 *
397
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
398
 */
399
VL53L1_Error VL53L1_GetTimerValue(int32_t *ptimer_count)
400
{
401
  apalDbgAssert(ptimer_count != NULL);
402
403
  (void)ptimer_count;
404
405
  return VL53L1_ERROR_NOT_SUPPORTED;
406 4dba9195 galberding
}
407
408 f0dd1ac4 Thomas Schöpping
/**
409
 * @brief   Set the mode for a GPIO.
410
 * @details Function not supported since it is not used by the ST API.
411
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
412
 *
413
 * @param[in] pin   GPIO pin to modify.
414
 * @param[in] mode  Mode to set for the GPIO.
415
 *
416
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
417
 */
418
VL53L1_Error VL53L1_GpioSetMode(uint8_t pin, uint8_t mode)
419
{
420
  (void)pin;
421
  (void)mode;
422
423
  return VL53L1_ERROR_NOT_SUPPORTED;
424 4dba9195 galberding
}
425
426 f0dd1ac4 Thomas Schöpping
/**
427
 * @brief   Set the output value for a GPIO.
428
 * @details Function not supported since it is not used by the ST API.
429
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
430
 *
431
 * @param[in] pin     GPIO pin to set.
432
 * @param[in] value   Value to set for the GPIO.
433
 *
434
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
435
 */
436
VL53L1_Error VL53L1_GpioSetValue(uint8_t pin, uint8_t value)
437
{
438
  (void)pin;
439
  (void)value;
440
441
  return VL53L1_ERROR_NOT_SUPPORTED;
442 4dba9195 galberding
}
443
444 f0dd1ac4 Thomas Schöpping
/**
445
 * @brief   Read the input value of a GPIO.
446
 * @details Function not supported since it is not used by the ST API.
447
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
448
 *
449
 * @param[in]   pin     GPIO pin to read.
450
 * @param[out]  pvalue  Buffer to write the read value to.
451
 *
452
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
453
 */
454
VL53L1_Error VL53L1_GpioGetValue(uint8_t pin, uint8_t *pvalue)
455
{
456
  (void)pin;
457
  (void)pvalue;
458 4dba9195 galberding
459 f0dd1ac4 Thomas Schöpping
  return VL53L1_ERROR_NOT_SUPPORTED;
460 4dba9195 galberding
}
461
462 f0dd1ac4 Thomas Schöpping
/**
463
 * @brief   Set to XSHUT GPIO.
464
 * @details Function not supported since it is not used by the ST API.
465
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
466
 *
467
 * @param[in] value   Value to set the XSHUT GPIO to.
468
 *
469
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
470
 */
471
VL53L1_Error VL53L1_GpioXshutdown(uint8_t value)
472
{
473
  (void)value;
474
475
  return VL53L1_ERROR_NOT_SUPPORTED;
476 4dba9195 galberding
}
477
478 f0dd1ac4 Thomas Schöpping
/**
479
 * @brief   Select a GPIO communication interface?
480
 * @details Function not supported since it is not used by the ST API.
481
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
482
 *
483
 * @param[in] value   Identifier of the interface to select?
484
 *
485
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
486
 */
487
VL53L1_Error VL53L1_GpioCommsSelect(uint8_t value)
488
{
489
  (void)value;
490
491
  return VL53L1_ERROR_NOT_SUPPORTED;
492 4dba9195 galberding
}
493
494 f0dd1ac4 Thomas Schöpping
/**
495
 * @brief   Enable power?
496
 * @details Function not supported since it is not used by the ST API.
497
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
498
 *
499
 * @param[in] value   ?
500
 *
501
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
502
 */
503
VL53L1_Error VL53L1_GpioPowerEnable(uint8_t value)
504 4dba9195 galberding
{
505 f0dd1ac4 Thomas Schöpping
  (void)value;
506
507
  return VL53L1_ERROR_NOT_SUPPORTED;
508 4dba9195 galberding
}
509
510 f0dd1ac4 Thomas Schöpping
/**
511
 * @brief   Enable GPIO interrupt.
512
 * @details Function not supported since it is not used by the ST API.
513
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
514
 *
515
 * @param[in] fucntion    Callback function to be executed on interrupt.
516
 * @param[in] edge_type   Signal edge to trigger an interrupts.
517
 *
518
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
519
 */
520
VL53L1_Error VL53L1_GpioInterruptEnable(void (*function)(void), uint8_t edge_type)
521
{
522
  (void)function;
523
  (void)edge_type;
524 4dba9195 galberding
525 f0dd1ac4 Thomas Schöpping
  return VL53L1_ERROR_NOT_SUPPORTED;
526
}
527 4dba9195 galberding
528 f0dd1ac4 Thomas Schöpping
/**
529
 * @brief   Disable GPIO interrupt.
530
 * @details Function not supported since it is not used by the ST API.
531
 * @note    Intention of this function is unknown as it is neither documented nor used by the ST API.
532
 *
533
 * @return  Always returns VL53L1_ERROR_NOT_SUPPORTED.
534
 */
535
VL53L1_Error VL53L1_GpioInterruptDisable(void)
536 4dba9195 galberding
{
537 f0dd1ac4 Thomas Schöpping
  return VL53L1_ERROR_NOT_SUPPORTED;
538 4dba9195 galberding
}
539
540 f0dd1ac4 Thomas Schöpping
/**
541
 * @brief   Retrieve the current system tick count.
542
 *
543
 * @param[out]  ptime_ms  Buffer to store the current system tick count to.
544
 *
545
 * @return  Always returns VL53L1_ERROR_NONE.
546
 */
547
VL53L1_Error VL53L1_GetTickCount(uint32_t *ptime_ms)
548
{
549
  *ptime_ms = apalGetTime() / 1000;
550
551
  return VL53L1_ERROR_NONE;
552 4dba9195 galberding
}
553
554 f0dd1ac4 Thomas Schöpping
/**
555
 * @brief   Wait for a specific register to match a (masked) value.
556
 *
557
 * @param[in] pdev            Device handle.
558
 * @param[in] timeout_ms      Timeout value in milliseconds.
559
 * @param[in] index           Register address to check.
560
 * @param[in] value           Value to be matched.
561
 * @param[in] mask            Mask to apply to the register value.
562
 *                            Must not be 0x00.
563
 * @param[in] poll_delay_ms   Poll delay in milliseconds.
564
 *
565
 * @return  Status indicating whether the call was successful.
566
 * @retval  VL53L1_ERROR_NONE       Value matched before timeout.
567
 * @retval  VL53L1_ERROR_TIME_OUT   Value did not match before timeout occurred.
568
 * @retval  VL53L1_ERROR_UNDEFINED  I2C communication failed.
569
 */
570
VL53L1_Error VL53L1_WaitValueMaskEx(VL53L1_Dev_t *pdev, uint32_t timeout_ms, uint16_t index, uint8_t value, uint8_t mask, uint32_t poll_delay_ms)
571
{
572
  apalDbgAssert(pdev != NULL);
573
  apalDbgAssert(mask != 0x00);
574
575
  // local variables
576
  const apalTime_t starttime = apalGetTime();
577
  apalTime_t transmissionstartoffset, transmissionendoffset;
578
  apalTime_t remaintime = timeout_ms * 1000;
579
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
580
  uint8_t rxbuf[sizeof(uint8_t)];
581
582
  // infinite loop will return from within
583
  for(;;) {
584
    // measure time before transmission
585
    transmissionstartoffset = apalGetTime() - starttime;
586
587
    // try transmission
588
    if (apalI2CMasterTransmit(pdev->Interface.i2cd, pdev->Interface.addr, txbuf, sizeof(txbuf), rxbuf, sizeof(rxbuf), (remaintime < VL53L1X_LLD_I2C_TIMEOUT) ? remaintime : VL53L1X_LLD_I2C_TIMEOUT) < 0) {
589
      return VL53L1_ERROR_UNDEFINED;
590
    }
591
592
    // check returned data
593
    if ((rxbuf[0] & mask) == value) {
594
      return VL53L1_ERROR_NONE;
595
    }
596
597
    // measure time after transmission
598
    transmissionendoffset = apalGetTime() - starttime;
599
600
    // check for timeout and recalculate remaining time
601
    if (transmissionendoffset < timeout_ms * 1000) {
602
      remaintime = (timeout_ms * 1000) - transmissionendoffset;
603
    } else {
604
      return VL53L1_ERROR_TIME_OUT;
605
    }
606
607
    // sleep one poll delay, or the remaining time
608
    if (remaintime > (poll_delay_ms * 1000) - (transmissionendoffset - transmissionstartoffset)) {
609
      apalSleep((poll_delay_ms * 1000) - (transmissionendoffset - transmissionstartoffset));
610
    } else {
611
      apalSleep(remaintime);
612
      return VL53L1_ERROR_TIME_OUT;
613
    }
614
615
    // recalculate remaining time
616
    remaintime = apalGetTime() - starttime;
617
    if (remaintime < timeout_ms * 1000) {
618
      remaintime = (timeout_ms * 1000) - remaintime;
619
    } else {
620
      return VL53L1_ERROR_TIME_OUT;
621
    }
622
  }
623 4dba9195 galberding
}
624
625 f0dd1ac4 Thomas Schöpping
/* driver functions ***********************************************************/
626
627
/**
628
 * @brief   Read the current activation state of the device.
629
 *
630
 * @param[in]   vl53l1x   Driver object to check.
631
 * @param[out]  state     State of the device.
632
 *
633
 * @return  Indicator whether the call was successful.
634
 */
635
apalExitStatus_t vl53l1x_lld_getState(VL53L1XDriver* vl53l1x, vl53l1x_lld_state_t* state)
636 4dba9195 galberding
{
637 f0dd1ac4 Thomas Schöpping
  apalDbgAssert(vl53l1x != NULL);
638
  apalDbgAssert(state != NULL);
639
640
  apalControlGpioState_t gpio_state;
641
  apalExitStatus_t status = apalControlGpioGet(vl53l1x->Interface.xshut, &gpio_state);
642
  *state = gpio_state == APAL_GPIO_ON ? VL53L1X_LLD_STATE_ON : VL53L1X_LLD_STATE_OFF;
643
644
  return status;
645 4dba9195 galberding
}
646
647 f0dd1ac4 Thomas Schöpping
/**
648
 * @brief   Performs initialization procedure according to manual after device
649
 *          brought out of reset.
650
 *
651
 * @param[in,out] vl53l1x   Device to be initialized.
652
 *
653
 * @return  Indicator whether the call was successful.
654
 */
655
apalExitStatus_t vl53l1x_lld_init(VL53L1XDriver* vl53l1x)
656
{
657
  apalDbgAssert(vl53l1x != NULL);
658
659
  // local variables
660
  apalExitStatus_t status;
661
  VL53L1_Error errval;
662
663
  // check whether the device is already active
664
  {
665
    vl53l1x_lld_state_t xshut_state;
666
    status = vl53l1x_lld_getState(vl53l1x, &xshut_state);
667
    if (status != APAL_STATUS_OK) {
668
      return APAL_STATUS_ERROR;
669
    }
670
    // return warning if the device is already active
671
    if (xshut_state == VL53L1X_LLD_STATE_ON) {
672
      return APAL_STATUS_WARNING;
673
    }
674
  }
675
676
  // activate the device
677
  apalDbgPrintf("apalControlGpioSet()\n");
678
  status |= apalControlGpioSet(vl53l1x->Interface.xshut, APAL_GPIO_ON);
679
  apalDbgPrintf("\tstatus: %d\n", status);
680
681
  // initialize device as described in manual
682
  apalDbgPrintf("VL53L1_WaitDeviceBooted()\n");
683
  errval = VL53L1_WaitDeviceBooted(vl53l1x);
684
  status |= (errval == VL53L1_ERROR_NONE) ? APAL_STATUS_SUCCESS : APAL_STATUS_ERROR;
685
  apalDbgPrintf("\tstatus: %d\terrval: %d\n", status, errval);
686
  apalDbgPrintf("VL53L1_DataInit()\n");
687
  errval = VL53L1_DataInit(vl53l1x);
688
  status |= (errval == VL53L1_ERROR_NONE) ? APAL_STATUS_SUCCESS : APAL_STATUS_ERROR;
689
  apalDbgPrintf("\tstatus: %d\terrval: %d\n", status, errval);
690
  apalDbgPrintf("VL53L1_StaticInit()\n");
691
  errval = VL53L1_StaticInit(vl53l1x);
692
  status |= (errval == VL53L1_ERROR_NONE) ? APAL_STATUS_SUCCESS : APAL_STATUS_ERROR;
693
  apalDbgPrintf("\tstatus: %d\terrval: %d\n", status, errval);
694
695
  return status;
696
}
697 4dba9195 galberding
698 f0dd1ac4 Thomas Schöpping
/**
699
 * @brief   Reset a device by deactivating the XSHUT signal.
700
 *
701
 * @param[in] vl53l1x   Device to reset.
702
 *
703
 * @return  Indicator whether the call was successful.
704
 */
705
apalExitStatus_t vl53l1x_lld_reset(VL53L1XDriver* vl53l1x)
706
{
707
  return apalControlGpioSet(vl53l1x->Interface.xshut, APAL_GPIO_OFF);
708
}
709 4dba9195 galberding
710 f0dd1ac4 Thomas Schöpping
/** @} */