ST Expansion SW Team / Vl6180

Dependencies:   VL6180_Board

Dependents:   X_NUCLEO_6180

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vl6180_class.cpp Source File

vl6180_class.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    vl6180_class.cpp
00004  * @author  JS
00005  * @version V0.0.1
00006  * @date    15-January-2019
00007  * @brief   Implementation file for the VL53L1 sensor component driver class
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */
00037 
00038 /* Includes */
00039 #include <stdlib.h>
00040 #include "vl6180_class.h"
00041 #include "vl6180_i2c_fn.h"
00042 #include "vl6180_def.h"
00043 #include "vl6180_api.h"
00044 
00045 
00046 #define ALGO__PART_TO_PART_RANGE_OFFSET_MM  0x001E
00047 #define MM_CONFIG__INNER_OFFSET_MM          0x0020
00048 #define MM_CONFIG__OUTER_OFFSET_MM          0x0022
00049 
00050 //#include "vl53l1x_configuration.h"
00051 
00052 
00053 #ifndef MIN
00054 #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
00055 #endif
00056 #ifndef MAX
00057 #define MAX(v1, v2) ((v1) < (v2) ? (v2) : (v1))
00058 #endif
00059 
00060 #define DMAX_REFLECTANCE_IDX 2
00061 
00062 #define LOWPOWER_AUTO_VHV_LOOP_DURATION_US 245
00063 #define LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING 1448
00064 #define LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING 2100
00065 
00066 #define FDA_MAX_TIMING_BUDGET_US 550000
00067 
00068 #define VL53L1DevStructGetLLDriverHandle(Dev) (&Dev->Data.LLData)
00069 
00070 
00071 VL6180_ERROR VL6180::vl6180_GetSWVersion(VL6180_Version_t *pVersion)
00072 {
00073     VL6180_ERROR Status = 0;
00074 
00075     pVersion->major  = VL53L1X_IMPLEMENTATION_VER_MAJOR;
00076     pVersion->minor  = VL53L1X_IMPLEMENTATION_VER_MINOR;
00077     pVersion->build  = VL53L1X_IMPLEMENTATION_VER_SUB;
00078     pVersion->revision  = VL53L1X_IMPLEMENTATION_VER_REVISION;
00079     return Status;
00080 }
00081 
00082 VL6180_ERROR VL6180::vl6180_SetI2CAddress(uint8_t new_address)
00083 {
00084     VL6180_ERROR status = 0;
00085 
00086    // Device.i2c_addr = new_address;  
00087     
00088     if ( Device.i2c_addr  != new_address)
00089     {
00090 
00091         status = VL6180_SetI2CAddress (&Device,new_address);
00092         printf("VL53L1X_SetI2CAddress %d to %d \n", Device.i2c_addr ,new_address);
00093         Device.i2c_addr  = new_address; 
00094 
00095     }
00096             printf("Sensor right VL53L1X_SetI2CAddress end\n\r");
00097     return status;
00098 }
00099 
00100 int VL6180::init_sensor(uint8_t new_addr)
00101 {
00102     Device.i2c_addr  = new_addr;
00103     int status = 0;
00104     VL53L1_Off();
00105     VL53L1_On();
00106 
00107     status = is_present();
00108     if (!status) {
00109         printf("Failed to init VL53L0X sensor!\n\r");
00110         return status;
00111     }
00112     printf("init_sensor %d \n",status);
00113     return status;
00114 }
00115 
00116 
00117 VL6180_ERROR VL6180::vl6180_ClearInterrupt()
00118 {
00119     VL6180_ERROR status = 0;
00120 
00121     //status = VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01);
00122     status = VL6180_ClearInterrupt(&Device, INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING);
00123     printf("VL6180::VL53L1X_ClearInterrupt()\n");
00124     return status;
00125 }
00126 
00127 
00128 VL6180_ERROR VL6180::vl6180_WaitDeviceBooted(VL6180Dev_t dev)
00129 {
00130     VL6180_ERROR status = 0;
00131     status = VL6180_WaitDeviceBooted(dev);
00132 
00133     return status;
00134 }
00135 
00136 VL6180_ERROR VL6180::vl6180_InitData(VL6180Dev_t dev)
00137 {
00138     VL6180_ERROR status = 0;
00139     status = VL6180_InitData(dev);
00140 
00141     return status;
00142 }
00143 
00144 
00145 VL6180_ERROR VL6180::vl6180_DMaxSetState(VL6180Dev_t dev, int state)
00146 {
00147     VL6180_ERROR status = 0;
00148     status = VL6180_DMaxSetState(dev,state);
00149 
00150     return status;
00151 }
00152 
00153 VL6180_ERROR VL6180::vl6180_UpscaleGetScaling(VL6180Dev_t dev)
00154 {
00155     VL6180_ERROR status = 0;
00156     status = VL6180_UpscaleGetScaling(dev);
00157 
00158     return status;
00159 }
00160 
00161 VL6180_ERROR VL6180::vl6180_FilterGetState(VL6180Dev_t dev)
00162 {
00163     VL6180_ERROR status = 0;
00164     status = VL6180_FilterGetState(dev);
00165 
00166     return status;
00167 }
00168 
00169 
00170 
00171 // required?
00172 VL6180_ERROR VL6180::vl6180_SetInterruptPolarity(uint8_t NewPolarity)
00173 {
00174   //  uint8_t Temp;
00175     VL6180_ERROR status = 0;
00176 
00177  //   status = VL53L1_RdByte(Device, GPIO_HV_MUX__CTRL, &Temp);
00178 //    Temp = Temp & 0xEF;
00179 //    status = VL53L1_WrByte(Device, GPIO_HV_MUX__CTRL, Temp | (!(NewPolarity & 1)) << 4);
00180 
00181     return status;
00182 }
00183 
00184 VL6180_ERROR VL6180::vl6180_Prepare(VL6180Dev_t dev)
00185 {
00186     VL6180_ERROR status = 0;
00187     status = VL6180_Prepare(dev);
00188 
00189     return status;
00190 }
00191 
00192 VL6180_ERROR VL6180::vl6180_RangePollMeasurement(VL6180Dev_t dev, VL6180_RangeData_t *pRangeData)
00193 {
00194     VL6180_ERROR status = 0;
00195     status = VL6180_RangePollMeasurement(dev,pRangeData);
00196 
00197     return status;
00198 }
00199 
00200 /*
00201 VL6180_ERROR VL6180::VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData)
00202 {
00203     VL6180_ERROR status = 0;
00204     status = VL6180x_AlsPollMeasurement(dev,pAlsData);
00205 
00206     return status;
00207 }
00208 */
00209 
00210 
00211 VL6180_ERROR VL6180::vl6180_GetInterruptPolarity(uint8_t *pInterruptPolarity)
00212 {
00213     uint8_t Temp;
00214     VL6180_ERROR status = 0;
00215 
00216     status = VL6180_RdByte(&Device, GPIO_HV_MUX__CTRL, &Temp);
00217     Temp = Temp & 0x10;
00218     *pInterruptPolarity = !(Temp>>4);
00219     return status;
00220 }
00221 
00222 VL6180_ERROR VL6180::VL6180_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
00223 {
00224     int ret;
00225     ret = dev_i2c->vl6180_i2c_write(pBuffer, DeviceAddr, RegisterAddr, NumByteToWrite);
00226     if (ret) {
00227         return -1;
00228     }
00229     return 0;
00230 
00231 }
00232 
00233 VL6180_ERROR VL6180::VL6180_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
00234 {
00235     int ret;
00236 
00237     ret = dev_i2c->vl6180_i2c_read(pBuffer, DeviceAddr, RegisterAddr, NumByteToRead);
00238     //ret = dev_i2c->i2c_read(pBuffer, DeviceAddr, RegisterAddr, NumByteToRead);
00239     if (ret) {
00240         return -1;
00241     }
00242     return 0;
00243 }
00244 
00245 VL6180_ERROR VL6180::vl6180_RangeStartContinuousMode(VL6180Dev_t dev)
00246 
00247 {
00248     VL6180_ERROR status = 0;
00249     status = VL6180_RangeStartContinuousMode(dev);
00250 
00251     return status;
00252 }
00253 
00254 
00255 VL6180_ERROR VL6180::vl6180_RangeStartSingleShot(VL6180Dev_t dev)
00256 {
00257     VL6180_ERROR status = 0;
00258     status = VL6180_RangeStartSingleShot(dev);
00259 
00260     return status;
00261 }
00262 
00263 
00264 VL6180_ERROR VL6180::vl6180_RangeSetInterMeasPeriod(VL6180Dev_t dev, uint32_t  InterMeasTime_msec)
00265 {
00266     VL6180_ERROR status = 0;
00267     status = VL6180_RangeSetInterMeasPeriod(dev,InterMeasTime_msec);
00268 
00269     return status;
00270 }
00271 
00272 VL6180_ERROR VL6180::vl6180_RangeGetMeasurement(VL6180Dev_t dev, VL6180_RangeData_t *pRangeData)
00273 {
00274     VL6180_ERROR status = 0;
00275     status = VL6180_RangeGetMeasurement(dev,pRangeData);
00276 
00277     return status;
00278 }
00279 
00280 
00281 VL6180_ERROR VL6180::vl6180_RangeGetMeasurementIfReady(VL6180Dev_t dev, VL6180_RangeData_t *pRangeData)
00282 {
00283     VL6180_ERROR status = 0;
00284     status = VL6180_RangeGetMeasurementIfReady(dev,pRangeData);
00285 
00286     return status;
00287 }
00288 
00289 VL6180_ERROR VL6180::vl6180_SetupGPIO1(VL6180Dev_t dev, uint8_t IntFunction, int ActiveHigh)
00290 {
00291     VL6180_ERROR status = 0;
00292     status = VL6180_SetupGPIO1(dev,IntFunction,ActiveHigh);
00293 
00294     return status;
00295 }
00296 
00297 
00298 VL6180_ERROR VL6180::vl6180_RangeConfigInterrupt(VL6180Dev_t dev, uint8_t ConfigGpioInt)
00299 {
00300     VL6180_ERROR status = 0;
00301     status = VL6180_RangeConfigInterrupt(dev,ConfigGpioInt);
00302 
00303     return status;
00304 }
00305 
00306 VL6180_ERROR VL6180::vl6180_ClearInterrupt(VL6180Dev_t dev, uint8_t IntClear)
00307 {
00308     VL6180_ERROR status = 0;
00309     status = VL6180_ClearInterrupt(dev,IntClear);
00310 
00311     return status;
00312 }
00313 
00314 
00315 VL6180_ERROR VL6180::vl6180_RangeSetThresholds(VL6180Dev_t dev, uint16_t low, uint16_t high, int UseSafeParamHold)
00316 {
00317     VL6180_ERROR status = 0;
00318     status = VL6180_RangeSetThresholds(dev,low,high,UseSafeParamHold);
00319 
00320     return status;
00321 }
00322 
00323 VL6180_ERROR VL6180::vl6180_FilterSetState(VL6180Dev_t dev, int state)
00324 {
00325     VL6180_ERROR status = 0;
00326     status = VL6180_FilterSetState(dev,state);
00327 
00328     return status;
00329 }
00330 
00331 VL6180_ERROR VL6180::vl6180_UpscaleSetScaling(VL6180Dev_t dev, uint8_t scaling)
00332 {
00333     VL6180_ERROR status = 0;
00334     status = VL6180_UpscaleSetScaling(dev,scaling);
00335 
00336     return status;
00337 }