Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / VL53L1X / v1 / alld_VL53L1X.c @ 3ed0cc4d

History | View | Annotate | Download (24.283 KB)

1 3ed0cc4d 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..2019  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 3ed0cc4d Thomas Schöpping
#include <string.h>
30 4dba9195 galberding
31 3ed0cc4d Thomas Schöpping
/******************************************************************************/
32
/* LOCAL DEFINITIONS                                                          */
33
/******************************************************************************/
34 4dba9195 galberding
35 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d Thomas Schöpping
VL53L1_Error VL53L1_CommsClose(VL53L1_Dev_t *pdev)
97 4dba9195 galberding
{
98 3ed0cc4d Thomas Schöpping
  apalDbgAssert(pdev != NULL);
99
100
  (void)pdev;
101
102
  return VL53L1_ERROR_NOT_SUPPORTED;
103 4dba9195 galberding
}
104
105 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d Thomas Schöpping
  // prepare address buffer
153
  uint8_t txbuf[sizeof(uint16_t)] = {index >> 8, index & 0xFF};
154 4dba9195 galberding
155 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 3ed0cc4d 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 da