Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2
vl53l1_platform.c
00001 00002 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause 00003 /****************************************************************************** 00004 * Copyright (c) 2020, STMicroelectronics - All Rights Reserved 00005 00006 This file is part of VL53L1 and is dual licensed, 00007 either GPL-2.0+ 00008 or 'BSD 3-clause "New" or "Revised" License' , at your option. 00009 ****************************************************************************** 00010 */ 00011 00012 unsigned int i2creadCount = 0; 00013 unsigned int i2cwriteCount = 0; 00014 unsigned char SPI2C_Buffer[256]; 00015 00016 00017 #include <mbed_wait_api.h> 00018 #include "vl53l1_platform.h" 00019 #include "vl53l1_platform_log.h" 00020 #include "vl53l1_api.h" 00021 #include "spi_interface.h" 00022 #include <string.h> 00023 #include <time.h> 00024 #include <math.h> 00025 00026 00027 00028 #define I2C_TIME_OUT_BASE 10 00029 #define I2C_TIME_OUT_BYTE 1 00030 00031 #ifdef VL53L1_LOG_ENABLE 00032 #define trace_print(level, ...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_PLATFORM, level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__) 00033 #define trace_i2c(...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_NONE, VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__) 00034 #endif 00035 00036 /* when not customized by application define dummy one */ 00037 #ifndef VL53L1_GetI2cBus 00038 /** This macro can be overloaded by user to enforce i2c sharing in RTOS context 00039 */ 00040 # define VL53L1_GetI2cBus(...) (void)0 00041 #endif 00042 00043 #ifndef VL53L1_PutI2cBus 00044 /** This macro can be overloaded by user to enforce i2c sharing in RTOS context 00045 */ 00046 # define VL53L1_PutI2cBus(...) (void)0 00047 #endif 00048 00049 uint8_t _I2CBuffer[256]; 00050 00051 00052 VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) 00053 { 00054 int status; 00055 // printf("VL53L1_WriteMulti %d %d %d \n",Dev->I2cDevAddr,index,count); 00056 status = v53l1cb_i2c_write_if(Dev->dev_i2c, pdata,Dev->i2c_slave_address, index,count); 00057 return status; 00058 } 00059 00060 00061 VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) 00062 { 00063 int status; 00064 00065 status = v53l1cb_i2c_read_if(Dev->dev_i2c, pdata,Dev->i2c_slave_address, index,count); 00066 00067 return status; 00068 } 00069 00070 00071 VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) 00072 { 00073 int status; 00074 00075 status = v53l1cb_i2c_write_if(Dev->dev_i2c, &data,Dev->i2c_slave_address, index,1); 00076 return status; 00077 } 00078 00079 00080 VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) 00081 { 00082 int status; 00083 uint8_t buffer[2]; 00084 00085 buffer[0] = data >> 8; 00086 buffer[1] = data & 0x00FF; 00087 status = v53l1cb_i2c_write_if(Dev->dev_i2c, (uint8_t *)buffer,Dev->i2c_slave_address, index,2); 00088 return status; 00089 } 00090 00091 00092 VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) 00093 { 00094 int status; 00095 uint8_t buffer[4]; 00096 00097 buffer[0] = (data >> 24) & 0xFF; 00098 buffer[1] = (data >> 16) & 0xFF; 00099 buffer[2] = (data >> 8) & 0xFF; 00100 buffer[3] = (data >> 0) & 0xFF; 00101 status = v53l1cb_i2c_write_if(Dev->dev_i2c, (uint8_t *)buffer,Dev->i2c_slave_address, index,4); 00102 return status; 00103 } 00104 00105 VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData) 00106 { 00107 int status; 00108 uint8_t buffer = 0; 00109 00110 /* read data direct onto buffer */ 00111 status = v53l1cb_i2c_read_if(Dev->dev_i2c, &buffer,Dev->i2c_slave_address, index,1); 00112 if (!status) 00113 { 00114 buffer = (buffer & AndData) | OrData; 00115 status = v53l1cb_i2c_write_if(Dev->dev_i2c, &buffer,Dev->i2c_slave_address, index,1); 00116 } 00117 return status; 00118 } 00119 00120 VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) 00121 { 00122 int status; 00123 00124 status = v53l1cb_i2c_read_if(Dev->dev_i2c, data,Dev->i2c_slave_address, index,1); //is this correct 00125 // printf("VL53L1_RdByte %d %d %d\n",Dev->i2c_slave_address, status,*data); 00126 if(status) 00127 return -1; 00128 00129 return 0; 00130 } 00131 00132 00133 VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) 00134 { 00135 int status; 00136 uint8_t buffer[2] = {0,0}; 00137 00138 status = v53l1cb_i2c_read_if(Dev->dev_i2c, buffer,Dev->i2c_slave_address, index,2); //is this correct 00139 if (!status) 00140 { 00141 *data = (buffer[0] << 8) + buffer[1]; 00142 } 00143 return status; 00144 00145 } 00146 00147 00148 00149 VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) 00150 { 00151 int status; 00152 uint8_t buffer[4] = {0,0,0,0}; 00153 00154 status = v53l1cb_i2c_read_if(Dev->dev_i2c, buffer,Dev->i2c_slave_address, index,4); 00155 if(!status) 00156 { 00157 *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; 00158 } 00159 return status; 00160 00161 } 00162 00163 00164 VL53L1_Error VL53L1_GetTickCount( 00165 VL53L1_DEV Dev, 00166 uint32_t *ptick_count_ms) 00167 { 00168 00169 /* Returns current tick count in [ms] */ 00170 00171 VL53L1_Error status = VL53L1_ERROR_NONE; 00172 (void) Dev; 00173 00174 GetTickCount( ptick_count_ms); 00175 00176 #ifdef VL53L1_LOG_ENABLE 00177 trace_print( 00178 VL53L1_TRACE_LEVEL_DEBUG, 00179 "VL53L1_GetTickCount() = %5u ms;\n", 00180 *ptick_count_ms); 00181 #endif 00182 00183 return status; 00184 } 00185 00186 00187 #define trace_print(level, ...) \ 00188 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_PLATFORM, \ 00189 level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__) 00190 00191 #define trace_i2c(...) \ 00192 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_NONE, \ 00193 VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__) 00194 00195 00196 VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz) 00197 { 00198 *ptimer_freq_hz = 0; 00199 00200 trace_print(VL53L1_TRACE_LEVEL_INFO, "VL53L1_GetTimerFrequency: Freq : %dHz\n", *ptimer_freq_hz); 00201 return VL53L1_ERROR_NONE; 00202 } 00203 00204 00205 VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_time){ 00206 (void)pdev; 00207 wait_us(wait_time * 1000); 00208 return VL53L1_ERROR_NONE; 00209 } 00210 00211 00212 VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_time){ 00213 (void)pdev; 00214 wait_us(wait_time); 00215 return VL53L1_ERROR_NONE; 00216 } 00217 00218 VL53L1_Error VL53L1_WaitValueMaskEx( 00219 VL53L1_Dev_t *pdev, 00220 uint32_t timeout_ms, 00221 uint16_t index, 00222 uint8_t value, 00223 uint8_t mask, 00224 uint32_t poll_delay_ms) 00225 { 00226 00227 /* 00228 * Platform implementation of WaitValueMaskEx V2WReg script command 00229 * 00230 * WaitValueMaskEx( 00231 * duration_ms, 00232 * index, 00233 * value, 00234 * mask, 00235 * poll_delay_ms); 00236 */ 00237 00238 VL53L1_Error status = VL53L1_ERROR_NONE; 00239 uint32_t start_time_ms = 0; 00240 uint32_t current_time_ms = 0; 00241 uint32_t polling_time_ms = 0; 00242 uint8_t byte_value = 0; 00243 uint8_t found = 0; 00244 #ifdef VL53L1_LOG_ENABLE 00245 uint8_t trace_functions = VL53L1_TRACE_FUNCTION_NONE; 00246 #endif 00247 00248 char register_name[VL53L1_MAX_STRING_LENGTH]; 00249 00250 /* look up register name */ 00251 #ifdef PAL_EXTENDED 00252 VL53L1_get_register_name( 00253 index, 00254 register_name); 00255 #else 00256 VL53L1_COPYSTRING(register_name, ""); 00257 #endif 00258 00259 /* Output to I2C logger for FMT/DFT */ 00260 00261 /*trace_i2c("WaitValueMaskEx(%5d, 0x%04X, 0x%02X, 0x%02X, %5d);\n", 00262 timeout_ms, index, value, mask, poll_delay_ms); */ 00263 trace_i2c("WaitValueMaskEx(%5d, %s, 0x%02X, 0x%02X, %5d);\n", 00264 timeout_ms, register_name, value, mask, poll_delay_ms); 00265 00266 /* calculate time limit in absolute time */ 00267 00268 VL53L1_GetTickCount(pdev, &start_time_ms); 00269 00270 00271 VL53L1_WaitMs(pdev, 10); 00272 00273 /* remember current trace functions and temporarily disable 00274 * function logging 00275 */ 00276 00277 #ifdef VL53L1_LOG_ENABLE 00278 trace_functions = VL53L1_get_trace_functions(); 00279 VL53L1_set_trace_functions(VL53L1_TRACE_FUNCTION_NONE); 00280 #endif 00281 00282 /* wait until value is found, timeout reached on error occurred */ 00283 while ((status == VL53L1_ERROR_NONE) && 00284 (polling_time_ms < timeout_ms) && 00285 (found == 0)) { 00286 00287 if (status == VL53L1_ERROR_NONE) 00288 status = VL53L1_RdByte( 00289 pdev, 00290 index, 00291 &byte_value); 00292 00293 if ((byte_value & mask) == value) 00294 found = 1; 00295 00296 if (status == VL53L1_ERROR_NONE && 00297 found == 0 && 00298 poll_delay_ms > 0) 00299 status = VL53L1_WaitMs( 00300 pdev, 00301 poll_delay_ms); 00302 /* Update polling time (Compare difference rather than absolute to 00303 negate 32bit wrap around issue) */ 00304 VL53L1_GetTickCount(pdev, ¤t_time_ms); 00305 polling_time_ms = current_time_ms - start_time_ms; 00306 00307 } 00308 // printf("polling_time_ms %d \n",polling_time_ms); 00309 #ifdef VL53L1_LOG_ENABLE 00310 /* Restore function logging */ 00311 VL53L1_set_trace_functions(trace_functions); 00312 #endif 00313 00314 if (found == 0 && status == VL53L1_ERROR_NONE) 00315 status = VL53L1_ERROR_TIME_OUT; 00316 00317 return status; 00318 } 00319 00320 00321 00322
Generated on Thu Jul 14 2022 10:14:48 by
1.7.2