1 /*******************************************************************************
2  Copyright � 2016, STMicroelectronics International N.V.
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of STMicroelectronics nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
19  NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
20  IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
21  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  ******************************************************************************/
28 
29 #include "vl53l0x_api.h"
30 #include "vl53l0x_api_core.h"
31 #include "vl53l0x_api_strings.h"
32 
33 #ifndef __KERNEL__
34 #include <stdlib.h>
35 #endif
36 
37 #define LOG_FUNCTION_START(fmt, ...) \
38 	_LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__)
39 #define LOG_FUNCTION_END(status, ...) \
40 	_LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__)
41 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
42 	_LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__)
43 
44 
VL53L0X_check_part_used(VL53L0X_DEV Dev,uint8_t * Revision,VL53L0X_DeviceInfo_t * pVL53L0X_DeviceInfo)45 VL53L0X_Error VL53L0X_check_part_used(VL53L0X_DEV Dev,
46 		uint8_t *Revision,
47 		VL53L0X_DeviceInfo_t *pVL53L0X_DeviceInfo)
48 {
49 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
50 	uint8_t ModuleIdInt;
51 	char *ProductId_tmp;
52 
53 	LOG_FUNCTION_START("");
54 
55 	Status = VL53L0X_get_info_from_device(Dev, 2);
56 
57 	if (Status == VL53L0X_ERROR_NONE) {
58 		ModuleIdInt = VL53L0X_GETDEVICESPECIFICPARAMETER(Dev, ModuleId);
59 
60 	if (ModuleIdInt == 0) {
61 		*Revision = 0;
62 		VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->ProductId, "");
63 	} else {
64 		*Revision = VL53L0X_GETDEVICESPECIFICPARAMETER(Dev, Revision);
65 		ProductId_tmp = VL53L0X_GETDEVICESPECIFICPARAMETER(Dev,
66 			ProductId);
67 		VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->ProductId, ProductId_tmp);
68 	}
69 	}
70 
71 	LOG_FUNCTION_END(Status);
72 	return Status;
73 }
74 
75 
VL53L0X_get_device_info(VL53L0X_DEV Dev,VL53L0X_DeviceInfo_t * pVL53L0X_DeviceInfo)76 VL53L0X_Error VL53L0X_get_device_info(VL53L0X_DEV Dev,
77 				VL53L0X_DeviceInfo_t *pVL53L0X_DeviceInfo)
78 {
79 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
80 	uint8_t revision_id;
81 	uint8_t Revision;
82 
83 	Status = VL53L0X_check_part_used(Dev, &Revision, pVL53L0X_DeviceInfo);
84 
85 	if (Status == VL53L0X_ERROR_NONE) {
86 		if (Revision == 0) {
87 			VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->Name,
88 					VL53L0X_STRING_DEVICE_INFO_NAME_TS0);
89 		} else if ((Revision <= 34) && (Revision != 32)) {
90 			VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->Name,
91 					VL53L0X_STRING_DEVICE_INFO_NAME_TS1);
92 		} else if (Revision < 39) {
93 			VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->Name,
94 					VL53L0X_STRING_DEVICE_INFO_NAME_TS2);
95 		} else {
96 			VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->Name,
97 					VL53L0X_STRING_DEVICE_INFO_NAME_ES1);
98 		}
99 
100 		VL53L0X_COPYSTRING(pVL53L0X_DeviceInfo->Type,
101 				VL53L0X_STRING_DEVICE_INFO_TYPE);
102 
103 	}
104 
105 	if (Status == VL53L0X_ERROR_NONE) {
106 		Status = VL53L0X_RdByte(Dev, VL53L0X_REG_IDENTIFICATION_MODEL_ID,
107 				&pVL53L0X_DeviceInfo->ProductType);
108 	}
109 
110 	if (Status == VL53L0X_ERROR_NONE) {
111 		Status = VL53L0X_RdByte(Dev,
112 			VL53L0X_REG_IDENTIFICATION_REVISION_ID,
113 				&revision_id);
114 		pVL53L0X_DeviceInfo->ProductRevisionMajor = 1;
115 		pVL53L0X_DeviceInfo->ProductRevisionMinor =
116 					(revision_id & 0xF0) >> 4;
117 	}
118 
119 	return Status;
120 }
121 
122 
VL53L0X_get_device_error_string(VL53L0X_DeviceError ErrorCode,char * pDeviceErrorString)123 VL53L0X_Error VL53L0X_get_device_error_string(VL53L0X_DeviceError ErrorCode,
124 		char *pDeviceErrorString)
125 {
126 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
127 
128 	LOG_FUNCTION_START("");
129 
130 	switch (ErrorCode) {
131 	case VL53L0X_DEVICEERROR_NONE:
132 		VL53L0X_COPYSTRING(pDeviceErrorString,
133 			VL53L0X_STRING_DEVICEERROR_NONE);
134 	break;
135 	case VL53L0X_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
136 		VL53L0X_COPYSTRING(pDeviceErrorString,
137 			VL53L0X_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE);
138 	break;
139 	case VL53L0X_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
140 		VL53L0X_COPYSTRING(pDeviceErrorString,
141 			VL53L0X_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE);
142 	break;
143 	case VL53L0X_DEVICEERROR_NOVHVVALUEFOUND:
144 		VL53L0X_COPYSTRING(pDeviceErrorString,
145 			VL53L0X_STRING_DEVICEERROR_NOVHVVALUEFOUND);
146 	break;
147 	case VL53L0X_DEVICEERROR_MSRCNOTARGET:
148 		VL53L0X_COPYSTRING(pDeviceErrorString,
149 			VL53L0X_STRING_DEVICEERROR_MSRCNOTARGET);
150 	break;
151 	case VL53L0X_DEVICEERROR_SNRCHECK:
152 		VL53L0X_COPYSTRING(pDeviceErrorString,
153 			VL53L0X_STRING_DEVICEERROR_SNRCHECK);
154 	break;
155 	case VL53L0X_DEVICEERROR_RANGEPHASECHECK:
156 		VL53L0X_COPYSTRING(pDeviceErrorString,
157 			VL53L0X_STRING_DEVICEERROR_RANGEPHASECHECK);
158 	break;
159 	case VL53L0X_DEVICEERROR_SIGMATHRESHOLDCHECK:
160 		VL53L0X_COPYSTRING(pDeviceErrorString,
161 			VL53L0X_STRING_DEVICEERROR_SIGMATHRESHOLDCHECK);
162 	break;
163 	case VL53L0X_DEVICEERROR_TCC:
164 		VL53L0X_COPYSTRING(pDeviceErrorString,
165 			VL53L0X_STRING_DEVICEERROR_TCC);
166 	break;
167 	case VL53L0X_DEVICEERROR_PHASECONSISTENCY:
168 		VL53L0X_COPYSTRING(pDeviceErrorString,
169 			VL53L0X_STRING_DEVICEERROR_PHASECONSISTENCY);
170 	break;
171 	case VL53L0X_DEVICEERROR_MINCLIP:
172 		VL53L0X_COPYSTRING(pDeviceErrorString,
173 			VL53L0X_STRING_DEVICEERROR_MINCLIP);
174 	break;
175 	case VL53L0X_DEVICEERROR_RANGECOMPLETE:
176 		VL53L0X_COPYSTRING(pDeviceErrorString,
177 			VL53L0X_STRING_DEVICEERROR_RANGECOMPLETE);
178 	break;
179 	case VL53L0X_DEVICEERROR_ALGOUNDERFLOW:
180 		VL53L0X_COPYSTRING(pDeviceErrorString,
181 			VL53L0X_STRING_DEVICEERROR_ALGOUNDERFLOW);
182 	break;
183 	case VL53L0X_DEVICEERROR_ALGOOVERFLOW:
184 		VL53L0X_COPYSTRING(pDeviceErrorString,
185 			VL53L0X_STRING_DEVICEERROR_ALGOOVERFLOW);
186 	break;
187 	case VL53L0X_DEVICEERROR_RANGEIGNORETHRESHOLD:
188 		VL53L0X_COPYSTRING(pDeviceErrorString,
189 			VL53L0X_STRING_DEVICEERROR_RANGEIGNORETHRESHOLD);
190 	break;
191 
192 	default:
193 		VL53L0X_COPYSTRING(pDeviceErrorString,
194 			VL53L0X_STRING_UNKNOW_ERROR_CODE);
195 
196 	}
197 
198 	LOG_FUNCTION_END(Status);
199 	return Status;
200 }
201 
VL53L0X_get_range_status_string(uint8_t RangeStatus,char * pRangeStatusString)202 VL53L0X_Error VL53L0X_get_range_status_string(uint8_t RangeStatus,
203 		char *pRangeStatusString)
204 {
205 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
206 
207 	LOG_FUNCTION_START("");
208 
209 	switch (RangeStatus) {
210 	case 0:
211 		VL53L0X_COPYSTRING(pRangeStatusString,
212 			VL53L0X_STRING_RANGESTATUS_RANGEVALID);
213 	break;
214 	case 1:
215 		VL53L0X_COPYSTRING(pRangeStatusString,
216 			VL53L0X_STRING_RANGESTATUS_SIGMA);
217 	break;
218 	case 2:
219 		VL53L0X_COPYSTRING(pRangeStatusString,
220 			VL53L0X_STRING_RANGESTATUS_SIGNAL);
221 	break;
222 	case 3:
223 		VL53L0X_COPYSTRING(pRangeStatusString,
224 			VL53L0X_STRING_RANGESTATUS_MINRANGE);
225 	break;
226 	case 4:
227 		VL53L0X_COPYSTRING(pRangeStatusString,
228 			VL53L0X_STRING_RANGESTATUS_PHASE);
229 	break;
230 	case 5:
231 		VL53L0X_COPYSTRING(pRangeStatusString,
232 			VL53L0X_STRING_RANGESTATUS_HW);
233 	break;
234 
235 	default: /**/
236 		VL53L0X_COPYSTRING(pRangeStatusString,
237 				VL53L0X_STRING_RANGESTATUS_NONE);
238 	}
239 
240 	LOG_FUNCTION_END(Status);
241 	return Status;
242 }
243 
VL53L0X_get_pal_error_string(VL53L0X_Error PalErrorCode,char * pPalErrorString)244 VL53L0X_Error VL53L0X_get_pal_error_string(VL53L0X_Error PalErrorCode,
245 		char *pPalErrorString)
246 {
247 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
248 
249 	LOG_FUNCTION_START("");
250 
251 	switch (PalErrorCode) {
252 	case VL53L0X_ERROR_NONE:
253 		VL53L0X_COPYSTRING(pPalErrorString,
254 			VL53L0X_STRING_ERROR_NONE);
255 	break;
256 	case VL53L0X_ERROR_CALIBRATION_WARNING:
257 		VL53L0X_COPYSTRING(pPalErrorString,
258 			VL53L0X_STRING_ERROR_CALIBRATION_WARNING);
259 	break;
260 	case VL53L0X_ERROR_MIN_CLIPPED:
261 		VL53L0X_COPYSTRING(pPalErrorString,
262 			VL53L0X_STRING_ERROR_MIN_CLIPPED);
263 	break;
264 	case VL53L0X_ERROR_UNDEFINED:
265 		VL53L0X_COPYSTRING(pPalErrorString,
266 			VL53L0X_STRING_ERROR_UNDEFINED);
267 	break;
268 	case VL53L0X_ERROR_INVALID_PARAMS:
269 		VL53L0X_COPYSTRING(pPalErrorString,
270 			VL53L0X_STRING_ERROR_INVALID_PARAMS);
271 	break;
272 	case VL53L0X_ERROR_NOT_SUPPORTED:
273 		VL53L0X_COPYSTRING(pPalErrorString,
274 			VL53L0X_STRING_ERROR_NOT_SUPPORTED);
275 	break;
276 	case VL53L0X_ERROR_INTERRUPT_NOT_CLEARED:
277 		VL53L0X_COPYSTRING(pPalErrorString,
278 			VL53L0X_STRING_ERROR_INTERRUPT_NOT_CLEARED);
279 	break;
280 	case VL53L0X_ERROR_RANGE_ERROR:
281 		VL53L0X_COPYSTRING(pPalErrorString,
282 			VL53L0X_STRING_ERROR_RANGE_ERROR);
283 	break;
284 	case VL53L0X_ERROR_TIME_OUT:
285 		VL53L0X_COPYSTRING(pPalErrorString,
286 			VL53L0X_STRING_ERROR_TIME_OUT);
287 	break;
288 	case VL53L0X_ERROR_MODE_NOT_SUPPORTED:
289 		VL53L0X_COPYSTRING(pPalErrorString,
290 			VL53L0X_STRING_ERROR_MODE_NOT_SUPPORTED);
291 	break;
292 	case VL53L0X_ERROR_BUFFER_TOO_SMALL:
293 		VL53L0X_COPYSTRING(pPalErrorString,
294 			VL53L0X_STRING_ERROR_BUFFER_TOO_SMALL);
295 	break;
296 	case VL53L0X_ERROR_GPIO_NOT_EXISTING:
297 		VL53L0X_COPYSTRING(pPalErrorString,
298 			VL53L0X_STRING_ERROR_GPIO_NOT_EXISTING);
299 	break;
300 	case VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED:
301 		VL53L0X_COPYSTRING(pPalErrorString,
302 			VL53L0X_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED);
303 	break;
304 	case VL53L0X_ERROR_CONTROL_INTERFACE:
305 		VL53L0X_COPYSTRING(pPalErrorString,
306 			VL53L0X_STRING_ERROR_CONTROL_INTERFACE);
307 	break;
308 	case VL53L0X_ERROR_INVALID_COMMAND:
309 		VL53L0X_COPYSTRING(pPalErrorString,
310 			VL53L0X_STRING_ERROR_INVALID_COMMAND);
311 	break;
312 	case VL53L0X_ERROR_DIVISION_BY_ZERO:
313 		VL53L0X_COPYSTRING(pPalErrorString,
314 			VL53L0X_STRING_ERROR_DIVISION_BY_ZERO);
315 	break;
316 	case VL53L0X_ERROR_REF_SPAD_INIT:
317 		VL53L0X_COPYSTRING(pPalErrorString,
318 			VL53L0X_STRING_ERROR_REF_SPAD_INIT);
319 	break;
320 	case VL53L0X_ERROR_NOT_IMPLEMENTED:
321 		VL53L0X_COPYSTRING(pPalErrorString,
322 			VL53L0X_STRING_ERROR_NOT_IMPLEMENTED);
323 	break;
324 
325 	default:
326 		VL53L0X_COPYSTRING(pPalErrorString,
327 				VL53L0X_STRING_UNKNOW_ERROR_CODE);
328 	}
329 
330 	LOG_FUNCTION_END(Status);
331 	return Status;
332 }
333 
VL53L0X_get_pal_state_string(VL53L0X_State PalStateCode,char * pPalStateString)334 VL53L0X_Error VL53L0X_get_pal_state_string(VL53L0X_State PalStateCode,
335 		char *pPalStateString)
336 {
337 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
338 
339 	LOG_FUNCTION_START("");
340 
341 	switch (PalStateCode) {
342 	case VL53L0X_STATE_POWERDOWN:
343 		VL53L0X_COPYSTRING(pPalStateString,
344 			VL53L0X_STRING_STATE_POWERDOWN);
345 	break;
346 	case VL53L0X_STATE_WAIT_STATICINIT:
347 		VL53L0X_COPYSTRING(pPalStateString,
348 			VL53L0X_STRING_STATE_WAIT_STATICINIT);
349 	break;
350 	case VL53L0X_STATE_STANDBY:
351 		VL53L0X_COPYSTRING(pPalStateString,
352 			VL53L0X_STRING_STATE_STANDBY);
353 	break;
354 	case VL53L0X_STATE_IDLE:
355 		VL53L0X_COPYSTRING(pPalStateString,
356 			VL53L0X_STRING_STATE_IDLE);
357 	break;
358 	case VL53L0X_STATE_RUNNING:
359 		VL53L0X_COPYSTRING(pPalStateString,
360 			VL53L0X_STRING_STATE_RUNNING);
361 	break;
362 	case VL53L0X_STATE_UNKNOWN:
363 		VL53L0X_COPYSTRING(pPalStateString,
364 			VL53L0X_STRING_STATE_UNKNOWN);
365 	break;
366 	case VL53L0X_STATE_ERROR:
367 		VL53L0X_COPYSTRING(pPalStateString,
368 			VL53L0X_STRING_STATE_ERROR);
369 	break;
370 
371 	default:
372 		VL53L0X_COPYSTRING(pPalStateString,
373 			VL53L0X_STRING_STATE_UNKNOWN);
374 	}
375 
376 	LOG_FUNCTION_END(Status);
377 	return Status;
378 }
379 
VL53L0X_get_sequence_steps_info(VL53L0X_SequenceStepId SequenceStepId,char * pSequenceStepsString)380 VL53L0X_Error VL53L0X_get_sequence_steps_info(
381 		VL53L0X_SequenceStepId SequenceStepId,
382 		char *pSequenceStepsString)
383 {
384 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
385 	LOG_FUNCTION_START("");
386 
387 	switch (SequenceStepId) {
388 	case VL53L0X_SEQUENCESTEP_TCC:
389 		VL53L0X_COPYSTRING(pSequenceStepsString,
390 			VL53L0X_STRING_SEQUENCESTEP_TCC);
391 	break;
392 	case VL53L0X_SEQUENCESTEP_DSS:
393 		VL53L0X_COPYSTRING(pSequenceStepsString,
394 			VL53L0X_STRING_SEQUENCESTEP_DSS);
395 	break;
396 	case VL53L0X_SEQUENCESTEP_MSRC:
397 		VL53L0X_COPYSTRING(pSequenceStepsString,
398 			VL53L0X_STRING_SEQUENCESTEP_MSRC);
399 	break;
400 	case VL53L0X_SEQUENCESTEP_PRE_RANGE:
401 		VL53L0X_COPYSTRING(pSequenceStepsString,
402 			VL53L0X_STRING_SEQUENCESTEP_PRE_RANGE);
403 	break;
404 	case VL53L0X_SEQUENCESTEP_FINAL_RANGE:
405 		VL53L0X_COPYSTRING(pSequenceStepsString,
406 			VL53L0X_STRING_SEQUENCESTEP_FINAL_RANGE);
407 	break;
408 
409 	default:
410 		Status = VL53L0X_ERROR_INVALID_PARAMS;
411 	}
412 
413 	LOG_FUNCTION_END(Status);
414 
415 	return Status;
416 }
417 
418 
VL53L0X_get_limit_check_info(VL53L0X_DEV Dev,uint16_t LimitCheckId,char * pLimitCheckString)419 VL53L0X_Error VL53L0X_get_limit_check_info(VL53L0X_DEV Dev, uint16_t LimitCheckId,
420 	char *pLimitCheckString)
421 {
422 	VL53L0X_Error Status = VL53L0X_ERROR_NONE;
423 
424 	LOG_FUNCTION_START("");
425 
426 	switch (LimitCheckId) {
427 	case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE:
428 		VL53L0X_COPYSTRING(pLimitCheckString,
429 			VL53L0X_STRING_CHECKENABLE_SIGMA_FINAL_RANGE);
430 	break;
431 	case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
432 		VL53L0X_COPYSTRING(pLimitCheckString,
433 			VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE);
434 	break;
435 	case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP:
436 		VL53L0X_COPYSTRING(pLimitCheckString,
437 			VL53L0X_STRING_CHECKENABLE_SIGNAL_REF_CLIP);
438 	break;
439 	case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD:
440 		VL53L0X_COPYSTRING(pLimitCheckString,
441 			VL53L0X_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD);
442 	break;
443 
444 	case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC:
445 		VL53L0X_COPYSTRING(pLimitCheckString,
446 			VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_MSRC);
447 	break;
448 
449 	case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE:
450 		VL53L0X_COPYSTRING(pLimitCheckString,
451 			VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_PRE_RANGE);
452 	break;
453 
454 	default:
455 		VL53L0X_COPYSTRING(pLimitCheckString,
456 			VL53L0X_STRING_UNKNOW_ERROR_CODE);
457 
458 	}
459 
460 	LOG_FUNCTION_END(Status);
461 	return Status;
462 }
463