Initial release. Mbed library for VL53L1CB
Embed:
(wiki syntax)
Show/hide line numbers
vl53l1_platform.c
00001 00002 /* 00003 * This file is part of VL53L1 Platform 00004 * 00005 * Copyright (c) 2016, STMicroelectronics - All Rights Reserved 00006 * 00007 * License terms: BSD 3-clause "New" or "Revised" License. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright notice, this 00013 * list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 00019 * 3. Neither the name of the copyright holder nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00031 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 */ 00035 00036 unsigned int i2creadCount = 0; 00037 unsigned int i2cwriteCount = 0; 00038 unsigned char SPI2C_Buffer[256]; 00039 00040 00041 #include <mbed_wait_api.h> 00042 #include "vl53l1_platform.h" 00043 #ifndef SMALL_FOOTPRINT 00044 #endif 00045 #include "vl53l1_platform_log.h" 00046 #include "vl53l1_api.h" 00047 #include "spi_interface.h" 00048 #include <string.h> 00049 #include <time.h> 00050 #include <math.h> 00051 00052 00053 00054 #define I2C_TIME_OUT_BASE 10 00055 #define I2C_TIME_OUT_BYTE 1 00056 00057 #ifdef VL53L1_LOG_ENABLE 00058 #define trace_print(level, ...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_PLATFORM, level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__) 00059 #define trace_i2c(...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_NONE, VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__) 00060 #endif 00061 00062 /* when not customized by application define dummy one */ 00063 #ifndef VL53L1_GetI2cBus 00064 /** This macro can be overloaded by user to enforce i2c sharing in RTOS context 00065 */ 00066 # define VL53L1_GetI2cBus(...) (void)0 00067 #endif 00068 00069 #ifndef VL53L1_PutI2cBus 00070 /** This macro can be overloaded by user to enforce i2c sharing in RTOS context 00071 */ 00072 # define VL53L1_PutI2cBus(...) (void)0 00073 #endif 00074 00075 uint8_t _I2CBuffer[256]; 00076 00077 00078 VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) 00079 { 00080 int status; 00081 // printf("VL53L1_WriteMulti %d %d %d \n",Dev->I2cDevAddr,index,count); 00082 status = v53l1x_i2c_write_if(pdata,Dev->i2c_slave_address, index,count); 00083 return status; 00084 } 00085 00086 00087 VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) 00088 { 00089 int status; 00090 00091 status = v53l1x_i2c_read_if(pdata,Dev->i2c_slave_address, index,count); 00092 00093 return status; 00094 } 00095 00096 00097 VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) 00098 { 00099 int status; 00100 00101 status = v53l1x_i2c_write_if(&data,Dev->i2c_slave_address, index,1); 00102 return status; 00103 } 00104 00105 00106 VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) 00107 { 00108 int status; 00109 uint8_t buffer[2]; 00110 00111 buffer[0] = data >> 8; 00112 buffer[1] = data & 0x00FF; 00113 status = v53l1x_i2c_write_if((uint8_t *)buffer,Dev->i2c_slave_address, index,2); 00114 return status; 00115 } 00116 00117 00118 VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) 00119 { 00120 int status; 00121 uint8_t buffer[4]; 00122 00123 buffer[0] = (data >> 24) & 0xFF; 00124 buffer[1] = (data >> 16) & 0xFF; 00125 buffer[2] = (data >> 8) & 0xFF; 00126 buffer[3] = (data >> 0) & 0xFF; 00127 status = v53l1x_i2c_write_if((uint8_t *)buffer,Dev->i2c_slave_address, index,4); 00128 return status; 00129 } 00130 00131 VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData) 00132 { 00133 int status; 00134 uint8_t buffer = 0; 00135 00136 /* read data direct onto buffer */ 00137 status = v53l1x_i2c_read_if(&buffer,Dev->i2c_slave_address, index,1); 00138 if (!status) 00139 { 00140 buffer = (buffer & AndData) | OrData; 00141 status = v53l1x_i2c_write_if(&buffer,Dev->i2c_slave_address, index,1); 00142 } 00143 return status; 00144 } 00145 00146 VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) 00147 { 00148 int status; 00149 00150 status = v53l1x_i2c_read_if(data,Dev->i2c_slave_address, index,1); //is this correct 00151 // printf("VL53L1_RdByte %d %d %d\n",Dev->i2c_slave_address, status,*data); 00152 if(status) 00153 return -1; 00154 00155 return 0; 00156 } 00157 00158 00159 VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) 00160 { 00161 int status; 00162 uint8_t buffer[2] = {0,0}; 00163 00164 status = v53l1x_i2c_read_if(buffer,Dev->i2c_slave_address, index,2); //is this correct 00165 if (!status) 00166 { 00167 *data = (buffer[0] << 8) + buffer[1]; 00168 } 00169 return status; 00170 00171 } 00172 00173 00174 00175 VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) 00176 { 00177 int status; 00178 uint8_t buffer[4] = {0,0,0,0}; 00179 00180 status = v53l1x_i2c_read_if(buffer,Dev->i2c_slave_address, index,4); 00181 if(!status) 00182 { 00183 *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; 00184 } 00185 return status; 00186 00187 } 00188 00189 00190 VL53L1_Error VL53L1_GetTickCount( 00191 uint32_t *ptick_count_ms) 00192 { 00193 00194 /* Returns current tick count in [ms] */ 00195 00196 VL53L1_Error status = VL53L1_ERROR_NONE; 00197 00198 GetTickCount( ptick_count_ms); 00199 00200 #ifdef VL53L1_LOG_ENABLE 00201 trace_print( 00202 VL53L1_TRACE_LEVEL_DEBUG, 00203 "VL53L1_GetTickCount() = %5u ms;\n", 00204 *ptick_count_ms); 00205 #endif 00206 00207 return status; 00208 } 00209 00210 00211 #define trace_print(level, ...) \ 00212 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_PLATFORM, \ 00213 level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__) 00214 00215 #define trace_i2c(...) \ 00216 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_NONE, \ 00217 VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__) 00218 00219 00220 VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz) 00221 { 00222 *ptimer_freq_hz = 0; 00223 00224 trace_print(VL53L1_TRACE_LEVEL_INFO, "VL53L1_GetTimerFrequency: Freq : %dHz\n", *ptimer_freq_hz); 00225 return VL53L1_ERROR_NONE; 00226 } 00227 00228 00229 VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_time){ 00230 (void)pdev; 00231 wait_ms(wait_time); 00232 return VL53L1_ERROR_NONE; 00233 } 00234 00235 00236 VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_time){ 00237 (void)pdev; 00238 wait_us(wait_time); 00239 return VL53L1_ERROR_NONE; 00240 } 00241 00242 VL53L1_Error VL53L1_WaitValueMaskEx( 00243 VL53L1_Dev_t *pdev, 00244 uint32_t timeout_ms, 00245 uint16_t index, 00246 uint8_t value, 00247 uint8_t mask, 00248 uint32_t poll_delay_ms) 00249 { 00250 00251 /* 00252 * Platform implementation of WaitValueMaskEx V2WReg script command 00253 * 00254 * WaitValueMaskEx( 00255 * duration_ms, 00256 * index, 00257 * value, 00258 * mask, 00259 * poll_delay_ms); 00260 */ 00261 00262 VL53L1_Error status = VL53L1_ERROR_NONE; 00263 uint32_t start_time_ms = 0; 00264 uint32_t current_time_ms = 0; 00265 uint32_t polling_time_ms = 0; 00266 uint8_t byte_value = 0; 00267 uint8_t found = 0; 00268 #ifdef VL53L1_LOG_ENABLE 00269 uint8_t trace_functions = VL53L1_TRACE_FUNCTION_NONE; 00270 #endif 00271 00272 char register_name[VL53L1_MAX_STRING_LENGTH]; 00273 00274 /* look up register name */ 00275 #ifdef PAL_EXTENDED 00276 VL53L1_get_register_name( 00277 index, 00278 register_name); 00279 #else 00280 VL53L1_COPYSTRING(register_name, ""); 00281 #endif 00282 00283 /* Output to I2C logger for FMT/DFT */ 00284 00285 /*trace_i2c("WaitValueMaskEx(%5d, 0x%04X, 0x%02X, 0x%02X, %5d);\n", 00286 timeout_ms, index, value, mask, poll_delay_ms); */ 00287 trace_i2c("WaitValueMaskEx(%5d, %s, 0x%02X, 0x%02X, %5d);\n", 00288 timeout_ms, register_name, value, mask, poll_delay_ms); 00289 00290 /* calculate time limit in absolute time */ 00291 00292 VL53L1_GetTickCount(&start_time_ms); 00293 00294 00295 wait_ms(10); 00296 00297 /* remember current trace functions and temporarily disable 00298 * function logging 00299 */ 00300 00301 #ifdef VL53L1_LOG_ENABLE 00302 trace_functions = VL53L1_get_trace_functions(); 00303 VL53L1_set_trace_functions(VL53L1_TRACE_FUNCTION_NONE); 00304 #endif 00305 00306 /* wait until value is found, timeout reached on error occurred */ 00307 while ((status == VL53L1_ERROR_NONE) && 00308 (polling_time_ms < timeout_ms) && 00309 (found == 0)) { 00310 00311 if (status == VL53L1_ERROR_NONE) 00312 status = VL53L1_RdByte( 00313 pdev, 00314 index, 00315 &byte_value); 00316 00317 if ((byte_value & mask) == value) 00318 found = 1; 00319 if (status) 00320 { 00321 printf("VL53L1_WaitValueMaskEx 1 %d %d\n",pdev->i2c_slave_address,status); 00322 } 00323 00324 if (status == VL53L1_ERROR_NONE && 00325 found == 0 && 00326 poll_delay_ms > 0) 00327 status = VL53L1_WaitMs( 00328 pdev, 00329 poll_delay_ms); 00330 /* Update polling time (Compare difference rather than absolute to 00331 negate 32bit wrap around issue) */ 00332 if (status) 00333 { 00334 printf("VL53L1_WaitValueMaskEx 2 %d\n",status); 00335 } 00336 VL53L1_GetTickCount(¤t_time_ms); 00337 polling_time_ms = current_time_ms - start_time_ms; 00338 00339 } 00340 // printf("polling_time_ms %d \n",polling_time_ms); 00341 #ifdef VL53L1_LOG_ENABLE 00342 /* Restore function logging */ 00343 VL53L1_set_trace_functions(trace_functions); 00344 #endif 00345 00346 if (found == 0 && status == VL53L1_ERROR_NONE) 00347 status = VL53L1_ERROR_TIME_OUT; 00348 00349 return status; 00350 } 00351 00352 00353 00354
Generated on Tue Jul 12 2022 20:07:15 by 1.7.2