-
Notifications
You must be signed in to change notification settings - Fork 25
/
vl53l1_platform.cpp
executable file
·304 lines (259 loc) · 9.41 KB
/
vl53l1_platform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*******************************************************************************
This file is derived from vl53l1_platform.c in the STSW-IMG007 VL53L1X API.
********************************************************************************
*
* Copyright (c) 2017, STMicroelectronics - All Rights Reserved
* Copyright (c) 2018 Pololu Corporation
*
* This file is part of VL53L1 Core and is dual licensed,
* either 'STMicroelectronics
* Proprietary license'
* or 'BSD 3-clause "New" or "Revised" License' , at your option.
*
********************************************************************************
*
* 'STMicroelectronics Proprietary license'
*
********************************************************************************
*
* License terms: STMicroelectronics Proprietary in accordance with licensing
* terms at www.st.com/sla0081
*
* STMicroelectronics confidential
* Reproduction and Communication of this document is strictly prohibited unless
* specifically authorized in writing by STMicroelectronics.
*
*
********************************************************************************
*
* Alternatively, VL53L1 Core may be distributed under the terms of
* 'BSD 3-clause "New" or "Revised" License', in which case the following
* provisions apply instead of the ones mentioned above :
*
********************************************************************************
*
* License terms: BSD 3-clause "New" or "Revised" License.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
********************************************************************************
*
*/
#include <Arduino.h>
#include <Wire.h>
#include "vl53l1_platform.h"
// #include "vl53l1_platform_log.h"
#include "vl53l1_api.h"
// #include "stm32xxx_hal.h"
#include <string.h>
// #include <time.h>
// #include <math.h>
// #define I2C_TIME_OUT_BASE 10
// #define I2C_TIME_OUT_BYTE 1
// #ifdef VL53L1_LOG_ENABLE
// #define trace_print(level, ...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_PLATFORM, level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
// #define trace_i2c(...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_NONE, VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__)
// #endif
// #ifndef HAL_I2C_MODULE_ENABLED
// #warning "HAL I2C module must be enable "
// #endif
//extern I2C_HandleTypeDef hi2c1;
//#define VL53L0X_pI2cHandle (&hi2c1)
/* when not customized by application define dummy one */
// #ifndef VL53L1_GetI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
// # define VL53L1_GetI2cBus(...) (void)0
// #endif
// #ifndef VL53L1_PutI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
// # define VL53L1_PutI2cBus(...) (void)0
// #endif
// uint8_t _I2CBuffer[256];
// int _I2CWrite(VL53L1_DEV Dev, uint8_t *pdata, uint32_t count) {
// int status = 0;
// return status;
// }
// int _I2CRead(VL53L1_DEV Dev, uint8_t *pdata, uint32_t count) {
// int status = 0;
// return Status;
// }
VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
while (count > 0)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
uint8_t writing = 0;
while (count > 0 && Wire.write(*pdata) != 0)
{
pdata++;
writing++;
count--;
}
if (writing == 0 || Wire.endTransmission() != 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
index += writing;
}
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
if (Wire.endTransmission() != 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
while (count > 0)
{
uint8_t reading = Wire.requestFrom(Dev->I2cDevAddr >> 1, count);
if (reading == 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
count -= reading;
while (reading-- > 0)
{
*pdata++ = Wire.read();
}
}
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
Wire.write(data);
return (Wire.endTransmission() == 0 ? VL53L1_ERROR_NONE : VL53L1_ERROR_CONTROL_INTERFACE);
}
VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
Wire.write((data >> 8) & 0xff);
Wire.write(data & 0xff);
return (Wire.endTransmission() == 0 ? VL53L1_ERROR_NONE : VL53L1_ERROR_CONTROL_INTERFACE);
}
VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
Wire.write((data >> 24) & 0xff);
Wire.write((data >> 16) & 0xff);
Wire.write((data >> 8) & 0xff);
Wire.write(data & 0xff);
return (Wire.endTransmission() == 0 ? VL53L1_ERROR_NONE : VL53L1_ERROR_CONTROL_INTERFACE);
}
VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData)
{
uint8_t data;
VL53L1_Error status = VL53L1_RdByte(Dev, index, &data);
if (status != VL53L1_ERROR_NONE) { return status; }
data &= AndData;
data |= OrData;
return VL53L1_WrByte(Dev, index, data);
}
VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
if (Wire.endTransmission() != 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
if (Wire.requestFrom(Dev->I2cDevAddr >> 1, 1) != 1) { return VL53L1_ERROR_CONTROL_INTERFACE; }
*data = Wire.read();
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
if (Wire.endTransmission() != 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
if (Wire.requestFrom(Dev->I2cDevAddr >> 1, 2) != 2) { return VL53L1_ERROR_CONTROL_INTERFACE; }
*data = (uint16_t)Wire.read() << 8;
*data |= Wire.read();
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data)
{
Wire.beginTransmission(Dev->I2cDevAddr >> 1);
Wire.write((index >> 8) & 0xff);
Wire.write(index & 0xff);
if (Wire.endTransmission() != 0) { return VL53L1_ERROR_CONTROL_INTERFACE; }
if (Wire.requestFrom(Dev->I2cDevAddr >> 1, 4) != 4) { return VL53L1_ERROR_CONTROL_INTERFACE; }
*data = (uint32_t)Wire.read() << 24;
*data |= (uint32_t)Wire.read() << 16;
*data |= (uint16_t)Wire.read() << 8;
*data |= Wire.read();
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_GetTickCount(
uint32_t *ptick_count_ms)
{
*ptick_count_ms = millis();
return VL53L1_ERROR_NONE;
}
//#define trace_print(level, ...) \
// _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_PLATFORM, \
// level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
//#define trace_i2c(...) \
// _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_NONE, \
// VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__)
VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz)
{
return VL53L1_ERROR_NOT_IMPLEMENTED;
}
VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms)
{
delay(wait_ms);
return VL53L1_ERROR_NONE;
}
VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us)
{
delayMicroseconds(wait_us);
return VL53L1_ERROR_NONE;
}
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)
{
uint8_t data;
VL53L1_Error status;
while (timeout_ms > 0)
{
status = VL53L1_RdByte(pdev, index, &data);
if (status != VL53L1_ERROR_NONE) { return status; }
if ((data & mask) == value) { return VL53L1_ERROR_NONE; }
delay(poll_delay_ms);
timeout_ms -= min(poll_delay_ms, timeout_ms);
}
return VL53L1_ERROR_TIME_OUT;
}