├── component.mk ├── README.md ├── platform ├── src │ ├── vl53l0x_platform_log.c │ └── vl53l0x_platform.c └── inc │ ├── vl53l0x_types.h │ ├── vl53l0x_platform_log.h │ └── vl53l0x_platform.h ├── LICENSE └── core ├── inc ├── vl53l0x_api_ranging.h ├── vl53l0x_api_calibration.h ├── vl53l0x_tuning.h ├── vl53l0x_api_core.h ├── vl53l0x_interrupt_threshold_settings.h ├── vl53l0x_device.h ├── vl53l0x_api_strings.h └── vl53l0x_def.h └── src ├── vl53l0x_api_ranging.c ├── vl53l0x_api_strings.c └── vl53l0x_api_calibration.c /component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_INCLUDEDIRS := . core/inc platform/inc 2 | COMPONENT_SRCDIRS := . core/src platform/src 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp32-vl53l0x 2 | A VL53L0X driver for the ESP32 3 | 4 | Based off of the [official VL53L0X API](http://www.st.com/content/st_com/en/products/embedded-software/proximity-sensors-software/stsw-img005.html) 5 | -------------------------------------------------------------------------------- /platform/src/vl53l0x_platform_log.c: -------------------------------------------------------------------------------- 1 | #include "vl53l0x_platform_log.h" 2 | 3 | uint32_t _trace_level = TRACE_LEVEL_ALL; 4 | int _modules = TRACE_MODULE_ALL; 5 | 6 | int32_t VL53L0X_trace_config(char *filename, uint32_t modules, uint32_t level, uint32_t functions) 7 | { 8 | _trace_level = level; 9 | _modules = modules; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Kyle Hendricks 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_api_ranging.h: -------------------------------------------------------------------------------- 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 | #ifndef _VL53L0X_API_RANGING_H_ 30 | #define _VL53L0X_API_RANGING_H_ 31 | 32 | #include "vl53l0x_def.h" 33 | #include "vl53l0x_platform.h" 34 | 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | 41 | 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* _VL53L0X_API_RANGING_H_ */ 48 | -------------------------------------------------------------------------------- /core/src/vl53l0x_api_ranging.c: -------------------------------------------------------------------------------- 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 | 32 | 33 | #ifndef __KERNEL__ 34 | #include 35 | #endif 36 | #define LOG_FUNCTION_START(fmt, ...) \ 37 | _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) 38 | #define LOG_FUNCTION_END(status, ...) \ 39 | _LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__) 40 | #define LOG_FUNCTION_END_FMT(status, fmt, ...) \ 41 | _LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__) 42 | 43 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_api_calibration.h: -------------------------------------------------------------------------------- 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 | #ifndef _VL53L0X_API_CALIBRATION_H_ 30 | #define _VL53L0X_API_CALIBRATION_H_ 31 | 32 | #include "vl53l0x_def.h" 33 | #include "vl53l0x_platform.h" 34 | 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | VL53L0X_Error VL53L0X_perform_xtalk_calibration(VL53L0X_DEV Dev, 41 | FixPoint1616_t XTalkCalDistance, 42 | FixPoint1616_t *pXTalkCompensationRateMegaCps); 43 | 44 | VL53L0X_Error VL53L0X_perform_offset_calibration(VL53L0X_DEV Dev, 45 | FixPoint1616_t CalDistanceMilliMeter, 46 | int32_t *pOffsetMicroMeter); 47 | 48 | VL53L0X_Error VL53L0X_set_offset_calibration_data_micro_meter(VL53L0X_DEV Dev, 49 | int32_t OffsetCalibrationDataMicroMeter); 50 | 51 | VL53L0X_Error VL53L0X_get_offset_calibration_data_micro_meter(VL53L0X_DEV Dev, 52 | int32_t *pOffsetCalibrationDataMicroMeter); 53 | 54 | VL53L0X_Error VL53L0X_apply_offset_adjustment(VL53L0X_DEV Dev); 55 | 56 | VL53L0X_Error VL53L0X_perform_ref_spad_management(VL53L0X_DEV Dev, 57 | uint32_t *refSpadCount, uint8_t *isApertureSpads); 58 | 59 | VL53L0X_Error VL53L0X_set_reference_spads(VL53L0X_DEV Dev, 60 | uint32_t count, uint8_t isApertureSpads); 61 | 62 | VL53L0X_Error VL53L0X_get_reference_spads(VL53L0X_DEV Dev, 63 | uint32_t *pSpadCount, uint8_t *pIsApertureSpads); 64 | 65 | VL53L0X_Error VL53L0X_perform_phase_calibration(VL53L0X_DEV Dev, 66 | uint8_t *pPhaseCal, const uint8_t get_data_enable, 67 | const uint8_t restore_config); 68 | 69 | VL53L0X_Error VL53L0X_perform_ref_calibration(VL53L0X_DEV Dev, 70 | uint8_t *pVhvSettings, uint8_t *pPhaseCal, uint8_t get_data_enable); 71 | 72 | VL53L0X_Error VL53L0X_set_ref_calibration(VL53L0X_DEV Dev, 73 | uint8_t VhvSettings, uint8_t PhaseCal); 74 | 75 | VL53L0X_Error VL53L0X_get_ref_calibration(VL53L0X_DEV Dev, 76 | uint8_t *pVhvSettings, uint8_t *pPhaseCal); 77 | 78 | 79 | 80 | 81 | #ifdef __cplusplus 82 | } 83 | #endif 84 | 85 | #endif /* _VL53L0X_API_CALIBRATION_H_ */ 86 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_tuning.h: -------------------------------------------------------------------------------- 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 | 30 | #ifndef _VL53L0X_TUNING_H_ 31 | #define _VL53L0X_TUNING_H_ 32 | 33 | #include "vl53l0x_def.h" 34 | 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | 41 | uint8_t DefaultTuningSettings[] = { 42 | 43 | /* update 02/11/2015_v36 */ 44 | 0x01, 0xFF, 0x01, 45 | 0x01, 0x00, 0x00, 46 | 47 | 0x01, 0xFF, 0x00, 48 | 0x01, 0x09, 0x00, 49 | 0x01, 0x10, 0x00, 50 | 0x01, 0x11, 0x00, 51 | 52 | 0x01, 0x24, 0x01, 53 | 0x01, 0x25, 0xff, 54 | 0x01, 0x75, 0x00, 55 | 56 | 0x01, 0xFF, 0x01, 57 | 0x01, 0x4e, 0x2c, 58 | 0x01, 0x48, 0x00, 59 | 0x01, 0x30, 0x20, 60 | 61 | 0x01, 0xFF, 0x00, 62 | 0x01, 0x30, 0x09, /* mja changed from 0x64. */ 63 | 0x01, 0x54, 0x00, 64 | 0x01, 0x31, 0x04, 65 | 0x01, 0x32, 0x03, 66 | 0x01, 0x40, 0x83, 67 | 0x01, 0x46, 0x25, 68 | 0x01, 0x60, 0x00, 69 | 0x01, 0x27, 0x00, 70 | 0x01, 0x50, 0x06, 71 | 0x01, 0x51, 0x00, 72 | 0x01, 0x52, 0x96, 73 | 0x01, 0x56, 0x08, 74 | 0x01, 0x57, 0x30, 75 | 0x01, 0x61, 0x00, 76 | 0x01, 0x62, 0x00, 77 | 0x01, 0x64, 0x00, 78 | 0x01, 0x65, 0x00, 79 | 0x01, 0x66, 0xa0, 80 | 81 | 0x01, 0xFF, 0x01, 82 | 0x01, 0x22, 0x32, 83 | 0x01, 0x47, 0x14, 84 | 0x01, 0x49, 0xff, 85 | 0x01, 0x4a, 0x00, 86 | 87 | 0x01, 0xFF, 0x00, 88 | 0x01, 0x7a, 0x0a, 89 | 0x01, 0x7b, 0x00, 90 | 0x01, 0x78, 0x21, 91 | 92 | 0x01, 0xFF, 0x01, 93 | 0x01, 0x23, 0x34, 94 | 0x01, 0x42, 0x00, 95 | 0x01, 0x44, 0xff, 96 | 0x01, 0x45, 0x26, 97 | 0x01, 0x46, 0x05, 98 | 0x01, 0x40, 0x40, 99 | 0x01, 0x0E, 0x06, 100 | 0x01, 0x20, 0x1a, 101 | 0x01, 0x43, 0x40, 102 | 103 | 0x01, 0xFF, 0x00, 104 | 0x01, 0x34, 0x03, 105 | 0x01, 0x35, 0x44, 106 | 107 | 0x01, 0xFF, 0x01, 108 | 0x01, 0x31, 0x04, 109 | 0x01, 0x4b, 0x09, 110 | 0x01, 0x4c, 0x05, 111 | 0x01, 0x4d, 0x04, 112 | 113 | 114 | 0x01, 0xFF, 0x00, 115 | 0x01, 0x44, 0x00, 116 | 0x01, 0x45, 0x20, 117 | 0x01, 0x47, 0x08, 118 | 0x01, 0x48, 0x28, 119 | 0x01, 0x67, 0x00, 120 | 0x01, 0x70, 0x04, 121 | 0x01, 0x71, 0x01, 122 | 0x01, 0x72, 0xfe, 123 | 0x01, 0x76, 0x00, 124 | 0x01, 0x77, 0x00, 125 | 126 | 0x01, 0xFF, 0x01, 127 | 0x01, 0x0d, 0x01, 128 | 129 | 0x01, 0xFF, 0x00, 130 | 0x01, 0x80, 0x01, 131 | 0x01, 0x01, 0xF8, 132 | 133 | 0x01, 0xFF, 0x01, 134 | 0x01, 0x8e, 0x01, 135 | 0x01, 0x00, 0x01, 136 | 0x01, 0xFF, 0x00, 137 | 0x01, 0x80, 0x00, 138 | 139 | 0x00, 0x00, 0x00 140 | }; 141 | 142 | #ifdef __cplusplus 143 | } 144 | #endif 145 | 146 | #endif /* _VL53L0X_TUNING_H_ */ 147 | -------------------------------------------------------------------------------- /platform/inc/vl53l0x_types.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Copyright © 2015, 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 | * @file vl53l0x_types.h 30 | * @brief VL53L0X types definition 31 | */ 32 | 33 | #ifndef VL53L0X_TYPES_H_ 34 | #define VL53L0X_TYPES_H_ 35 | 36 | /** @defgroup porting_type Basic type definition 37 | * @ingroup VL53L0X_platform_group 38 | * 39 | * @brief file vl53l0x_types.h files hold basic type definition that may requires porting 40 | * 41 | * contains type that must be defined for the platform\n 42 | * when target platform and compiler provide stdint.h and stddef.h it is enough to include it.\n 43 | * If stdint.h is not available review and adapt all signed and unsigned 8/16/32 bits basic types. \n 44 | * If stddef.h is not available review and adapt NULL definition . 45 | */ 46 | #include 47 | #include 48 | 49 | #ifndef NULL 50 | #error "Error NULL definition should be done. Please add required include " 51 | #endif 52 | 53 | 54 | #if ! defined(_STDINT_H) 55 | 56 | #pragma message("Please review type definition of STDINT define for your platform and add to list above ") 57 | 58 | /* 59 | * target platform do not provide stdint or use a different #define than above 60 | * to avoid seeing the message below addapt the #define list above or implement 61 | * all type and delete these pragma 62 | */ 63 | 64 | /** \ingroup VL53L0X_portingType_group 65 | * @{ 66 | */ 67 | 68 | 69 | typedef unsigned long long uint64_t; 70 | 71 | 72 | /** @brief Typedef defining 32 bit unsigned int type.\n 73 | * The developer should modify this to suit the platform being deployed. 74 | */ 75 | typedef unsigned int uint32_t; 76 | 77 | /** @brief Typedef defining 32 bit int type.\n 78 | * The developer should modify this to suit the platform being deployed. 79 | */ 80 | typedef int int32_t; 81 | 82 | /** @brief Typedef defining 16 bit unsigned short type.\n 83 | * The developer should modify this to suit the platform being deployed. 84 | */ 85 | typedef unsigned short uint16_t; 86 | 87 | /** @brief Typedef defining 16 bit short type.\n 88 | * The developer should modify this to suit the platform being deployed. 89 | */ 90 | typedef short int16_t; 91 | 92 | /** @brief Typedef defining 8 bit unsigned char type.\n 93 | * The developer should modify this to suit the platform being deployed. 94 | */ 95 | typedef unsigned char uint8_t; 96 | 97 | /** @brief Typedef defining 8 bit char type.\n 98 | * The developer should modify this to suit the platform being deployed. 99 | */ 100 | typedef signed char int8_t; 101 | 102 | /** @} */ 103 | #endif /* _STDINT_H */ 104 | 105 | 106 | /** use where fractional values are expected 107 | * 108 | * Given a floating point value f it's .16 bit point is (int)(f*(1<<16))*/ 109 | typedef uint32_t FixPoint1616_t; 110 | 111 | #endif /* VL53L0X_TYPES_H_ */ 112 | -------------------------------------------------------------------------------- /platform/inc/vl53l0x_platform_log.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Copyright � 2015, 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 | 30 | #ifndef _VL53L0X_PLATFORM_LOG_H_ 31 | #define _VL53L0X_PLATFORM_LOG_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include "esp_log.h" 38 | 39 | /* LOG Functions */ 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | /** 46 | * @file vl53l0x_platform_log.h 47 | * 48 | * @brief platform log function definition 49 | */ 50 | 51 | //#define VL53L0X_LOG_ENABLE 0 52 | 53 | enum { 54 | TRACE_LEVEL_NONE = ESP_LOG_NONE, 55 | TRACE_LEVEL_ERRORS = ESP_LOG_ERROR, 56 | TRACE_LEVEL_WARNING = ESP_LOG_WARN, 57 | TRACE_LEVEL_INFO = ESP_LOG_INFO, 58 | TRACE_LEVEL_DEBUG = ESP_LOG_DEBUG, 59 | TRACE_LEVEL_ALL = ESP_LOG_VERBOSE, 60 | TRACE_LEVEL_IGNORE 61 | }; 62 | 63 | enum { 64 | TRACE_FUNCTION_NONE = 0, 65 | TRACE_FUNCTION_I2C = 1, 66 | TRACE_FUNCTION_ALL = 0x7fffffff //all bits except sign 67 | }; 68 | 69 | enum { 70 | TRACE_MODULE_NONE = 0x0, 71 | TRACE_MODULE_API = 0x1, 72 | TRACE_MODULE_PLATFORM = 0x2, 73 | TRACE_MODULE_ALL = 0x7fffffff //all bits except sign 74 | }; 75 | 76 | #define VL53L0X_LOG_ENABLE 77 | 78 | #ifdef VL53L0X_LOG_ENABLE 79 | 80 | extern uint32_t _trace_level; 81 | extern int _modules; 82 | 83 | int32_t VL53L0X_trace_config(char *filename, uint32_t modules, uint32_t level, uint32_t functions); 84 | 85 | #define trace_print_module_function(module, level, function, format, ...) \ 86 | if (level <= LOG_LOCAL_LEVEL && (module & _modules) != 0) \ 87 | ESP_LOG_LEVEL(level, "VL53L0X", format, ##__VA_ARGS__) 88 | 89 | #define LOG_GET_TIME() esp_log_timestamp() 90 | 91 | #define _LOG_FUNCTION_START(module, fmt, ... ) \ 92 | trace_print_module_function(module, _trace_level, TRACE_FUNCTION_ALL, "%u %s "fmt"\n", LOG_GET_TIME(), __FUNCTION__, ##__VA_ARGS__); 93 | 94 | #define _LOG_FUNCTION_END(module, status, ... )\ 95 | trace_print_module_function(module, _trace_level, TRACE_FUNCTION_ALL, "%u %s %d\n", LOG_GET_TIME(), __FUNCTION__, (int)status, ##__VA_ARGS__) 96 | 97 | #define _LOG_FUNCTION_END_FMT(module, status, fmt, ... )\ 98 | trace_print_module_function(module, _trace_level, TRACE_FUNCTION_ALL, "%u %s %d "fmt"\n", LOG_GET_TIME(), __FUNCTION__, (int)status,##__VA_ARGS__) 99 | 100 | #else /* VL53L0X_LOG_ENABLE no logging */ 101 | #define VL53L0X_ErrLog(...) (void)0 102 | #define _LOG_FUNCTION_START(module, fmt, ... ) (void)0 103 | #define _LOG_FUNCTION_END(module, status, ... ) (void)0 104 | #define _LOG_FUNCTION_END_FMT(module, status, fmt, ... ) (void)0 105 | #endif /* else */ 106 | 107 | #define VL53L0X_COPYSTRING(str, ...) strcpy(str, ##__VA_ARGS__) 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif /* _VL53L0X_PLATFORM_LOG_H_ */ 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_api_core.h: -------------------------------------------------------------------------------- 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 | #ifndef _VL53L0X_API_CORE_H_ 30 | #define _VL53L0X_API_CORE_H_ 31 | 32 | #include "vl53l0x_def.h" 33 | #include "vl53l0x_platform.h" 34 | 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | 41 | VL53L0X_Error VL53L0X_reverse_bytes(uint8_t *data, uint32_t size); 42 | 43 | VL53L0X_Error VL53L0X_measurement_poll_for_completion(VL53L0X_DEV Dev); 44 | 45 | uint8_t VL53L0X_encode_vcsel_period(uint8_t vcsel_period_pclks); 46 | 47 | uint8_t VL53L0X_decode_vcsel_period(uint8_t vcsel_period_reg); 48 | 49 | uint32_t VL53L0X_isqrt(uint32_t num); 50 | 51 | uint32_t VL53L0X_quadrature_sum(uint32_t a, uint32_t b); 52 | 53 | VL53L0X_Error VL53L0X_get_info_from_device(VL53L0X_DEV Dev, uint8_t option); 54 | 55 | VL53L0X_Error VL53L0X_set_vcsel_pulse_period(VL53L0X_DEV Dev, 56 | VL53L0X_VcselPeriod VcselPeriodType, uint8_t VCSELPulsePeriodPCLK); 57 | 58 | VL53L0X_Error VL53L0X_get_vcsel_pulse_period(VL53L0X_DEV Dev, 59 | VL53L0X_VcselPeriod VcselPeriodType, uint8_t *pVCSELPulsePeriodPCLK); 60 | 61 | uint32_t VL53L0X_decode_timeout(uint16_t encoded_timeout); 62 | 63 | VL53L0X_Error get_sequence_step_timeout(VL53L0X_DEV Dev, 64 | VL53L0X_SequenceStepId SequenceStepId, 65 | uint32_t *pTimeOutMicroSecs); 66 | 67 | VL53L0X_Error set_sequence_step_timeout(VL53L0X_DEV Dev, 68 | VL53L0X_SequenceStepId SequenceStepId, 69 | uint32_t TimeOutMicroSecs); 70 | 71 | VL53L0X_Error VL53L0X_set_measurement_timing_budget_micro_seconds(VL53L0X_DEV Dev, 72 | uint32_t MeasurementTimingBudgetMicroSeconds); 73 | 74 | VL53L0X_Error VL53L0X_get_measurement_timing_budget_micro_seconds(VL53L0X_DEV Dev, 75 | uint32_t *pMeasurementTimingBudgetMicroSeconds); 76 | 77 | VL53L0X_Error VL53L0X_load_tuning_settings(VL53L0X_DEV Dev, 78 | uint8_t *pTuningSettingBuffer); 79 | 80 | VL53L0X_Error VL53L0X_calc_sigma_estimate(VL53L0X_DEV Dev, 81 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData, 82 | FixPoint1616_t *pSigmaEstimate, uint32_t *pDmax_mm); 83 | 84 | VL53L0X_Error VL53L0X_get_total_xtalk_rate(VL53L0X_DEV Dev, 85 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData, 86 | FixPoint1616_t *ptotal_xtalk_rate_mcps); 87 | 88 | VL53L0X_Error VL53L0X_get_total_signal_rate(VL53L0X_DEV Dev, 89 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData, 90 | FixPoint1616_t *ptotal_signal_rate_mcps); 91 | 92 | VL53L0X_Error VL53L0X_get_pal_range_status(VL53L0X_DEV Dev, 93 | uint8_t DeviceRangeStatus, 94 | FixPoint1616_t SignalRate, 95 | uint16_t EffectiveSpadRtnCount, 96 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData, 97 | uint8_t *pPalRangeStatus); 98 | 99 | uint32_t VL53L0X_calc_timeout_mclks(VL53L0X_DEV Dev, 100 | uint32_t timeout_period_us, uint8_t vcsel_period_pclks); 101 | 102 | uint16_t VL53L0X_encode_timeout(uint32_t timeout_macro_clks); 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #endif /* _VL53L0X_API_CORE_H_ */ 109 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_interrupt_threshold_settings.h: -------------------------------------------------------------------------------- 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 | 30 | #ifndef _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ 31 | #define _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ 32 | 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | 39 | uint8_t InterruptThresholdSettings[] = { 40 | 41 | /* Start of Interrupt Threshold Settings */ 42 | 0x1, 0xff, 0x00, 43 | 0x1, 0x80, 0x01, 44 | 0x1, 0xff, 0x01, 45 | 0x1, 0x00, 0x00, 46 | 0x1, 0xff, 0x01, 47 | 0x1, 0x4f, 0x02, 48 | 0x1, 0xFF, 0x0E, 49 | 0x1, 0x00, 0x03, 50 | 0x1, 0x01, 0x84, 51 | 0x1, 0x02, 0x0A, 52 | 0x1, 0x03, 0x03, 53 | 0x1, 0x04, 0x08, 54 | 0x1, 0x05, 0xC8, 55 | 0x1, 0x06, 0x03, 56 | 0x1, 0x07, 0x8D, 57 | 0x1, 0x08, 0x08, 58 | 0x1, 0x09, 0xC6, 59 | 0x1, 0x0A, 0x01, 60 | 0x1, 0x0B, 0x02, 61 | 0x1, 0x0C, 0x00, 62 | 0x1, 0x0D, 0xD5, 63 | 0x1, 0x0E, 0x18, 64 | 0x1, 0x0F, 0x12, 65 | 0x1, 0x10, 0x01, 66 | 0x1, 0x11, 0x82, 67 | 0x1, 0x12, 0x00, 68 | 0x1, 0x13, 0xD5, 69 | 0x1, 0x14, 0x18, 70 | 0x1, 0x15, 0x13, 71 | 0x1, 0x16, 0x03, 72 | 0x1, 0x17, 0x86, 73 | 0x1, 0x18, 0x0A, 74 | 0x1, 0x19, 0x09, 75 | 0x1, 0x1A, 0x08, 76 | 0x1, 0x1B, 0xC2, 77 | 0x1, 0x1C, 0x03, 78 | 0x1, 0x1D, 0x8F, 79 | 0x1, 0x1E, 0x0A, 80 | 0x1, 0x1F, 0x06, 81 | 0x1, 0x20, 0x01, 82 | 0x1, 0x21, 0x02, 83 | 0x1, 0x22, 0x00, 84 | 0x1, 0x23, 0xD5, 85 | 0x1, 0x24, 0x18, 86 | 0x1, 0x25, 0x22, 87 | 0x1, 0x26, 0x01, 88 | 0x1, 0x27, 0x82, 89 | 0x1, 0x28, 0x00, 90 | 0x1, 0x29, 0xD5, 91 | 0x1, 0x2A, 0x18, 92 | 0x1, 0x2B, 0x0B, 93 | 0x1, 0x2C, 0x28, 94 | 0x1, 0x2D, 0x78, 95 | 0x1, 0x2E, 0x28, 96 | 0x1, 0x2F, 0x91, 97 | 0x1, 0x30, 0x00, 98 | 0x1, 0x31, 0x0B, 99 | 0x1, 0x32, 0x00, 100 | 0x1, 0x33, 0x0B, 101 | 0x1, 0x34, 0x00, 102 | 0x1, 0x35, 0xA1, 103 | 0x1, 0x36, 0x00, 104 | 0x1, 0x37, 0xA0, 105 | 0x1, 0x38, 0x00, 106 | 0x1, 0x39, 0x04, 107 | 0x1, 0x3A, 0x28, 108 | 0x1, 0x3B, 0x30, 109 | 0x1, 0x3C, 0x0C, 110 | 0x1, 0x3D, 0x04, 111 | 0x1, 0x3E, 0x0F, 112 | 0x1, 0x3F, 0x79, 113 | 0x1, 0x40, 0x28, 114 | 0x1, 0x41, 0x1E, 115 | 0x1, 0x42, 0x2F, 116 | 0x1, 0x43, 0x87, 117 | 0x1, 0x44, 0x00, 118 | 0x1, 0x45, 0x0B, 119 | 0x1, 0x46, 0x00, 120 | 0x1, 0x47, 0x0B, 121 | 0x1, 0x48, 0x00, 122 | 0x1, 0x49, 0xA7, 123 | 0x1, 0x4A, 0x00, 124 | 0x1, 0x4B, 0xA6, 125 | 0x1, 0x4C, 0x00, 126 | 0x1, 0x4D, 0x04, 127 | 0x1, 0x4E, 0x01, 128 | 0x1, 0x4F, 0x00, 129 | 0x1, 0x50, 0x00, 130 | 0x1, 0x51, 0x80, 131 | 0x1, 0x52, 0x09, 132 | 0x1, 0x53, 0x08, 133 | 0x1, 0x54, 0x01, 134 | 0x1, 0x55, 0x00, 135 | 0x1, 0x56, 0x0F, 136 | 0x1, 0x57, 0x79, 137 | 0x1, 0x58, 0x09, 138 | 0x1, 0x59, 0x05, 139 | 0x1, 0x5A, 0x00, 140 | 0x1, 0x5B, 0x60, 141 | 0x1, 0x5C, 0x05, 142 | 0x1, 0x5D, 0xD1, 143 | 0x1, 0x5E, 0x0C, 144 | 0x1, 0x5F, 0x3C, 145 | 0x1, 0x60, 0x00, 146 | 0x1, 0x61, 0xD0, 147 | 0x1, 0x62, 0x0B, 148 | 0x1, 0x63, 0x03, 149 | 0x1, 0x64, 0x28, 150 | 0x1, 0x65, 0x10, 151 | 0x1, 0x66, 0x2A, 152 | 0x1, 0x67, 0x39, 153 | 0x1, 0x68, 0x0B, 154 | 0x1, 0x69, 0x02, 155 | 0x1, 0x6A, 0x28, 156 | 0x1, 0x6B, 0x10, 157 | 0x1, 0x6C, 0x2A, 158 | 0x1, 0x6D, 0x61, 159 | 0x1, 0x6E, 0x0C, 160 | 0x1, 0x6F, 0x00, 161 | 0x1, 0x70, 0x0F, 162 | 0x1, 0x71, 0x79, 163 | 0x1, 0x72, 0x00, 164 | 0x1, 0x73, 0x0B, 165 | 0x1, 0x74, 0x00, 166 | 0x1, 0x75, 0x0B, 167 | 0x1, 0x76, 0x00, 168 | 0x1, 0x77, 0xA1, 169 | 0x1, 0x78, 0x00, 170 | 0x1, 0x79, 0xA0, 171 | 0x1, 0x7A, 0x00, 172 | 0x1, 0x7B, 0x04, 173 | 0x1, 0xFF, 0x04, 174 | 0x1, 0x79, 0x1D, 175 | 0x1, 0x7B, 0x27, 176 | 0x1, 0x96, 0x0E, 177 | 0x1, 0x97, 0xFE, 178 | 0x1, 0x98, 0x03, 179 | 0x1, 0x99, 0xEF, 180 | 0x1, 0x9A, 0x02, 181 | 0x1, 0x9B, 0x44, 182 | 0x1, 0x73, 0x07, 183 | 0x1, 0x70, 0x01, 184 | 0x1, 0xff, 0x01, 185 | 0x1, 0x00, 0x01, 186 | 0x1, 0xff, 0x00, 187 | 0x00, 0x00, 0x00 188 | }; 189 | 190 | #ifdef __cplusplus 191 | } 192 | #endif 193 | 194 | #endif /* _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ */ 195 | -------------------------------------------------------------------------------- /platform/inc/vl53l0x_platform.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Copyright � 2015, 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 | 30 | #ifndef _VL53L0X_PLATFORM_H_ 31 | #define _VL53L0X_PLATFORM_H_ 32 | 33 | #include "vl53l0x_def.h" 34 | #include "vl53l0x_platform_log.h" 35 | 36 | #include "driver/i2c.h" 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * @file vl53l0x_platform.h 44 | * 45 | * @brief All end user OS/platform/application porting 46 | */ 47 | 48 | /** 49 | * @defgroup VL53L0X_platform_group VL53L0X Platform Functions 50 | * @brief VL53L0X Platform Functions 51 | * @{ 52 | */ 53 | 54 | /** 55 | * @struct VL53L0X_Dev_t 56 | * @brief Generic PAL device type that does link between API and platform abstraction layer 57 | * 58 | */ 59 | typedef struct { 60 | VL53L0X_DevData_t Data; /*!< embed ST Ewok Dev data as "Data"*/ 61 | 62 | /*!< user specific field */ 63 | uint8_t i2c_address; /*!< i2c device address user specific field */ 64 | i2c_port_t i2c_port_num; // ESP32 i2c port number 65 | 66 | } VL53L0X_Dev_t; 67 | 68 | 69 | /** 70 | * @brief Declare the device Handle as a pointer of the structure @a VL53L0X_Dev_t. 71 | * 72 | */ 73 | typedef VL53L0X_Dev_t* VL53L0X_DEV; 74 | 75 | /** 76 | * @def PALDevDataGet 77 | * @brief Get ST private structure @a VL53L0X_DevData_t data access 78 | * 79 | * @param Dev Device Handle 80 | * @param field ST structure field name 81 | * It maybe used and as real data "ref" not just as "get" for sub-structure item 82 | * like PALDevDataGet(FilterData.field)[i] or PALDevDataGet(FilterData.MeasurementIndex)++ 83 | */ 84 | #define PALDevDataGet(Dev, field) (Dev->Data.field) 85 | 86 | /** 87 | * @def PALDevDataSet(Dev, field, data) 88 | * @brief Set ST private structure @a VL53L0X_DevData_t data field 89 | * @param Dev Device Handle 90 | * @param field ST structure field name 91 | * @param data Data to be set 92 | */ 93 | #define PALDevDataSet(Dev, field, data) (Dev->Data.field)=(data) 94 | 95 | 96 | /** 97 | * @defgroup VL53L0X_registerAccess_group PAL Register Access Functions 98 | * @brief PAL Register Access Functions 99 | * @{ 100 | */ 101 | 102 | /** 103 | * Writes the supplied byte buffer to the device 104 | * @param Dev Device Handle 105 | * @param index The register index 106 | * @param pdata Pointer to uint8_t buffer containing the data to be written 107 | * @param count Number of bytes in the supplied byte buffer 108 | * @return VL53L0X_ERROR_NONE Success 109 | * @return "Other error code" See ::VL53L0X_Error 110 | */ 111 | VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count); 112 | 113 | /** 114 | * Reads the requested number of bytes from the device 115 | * @param Dev Device Handle 116 | * @param index The register index 117 | * @param pdata Pointer to the uint8_t buffer to store read data 118 | * @param count Number of uint8_t's to read 119 | * @return VL53L0X_ERROR_NONE Success 120 | * @return "Other error code" See ::VL53L0X_Error 121 | */ 122 | VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count); 123 | 124 | /** 125 | * Write single byte register 126 | * @param Dev Device Handle 127 | * @param index The register index 128 | * @param data 8 bit register data 129 | * @return VL53L0X_ERROR_NONE Success 130 | * @return "Other error code" See ::VL53L0X_Error 131 | */ 132 | VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data); 133 | 134 | /** 135 | * Write word register 136 | * @param Dev Device Handle 137 | * @param index The register index 138 | * @param data 16 bit register data 139 | * @return VL53L0X_ERROR_NONE Success 140 | * @return "Other error code" See ::VL53L0X_Error 141 | */ 142 | VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data); 143 | 144 | /** 145 | * Write double word (4 byte) register 146 | * @param Dev Device Handle 147 | * @param index The register index 148 | * @param data 32 bit register data 149 | * @return VL53L0X_ERROR_NONE Success 150 | * @return "Other error code" See ::VL53L0X_Error 151 | */ 152 | VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data); 153 | 154 | /** 155 | * Read single byte register 156 | * @param Dev Device Handle 157 | * @param index The register index 158 | * @param data pointer to 8 bit data 159 | * @return VL53L0X_ERROR_NONE Success 160 | * @return "Other error code" See ::VL53L0X_Error 161 | */ 162 | VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data); 163 | 164 | /** 165 | * Read word (2byte) register 166 | * @param Dev Device Handle 167 | * @param index The register index 168 | * @param data pointer to 16 bit data 169 | * @return VL53L0X_ERROR_NONE Success 170 | * @return "Other error code" See ::VL53L0X_Error 171 | */ 172 | VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data); 173 | 174 | /** 175 | * Read dword (4byte) register 176 | * @param Dev Device Handle 177 | * @param index The register index 178 | * @param data pointer to 32 bit data 179 | * @return VL53L0X_ERROR_NONE Success 180 | * @return "Other error code" See ::VL53L0X_Error 181 | */ 182 | VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data); 183 | 184 | /** 185 | * Threat safe Update (read/modify/write) single byte register 186 | * 187 | * Final_reg = (Initial_reg & and_data) |or_data 188 | * 189 | * @param Dev Device Handle 190 | * @param index The register index 191 | * @param AndData 8 bit and data 192 | * @param OrData 8 bit or data 193 | * @return VL53L0X_ERROR_NONE Success 194 | * @return "Other error code" See ::VL53L0X_Error 195 | */ 196 | VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData); 197 | 198 | /** @} end of VL53L0X_registerAccess_group */ 199 | 200 | 201 | /** 202 | * @brief execute delay in all polling API call 203 | * 204 | * A typical multi-thread or RTOs implementation is to sleep the task for some 5ms (with 100Hz max rate faster polling is not needed) 205 | * if nothing specific is need you can define it as an empty/void macro 206 | * @code 207 | * #define VL53L0X_PollingDelay(...) (void)0 208 | * @endcode 209 | * @param Dev Device Handle 210 | * @return VL53L0X_ERROR_NONE Success 211 | * @return "Other error code" See ::VL53L0X_Error 212 | */ 213 | VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev); /* usually best implemented as a real function */ 214 | 215 | /** @} end of VL53L0X_platform_group */ 216 | 217 | #ifdef __cplusplus 218 | } 219 | #endif 220 | 221 | #endif /* _VL53L0X_PLATFORM_H_ */ 222 | 223 | 224 | 225 | -------------------------------------------------------------------------------- /platform/src/vl53l0x_platform.c: -------------------------------------------------------------------------------- 1 | #include "vl53l0x_platform.h" 2 | 3 | #include "driver/i2c.h" 4 | #include "esp_err.h" 5 | #include "esp_log.h" 6 | 7 | #define ACK_CHECK_EN true 8 | 9 | VL53L0X_Error esp_to_vl53l0x_error(esp_err_t esp_err) { 10 | switch (esp_err) { 11 | case ESP_OK: 12 | return VL53L0X_ERROR_NONE; 13 | case ESP_ERR_INVALID_ARG: 14 | return VL53L0X_ERROR_INVALID_PARAMS; 15 | case ESP_FAIL: 16 | case ESP_ERR_INVALID_STATE: 17 | return VL53L0X_ERROR_CONTROL_INTERFACE; 18 | case ESP_ERR_TIMEOUT: 19 | return VL53L0X_ERROR_TIME_OUT; 20 | default: 21 | return VL53L0X_ERROR_UNDEFINED; 22 | } 23 | } 24 | 25 | /** 26 | * Writes the supplied byte buffer to the device 27 | * @param Dev Device Handle 28 | * @param index The register index 29 | * @param pdata Pointer to uint8_t buffer containing the data to be written 30 | * @param count Number of bytes in the supplied byte buffer 31 | * @return VL53L0X_ERROR_NONE Success 32 | * @return "Other error code" See ::VL53L0X_Error 33 | */ 34 | VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) 35 | { 36 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 37 | i2c_master_start(cmd); 38 | 39 | // write I2C address 40 | i2c_master_write_byte(cmd, ( Dev->i2c_address << 1 ) | I2C_MASTER_WRITE, ACK_CHECK_EN); 41 | 42 | // write register 43 | i2c_master_write_byte(cmd, index, ACK_CHECK_EN); 44 | 45 | // Data 46 | // Note: Needed to use i2c_master_write_byte as i2c_master_write will not expect an ack 47 | // after each byte 48 | for (int i = 0; i < count; i++) 49 | { 50 | i2c_master_write_byte(cmd, *(pdata + i), ACK_CHECK_EN); 51 | } 52 | 53 | i2c_master_stop(cmd); 54 | esp_err_t ret = i2c_master_cmd_begin(Dev->i2c_port_num, cmd, 1000 / portTICK_RATE_MS); 55 | i2c_cmd_link_delete(cmd); 56 | 57 | return esp_to_vl53l0x_error(ret); 58 | } 59 | 60 | /** 61 | * Reads the requested number of bytes from the device 62 | * @param Dev Device Handle 63 | * @param index The register index 64 | * @param pdata Pointer to the uint8_t buffer to store read data 65 | * @param count Number of uint8_t's to read 66 | * @return VL53L0X_ERROR_NONE Success 67 | * @return "Other error code" See ::VL53L0X_Error 68 | */ 69 | VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) 70 | { 71 | // I2C write 72 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 73 | 74 | ////// First tell the VL53L0X which register we are reading from 75 | ESP_ERROR_CHECK(i2c_master_start(cmd)); 76 | 77 | // Write I2C address 78 | ESP_ERROR_CHECK(i2c_master_write_byte(cmd, ( Dev->i2c_address << 1 ) | I2C_MASTER_WRITE, ACK_CHECK_EN)); 79 | // Write register 80 | ESP_ERROR_CHECK(i2c_master_write_byte(cmd, index, ACK_CHECK_EN)); 81 | 82 | ////// Second, read from the register 83 | ESP_ERROR_CHECK(i2c_master_start(cmd)); 84 | 85 | // Write I2C address 86 | ESP_ERROR_CHECK(i2c_master_write_byte(cmd, ( Dev->i2c_address << 1 ) | I2C_MASTER_READ, ACK_CHECK_EN)); 87 | 88 | // Read data from register 89 | ESP_ERROR_CHECK(i2c_master_read(cmd, pdata, count, I2C_MASTER_LAST_NACK)); 90 | 91 | ESP_ERROR_CHECK(i2c_master_stop(cmd)); 92 | esp_err_t ret = i2c_master_cmd_begin(Dev->i2c_port_num, cmd, 1000 / portTICK_RATE_MS); 93 | i2c_cmd_link_delete(cmd); 94 | 95 | return esp_to_vl53l0x_error(ret); 96 | } 97 | 98 | /** 99 | * Write single byte register 100 | * @param Dev Device Handle 101 | * @param index The register index 102 | * @param data 8 bit register data 103 | * @return VL53L0X_ERROR_NONE Success 104 | * @return "Other error code" See ::VL53L0X_Error 105 | */ 106 | VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data) 107 | { 108 | return VL53L0X_WriteMulti(Dev, index, &data, 1); 109 | } 110 | 111 | /** 112 | * Write word register 113 | * @param Dev Device Handle 114 | * @param index The register index 115 | * @param data 16 bit register data 116 | * @return VL53L0X_ERROR_NONE Success 117 | * @return "Other error code" See ::VL53L0X_Error 118 | */ 119 | VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data) 120 | { 121 | uint8_t buffer[2]; // 2 122 | buffer[0] = (uint8_t)(data >> 8); 123 | buffer[1] = (uint8_t)(data & 0x00FF); 124 | 125 | return VL53L0X_WriteMulti(Dev, index, buffer, 2); 126 | } 127 | 128 | /** 129 | * Write double word (4 byte) register 130 | * @param Dev Device Handle 131 | * @param index The register index 132 | * @param data 32 bit register data 133 | * @return VL53L0X_ERROR_NONE Success 134 | * @return "Other error code" See ::VL53L0X_Error 135 | */ 136 | VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data) 137 | { 138 | uint8_t buffer[4]; // 4 139 | 140 | buffer[0] = (uint8_t) (data >> 24); 141 | buffer[1] = (uint8_t)((data & 0x00FF0000) >> 16); 142 | buffer[2] = (uint8_t)((data & 0x0000FF00) >> 8); 143 | buffer[3] = (uint8_t) (data & 0x000000FF); 144 | 145 | return VL53L0X_WriteMulti(Dev, index, buffer, 4); 146 | } 147 | 148 | /** 149 | * Read single byte register 150 | * @param Dev Device Handle 151 | * @param index The register index 152 | * @param data pointer to 8 bit data 153 | * @return VL53L0X_ERROR_NONE Success 154 | * @return "Other error code" See ::VL53L0X_Error 155 | */ 156 | VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data) 157 | { 158 | return VL53L0X_ReadMulti(Dev, index, data, 1); 159 | } 160 | 161 | /** 162 | * Read word (2byte) register 163 | * @param Dev Device Handle 164 | * @param index The register index 165 | * @param data pointer to 16 bit data 166 | * @return VL53L0X_ERROR_NONE Success 167 | * @return "Other error code" See ::VL53L0X_Error 168 | */ 169 | VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data) 170 | { 171 | VL53L0X_Error status; 172 | uint8_t buffer[2]; 173 | 174 | status = VL53L0X_ReadMulti(Dev, index, buffer, 2); 175 | *data = ((uint16_t)buffer[0]<<8) + (uint16_t)buffer[1]; 176 | 177 | return status; 178 | } 179 | 180 | /** 181 | * Read dword (4byte) register 182 | * @param Dev Device Handle 183 | * @param index The register index 184 | * @param data pointer to 32 bit data 185 | * @return VL53L0X_ERROR_NONE Success 186 | * @return "Other error code" See ::VL53L0X_Error 187 | */ 188 | VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data) 189 | { 190 | VL53L0X_Error status; 191 | uint8_t buffer[4]; 192 | 193 | status = VL53L0X_ReadMulti(Dev, index, buffer, 4); 194 | *data = ((uint32_t)buffer[0]<<24) + ((uint32_t)buffer[1]<<16) + 195 | ((uint32_t)buffer[2]<<8) + (uint32_t)buffer[3]; 196 | 197 | return status; 198 | } 199 | 200 | /** 201 | * Threat safe Update (read/modify/write) single byte register 202 | * 203 | * Final_reg = (Initial_reg & and_data) |or_data 204 | * 205 | * @param Dev Device Handle 206 | * @param index The register index 207 | * @param AndData 8 bit and data 208 | * @param OrData 8 bit or data 209 | * @return VL53L0X_ERROR_NONE Success 210 | * @return "Other error code" See ::VL53L0X_Error 211 | */ 212 | VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData) 213 | { 214 | VL53L0X_Error status; 215 | uint8_t data; 216 | 217 | status = VL53L0X_RdByte(Dev, index, &data); 218 | 219 | if (status != VL53L0X_ERROR_NONE) 220 | return status; 221 | 222 | data = (data & AndData) | OrData; 223 | 224 | return VL53L0X_WrByte(Dev, index, data); 225 | } 226 | 227 | /** @} end of VL53L0X_registerAccess_group */ 228 | 229 | 230 | /** 231 | * @brief execute delay in all polling API call 232 | * 233 | * A typical multi-thread or RTOs implementation is to sleep the task for some 5ms (with 100Hz max rate faster polling is not needed) 234 | * if nothing specific is need you can define it as an empty/void macro 235 | * @code 236 | * #define VL53L0X_PollingDelay(...) (void)0 237 | * @endcode 238 | * @param Dev Device Handle 239 | * @return VL53L0X_ERROR_NONE Success 240 | * @return "Other error code" See ::VL53L0X_Error 241 | */ 242 | VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev) 243 | { 244 | // Doesn't seem to be needed? 245 | //vTaskDelay(5 / portTICK_RATE_MS); 246 | return VL53L0X_ERROR_NONE; 247 | } 248 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_device.h: -------------------------------------------------------------------------------- 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 | /** 30 | * Device specific defines. To be adapted by implementer for the targeted 31 | * device. 32 | */ 33 | 34 | #ifndef _VL53L0X_DEVICE_H_ 35 | #define _VL53L0X_DEVICE_H_ 36 | 37 | #include "vl53l0x_types.h" 38 | 39 | 40 | /** @defgroup VL53L0X_DevSpecDefines_group VL53L0X cut1.1 Device Specific Defines 41 | * @brief VL53L0X cut1.1 Device Specific Defines 42 | * @{ 43 | */ 44 | 45 | 46 | /** @defgroup VL53L0X_DeviceError_group Device Error 47 | * @brief Device Error code 48 | * 49 | * This enum is Device specific it should be updated in the implementation 50 | * Use @a VL53L0X_GetStatusErrorString() to get the string. 51 | * It is related to Status Register of the Device. 52 | * @{ 53 | */ 54 | typedef uint8_t VL53L0X_DeviceError; 55 | 56 | #define VL53L0X_DEVICEERROR_NONE ((VL53L0X_DeviceError) 0) 57 | /*!< 0 NoError */ 58 | #define VL53L0X_DEVICEERROR_VCSELCONTINUITYTESTFAILURE ((VL53L0X_DeviceError) 1) 59 | #define VL53L0X_DEVICEERROR_VCSELWATCHDOGTESTFAILURE ((VL53L0X_DeviceError) 2) 60 | #define VL53L0X_DEVICEERROR_NOVHVVALUEFOUND ((VL53L0X_DeviceError) 3) 61 | #define VL53L0X_DEVICEERROR_MSRCNOTARGET ((VL53L0X_DeviceError) 4) 62 | #define VL53L0X_DEVICEERROR_SNRCHECK ((VL53L0X_DeviceError) 5) 63 | #define VL53L0X_DEVICEERROR_RANGEPHASECHECK ((VL53L0X_DeviceError) 6) 64 | #define VL53L0X_DEVICEERROR_SIGMATHRESHOLDCHECK ((VL53L0X_DeviceError) 7) 65 | #define VL53L0X_DEVICEERROR_TCC ((VL53L0X_DeviceError) 8) 66 | #define VL53L0X_DEVICEERROR_PHASECONSISTENCY ((VL53L0X_DeviceError) 9) 67 | #define VL53L0X_DEVICEERROR_MINCLIP ((VL53L0X_DeviceError) 10) 68 | #define VL53L0X_DEVICEERROR_RANGECOMPLETE ((VL53L0X_DeviceError) 11) 69 | #define VL53L0X_DEVICEERROR_ALGOUNDERFLOW ((VL53L0X_DeviceError) 12) 70 | #define VL53L0X_DEVICEERROR_ALGOOVERFLOW ((VL53L0X_DeviceError) 13) 71 | #define VL53L0X_DEVICEERROR_RANGEIGNORETHRESHOLD ((VL53L0X_DeviceError) 14) 72 | 73 | /** @} end of VL53L0X_DeviceError_group */ 74 | 75 | 76 | /** @defgroup VL53L0X_CheckEnable_group Check Enable list 77 | * @brief Check Enable code 78 | * 79 | * Define used to specify the LimitCheckId. 80 | * Use @a VL53L0X_GetLimitCheckInfo() to get the string. 81 | * @{ 82 | */ 83 | 84 | #define VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE 0 85 | #define VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE 1 86 | #define VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP 2 87 | #define VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD 3 88 | #define VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC 4 89 | #define VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE 5 90 | 91 | #define VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS 6 92 | 93 | /** @} end of VL53L0X_CheckEnable_group */ 94 | 95 | 96 | /** @defgroup VL53L0X_GpioFunctionality_group Gpio Functionality 97 | * @brief Defines the different functionalities for the device GPIO(s) 98 | * @{ 99 | */ 100 | typedef uint8_t VL53L0X_GpioFunctionality; 101 | 102 | #define VL53L0X_GPIOFUNCTIONALITY_OFF \ 103 | ((VL53L0X_GpioFunctionality) 0) /*!< NO Interrupt */ 104 | #define VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW \ 105 | ((VL53L0X_GpioFunctionality) 1) /*!< Level Low (value < thresh_low) */ 106 | #define VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH \ 107 | ((VL53L0X_GpioFunctionality) 2) /*!< Level High (value > thresh_high) */ 108 | #define VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT \ 109 | ((VL53L0X_GpioFunctionality) 3) 110 | /*!< Out Of Window (value < thresh_low OR value > thresh_high) */ 111 | #define VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY \ 112 | ((VL53L0X_GpioFunctionality) 4) /*!< New Sample Ready */ 113 | 114 | /** @} end of VL53L0X_GpioFunctionality_group */ 115 | 116 | 117 | /* Device register map */ 118 | 119 | /** @defgroup VL53L0X_DefineRegisters_group Define Registers 120 | * @brief List of all the defined registers 121 | * @{ 122 | */ 123 | #define VL53L0X_REG_SYSRANGE_START 0x000 124 | /** mask existing bit in #VL53L0X_REG_SYSRANGE_START*/ 125 | #define VL53L0X_REG_SYSRANGE_MODE_MASK 0x0F 126 | /** bit 0 in #VL53L0X_REG_SYSRANGE_START write 1 toggle state in 127 | * continuous mode and arm next shot in single shot mode */ 128 | #define VL53L0X_REG_SYSRANGE_MODE_START_STOP 0x01 129 | /** bit 1 write 0 in #VL53L0X_REG_SYSRANGE_START set single shot mode */ 130 | #define VL53L0X_REG_SYSRANGE_MODE_SINGLESHOT 0x00 131 | /** bit 1 write 1 in #VL53L0X_REG_SYSRANGE_START set back-to-back 132 | * operation mode */ 133 | #define VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK 0x02 134 | /** bit 2 write 1 in #VL53L0X_REG_SYSRANGE_START set timed operation 135 | * mode */ 136 | #define VL53L0X_REG_SYSRANGE_MODE_TIMED 0x04 137 | /** bit 3 write 1 in #VL53L0X_REG_SYSRANGE_START set histogram operation 138 | * mode */ 139 | #define VL53L0X_REG_SYSRANGE_MODE_HISTOGRAM 0x08 140 | 141 | 142 | #define VL53L0X_REG_SYSTEM_THRESH_HIGH 0x000C 143 | #define VL53L0X_REG_SYSTEM_THRESH_LOW 0x000E 144 | 145 | 146 | #define VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG 0x0001 147 | #define VL53L0X_REG_SYSTEM_RANGE_CONFIG 0x0009 148 | #define VL53L0X_REG_SYSTEM_INTERMEASUREMENT_PERIOD 0x0004 149 | 150 | 151 | #define VL53L0X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x000A 152 | #define VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_DISABLED 0x00 153 | #define VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_LEVEL_LOW 0x01 154 | #define VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_LEVEL_HIGH 0x02 155 | #define VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_OUT_OF_WINDOW 0x03 156 | #define VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY 0x04 157 | 158 | #define VL53L0X_REG_GPIO_HV_MUX_ACTIVE_HIGH 0x0084 159 | 160 | 161 | #define VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR 0x000B 162 | 163 | /* Result registers */ 164 | #define VL53L0X_REG_RESULT_INTERRUPT_STATUS 0x0013 165 | #define VL53L0X_REG_RESULT_RANGE_STATUS 0x0014 166 | 167 | #define VL53L0X_REG_RESULT_CORE_PAGE 1 168 | #define VL53L0X_REG_RESULT_CORE_AMBIENT_WINDOW_EVENTS_RTN 0x00BC 169 | #define VL53L0X_REG_RESULT_CORE_RANGING_TOTAL_EVENTS_RTN 0x00C0 170 | #define VL53L0X_REG_RESULT_CORE_AMBIENT_WINDOW_EVENTS_REF 0x00D0 171 | #define VL53L0X_REG_RESULT_CORE_RANGING_TOTAL_EVENTS_REF 0x00D4 172 | #define VL53L0X_REG_RESULT_PEAK_SIGNAL_RATE_REF 0x00B6 173 | 174 | /* Algo register */ 175 | 176 | #define VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM 0x0028 177 | 178 | #define VL53L0X_REG_I2C_SLAVE_DEVICE_ADDRESS 0x008a 179 | 180 | /* Check Limit registers */ 181 | #define VL53L0X_REG_MSRC_CONFIG_CONTROL 0x0060 182 | 183 | #define VL53L0X_REG_PRE_RANGE_CONFIG_MIN_SNR 0X0027 184 | #define VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW 0x0056 185 | #define VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH 0x0057 186 | #define VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT 0x0064 187 | 188 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_SNR 0X0067 189 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW 0x0047 190 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH 0x0048 191 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT 0x0044 192 | 193 | 194 | #define VL53L0X_REG_PRE_RANGE_CONFIG_SIGMA_THRESH_HI 0X0061 195 | #define VL53L0X_REG_PRE_RANGE_CONFIG_SIGMA_THRESH_LO 0X0062 196 | 197 | /* PRE RANGE registers */ 198 | #define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD 0x0050 199 | #define VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI 0x0051 200 | #define VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_LO 0x0052 201 | 202 | #define VL53L0X_REG_SYSTEM_HISTOGRAM_BIN 0x0081 203 | #define VL53L0X_REG_HISTOGRAM_CONFIG_INITIAL_PHASE_SELECT 0x0033 204 | #define VL53L0X_REG_HISTOGRAM_CONFIG_READOUT_CTRL 0x0055 205 | 206 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x0070 207 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI 0x0071 208 | #define VL53L0X_REG_FINAL_RANGE_CONFIG_TIMEOUT_MACROP_LO 0x0072 209 | #define VL53L0X_REG_CROSSTALK_COMPENSATION_PEAK_RATE_MCPS 0x0020 210 | 211 | #define VL53L0X_REG_MSRC_CONFIG_TIMEOUT_MACROP 0x0046 212 | 213 | 214 | #define VL53L0X_REG_SOFT_RESET_GO2_SOFT_RESET_N 0x00bf 215 | #define VL53L0X_REG_IDENTIFICATION_MODEL_ID 0x00c0 216 | #define VL53L0X_REG_IDENTIFICATION_REVISION_ID 0x00c2 217 | 218 | #define VL53L0X_REG_OSC_CALIBRATE_VAL 0x00f8 219 | 220 | 221 | #define VL53L0X_SIGMA_ESTIMATE_MAX_VALUE 65535 222 | /* equivalent to a range sigma of 655.35mm */ 223 | 224 | #define VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH 0x032 225 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0 0x0B0 226 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_1 0x0B1 227 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_2 0x0B2 228 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_3 0x0B3 229 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_4 0x0B4 230 | #define VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_5 0x0B5 231 | 232 | #define VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT 0xB6 233 | #define VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD 0x4E /* 0x14E */ 234 | #define VL53L0X_REG_DYNAMIC_SPAD_REF_EN_START_OFFSET 0x4F /* 0x14F */ 235 | #define VL53L0X_REG_POWER_MANAGEMENT_GO1_POWER_FORCE 0x80 236 | 237 | /* 238 | * Speed of light in um per 1E-10 Seconds 239 | */ 240 | 241 | #define VL53L0X_SPEED_OF_LIGHT_IN_AIR 2997 242 | 243 | #define VL53L0X_REG_VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV 0x0089 244 | 245 | #define VL53L0X_REG_ALGO_PHASECAL_LIM 0x0030 /* 0x130 */ 246 | #define VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT 0x0030 247 | 248 | /** @} VL53L0X_DefineRegisters_group */ 249 | 250 | /** @} VL53L0X_DevSpecDefines_group */ 251 | 252 | 253 | #endif 254 | 255 | /* _VL53L0X_DEVICE_H_ */ 256 | 257 | 258 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_api_strings.h: -------------------------------------------------------------------------------- 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 | #ifndef VL53L0X_API_STRINGS_H_ 30 | #define VL53L0X_API_STRINGS_H_ 31 | 32 | #include "vl53l0x_def.h" 33 | #include "vl53l0x_platform.h" 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | 40 | VL53L0X_Error VL53L0X_get_device_info(VL53L0X_DEV Dev, 41 | VL53L0X_DeviceInfo_t *pVL53L0X_DeviceInfo); 42 | 43 | VL53L0X_Error VL53L0X_get_device_error_string(VL53L0X_DeviceError ErrorCode, 44 | char *pDeviceErrorString); 45 | 46 | VL53L0X_Error VL53L0X_get_range_status_string(uint8_t RangeStatus, 47 | char *pRangeStatusString); 48 | 49 | VL53L0X_Error VL53L0X_get_pal_error_string(VL53L0X_Error PalErrorCode, 50 | char *pPalErrorString); 51 | 52 | VL53L0X_Error VL53L0X_get_pal_state_string(VL53L0X_State PalStateCode, 53 | char *pPalStateString); 54 | 55 | VL53L0X_Error VL53L0X_get_sequence_steps_info( 56 | VL53L0X_SequenceStepId SequenceStepId, 57 | char *pSequenceStepsString); 58 | 59 | VL53L0X_Error VL53L0X_get_limit_check_info(VL53L0X_DEV Dev, uint16_t LimitCheckId, 60 | char *pLimitCheckString); 61 | 62 | 63 | #ifdef USE_EMPTY_STRING 64 | #define VL53L0X_STRING_DEVICE_INFO_NAME "" 65 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS0 "" 66 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS1 "" 67 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS2 "" 68 | #define VL53L0X_STRING_DEVICE_INFO_NAME_ES1 "" 69 | #define VL53L0X_STRING_DEVICE_INFO_TYPE "" 70 | 71 | /* PAL ERROR strings */ 72 | #define VL53L0X_STRING_ERROR_NONE "" 73 | #define VL53L0X_STRING_ERROR_CALIBRATION_WARNING "" 74 | #define VL53L0X_STRING_ERROR_MIN_CLIPPED "" 75 | #define VL53L0X_STRING_ERROR_UNDEFINED "" 76 | #define VL53L0X_STRING_ERROR_INVALID_PARAMS "" 77 | #define VL53L0X_STRING_ERROR_NOT_SUPPORTED "" 78 | #define VL53L0X_STRING_ERROR_RANGE_ERROR "" 79 | #define VL53L0X_STRING_ERROR_TIME_OUT "" 80 | #define VL53L0X_STRING_ERROR_MODE_NOT_SUPPORTED "" 81 | #define VL53L0X_STRING_ERROR_BUFFER_TOO_SMALL "" 82 | #define VL53L0X_STRING_ERROR_GPIO_NOT_EXISTING "" 83 | #define VL53L0X_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED "" 84 | #define VL53L0X_STRING_ERROR_CONTROL_INTERFACE "" 85 | #define VL53L0X_STRING_ERROR_INVALID_COMMAND "" 86 | #define VL53L0X_STRING_ERROR_DIVISION_BY_ZERO "" 87 | #define VL53L0X_STRING_ERROR_REF_SPAD_INIT "" 88 | #define VL53L0X_STRING_ERROR_NOT_IMPLEMENTED "" 89 | 90 | #define VL53L0X_STRING_UNKNOW_ERROR_CODE "" 91 | 92 | 93 | 94 | /* Range Status */ 95 | #define VL53L0X_STRING_RANGESTATUS_NONE "" 96 | #define VL53L0X_STRING_RANGESTATUS_RANGEVALID "" 97 | #define VL53L0X_STRING_RANGESTATUS_SIGMA "" 98 | #define VL53L0X_STRING_RANGESTATUS_SIGNAL "" 99 | #define VL53L0X_STRING_RANGESTATUS_MINRANGE "" 100 | #define VL53L0X_STRING_RANGESTATUS_PHASE "" 101 | #define VL53L0X_STRING_RANGESTATUS_HW "" 102 | 103 | 104 | /* Range Status */ 105 | #define VL53L0X_STRING_STATE_POWERDOWN "" 106 | #define VL53L0X_STRING_STATE_WAIT_STATICINIT "" 107 | #define VL53L0X_STRING_STATE_STANDBY "" 108 | #define VL53L0X_STRING_STATE_IDLE "" 109 | #define VL53L0X_STRING_STATE_RUNNING "" 110 | #define VL53L0X_STRING_STATE_UNKNOWN "" 111 | #define VL53L0X_STRING_STATE_ERROR "" 112 | 113 | 114 | /* Device Specific */ 115 | #define VL53L0X_STRING_DEVICEERROR_NONE "" 116 | #define VL53L0X_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE "" 117 | #define VL53L0X_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE "" 118 | #define VL53L0X_STRING_DEVICEERROR_NOVHVVALUEFOUND "" 119 | #define VL53L0X_STRING_DEVICEERROR_MSRCNOTARGET "" 120 | #define VL53L0X_STRING_DEVICEERROR_SNRCHECK "" 121 | #define VL53L0X_STRING_DEVICEERROR_RANGEPHASECHECK "" 122 | #define VL53L0X_STRING_DEVICEERROR_SIGMATHRESHOLDCHECK "" 123 | #define VL53L0X_STRING_DEVICEERROR_TCC "" 124 | #define VL53L0X_STRING_DEVICEERROR_PHASECONSISTENCY "" 125 | #define VL53L0X_STRING_DEVICEERROR_MINCLIP "" 126 | #define VL53L0X_STRING_DEVICEERROR_RANGECOMPLETE "" 127 | #define VL53L0X_STRING_DEVICEERROR_ALGOUNDERFLOW "" 128 | #define VL53L0X_STRING_DEVICEERROR_ALGOOVERFLOW "" 129 | #define VL53L0X_STRING_DEVICEERROR_RANGEIGNORETHRESHOLD "" 130 | #define VL53L0X_STRING_DEVICEERROR_UNKNOWN "" 131 | 132 | /* Check Enable */ 133 | #define VL53L0X_STRING_CHECKENABLE_SIGMA_FINAL_RANGE "" 134 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE "" 135 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_REF_CLIP "" 136 | #define VL53L0X_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD "" 137 | 138 | /* Sequence Step */ 139 | #define VL53L0X_STRING_SEQUENCESTEP_TCC "" 140 | #define VL53L0X_STRING_SEQUENCESTEP_DSS "" 141 | #define VL53L0X_STRING_SEQUENCESTEP_MSRC "" 142 | #define VL53L0X_STRING_SEQUENCESTEP_PRE_RANGE "" 143 | #define VL53L0X_STRING_SEQUENCESTEP_FINAL_RANGE "" 144 | #else 145 | #define VL53L0X_STRING_DEVICE_INFO_NAME "VL53L0X cut1.0" 146 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS0 "VL53L0X TS0" 147 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS1 "VL53L0X TS1" 148 | #define VL53L0X_STRING_DEVICE_INFO_NAME_TS2 "VL53L0X TS2" 149 | #define VL53L0X_STRING_DEVICE_INFO_NAME_ES1 "VL53L0X ES1 or later" 150 | #define VL53L0X_STRING_DEVICE_INFO_TYPE "VL53L0X" 151 | 152 | /* PAL ERROR strings */ 153 | #define VL53L0X_STRING_ERROR_NONE \ 154 | "No Error" 155 | #define VL53L0X_STRING_ERROR_CALIBRATION_WARNING \ 156 | "Calibration Warning Error" 157 | #define VL53L0X_STRING_ERROR_MIN_CLIPPED \ 158 | "Min clipped error" 159 | #define VL53L0X_STRING_ERROR_UNDEFINED \ 160 | "Undefined error" 161 | #define VL53L0X_STRING_ERROR_INVALID_PARAMS \ 162 | "Invalid parameters error" 163 | #define VL53L0X_STRING_ERROR_NOT_SUPPORTED \ 164 | "Not supported error" 165 | #define VL53L0X_STRING_ERROR_RANGE_ERROR \ 166 | "Range error" 167 | #define VL53L0X_STRING_ERROR_TIME_OUT \ 168 | "Time out error" 169 | #define VL53L0X_STRING_ERROR_MODE_NOT_SUPPORTED \ 170 | "Mode not supported error" 171 | #define VL53L0X_STRING_ERROR_BUFFER_TOO_SMALL \ 172 | "Buffer too small" 173 | #define VL53L0X_STRING_ERROR_GPIO_NOT_EXISTING \ 174 | "GPIO not existing" 175 | #define VL53L0X_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED \ 176 | "GPIO funct not supported" 177 | #define VL53L0X_STRING_ERROR_INTERRUPT_NOT_CLEARED \ 178 | "Interrupt not Cleared" 179 | #define VL53L0X_STRING_ERROR_CONTROL_INTERFACE \ 180 | "Control Interface Error" 181 | #define VL53L0X_STRING_ERROR_INVALID_COMMAND \ 182 | "Invalid Command Error" 183 | #define VL53L0X_STRING_ERROR_DIVISION_BY_ZERO \ 184 | "Division by zero Error" 185 | #define VL53L0X_STRING_ERROR_REF_SPAD_INIT \ 186 | "Reference Spad Init Error" 187 | #define VL53L0X_STRING_ERROR_NOT_IMPLEMENTED \ 188 | "Not implemented error" 189 | 190 | #define VL53L0X_STRING_UNKNOW_ERROR_CODE \ 191 | "Unknown Error Code" 192 | 193 | 194 | 195 | /* Range Status */ 196 | #define VL53L0X_STRING_RANGESTATUS_NONE "No Update" 197 | #define VL53L0X_STRING_RANGESTATUS_RANGEVALID "Range Valid" 198 | #define VL53L0X_STRING_RANGESTATUS_SIGMA "Sigma Fail" 199 | #define VL53L0X_STRING_RANGESTATUS_SIGNAL "Signal Fail" 200 | #define VL53L0X_STRING_RANGESTATUS_MINRANGE "Min Range Fail" 201 | #define VL53L0X_STRING_RANGESTATUS_PHASE "Phase Fail" 202 | #define VL53L0X_STRING_RANGESTATUS_HW "Hardware Fail" 203 | 204 | 205 | /* Range Status */ 206 | #define VL53L0X_STRING_STATE_POWERDOWN "POWERDOWN State" 207 | #define VL53L0X_STRING_STATE_WAIT_STATICINIT \ 208 | "Wait for staticinit State" 209 | #define VL53L0X_STRING_STATE_STANDBY "STANDBY State" 210 | #define VL53L0X_STRING_STATE_IDLE "IDLE State" 211 | #define VL53L0X_STRING_STATE_RUNNING "RUNNING State" 212 | #define VL53L0X_STRING_STATE_UNKNOWN "UNKNOWN State" 213 | #define VL53L0X_STRING_STATE_ERROR "ERROR State" 214 | 215 | 216 | /* Device Specific */ 217 | #define VL53L0X_STRING_DEVICEERROR_NONE "No Update" 218 | #define VL53L0X_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE \ 219 | "VCSEL Continuity Test Failure" 220 | #define VL53L0X_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE \ 221 | "VCSEL Watchdog Test Failure" 222 | #define VL53L0X_STRING_DEVICEERROR_NOVHVVALUEFOUND \ 223 | "No VHV Value found" 224 | #define VL53L0X_STRING_DEVICEERROR_MSRCNOTARGET \ 225 | "MSRC No Target Error" 226 | #define VL53L0X_STRING_DEVICEERROR_SNRCHECK \ 227 | "SNR Check Exit" 228 | #define VL53L0X_STRING_DEVICEERROR_RANGEPHASECHECK \ 229 | "Range Phase Check Error" 230 | #define VL53L0X_STRING_DEVICEERROR_SIGMATHRESHOLDCHECK \ 231 | "Sigma Threshold Check Error" 232 | #define VL53L0X_STRING_DEVICEERROR_TCC \ 233 | "TCC Error" 234 | #define VL53L0X_STRING_DEVICEERROR_PHASECONSISTENCY \ 235 | "Phase Consistency Error" 236 | #define VL53L0X_STRING_DEVICEERROR_MINCLIP \ 237 | "Min Clip Error" 238 | #define VL53L0X_STRING_DEVICEERROR_RANGECOMPLETE \ 239 | "Range Complete" 240 | #define VL53L0X_STRING_DEVICEERROR_ALGOUNDERFLOW \ 241 | "Range Algo Underflow Error" 242 | #define VL53L0X_STRING_DEVICEERROR_ALGOOVERFLOW \ 243 | "Range Algo Overlow Error" 244 | #define VL53L0X_STRING_DEVICEERROR_RANGEIGNORETHRESHOLD \ 245 | "Range Ignore Threshold Error" 246 | #define VL53L0X_STRING_DEVICEERROR_UNKNOWN \ 247 | "Unknown error code" 248 | 249 | /* Check Enable */ 250 | #define VL53L0X_STRING_CHECKENABLE_SIGMA_FINAL_RANGE \ 251 | "SIGMA FINAL RANGE" 252 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE \ 253 | "SIGNAL RATE FINAL RANGE" 254 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_REF_CLIP \ 255 | "SIGNAL REF CLIP" 256 | #define VL53L0X_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD \ 257 | "RANGE IGNORE THRESHOLD" 258 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_MSRC \ 259 | "SIGNAL RATE MSRC" 260 | #define VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_PRE_RANGE \ 261 | "SIGNAL RATE PRE RANGE" 262 | 263 | /* Sequence Step */ 264 | #define VL53L0X_STRING_SEQUENCESTEP_TCC "TCC" 265 | #define VL53L0X_STRING_SEQUENCESTEP_DSS "DSS" 266 | #define VL53L0X_STRING_SEQUENCESTEP_MSRC "MSRC" 267 | #define VL53L0X_STRING_SEQUENCESTEP_PRE_RANGE "PRE RANGE" 268 | #define VL53L0X_STRING_SEQUENCESTEP_FINAL_RANGE "FINAL RANGE" 269 | #endif /* USE_EMPTY_STRING */ 270 | 271 | 272 | #ifdef __cplusplus 273 | } 274 | #endif 275 | 276 | #endif 277 | 278 | -------------------------------------------------------------------------------- /core/src/vl53l0x_api_strings.c: -------------------------------------------------------------------------------- 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 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | -------------------------------------------------------------------------------- /core/inc/vl53l0x_def.h: -------------------------------------------------------------------------------- 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 | /** 30 | * @file VL53L0X_def.h 31 | * 32 | * @brief Type definitions for VL53L0X API. 33 | * 34 | */ 35 | 36 | 37 | #ifndef _VL53L0X_DEF_H_ 38 | #define _VL53L0X_DEF_H_ 39 | 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | /** @defgroup VL53L0X_globaldefine_group VL53L0X Defines 46 | * @brief VL53L0X Defines 47 | * @{ 48 | */ 49 | 50 | 51 | /** PAL SPECIFICATION major version */ 52 | #define VL53L0X10_SPECIFICATION_VER_MAJOR 1 53 | /** PAL SPECIFICATION minor version */ 54 | #define VL53L0X10_SPECIFICATION_VER_MINOR 2 55 | /** PAL SPECIFICATION sub version */ 56 | #define VL53L0X10_SPECIFICATION_VER_SUB 7 57 | /** PAL SPECIFICATION sub version */ 58 | #define VL53L0X10_SPECIFICATION_VER_REVISION 1440 59 | 60 | /** VL53L0X PAL IMPLEMENTATION major version */ 61 | #define VL53L0X10_IMPLEMENTATION_VER_MAJOR 1 62 | /** VL53L0X PAL IMPLEMENTATION minor version */ 63 | #define VL53L0X10_IMPLEMENTATION_VER_MINOR 0 64 | /** VL53L0X PAL IMPLEMENTATION sub version */ 65 | #define VL53L0X10_IMPLEMENTATION_VER_SUB 9 66 | /** VL53L0X PAL IMPLEMENTATION sub version */ 67 | #define VL53L0X10_IMPLEMENTATION_VER_REVISION 3673 68 | 69 | /** PAL SPECIFICATION major version */ 70 | #define VL53L0X_SPECIFICATION_VER_MAJOR 1 71 | /** PAL SPECIFICATION minor version */ 72 | #define VL53L0X_SPECIFICATION_VER_MINOR 2 73 | /** PAL SPECIFICATION sub version */ 74 | #define VL53L0X_SPECIFICATION_VER_SUB 7 75 | /** PAL SPECIFICATION sub version */ 76 | #define VL53L0X_SPECIFICATION_VER_REVISION 1440 77 | 78 | /** VL53L0X PAL IMPLEMENTATION major version */ 79 | #define VL53L0X_IMPLEMENTATION_VER_MAJOR 1 80 | /** VL53L0X PAL IMPLEMENTATION minor version */ 81 | #define VL53L0X_IMPLEMENTATION_VER_MINOR 0 82 | /** VL53L0X PAL IMPLEMENTATION sub version */ 83 | #define VL53L0X_IMPLEMENTATION_VER_SUB 2 84 | /** VL53L0X PAL IMPLEMENTATION sub version */ 85 | #define VL53L0X_IMPLEMENTATION_VER_REVISION 4823 86 | #define VL53L0X_DEFAULT_MAX_LOOP 2000 87 | #define VL53L0X_MAX_STRING_LENGTH 32 88 | 89 | 90 | #include "vl53l0x_device.h" 91 | #include "vl53l0x_types.h" 92 | 93 | 94 | /**************************************** 95 | * PRIVATE define do not edit 96 | ****************************************/ 97 | 98 | /** @brief Defines the parameters of the Get Version Functions 99 | */ 100 | typedef struct { 101 | uint32_t revision; /*!< revision number */ 102 | uint8_t major; /*!< major number */ 103 | uint8_t minor; /*!< minor number */ 104 | uint8_t build; /*!< build number */ 105 | } VL53L0X_Version_t; 106 | 107 | 108 | /** @brief Defines the parameters of the Get Device Info Functions 109 | */ 110 | typedef struct { 111 | char Name[VL53L0X_MAX_STRING_LENGTH]; 112 | /*!< Name of the Device e.g. Left_Distance */ 113 | char Type[VL53L0X_MAX_STRING_LENGTH]; 114 | /*!< Type of the Device e.g VL53L0X */ 115 | char ProductId[VL53L0X_MAX_STRING_LENGTH]; 116 | /*!< Product Identifier String */ 117 | uint8_t ProductType; 118 | /*!< Product Type, VL53L0X = 1, VL53L1 = 2 */ 119 | uint8_t ProductRevisionMajor; 120 | /*!< Product revision major */ 121 | uint8_t ProductRevisionMinor; 122 | /*!< Product revision minor */ 123 | } VL53L0X_DeviceInfo_t; 124 | 125 | 126 | /** @defgroup VL53L0X_define_Error_group Error and Warning code returned by API 127 | * The following DEFINE are used to identify the PAL ERROR 128 | * @{ 129 | */ 130 | 131 | typedef int8_t VL53L0X_Error; 132 | 133 | #define VL53L0X_ERROR_NONE ((VL53L0X_Error) 0) 134 | #define VL53L0X_ERROR_CALIBRATION_WARNING ((VL53L0X_Error) -1) 135 | /*!< Warning invalid calibration data may be in used 136 | \a VL53L0X_InitData() 137 | \a VL53L0X_GetOffsetCalibrationData 138 | \a VL53L0X_SetOffsetCalibrationData */ 139 | #define VL53L0X_ERROR_MIN_CLIPPED ((VL53L0X_Error) -2) 140 | /*!< Warning parameter passed was clipped to min before to be applied */ 141 | 142 | #define VL53L0X_ERROR_UNDEFINED ((VL53L0X_Error) -3) 143 | /*!< Unqualified error */ 144 | #define VL53L0X_ERROR_INVALID_PARAMS ((VL53L0X_Error) -4) 145 | /*!< Parameter passed is invalid or out of range */ 146 | #define VL53L0X_ERROR_NOT_SUPPORTED ((VL53L0X_Error) -5) 147 | /*!< Function is not supported in current mode or configuration */ 148 | #define VL53L0X_ERROR_RANGE_ERROR ((VL53L0X_Error) -6) 149 | /*!< Device report a ranging error interrupt status */ 150 | #define VL53L0X_ERROR_TIME_OUT ((VL53L0X_Error) -7) 151 | /*!< Aborted due to time out */ 152 | #define VL53L0X_ERROR_MODE_NOT_SUPPORTED ((VL53L0X_Error) -8) 153 | /*!< Asked mode is not supported by the device */ 154 | #define VL53L0X_ERROR_BUFFER_TOO_SMALL ((VL53L0X_Error) -9) 155 | /*!< ... */ 156 | #define VL53L0X_ERROR_GPIO_NOT_EXISTING ((VL53L0X_Error) -10) 157 | /*!< User tried to setup a non-existing GPIO pin */ 158 | #define VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED ((VL53L0X_Error) -11) 159 | /*!< unsupported GPIO functionality */ 160 | #define VL53L0X_ERROR_INTERRUPT_NOT_CLEARED ((VL53L0X_Error) -12) 161 | /*!< Error during interrupt clear */ 162 | #define VL53L0X_ERROR_CONTROL_INTERFACE ((VL53L0X_Error) -20) 163 | /*!< error reported from IO functions */ 164 | #define VL53L0X_ERROR_INVALID_COMMAND ((VL53L0X_Error) -30) 165 | /*!< The command is not allowed in the current device state 166 | * (power down) */ 167 | #define VL53L0X_ERROR_DIVISION_BY_ZERO ((VL53L0X_Error) -40) 168 | /*!< In the function a division by zero occurs */ 169 | #define VL53L0X_ERROR_REF_SPAD_INIT ((VL53L0X_Error) -50) 170 | /*!< Error during reference SPAD initialization */ 171 | #define VL53L0X_ERROR_NOT_IMPLEMENTED ((VL53L0X_Error) -99) 172 | /*!< Tells requested functionality has not been implemented yet or 173 | * not compatible with the device */ 174 | /** @} VL53L0X_define_Error_group */ 175 | 176 | 177 | /** @defgroup VL53L0X_define_DeviceModes_group Defines Device modes 178 | * Defines all possible modes for the device 179 | * @{ 180 | */ 181 | typedef uint8_t VL53L0X_DeviceModes; 182 | 183 | #define VL53L0X_DEVICEMODE_SINGLE_RANGING ((VL53L0X_DeviceModes) 0) 184 | #define VL53L0X_DEVICEMODE_CONTINUOUS_RANGING ((VL53L0X_DeviceModes) 1) 185 | #define VL53L0X_DEVICEMODE_SINGLE_HISTOGRAM ((VL53L0X_DeviceModes) 2) 186 | #define VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING ((VL53L0X_DeviceModes) 3) 187 | #define VL53L0X_DEVICEMODE_SINGLE_ALS ((VL53L0X_DeviceModes) 10) 188 | #define VL53L0X_DEVICEMODE_GPIO_DRIVE ((VL53L0X_DeviceModes) 20) 189 | #define VL53L0X_DEVICEMODE_GPIO_OSC ((VL53L0X_DeviceModes) 21) 190 | /* ... Modes to be added depending on device */ 191 | /** @} VL53L0X_define_DeviceModes_group */ 192 | 193 | 194 | 195 | /** @defgroup VL53L0X_define_HistogramModes_group Defines Histogram modes 196 | * Defines all possible Histogram modes for the device 197 | * @{ 198 | */ 199 | typedef uint8_t VL53L0X_HistogramModes; 200 | 201 | #define VL53L0X_HISTOGRAMMODE_DISABLED ((VL53L0X_HistogramModes) 0) 202 | /*!< Histogram Disabled */ 203 | #define VL53L0X_HISTOGRAMMODE_REFERENCE_ONLY ((VL53L0X_HistogramModes) 1) 204 | /*!< Histogram Reference array only */ 205 | #define VL53L0X_HISTOGRAMMODE_RETURN_ONLY ((VL53L0X_HistogramModes) 2) 206 | /*!< Histogram Return array only */ 207 | #define VL53L0X_HISTOGRAMMODE_BOTH ((VL53L0X_HistogramModes) 3) 208 | /*!< Histogram both Reference and Return Arrays */ 209 | /* ... Modes to be added depending on device */ 210 | /** @} VL53L0X_define_HistogramModes_group */ 211 | 212 | 213 | /** @defgroup VL53L0X_define_PowerModes_group List of available Power Modes 214 | * List of available Power Modes 215 | * @{ 216 | */ 217 | 218 | typedef uint8_t VL53L0X_PowerModes; 219 | 220 | #define VL53L0X_POWERMODE_STANDBY_LEVEL1 ((VL53L0X_PowerModes) 0) 221 | /*!< Standby level 1 */ 222 | #define VL53L0X_POWERMODE_STANDBY_LEVEL2 ((VL53L0X_PowerModes) 1) 223 | /*!< Standby level 2 */ 224 | #define VL53L0X_POWERMODE_IDLE_LEVEL1 ((VL53L0X_PowerModes) 2) 225 | /*!< Idle level 1 */ 226 | #define VL53L0X_POWERMODE_IDLE_LEVEL2 ((VL53L0X_PowerModes) 3) 227 | /*!< Idle level 2 */ 228 | 229 | /** @} VL53L0X_define_PowerModes_group */ 230 | 231 | 232 | /** @brief Defines all parameters for the device 233 | */ 234 | typedef struct { 235 | VL53L0X_DeviceModes DeviceMode; 236 | /*!< Defines type of measurement to be done for the next measure */ 237 | VL53L0X_HistogramModes HistogramMode; 238 | /*!< Defines type of histogram measurement to be done for the next 239 | * measure */ 240 | uint32_t MeasurementTimingBudgetMicroSeconds; 241 | /*!< Defines the allowed total time for a single measurement */ 242 | uint32_t InterMeasurementPeriodMilliSeconds; 243 | /*!< Defines time between two consecutive measurements (between two 244 | * measurement starts). If set to 0 means back-to-back mode */ 245 | uint8_t XTalkCompensationEnable; 246 | /*!< Tells if Crosstalk compensation shall be enable or not */ 247 | uint16_t XTalkCompensationRangeMilliMeter; 248 | /*!< CrossTalk compensation range in millimeter */ 249 | FixPoint1616_t XTalkCompensationRateMegaCps; 250 | /*!< CrossTalk compensation rate in Mega counts per seconds. 251 | * Expressed in 16.16 fixed point format. */ 252 | int32_t RangeOffsetMicroMeters; 253 | /*!< Range offset adjustment (mm). */ 254 | 255 | uint8_t LimitChecksEnable[VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS]; 256 | /*!< This Array store all the Limit Check enable for this device. */ 257 | uint8_t LimitChecksStatus[VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS]; 258 | /*!< This Array store all the Status of the check linked to last 259 | * measurement. */ 260 | FixPoint1616_t LimitChecksValue[VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS]; 261 | /*!< This Array store all the Limit Check value for this device */ 262 | 263 | uint8_t WrapAroundCheckEnable; 264 | /*!< Tells if Wrap Around Check shall be enable or not */ 265 | } VL53L0X_DeviceParameters_t; 266 | 267 | 268 | /** @defgroup VL53L0X_define_State_group Defines the current status of the device 269 | * Defines the current status of the device 270 | * @{ 271 | */ 272 | 273 | typedef uint8_t VL53L0X_State; 274 | 275 | #define VL53L0X_STATE_POWERDOWN ((VL53L0X_State) 0) 276 | /*!< Device is in HW reset */ 277 | #define VL53L0X_STATE_WAIT_STATICINIT ((VL53L0X_State) 1) 278 | /*!< Device is initialized and wait for static initialization */ 279 | #define VL53L0X_STATE_STANDBY ((VL53L0X_State) 2) 280 | /*!< Device is in Low power Standby mode */ 281 | #define VL53L0X_STATE_IDLE ((VL53L0X_State) 3) 282 | /*!< Device has been initialized and ready to do measurements */ 283 | #define VL53L0X_STATE_RUNNING ((VL53L0X_State) 4) 284 | /*!< Device is performing measurement */ 285 | #define VL53L0X_STATE_UNKNOWN ((VL53L0X_State) 98) 286 | /*!< Device is in unknown state and need to be rebooted */ 287 | #define VL53L0X_STATE_ERROR ((VL53L0X_State) 99) 288 | /*!< Device is in error state and need to be rebooted */ 289 | 290 | /** @} VL53L0X_define_State_group */ 291 | 292 | 293 | /** @brief Structure containing the Dmax computation parameters and data 294 | */ 295 | typedef struct { 296 | int32_t AmbTuningWindowFactor_K; 297 | /*!< internal algo tuning (*1000) */ 298 | int32_t RetSignalAt0mm; 299 | /*!< intermediate dmax computation value caching */ 300 | } VL53L0X_DMaxData_t; 301 | 302 | /** 303 | * @struct VL53L0X_RangeData_t 304 | * @brief Range measurement data. 305 | */ 306 | typedef struct { 307 | uint32_t TimeStamp; /*!< 32-bit time stamp. */ 308 | uint32_t MeasurementTimeUsec; 309 | /*!< Give the Measurement time needed by the device to do the 310 | * measurement.*/ 311 | 312 | 313 | uint16_t RangeMilliMeter; /*!< range distance in millimeter. */ 314 | 315 | uint16_t RangeDMaxMilliMeter; 316 | /*!< Tells what is the maximum detection distance of the device 317 | * in current setup and environment conditions (Filled when 318 | * applicable) */ 319 | 320 | FixPoint1616_t SignalRateRtnMegaCps; 321 | /*!< Return signal rate (MCPS)\n these is a 16.16 fix point 322 | * value, which is effectively a measure of target 323 | * reflectance.*/ 324 | FixPoint1616_t AmbientRateRtnMegaCps; 325 | /*!< Return ambient rate (MCPS)\n these is a 16.16 fix point 326 | * value, which is effectively a measure of the ambien 327 | * t light.*/ 328 | 329 | uint16_t EffectiveSpadRtnCount; 330 | /*!< Return the effective SPAD count for the return signal. 331 | * To obtain Real value it should be divided by 256 */ 332 | 333 | uint8_t ZoneId; 334 | /*!< Denotes which zone and range scheduler stage the range 335 | * data relates to. */ 336 | uint8_t RangeFractionalPart; 337 | /*!< Fractional part of range distance. Final value is a 338 | * FixPoint168 value. */ 339 | uint8_t RangeStatus; 340 | /*!< Range Status for the current measurement. This is device 341 | * dependent. Value = 0 means value is valid. 342 | * See \ref RangeStatusPage */ 343 | } VL53L0X_RangingMeasurementData_t; 344 | 345 | 346 | #define VL53L0X_HISTOGRAM_BUFFER_SIZE 24 347 | 348 | /** 349 | * @struct VL53L0X_HistogramData_t 350 | * @brief Histogram measurement data. 351 | */ 352 | typedef struct { 353 | /* Histogram Measurement data */ 354 | uint32_t HistogramData[VL53L0X_HISTOGRAM_BUFFER_SIZE]; 355 | /*!< Histogram data */ 356 | uint8_t HistogramType; /*!< Indicate the types of histogram data : 357 | Return only, Reference only, both Return and Reference */ 358 | uint8_t FirstBin; /*!< First Bin value */ 359 | uint8_t BufferSize; /*!< Buffer Size - Set by the user.*/ 360 | uint8_t NumberOfBins; 361 | /*!< Number of bins filled by the histogram measurement */ 362 | 363 | VL53L0X_DeviceError ErrorStatus; 364 | /*!< Error status of the current measurement. \n 365 | see @a ::VL53L0X_DeviceError @a VL53L0X_GetStatusErrorString() */ 366 | } VL53L0X_HistogramMeasurementData_t; 367 | 368 | #define VL53L0X_REF_SPAD_BUFFER_SIZE 6 369 | 370 | /** 371 | * @struct VL53L0X_SpadData_t 372 | * @brief Spad Configuration Data. 373 | */ 374 | typedef struct { 375 | uint8_t RefSpadEnables[VL53L0X_REF_SPAD_BUFFER_SIZE]; 376 | /*!< Reference Spad Enables */ 377 | uint8_t RefGoodSpadMap[VL53L0X_REF_SPAD_BUFFER_SIZE]; 378 | /*!< Reference Spad Good Spad Map */ 379 | } VL53L0X_SpadData_t; 380 | 381 | typedef struct { 382 | FixPoint1616_t OscFrequencyMHz; /* Frequency used */ 383 | 384 | uint16_t LastEncodedTimeout; 385 | /* last encoded Time out used for timing budget*/ 386 | 387 | VL53L0X_GpioFunctionality Pin0GpioFunctionality; 388 | /* store the functionality of the GPIO: pin0 */ 389 | 390 | uint32_t FinalRangeTimeoutMicroSecs; 391 | /*!< Execution time of the final range*/ 392 | uint8_t FinalRangeVcselPulsePeriod; 393 | /*!< Vcsel pulse period (pll clocks) for the final range measurement*/ 394 | uint32_t PreRangeTimeoutMicroSecs; 395 | /*!< Execution time of the final range*/ 396 | uint8_t PreRangeVcselPulsePeriod; 397 | /*!< Vcsel pulse period (pll clocks) for the pre-range measurement*/ 398 | 399 | uint16_t SigmaEstRefArray; 400 | /*!< Reference array sigma value in 1/100th of [mm] e.g. 100 = 1mm */ 401 | uint16_t SigmaEstEffPulseWidth; 402 | /*!< Effective Pulse width for sigma estimate in 1/100th 403 | * of ns e.g. 900 = 9.0ns */ 404 | uint16_t SigmaEstEffAmbWidth; 405 | /*!< Effective Ambient width for sigma estimate in 1/100th of ns 406 | * e.g. 500 = 5.0ns */ 407 | 408 | 409 | uint8_t ReadDataFromDeviceDone; /* Indicate if read from device has 410 | been done (==1) or not (==0) */ 411 | uint8_t ModuleId; /* Module ID */ 412 | uint8_t Revision; /* test Revision */ 413 | char ProductId[VL53L0X_MAX_STRING_LENGTH]; 414 | /* Product Identifier String */ 415 | uint8_t ReferenceSpadCount; /* used for ref spad management */ 416 | uint8_t ReferenceSpadType; /* used for ref spad management */ 417 | uint8_t RefSpadsInitialised; /* reports if ref spads are initialised. */ 418 | uint32_t PartUIDUpper; /*!< Unique Part ID Upper */ 419 | uint32_t PartUIDLower; /*!< Unique Part ID Lower */ 420 | FixPoint1616_t SignalRateMeasFixed400mm; /*!< Peek Signal rate 421 | at 400 mm*/ 422 | 423 | } VL53L0X_DeviceSpecificParameters_t; 424 | 425 | /** 426 | * @struct VL53L0X_DevData_t 427 | * 428 | * @brief VL53L0X PAL device ST private data structure \n 429 | * End user should never access any of these field directly 430 | * 431 | * These must never access directly but only via macro 432 | */ 433 | typedef struct { 434 | VL53L0X_DMaxData_t DMaxData; 435 | /*!< Dmax Data */ 436 | int32_t Part2PartOffsetNVMMicroMeter; 437 | /*!< backed up NVM value */ 438 | int32_t Part2PartOffsetAdjustmentNVMMicroMeter; 439 | /*!< backed up NVM value representing additional offset adjustment */ 440 | VL53L0X_DeviceParameters_t CurrentParameters; 441 | /*!< Current Device Parameter */ 442 | VL53L0X_RangingMeasurementData_t LastRangeMeasure; 443 | /*!< Ranging Data */ 444 | VL53L0X_HistogramMeasurementData_t LastHistogramMeasure; 445 | /*!< Histogram Data */ 446 | VL53L0X_DeviceSpecificParameters_t DeviceSpecificParameters; 447 | /*!< Parameters specific to the device */ 448 | VL53L0X_SpadData_t SpadData; 449 | /*!< Spad Data */ 450 | uint8_t SequenceConfig; 451 | /*!< Internal value for the sequence config */ 452 | uint8_t RangeFractionalEnable; 453 | /*!< Enable/Disable fractional part of ranging data */ 454 | VL53L0X_State PalState; 455 | /*!< Current state of the PAL for this device */ 456 | VL53L0X_PowerModes PowerMode; 457 | /*!< Current Power Mode */ 458 | uint16_t SigmaEstRefArray; 459 | /*!< Reference array sigma value in 1/100th of [mm] e.g. 100 = 1mm */ 460 | uint16_t SigmaEstEffPulseWidth; 461 | /*!< Effective Pulse width for sigma estimate in 1/100th 462 | * of ns e.g. 900 = 9.0ns */ 463 | uint16_t SigmaEstEffAmbWidth; 464 | /*!< Effective Ambient width for sigma estimate in 1/100th of ns 465 | * e.g. 500 = 5.0ns */ 466 | uint8_t StopVariable; 467 | /*!< StopVariable used during the stop sequence */ 468 | uint16_t targetRefRate; 469 | /*!< Target Ambient Rate for Ref spad management */ 470 | FixPoint1616_t SigmaEstimate; 471 | /*!< Sigma Estimate - based on ambient & VCSEL rates and 472 | * signal_total_events */ 473 | FixPoint1616_t SignalEstimate; 474 | /*!< Signal Estimate - based on ambient & VCSEL rates and cross talk */ 475 | FixPoint1616_t LastSignalRefMcps; 476 | /*!< Latest Signal ref in Mcps */ 477 | uint8_t *pTuningSettingsPointer; 478 | /*!< Pointer for Tuning Settings table */ 479 | uint8_t UseInternalTuningSettings; 480 | /*!< Indicate if we use Tuning Settings table */ 481 | uint16_t LinearityCorrectiveGain; 482 | /*!< Linearity Corrective Gain value in x1000 */ 483 | uint16_t DmaxCalRangeMilliMeter; 484 | /*!< Dmax Calibration Range millimeter */ 485 | FixPoint1616_t DmaxCalSignalRateRtnMegaCps; 486 | /*!< Dmax Calibration Signal Rate Return MegaCps */ 487 | 488 | } VL53L0X_DevData_t; 489 | 490 | 491 | /** @defgroup VL53L0X_define_InterruptPolarity_group Defines the Polarity 492 | * of the Interrupt 493 | * Defines the Polarity of the Interrupt 494 | * @{ 495 | */ 496 | typedef uint8_t VL53L0X_InterruptPolarity; 497 | 498 | #define VL53L0X_INTERRUPTPOLARITY_LOW ((VL53L0X_InterruptPolarity) 0) 499 | /*!< Set active low polarity best setup for falling edge. */ 500 | #define VL53L0X_INTERRUPTPOLARITY_HIGH ((VL53L0X_InterruptPolarity) 1) 501 | /*!< Set active high polarity best setup for rising edge. */ 502 | 503 | /** @} VL53L0X_define_InterruptPolarity_group */ 504 | 505 | 506 | /** @defgroup VL53L0X_define_VcselPeriod_group Vcsel Period Defines 507 | * Defines the range measurement for which to access the vcsel period. 508 | * @{ 509 | */ 510 | typedef uint8_t VL53L0X_VcselPeriod; 511 | 512 | #define VL53L0X_VCSEL_PERIOD_PRE_RANGE ((VL53L0X_VcselPeriod) 0) 513 | /*!>9)&0xFFFF) 589 | #define VL53L0X_FIXPOINT97TOFIXPOINT1616(Value) \ 590 | (FixPoint1616_t)(Value<<9) 591 | 592 | #define VL53L0X_FIXPOINT1616TOFIXPOINT88(Value) \ 593 | (uint16_t)((Value>>8)&0xFFFF) 594 | #define VL53L0X_FIXPOINT88TOFIXPOINT1616(Value) \ 595 | (FixPoint1616_t)(Value<<8) 596 | 597 | #define VL53L0X_FIXPOINT1616TOFIXPOINT412(Value) \ 598 | (uint16_t)((Value>>4)&0xFFFF) 599 | #define VL53L0X_FIXPOINT412TOFIXPOINT1616(Value) \ 600 | (FixPoint1616_t)(Value<<4) 601 | 602 | #define VL53L0X_FIXPOINT1616TOFIXPOINT313(Value) \ 603 | (uint16_t)((Value>>3)&0xFFFF) 604 | #define VL53L0X_FIXPOINT313TOFIXPOINT1616(Value) \ 605 | (FixPoint1616_t)(Value<<3) 606 | 607 | #define VL53L0X_FIXPOINT1616TOFIXPOINT08(Value) \ 608 | (uint8_t)((Value>>8)&0x00FF) 609 | #define VL53L0X_FIXPOINT08TOFIXPOINT1616(Value) \ 610 | (FixPoint1616_t)(Value<<8) 611 | 612 | #define VL53L0X_FIXPOINT1616TOFIXPOINT53(Value) \ 613 | (uint8_t)((Value>>13)&0x00FF) 614 | #define VL53L0X_FIXPOINT53TOFIXPOINT1616(Value) \ 615 | (FixPoint1616_t)(Value<<13) 616 | 617 | #define VL53L0X_FIXPOINT1616TOFIXPOINT102(Value) \ 618 | (uint16_t)((Value>>14)&0x0FFF) 619 | #define VL53L0X_FIXPOINT102TOFIXPOINT1616(Value) \ 620 | (FixPoint1616_t)(Value<<12) 621 | 622 | #define VL53L0X_MAKEUINT16(lsb, msb) (uint16_t)((((uint16_t)msb)<<8) + \ 623 | (uint16_t)lsb) 624 | 625 | /** @} VL53L0X_define_GeneralMacro_group */ 626 | 627 | /** @} VL53L0X_globaldefine_group */ 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | #ifdef __cplusplus 636 | } 637 | #endif 638 | 639 | 640 | #endif /* _VL53L0X_DEF_H_ */ 641 | -------------------------------------------------------------------------------- /core/src/vl53l0x_api_calibration.c: -------------------------------------------------------------------------------- 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_calibration.h" 32 | 33 | #ifndef __KERNEL__ 34 | #include 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 | #define REF_ARRAY_SPAD_0 0 45 | #define REF_ARRAY_SPAD_5 5 46 | #define REF_ARRAY_SPAD_10 10 47 | 48 | uint32_t refArrayQuadrants[4] = {REF_ARRAY_SPAD_10, REF_ARRAY_SPAD_5, 49 | REF_ARRAY_SPAD_0, REF_ARRAY_SPAD_5 }; 50 | 51 | VL53L0X_Error VL53L0X_perform_xtalk_calibration(VL53L0X_DEV Dev, 52 | FixPoint1616_t XTalkCalDistance, 53 | FixPoint1616_t *pXTalkCompensationRateMegaCps) 54 | { 55 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 56 | uint16_t sum_ranging = 0; 57 | uint16_t sum_spads = 0; 58 | FixPoint1616_t sum_signalRate = 0; 59 | FixPoint1616_t total_count = 0; 60 | uint8_t xtalk_meas = 0; 61 | VL53L0X_RangingMeasurementData_t RangingMeasurementData; 62 | FixPoint1616_t xTalkStoredMeanSignalRate; 63 | FixPoint1616_t xTalkStoredMeanRange; 64 | FixPoint1616_t xTalkStoredMeanRtnSpads; 65 | uint32_t signalXTalkTotalPerSpad; 66 | uint32_t xTalkStoredMeanRtnSpadsAsInt; 67 | uint32_t xTalkCalDistanceAsInt; 68 | FixPoint1616_t XTalkCompensationRateMegaCps; 69 | 70 | if (XTalkCalDistance <= 0) 71 | Status = VL53L0X_ERROR_INVALID_PARAMS; 72 | 73 | /* Disable the XTalk compensation */ 74 | if (Status == VL53L0X_ERROR_NONE) 75 | Status = VL53L0X_SetXTalkCompensationEnable(Dev, 0); 76 | 77 | /* Disable the RIT */ 78 | if (Status == VL53L0X_ERROR_NONE) { 79 | Status = VL53L0X_SetLimitCheckEnable(Dev, 80 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 0); 81 | } 82 | 83 | /* Perform 50 measurements and compute the averages */ 84 | if (Status == VL53L0X_ERROR_NONE) { 85 | sum_ranging = 0; 86 | sum_spads = 0; 87 | sum_signalRate = 0; 88 | total_count = 0; 89 | for (xtalk_meas = 0; xtalk_meas < 50; xtalk_meas++) { 90 | Status = VL53L0X_PerformSingleRangingMeasurement(Dev, 91 | &RangingMeasurementData); 92 | 93 | if (Status != VL53L0X_ERROR_NONE) 94 | break; 95 | 96 | /* The range is valid when RangeStatus = 0 */ 97 | if (RangingMeasurementData.RangeStatus == 0) { 98 | sum_ranging = sum_ranging + 99 | RangingMeasurementData.RangeMilliMeter; 100 | sum_signalRate = sum_signalRate + 101 | RangingMeasurementData.SignalRateRtnMegaCps; 102 | sum_spads = sum_spads + 103 | RangingMeasurementData.EffectiveSpadRtnCount 104 | / 256; 105 | total_count = total_count + 1; 106 | } 107 | } 108 | 109 | /* no valid values found */ 110 | if (total_count == 0) 111 | Status = VL53L0X_ERROR_RANGE_ERROR; 112 | 113 | } 114 | 115 | 116 | if (Status == VL53L0X_ERROR_NONE) { 117 | /* FixPoint1616_t / uint16_t = FixPoint1616_t */ 118 | xTalkStoredMeanSignalRate = sum_signalRate / total_count; 119 | xTalkStoredMeanRange = (FixPoint1616_t)((uint32_t)( 120 | sum_ranging << 16) / total_count); 121 | xTalkStoredMeanRtnSpads = (FixPoint1616_t)((uint32_t)( 122 | sum_spads << 16) / total_count); 123 | 124 | /* Round Mean Spads to Whole Number. 125 | * Typically the calculated mean SPAD count is a whole number 126 | * or very close to a whole 127 | * number, therefore any truncation will not result in a 128 | * significant loss in accuracy. 129 | * Also, for a grey target at a typical distance of around 130 | * 400mm, around 220 SPADs will 131 | * be enabled, therefore, any truncation will result in a loss 132 | * of accuracy of less than 133 | * 0.5%. 134 | */ 135 | xTalkStoredMeanRtnSpadsAsInt = (xTalkStoredMeanRtnSpads + 136 | 0x8000) >> 16; 137 | 138 | /* Round Cal Distance to Whole Number. 139 | * Note that the cal distance is in mm, therefore no resolution 140 | * is lost.*/ 141 | xTalkCalDistanceAsInt = (XTalkCalDistance + 0x8000) >> 16; 142 | 143 | if (xTalkStoredMeanRtnSpadsAsInt == 0 || 144 | xTalkCalDistanceAsInt == 0 || 145 | xTalkStoredMeanRange >= XTalkCalDistance) { 146 | XTalkCompensationRateMegaCps = 0; 147 | } else { 148 | /* Round Cal Distance to Whole Number. 149 | Note that the cal distance is in mm, therefore no 150 | resolution is lost.*/ 151 | xTalkCalDistanceAsInt = (XTalkCalDistance + 152 | 0x8000) >> 16; 153 | 154 | /* Apply division by mean spad count early in the 155 | * calculation to keep the numbers small. 156 | * This ensures we can maintain a 32bit calculation. 157 | * Fixed1616 / int := Fixed1616 */ 158 | signalXTalkTotalPerSpad = (xTalkStoredMeanSignalRate) / 159 | xTalkStoredMeanRtnSpadsAsInt; 160 | 161 | /* Complete the calculation for total Signal XTalk per 162 | * SPAD 163 | * Fixed1616 * (Fixed1616 - Fixed1616/int) := 164 | * (2^16 * Fixed1616) 165 | */ 166 | signalXTalkTotalPerSpad *= ((1 << 16) - 167 | (xTalkStoredMeanRange / xTalkCalDistanceAsInt)); 168 | 169 | /* Round from 2^16 * Fixed1616, to Fixed1616. */ 170 | XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad 171 | + 0x8000) >> 16; 172 | } 173 | 174 | *pXTalkCompensationRateMegaCps = XTalkCompensationRateMegaCps; 175 | 176 | /* Enable the XTalk compensation */ 177 | if (Status == VL53L0X_ERROR_NONE) 178 | Status = VL53L0X_SetXTalkCompensationEnable(Dev, 1); 179 | 180 | /* Enable the XTalk compensation */ 181 | if (Status == VL53L0X_ERROR_NONE) 182 | Status = VL53L0X_SetXTalkCompensationRateMegaCps(Dev, 183 | XTalkCompensationRateMegaCps); 184 | 185 | } 186 | 187 | return Status; 188 | } 189 | 190 | VL53L0X_Error VL53L0X_perform_offset_calibration(VL53L0X_DEV Dev, 191 | FixPoint1616_t CalDistanceMilliMeter, 192 | int32_t *pOffsetMicroMeter) 193 | { 194 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 195 | uint16_t sum_ranging = 0; 196 | FixPoint1616_t total_count = 0; 197 | VL53L0X_RangingMeasurementData_t RangingMeasurementData; 198 | FixPoint1616_t StoredMeanRange; 199 | uint32_t StoredMeanRangeAsInt; 200 | uint32_t CalDistanceAsInt_mm; 201 | uint8_t SequenceStepEnabled; 202 | int meas = 0; 203 | 204 | if (CalDistanceMilliMeter <= 0) 205 | Status = VL53L0X_ERROR_INVALID_PARAMS; 206 | 207 | if (Status == VL53L0X_ERROR_NONE) 208 | Status = VL53L0X_SetOffsetCalibrationDataMicroMeter(Dev, 0); 209 | 210 | 211 | /* Get the value of the TCC */ 212 | if (Status == VL53L0X_ERROR_NONE) 213 | Status = VL53L0X_GetSequenceStepEnable(Dev, 214 | VL53L0X_SEQUENCESTEP_TCC, &SequenceStepEnabled); 215 | 216 | 217 | /* Disable the TCC */ 218 | if (Status == VL53L0X_ERROR_NONE) 219 | Status = VL53L0X_SetSequenceStepEnable(Dev, 220 | VL53L0X_SEQUENCESTEP_TCC, 0); 221 | 222 | 223 | /* Disable the RIT */ 224 | if (Status == VL53L0X_ERROR_NONE) 225 | Status = VL53L0X_SetLimitCheckEnable(Dev, 226 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 0); 227 | 228 | /* Perform 50 measurements and compute the averages */ 229 | if (Status == VL53L0X_ERROR_NONE) { 230 | sum_ranging = 0; 231 | total_count = 0; 232 | for (meas = 0; meas < 50; meas++) { 233 | Status = VL53L0X_PerformSingleRangingMeasurement(Dev, 234 | &RangingMeasurementData); 235 | 236 | if (Status != VL53L0X_ERROR_NONE) 237 | break; 238 | 239 | /* The range is valid when RangeStatus = 0 */ 240 | if (RangingMeasurementData.RangeStatus == 0) { 241 | sum_ranging = sum_ranging + 242 | RangingMeasurementData.RangeMilliMeter; 243 | total_count = total_count + 1; 244 | } 245 | } 246 | 247 | /* no valid values found */ 248 | if (total_count == 0) 249 | Status = VL53L0X_ERROR_RANGE_ERROR; 250 | } 251 | 252 | 253 | if (Status == VL53L0X_ERROR_NONE) { 254 | /* FixPoint1616_t / uint16_t = FixPoint1616_t */ 255 | StoredMeanRange = (FixPoint1616_t)((uint32_t)(sum_ranging << 16) 256 | / total_count); 257 | 258 | StoredMeanRangeAsInt = (StoredMeanRange + 0x8000) >> 16; 259 | 260 | /* Round Cal Distance to Whole Number. 261 | * Note that the cal distance is in mm, therefore no resolution 262 | * is lost.*/ 263 | CalDistanceAsInt_mm = (CalDistanceMilliMeter + 0x8000) >> 16; 264 | 265 | *pOffsetMicroMeter = (CalDistanceAsInt_mm - 266 | StoredMeanRangeAsInt) * 1000; 267 | 268 | /* Apply the calculated offset */ 269 | if (Status == VL53L0X_ERROR_NONE) { 270 | VL53L0X_SETPARAMETERFIELD(Dev, RangeOffsetMicroMeters, 271 | *pOffsetMicroMeter); 272 | Status = VL53L0X_SetOffsetCalibrationDataMicroMeter(Dev, 273 | *pOffsetMicroMeter); 274 | } 275 | 276 | } 277 | 278 | /* Restore the TCC */ 279 | if (Status == VL53L0X_ERROR_NONE) { 280 | if (SequenceStepEnabled != 0) 281 | Status = VL53L0X_SetSequenceStepEnable(Dev, 282 | VL53L0X_SEQUENCESTEP_TCC, 1); 283 | } 284 | 285 | return Status; 286 | } 287 | 288 | 289 | VL53L0X_Error VL53L0X_set_offset_calibration_data_micro_meter(VL53L0X_DEV Dev, 290 | int32_t OffsetCalibrationDataMicroMeter) 291 | { 292 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 293 | int32_t cMaxOffsetMicroMeter = 511000; 294 | int32_t cMinOffsetMicroMeter = -512000; 295 | int16_t cOffsetRange = 4096; 296 | uint32_t encodedOffsetVal; 297 | 298 | LOG_FUNCTION_START(""); 299 | 300 | if (OffsetCalibrationDataMicroMeter > cMaxOffsetMicroMeter) 301 | OffsetCalibrationDataMicroMeter = cMaxOffsetMicroMeter; 302 | else if (OffsetCalibrationDataMicroMeter < cMinOffsetMicroMeter) 303 | OffsetCalibrationDataMicroMeter = cMinOffsetMicroMeter; 304 | 305 | /* The offset register is 10.2 format and units are mm 306 | * therefore conversion is applied by a division of 307 | * 250. 308 | */ 309 | if (OffsetCalibrationDataMicroMeter >= 0) { 310 | encodedOffsetVal = 311 | OffsetCalibrationDataMicroMeter/250; 312 | } else { 313 | encodedOffsetVal = 314 | cOffsetRange + 315 | OffsetCalibrationDataMicroMeter/250; 316 | } 317 | 318 | Status = VL53L0X_WrWord(Dev, 319 | VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM, 320 | encodedOffsetVal); 321 | 322 | LOG_FUNCTION_END(Status); 323 | return Status; 324 | } 325 | 326 | VL53L0X_Error VL53L0X_get_offset_calibration_data_micro_meter(VL53L0X_DEV Dev, 327 | int32_t *pOffsetCalibrationDataMicroMeter) 328 | { 329 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 330 | uint16_t RangeOffsetRegister; 331 | int16_t cMaxOffset = 2047; 332 | int16_t cOffsetRange = 4096; 333 | 334 | /* Note that offset has 10.2 format */ 335 | 336 | Status = VL53L0X_RdWord(Dev, 337 | VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM, 338 | &RangeOffsetRegister); 339 | 340 | if (Status == VL53L0X_ERROR_NONE) { 341 | RangeOffsetRegister = (RangeOffsetRegister & 0x0fff); 342 | 343 | /* Apply 12 bit 2's compliment conversion */ 344 | if (RangeOffsetRegister > cMaxOffset) 345 | *pOffsetCalibrationDataMicroMeter = 346 | (int16_t)(RangeOffsetRegister - cOffsetRange) 347 | * 250; 348 | else 349 | *pOffsetCalibrationDataMicroMeter = 350 | (int16_t)RangeOffsetRegister * 250; 351 | 352 | } 353 | 354 | return Status; 355 | } 356 | 357 | 358 | VL53L0X_Error VL53L0X_apply_offset_adjustment(VL53L0X_DEV Dev) 359 | { 360 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 361 | int32_t CorrectedOffsetMicroMeters; 362 | int32_t CurrentOffsetMicroMeters; 363 | 364 | /* if we run on this function we can read all the NVM info 365 | * used by the API */ 366 | Status = VL53L0X_get_info_from_device(Dev, 7); 367 | 368 | /* Read back current device offset */ 369 | if (Status == VL53L0X_ERROR_NONE) { 370 | Status = VL53L0X_GetOffsetCalibrationDataMicroMeter(Dev, 371 | &CurrentOffsetMicroMeters); 372 | } 373 | 374 | /* Apply Offset Adjustment derived from 400mm measurements */ 375 | if (Status == VL53L0X_ERROR_NONE) { 376 | 377 | /* Store initial device offset */ 378 | PALDevDataSet(Dev, Part2PartOffsetNVMMicroMeter, 379 | CurrentOffsetMicroMeters); 380 | 381 | CorrectedOffsetMicroMeters = CurrentOffsetMicroMeters + 382 | (int32_t)PALDevDataGet(Dev, 383 | Part2PartOffsetAdjustmentNVMMicroMeter); 384 | 385 | Status = VL53L0X_SetOffsetCalibrationDataMicroMeter(Dev, 386 | CorrectedOffsetMicroMeters); 387 | 388 | /* store current, adjusted offset */ 389 | if (Status == VL53L0X_ERROR_NONE) { 390 | VL53L0X_SETPARAMETERFIELD(Dev, RangeOffsetMicroMeters, 391 | CorrectedOffsetMicroMeters); 392 | } 393 | } 394 | 395 | return Status; 396 | } 397 | 398 | void get_next_good_spad(uint8_t goodSpadArray[], uint32_t size, 399 | uint32_t curr, int32_t *next) 400 | { 401 | uint32_t startIndex; 402 | uint32_t fineOffset; 403 | uint32_t cSpadsPerByte = 8; 404 | uint32_t coarseIndex; 405 | uint32_t fineIndex; 406 | uint8_t dataByte; 407 | uint8_t success = 0; 408 | 409 | /* 410 | * Starting with the current good spad, loop through the array to find 411 | * the next. i.e. the next bit set in the sequence. 412 | * 413 | * The coarse index is the byte index of the array and the fine index is 414 | * the index of the bit within each byte. 415 | */ 416 | 417 | *next = -1; 418 | 419 | startIndex = curr / cSpadsPerByte; 420 | fineOffset = curr % cSpadsPerByte; 421 | 422 | for (coarseIndex = startIndex; ((coarseIndex < size) && !success); 423 | coarseIndex++) { 424 | fineIndex = 0; 425 | dataByte = goodSpadArray[coarseIndex]; 426 | 427 | if (coarseIndex == startIndex) { 428 | /* locate the bit position of the provided current 429 | * spad bit before iterating */ 430 | dataByte >>= fineOffset; 431 | fineIndex = fineOffset; 432 | } 433 | 434 | while (fineIndex < cSpadsPerByte) { 435 | if ((dataByte & 0x1) == 1) { 436 | success = 1; 437 | *next = coarseIndex * cSpadsPerByte + fineIndex; 438 | break; 439 | } 440 | dataByte >>= 1; 441 | fineIndex++; 442 | } 443 | } 444 | } 445 | 446 | 447 | uint8_t is_aperture(uint32_t spadIndex) 448 | { 449 | /* 450 | * This function reports if a given spad index is an aperture SPAD by 451 | * deriving the quadrant. 452 | */ 453 | uint32_t quadrant; 454 | uint8_t isAperture = 1; 455 | quadrant = spadIndex >> 6; 456 | if (refArrayQuadrants[quadrant] == REF_ARRAY_SPAD_0) 457 | isAperture = 0; 458 | 459 | return isAperture; 460 | } 461 | 462 | 463 | VL53L0X_Error enable_spad_bit(uint8_t spadArray[], uint32_t size, 464 | uint32_t spadIndex) 465 | { 466 | VL53L0X_Error status = VL53L0X_ERROR_NONE; 467 | uint32_t cSpadsPerByte = 8; 468 | uint32_t coarseIndex; 469 | uint32_t fineIndex; 470 | 471 | coarseIndex = spadIndex / cSpadsPerByte; 472 | fineIndex = spadIndex % cSpadsPerByte; 473 | if (coarseIndex >= size) 474 | status = VL53L0X_ERROR_REF_SPAD_INIT; 475 | else 476 | spadArray[coarseIndex] |= (1 << fineIndex); 477 | 478 | return status; 479 | } 480 | 481 | VL53L0X_Error count_enabled_spads(uint8_t spadArray[], 482 | uint32_t byteCount, uint32_t maxSpads, 483 | uint32_t *pTotalSpadsEnabled, uint8_t *pIsAperture) 484 | { 485 | VL53L0X_Error status = VL53L0X_ERROR_NONE; 486 | uint32_t cSpadsPerByte = 8; 487 | uint32_t lastByte; 488 | uint32_t lastBit; 489 | uint32_t byteIndex = 0; 490 | uint32_t bitIndex = 0; 491 | uint8_t tempByte; 492 | uint8_t spadTypeIdentified = 0; 493 | 494 | /* The entire array will not be used for spads, therefore the last 495 | * byte and last bit is determined from the max spads value. 496 | */ 497 | 498 | lastByte = maxSpads / cSpadsPerByte; 499 | lastBit = maxSpads % cSpadsPerByte; 500 | 501 | /* Check that the max spads value does not exceed the array bounds. */ 502 | if (lastByte >= byteCount) 503 | status = VL53L0X_ERROR_REF_SPAD_INIT; 504 | 505 | *pTotalSpadsEnabled = 0; 506 | 507 | /* Count the bits enabled in the whole bytes */ 508 | for (byteIndex = 0; byteIndex <= (lastByte - 1); byteIndex++) { 509 | tempByte = spadArray[byteIndex]; 510 | 511 | for (bitIndex = 0; bitIndex <= cSpadsPerByte; bitIndex++) { 512 | if ((tempByte & 0x01) == 1) { 513 | (*pTotalSpadsEnabled)++; 514 | 515 | if (!spadTypeIdentified) { 516 | *pIsAperture = 1; 517 | if ((byteIndex < 2) && (bitIndex < 4)) 518 | *pIsAperture = 0; 519 | spadTypeIdentified = 1; 520 | } 521 | } 522 | tempByte >>= 1; 523 | } 524 | } 525 | 526 | /* Count the number of bits enabled in the last byte accounting 527 | * for the fact that not all bits in the byte may be used. 528 | */ 529 | tempByte = spadArray[lastByte]; 530 | 531 | for (bitIndex = 0; bitIndex <= lastBit; bitIndex++) { 532 | if ((tempByte & 0x01) == 1) 533 | (*pTotalSpadsEnabled)++; 534 | } 535 | 536 | return status; 537 | } 538 | 539 | VL53L0X_Error set_ref_spad_map(VL53L0X_DEV Dev, uint8_t *refSpadArray) 540 | { 541 | VL53L0X_Error status = VL53L0X_WriteMulti(Dev, 542 | VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, 543 | refSpadArray, 6); 544 | return status; 545 | } 546 | 547 | VL53L0X_Error get_ref_spad_map(VL53L0X_DEV Dev, uint8_t *refSpadArray) 548 | { 549 | VL53L0X_Error status = VL53L0X_ReadMulti(Dev, 550 | VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, 551 | refSpadArray, 552 | 6); 553 | return status; 554 | } 555 | 556 | VL53L0X_Error enable_ref_spads(VL53L0X_DEV Dev, 557 | uint8_t apertureSpads, 558 | uint8_t goodSpadArray[], 559 | uint8_t spadArray[], 560 | uint32_t size, 561 | uint32_t start, 562 | uint32_t offset, 563 | uint32_t spadCount, 564 | uint32_t *lastSpad) 565 | { 566 | VL53L0X_Error status = VL53L0X_ERROR_NONE; 567 | uint32_t index; 568 | uint32_t i; 569 | int32_t nextGoodSpad = offset; 570 | uint32_t currentSpad; 571 | uint8_t checkSpadArray[6]; 572 | 573 | /* 574 | * This function takes in a spad array which may or may not have SPADS 575 | * already enabled and appends from a given offset a requested number 576 | * of new SPAD enables. The 'good spad map' is applied to 577 | * determine the next SPADs to enable. 578 | * 579 | * This function applies to only aperture or only non-aperture spads. 580 | * Checks are performed to ensure this. 581 | */ 582 | 583 | currentSpad = offset; 584 | for (index = 0; index < spadCount; index++) { 585 | get_next_good_spad(goodSpadArray, size, currentSpad, 586 | &nextGoodSpad); 587 | 588 | if (nextGoodSpad == -1) { 589 | status = VL53L0X_ERROR_REF_SPAD_INIT; 590 | break; 591 | } 592 | 593 | /* Confirm that the next good SPAD is non-aperture */ 594 | if (is_aperture(start + nextGoodSpad) != apertureSpads) { 595 | /* if we can't get the required number of good aperture 596 | * spads from the current quadrant then this is an error 597 | */ 598 | status = VL53L0X_ERROR_REF_SPAD_INIT; 599 | break; 600 | } 601 | currentSpad = (uint32_t)nextGoodSpad; 602 | enable_spad_bit(spadArray, size, currentSpad); 603 | currentSpad++; 604 | } 605 | *lastSpad = currentSpad; 606 | 607 | if (status == VL53L0X_ERROR_NONE) 608 | status = set_ref_spad_map(Dev, spadArray); 609 | 610 | 611 | if (status == VL53L0X_ERROR_NONE) { 612 | status = get_ref_spad_map(Dev, checkSpadArray); 613 | 614 | i = 0; 615 | 616 | /* Compare spad maps. If not equal report error. */ 617 | while (i < size) { 618 | if (spadArray[i] != checkSpadArray[i]) { 619 | status = VL53L0X_ERROR_REF_SPAD_INIT; 620 | break; 621 | } 622 | i++; 623 | } 624 | } 625 | return status; 626 | } 627 | 628 | 629 | VL53L0X_Error perform_ref_signal_measurement(VL53L0X_DEV Dev, 630 | uint16_t *refSignalRate) 631 | { 632 | VL53L0X_Error status = VL53L0X_ERROR_NONE; 633 | VL53L0X_RangingMeasurementData_t rangingMeasurementData; 634 | 635 | uint8_t SequenceConfig = 0; 636 | 637 | /* store the value of the sequence config, 638 | * this will be reset before the end of the function 639 | */ 640 | 641 | SequenceConfig = PALDevDataGet(Dev, SequenceConfig); 642 | 643 | /* 644 | * This function performs a reference signal rate measurement. 645 | */ 646 | if (status == VL53L0X_ERROR_NONE) 647 | status = VL53L0X_WrByte(Dev, 648 | VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0xC0); 649 | 650 | if (status == VL53L0X_ERROR_NONE) 651 | status = VL53L0X_PerformSingleRangingMeasurement(Dev, 652 | &rangingMeasurementData); 653 | 654 | if (status == VL53L0X_ERROR_NONE) 655 | status = VL53L0X_WrByte(Dev, 0xFF, 0x01); 656 | 657 | if (status == VL53L0X_ERROR_NONE) 658 | status = VL53L0X_RdWord(Dev, 659 | VL53L0X_REG_RESULT_PEAK_SIGNAL_RATE_REF, 660 | refSignalRate); 661 | 662 | if (status == VL53L0X_ERROR_NONE) 663 | status = VL53L0X_WrByte(Dev, 0xFF, 0x00); 664 | 665 | if (status == VL53L0X_ERROR_NONE) { 666 | /* restore the previous Sequence Config */ 667 | status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 668 | SequenceConfig); 669 | if (status == VL53L0X_ERROR_NONE) 670 | PALDevDataSet(Dev, SequenceConfig, SequenceConfig); 671 | } 672 | 673 | return status; 674 | } 675 | 676 | VL53L0X_Error VL53L0X_perform_ref_spad_management(VL53L0X_DEV Dev, 677 | uint32_t *refSpadCount, 678 | uint8_t *isApertureSpads) 679 | { 680 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 681 | uint8_t lastSpadArray[6]; 682 | uint8_t startSelect = 0xB4; 683 | uint32_t minimumSpadCount = 3; 684 | uint32_t maxSpadCount = 44; 685 | uint32_t currentSpadIndex = 0; 686 | uint32_t lastSpadIndex = 0; 687 | int32_t nextGoodSpad = 0; 688 | uint16_t targetRefRate = 0x0A00; /* 20 MCPS in 9:7 format */ 689 | uint16_t peakSignalRateRef; 690 | uint32_t needAptSpads = 0; 691 | uint32_t index = 0; 692 | uint32_t spadArraySize = 6; 693 | uint32_t signalRateDiff = 0; 694 | uint32_t lastSignalRateDiff = 0; 695 | uint8_t complete = 0; 696 | uint8_t VhvSettings = 0; 697 | uint8_t PhaseCal = 0; 698 | uint32_t refSpadCount_int = 0; 699 | uint8_t isApertureSpads_int = 0; 700 | 701 | /* 702 | * The reference SPAD initialization procedure determines the minimum 703 | * amount of reference spads to be enables to achieve a target reference 704 | * signal rate and should be performed once during initialization. 705 | * 706 | * Either aperture or non-aperture spads are applied but never both. 707 | * Firstly non-aperture spads are set, begining with 5 spads, and 708 | * increased one spad at a time until the closest measurement to the 709 | * target rate is achieved. 710 | * 711 | * If the target rate is exceeded when 5 non-aperture spads are enabled, 712 | * initialization is performed instead with aperture spads. 713 | * 714 | * When setting spads, a 'Good Spad Map' is applied. 715 | * 716 | * This procedure operates within a SPAD window of interest of a maximum 717 | * 44 spads. 718 | * The start point is currently fixed to 180, which lies towards the end 719 | * of the non-aperture quadrant and runs in to the adjacent aperture 720 | * quadrant. 721 | */ 722 | 723 | 724 | targetRefRate = PALDevDataGet(Dev, targetRefRate); 725 | 726 | /* 727 | * Initialize Spad arrays. 728 | * Currently the good spad map is initialised to 'All good'. 729 | * This is a short term implementation. The good spad map will be 730 | * provided as an input. 731 | * Note that there are 6 bytes. Only the first 44 bits will be used to 732 | * represent spads. 733 | */ 734 | for (index = 0; index < spadArraySize; index++) 735 | Dev->Data.SpadData.RefSpadEnables[index] = 0; 736 | 737 | 738 | Status = VL53L0X_WrByte(Dev, 0xFF, 0x01); 739 | 740 | if (Status == VL53L0X_ERROR_NONE) 741 | Status = VL53L0X_WrByte(Dev, 742 | VL53L0X_REG_DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00); 743 | 744 | if (Status == VL53L0X_ERROR_NONE) 745 | Status = VL53L0X_WrByte(Dev, 746 | VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C); 747 | 748 | if (Status == VL53L0X_ERROR_NONE) 749 | Status = VL53L0X_WrByte(Dev, 0xFF, 0x00); 750 | 751 | if (Status == VL53L0X_ERROR_NONE) 752 | Status = VL53L0X_WrByte(Dev, 753 | VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, 754 | startSelect); 755 | 756 | 757 | if (Status == VL53L0X_ERROR_NONE) 758 | Status = VL53L0X_WrByte(Dev, 759 | VL53L0X_REG_POWER_MANAGEMENT_GO1_POWER_FORCE, 0); 760 | 761 | /* Perform ref calibration */ 762 | if (Status == VL53L0X_ERROR_NONE) 763 | Status = VL53L0X_perform_ref_calibration(Dev, &VhvSettings, 764 | &PhaseCal, 0); 765 | 766 | if (Status == VL53L0X_ERROR_NONE) { 767 | /* Enable Minimum NON-APERTURE Spads */ 768 | currentSpadIndex = 0; 769 | lastSpadIndex = currentSpadIndex; 770 | needAptSpads = 0; 771 | Status = enable_ref_spads(Dev, 772 | needAptSpads, 773 | Dev->Data.SpadData.RefGoodSpadMap, 774 | Dev->Data.SpadData.RefSpadEnables, 775 | spadArraySize, 776 | startSelect, 777 | currentSpadIndex, 778 | minimumSpadCount, 779 | &lastSpadIndex); 780 | } 781 | 782 | if (Status == VL53L0X_ERROR_NONE) { 783 | currentSpadIndex = lastSpadIndex; 784 | 785 | Status = perform_ref_signal_measurement(Dev, 786 | &peakSignalRateRef); 787 | if ((Status == VL53L0X_ERROR_NONE) && 788 | (peakSignalRateRef > targetRefRate)) { 789 | /* Signal rate measurement too high, 790 | * switch to APERTURE SPADs */ 791 | 792 | for (index = 0; index < spadArraySize; index++) 793 | Dev->Data.SpadData.RefSpadEnables[index] = 0; 794 | 795 | 796 | /* Increment to the first APERTURE spad */ 797 | while ((is_aperture(startSelect + currentSpadIndex) 798 | == 0) && (currentSpadIndex < maxSpadCount)) { 799 | currentSpadIndex++; 800 | } 801 | 802 | needAptSpads = 1; 803 | 804 | Status = enable_ref_spads(Dev, 805 | needAptSpads, 806 | Dev->Data.SpadData.RefGoodSpadMap, 807 | Dev->Data.SpadData.RefSpadEnables, 808 | spadArraySize, 809 | startSelect, 810 | currentSpadIndex, 811 | minimumSpadCount, 812 | &lastSpadIndex); 813 | 814 | if (Status == VL53L0X_ERROR_NONE) { 815 | currentSpadIndex = lastSpadIndex; 816 | Status = perform_ref_signal_measurement(Dev, 817 | &peakSignalRateRef); 818 | 819 | if ((Status == VL53L0X_ERROR_NONE) && 820 | (peakSignalRateRef > targetRefRate)) { 821 | /* Signal rate still too high after 822 | * setting the minimum number of 823 | * APERTURE spads. Can do no more 824 | * therefore set the min number of 825 | * aperture spads as the result. 826 | */ 827 | isApertureSpads_int = 1; 828 | refSpadCount_int = minimumSpadCount; 829 | } 830 | } 831 | } else { 832 | needAptSpads = 0; 833 | } 834 | } 835 | 836 | if ((Status == VL53L0X_ERROR_NONE) && 837 | (peakSignalRateRef < targetRefRate)) { 838 | /* At this point, the minimum number of either aperture 839 | * or non-aperture spads have been set. Proceed to add 840 | * spads and perform measurements until the target 841 | * reference is reached. 842 | */ 843 | isApertureSpads_int = needAptSpads; 844 | refSpadCount_int = minimumSpadCount; 845 | 846 | memcpy(lastSpadArray, Dev->Data.SpadData.RefSpadEnables, 847 | spadArraySize); 848 | lastSignalRateDiff = abs(peakSignalRateRef - 849 | targetRefRate); 850 | complete = 0; 851 | 852 | while (!complete) { 853 | get_next_good_spad( 854 | Dev->Data.SpadData.RefGoodSpadMap, 855 | spadArraySize, currentSpadIndex, 856 | &nextGoodSpad); 857 | 858 | if (nextGoodSpad == -1) { 859 | Status = VL53L0X_ERROR_REF_SPAD_INIT; 860 | break; 861 | } 862 | 863 | /* Cannot combine Aperture and Non-Aperture spads, so 864 | * ensure the current spad is of the correct type. 865 | */ 866 | if (is_aperture((uint32_t)startSelect + nextGoodSpad) != 867 | needAptSpads) { 868 | /* At this point we have enabled the maximum 869 | * number of Aperture spads. 870 | */ 871 | complete = 1; 872 | break; 873 | } 874 | 875 | (refSpadCount_int)++; 876 | 877 | currentSpadIndex = nextGoodSpad; 878 | Status = enable_spad_bit( 879 | Dev->Data.SpadData.RefSpadEnables, 880 | spadArraySize, currentSpadIndex); 881 | 882 | if (Status == VL53L0X_ERROR_NONE) { 883 | currentSpadIndex++; 884 | /* Proceed to apply the additional spad and 885 | * perform measurement. */ 886 | Status = set_ref_spad_map(Dev, 887 | Dev->Data.SpadData.RefSpadEnables); 888 | } 889 | 890 | if (Status != VL53L0X_ERROR_NONE) 891 | break; 892 | 893 | Status = perform_ref_signal_measurement(Dev, 894 | &peakSignalRateRef); 895 | 896 | if (Status != VL53L0X_ERROR_NONE) 897 | break; 898 | 899 | signalRateDiff = abs(peakSignalRateRef - targetRefRate); 900 | 901 | if (peakSignalRateRef > targetRefRate) { 902 | /* Select the spad map that provides the 903 | * measurement closest to the target rate, 904 | * either above or below it. 905 | */ 906 | if (signalRateDiff > lastSignalRateDiff) { 907 | /* Previous spad map produced a closer 908 | * measurement, so choose this. */ 909 | Status = set_ref_spad_map(Dev, 910 | lastSpadArray); 911 | memcpy( 912 | Dev->Data.SpadData.RefSpadEnables, 913 | lastSpadArray, spadArraySize); 914 | 915 | (refSpadCount_int)--; 916 | } 917 | complete = 1; 918 | } else { 919 | /* Continue to add spads */ 920 | lastSignalRateDiff = signalRateDiff; 921 | memcpy(lastSpadArray, 922 | Dev->Data.SpadData.RefSpadEnables, 923 | spadArraySize); 924 | } 925 | 926 | } /* while */ 927 | } 928 | 929 | if (Status == VL53L0X_ERROR_NONE) { 930 | *refSpadCount = refSpadCount_int; 931 | *isApertureSpads = isApertureSpads_int; 932 | 933 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, RefSpadsInitialised, 1); 934 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 935 | ReferenceSpadCount, (uint8_t)(*refSpadCount)); 936 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 937 | ReferenceSpadType, *isApertureSpads); 938 | } 939 | 940 | return Status; 941 | } 942 | 943 | VL53L0X_Error VL53L0X_set_reference_spads(VL53L0X_DEV Dev, 944 | uint32_t count, uint8_t isApertureSpads) 945 | { 946 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 947 | uint32_t currentSpadIndex = 0; 948 | uint8_t startSelect = 0xB4; 949 | uint32_t spadArraySize = 6; 950 | uint32_t maxSpadCount = 44; 951 | uint32_t lastSpadIndex; 952 | uint32_t index; 953 | 954 | /* 955 | * This function applies a requested number of reference spads, either 956 | * aperture or 957 | * non-aperture, as requested. 958 | * The good spad map will be applied. 959 | */ 960 | 961 | Status = VL53L0X_WrByte(Dev, 0xFF, 0x01); 962 | 963 | if (Status == VL53L0X_ERROR_NONE) 964 | Status = VL53L0X_WrByte(Dev, 965 | VL53L0X_REG_DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00); 966 | 967 | if (Status == VL53L0X_ERROR_NONE) 968 | Status = VL53L0X_WrByte(Dev, 969 | VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C); 970 | 971 | if (Status == VL53L0X_ERROR_NONE) 972 | Status = VL53L0X_WrByte(Dev, 0xFF, 0x00); 973 | 974 | if (Status == VL53L0X_ERROR_NONE) 975 | Status = VL53L0X_WrByte(Dev, 976 | VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, 977 | startSelect); 978 | 979 | for (index = 0; index < spadArraySize; index++) 980 | Dev->Data.SpadData.RefSpadEnables[index] = 0; 981 | 982 | if (isApertureSpads) { 983 | /* Increment to the first APERTURE spad */ 984 | while ((is_aperture(startSelect + currentSpadIndex) == 0) && 985 | (currentSpadIndex < maxSpadCount)) { 986 | currentSpadIndex++; 987 | } 988 | } 989 | Status = enable_ref_spads(Dev, 990 | isApertureSpads, 991 | Dev->Data.SpadData.RefGoodSpadMap, 992 | Dev->Data.SpadData.RefSpadEnables, 993 | spadArraySize, 994 | startSelect, 995 | currentSpadIndex, 996 | count, 997 | &lastSpadIndex); 998 | 999 | if (Status == VL53L0X_ERROR_NONE) { 1000 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, RefSpadsInitialised, 1); 1001 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 1002 | ReferenceSpadCount, (uint8_t)(count)); 1003 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 1004 | ReferenceSpadType, isApertureSpads); 1005 | } 1006 | 1007 | return Status; 1008 | } 1009 | 1010 | VL53L0X_Error VL53L0X_get_reference_spads(VL53L0X_DEV Dev, 1011 | uint32_t *pSpadCount, uint8_t *pIsApertureSpads) 1012 | { 1013 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1014 | uint8_t refSpadsInitialised; 1015 | uint8_t refSpadArray[6]; 1016 | uint32_t cMaxSpadCount = 44; 1017 | uint32_t cSpadArraySize = 6; 1018 | uint32_t spadsEnabled; 1019 | uint8_t isApertureSpads = 0; 1020 | 1021 | refSpadsInitialised = VL53L0X_GETDEVICESPECIFICPARAMETER(Dev, 1022 | RefSpadsInitialised); 1023 | 1024 | if (refSpadsInitialised == 1) { 1025 | 1026 | *pSpadCount = (uint32_t)VL53L0X_GETDEVICESPECIFICPARAMETER(Dev, 1027 | ReferenceSpadCount); 1028 | *pIsApertureSpads = VL53L0X_GETDEVICESPECIFICPARAMETER(Dev, 1029 | ReferenceSpadType); 1030 | } else { 1031 | 1032 | /* obtain spad info from device.*/ 1033 | Status = get_ref_spad_map(Dev, refSpadArray); 1034 | 1035 | if (Status == VL53L0X_ERROR_NONE) { 1036 | /* count enabled spads within spad map array and 1037 | * determine if Aperture or Non-Aperture. 1038 | */ 1039 | Status = count_enabled_spads(refSpadArray, 1040 | cSpadArraySize, 1041 | cMaxSpadCount, 1042 | &spadsEnabled, 1043 | &isApertureSpads); 1044 | 1045 | if (Status == VL53L0X_ERROR_NONE) { 1046 | 1047 | *pSpadCount = spadsEnabled; 1048 | *pIsApertureSpads = isApertureSpads; 1049 | 1050 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 1051 | RefSpadsInitialised, 1); 1052 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 1053 | ReferenceSpadCount, 1054 | (uint8_t)spadsEnabled); 1055 | VL53L0X_SETDEVICESPECIFICPARAMETER(Dev, 1056 | ReferenceSpadType, isApertureSpads); 1057 | } 1058 | } 1059 | } 1060 | 1061 | return Status; 1062 | } 1063 | 1064 | 1065 | VL53L0X_Error VL53L0X_perform_single_ref_calibration(VL53L0X_DEV Dev, 1066 | uint8_t vhv_init_byte) 1067 | { 1068 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1069 | 1070 | if (Status == VL53L0X_ERROR_NONE) 1071 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSRANGE_START, 1072 | VL53L0X_REG_SYSRANGE_MODE_START_STOP | 1073 | vhv_init_byte); 1074 | 1075 | if (Status == VL53L0X_ERROR_NONE) 1076 | Status = VL53L0X_measurement_poll_for_completion(Dev); 1077 | 1078 | if (Status == VL53L0X_ERROR_NONE) 1079 | Status = VL53L0X_ClearInterruptMask(Dev, 0); 1080 | 1081 | if (Status == VL53L0X_ERROR_NONE) 1082 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSRANGE_START, 0x00); 1083 | 1084 | return Status; 1085 | } 1086 | 1087 | 1088 | VL53L0X_Error VL53L0X_ref_calibration_io(VL53L0X_DEV Dev, uint8_t read_not_write, 1089 | uint8_t VhvSettings, uint8_t PhaseCal, 1090 | uint8_t *pVhvSettings, uint8_t *pPhaseCal, 1091 | const uint8_t vhv_enable, const uint8_t phase_enable) 1092 | { 1093 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1094 | uint8_t PhaseCalint = 0; 1095 | 1096 | /* Read VHV from device */ 1097 | Status |= VL53L0X_WrByte(Dev, 0xFF, 0x01); 1098 | Status |= VL53L0X_WrByte(Dev, 0x00, 0x00); 1099 | Status |= VL53L0X_WrByte(Dev, 0xFF, 0x00); 1100 | 1101 | if (read_not_write) { 1102 | if (vhv_enable) 1103 | Status |= VL53L0X_RdByte(Dev, 0xCB, pVhvSettings); 1104 | if (phase_enable) 1105 | Status |= VL53L0X_RdByte(Dev, 0xEE, &PhaseCalint); 1106 | } else { 1107 | if (vhv_enable) 1108 | Status |= VL53L0X_WrByte(Dev, 0xCB, VhvSettings); 1109 | if (phase_enable) 1110 | Status |= VL53L0X_UpdateByte(Dev, 0xEE, 0x80, PhaseCal); 1111 | } 1112 | 1113 | Status |= VL53L0X_WrByte(Dev, 0xFF, 0x01); 1114 | Status |= VL53L0X_WrByte(Dev, 0x00, 0x01); 1115 | Status |= VL53L0X_WrByte(Dev, 0xFF, 0x00); 1116 | 1117 | *pPhaseCal = (uint8_t)(PhaseCalint&0xEF); 1118 | 1119 | return Status; 1120 | } 1121 | 1122 | 1123 | VL53L0X_Error VL53L0X_perform_vhv_calibration(VL53L0X_DEV Dev, 1124 | uint8_t *pVhvSettings, const uint8_t get_data_enable, 1125 | const uint8_t restore_config) 1126 | { 1127 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1128 | uint8_t SequenceConfig = 0; 1129 | uint8_t VhvSettings = 0; 1130 | uint8_t PhaseCal = 0; 1131 | uint8_t PhaseCalInt = 0; 1132 | 1133 | /* store the value of the sequence config, 1134 | * this will be reset before the end of the function 1135 | */ 1136 | 1137 | if (restore_config) 1138 | SequenceConfig = PALDevDataGet(Dev, SequenceConfig); 1139 | 1140 | /* Run VHV */ 1141 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x01); 1142 | 1143 | if (Status == VL53L0X_ERROR_NONE) 1144 | Status = VL53L0X_perform_single_ref_calibration(Dev, 0x40); 1145 | 1146 | /* Read VHV from device */ 1147 | if ((Status == VL53L0X_ERROR_NONE) && (get_data_enable == 1)) { 1148 | Status = VL53L0X_ref_calibration_io(Dev, 1, 1149 | VhvSettings, PhaseCal, /* Not used here */ 1150 | pVhvSettings, &PhaseCalInt, 1151 | 1, 0); 1152 | } else 1153 | *pVhvSettings = 0; 1154 | 1155 | 1156 | if ((Status == VL53L0X_ERROR_NONE) && restore_config) { 1157 | /* restore the previous Sequence Config */ 1158 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 1159 | SequenceConfig); 1160 | if (Status == VL53L0X_ERROR_NONE) 1161 | PALDevDataSet(Dev, SequenceConfig, SequenceConfig); 1162 | 1163 | } 1164 | 1165 | return Status; 1166 | } 1167 | 1168 | VL53L0X_Error VL53L0X_perform_phase_calibration(VL53L0X_DEV Dev, 1169 | uint8_t *pPhaseCal, const uint8_t get_data_enable, 1170 | const uint8_t restore_config) 1171 | { 1172 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1173 | uint8_t SequenceConfig = 0; 1174 | uint8_t VhvSettings = 0; 1175 | uint8_t PhaseCal = 0; 1176 | uint8_t VhvSettingsint; 1177 | 1178 | /* store the value of the sequence config, 1179 | * this will be reset before the end of the function 1180 | */ 1181 | 1182 | if (restore_config) 1183 | SequenceConfig = PALDevDataGet(Dev, SequenceConfig); 1184 | 1185 | /* Run PhaseCal */ 1186 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x02); 1187 | 1188 | if (Status == VL53L0X_ERROR_NONE) 1189 | Status = VL53L0X_perform_single_ref_calibration(Dev, 0x0); 1190 | 1191 | /* Read PhaseCal from device */ 1192 | if ((Status == VL53L0X_ERROR_NONE) && (get_data_enable == 1)) { 1193 | Status = VL53L0X_ref_calibration_io(Dev, 1, 1194 | VhvSettings, PhaseCal, /* Not used here */ 1195 | &VhvSettingsint, pPhaseCal, 1196 | 0, 1); 1197 | } else 1198 | *pPhaseCal = 0; 1199 | 1200 | 1201 | if ((Status == VL53L0X_ERROR_NONE) && restore_config) { 1202 | /* restore the previous Sequence Config */ 1203 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 1204 | SequenceConfig); 1205 | if (Status == VL53L0X_ERROR_NONE) 1206 | PALDevDataSet(Dev, SequenceConfig, SequenceConfig); 1207 | 1208 | } 1209 | 1210 | return Status; 1211 | } 1212 | 1213 | VL53L0X_Error VL53L0X_perform_ref_calibration(VL53L0X_DEV Dev, 1214 | uint8_t *pVhvSettings, uint8_t *pPhaseCal, uint8_t get_data_enable) 1215 | { 1216 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1217 | uint8_t SequenceConfig = 0; 1218 | 1219 | /* store the value of the sequence config, 1220 | * this will be reset before the end of the function 1221 | */ 1222 | 1223 | SequenceConfig = PALDevDataGet(Dev, SequenceConfig); 1224 | 1225 | /* In the following function we don't save the config to optimize 1226 | * writes on device. Config is saved and restored only once. */ 1227 | Status = VL53L0X_perform_vhv_calibration( 1228 | Dev, pVhvSettings, get_data_enable, 0); 1229 | 1230 | 1231 | if (Status == VL53L0X_ERROR_NONE) 1232 | Status = VL53L0X_perform_phase_calibration( 1233 | Dev, pPhaseCal, get_data_enable, 0); 1234 | 1235 | 1236 | if (Status == VL53L0X_ERROR_NONE) { 1237 | /* restore the previous Sequence Config */ 1238 | Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 1239 | SequenceConfig); 1240 | if (Status == VL53L0X_ERROR_NONE) 1241 | PALDevDataSet(Dev, SequenceConfig, SequenceConfig); 1242 | 1243 | } 1244 | 1245 | return Status; 1246 | } 1247 | 1248 | VL53L0X_Error VL53L0X_set_ref_calibration(VL53L0X_DEV Dev, 1249 | uint8_t VhvSettings, uint8_t PhaseCal) 1250 | { 1251 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1252 | uint8_t pVhvSettings; 1253 | uint8_t pPhaseCal; 1254 | 1255 | Status = VL53L0X_ref_calibration_io(Dev, 0, 1256 | VhvSettings, PhaseCal, 1257 | &pVhvSettings, &pPhaseCal, 1258 | 1, 1); 1259 | 1260 | return Status; 1261 | } 1262 | 1263 | VL53L0X_Error VL53L0X_get_ref_calibration(VL53L0X_DEV Dev, 1264 | uint8_t *pVhvSettings, uint8_t *pPhaseCal) 1265 | { 1266 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; 1267 | uint8_t VhvSettings = 0; 1268 | uint8_t PhaseCal = 0; 1269 | 1270 | Status = VL53L0X_ref_calibration_io(Dev, 1, 1271 | VhvSettings, PhaseCal, 1272 | pVhvSettings, pPhaseCal, 1273 | 1, 1); 1274 | 1275 | return Status; 1276 | } 1277 | --------------------------------------------------------------------------------