hh

Dependencies:   VL6180x X_NUCLEO_COMMON X_NUCLEO_IHM01A1 mbed

Fork of 1-DoorCloser_jor by Jordan Ml

Committer:
julientiron
Date:
Fri Mar 25 21:27:03 2016 +0000
Revision:
0:1cb50d31c3b5
Code petit robot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julientiron 0:1cb50d31c3b5 1 /**
julientiron 0:1cb50d31c3b5 2 ******************************************************************************
julientiron 0:1cb50d31c3b5 3 * @file vl6180x_class.cpp
julientiron 0:1cb50d31c3b5 4 * @author AST / EST
julientiron 0:1cb50d31c3b5 5 * @version V0.0.1
julientiron 0:1cb50d31c3b5 6 * @date 14-April-2015
julientiron 0:1cb50d31c3b5 7 * @brief Implementation file for the HTS221 driver class
julientiron 0:1cb50d31c3b5 8 ******************************************************************************
julientiron 0:1cb50d31c3b5 9 * @attention
julientiron 0:1cb50d31c3b5 10 *
julientiron 0:1cb50d31c3b5 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
julientiron 0:1cb50d31c3b5 12 *
julientiron 0:1cb50d31c3b5 13 * Redistribution and use in source and binary forms, with or without modification,
julientiron 0:1cb50d31c3b5 14 * are permitted provided that the following conditions are met:
julientiron 0:1cb50d31c3b5 15 * 1. Redistributions of source code must retain the above copyright notice,
julientiron 0:1cb50d31c3b5 16 * this list of conditions and the following disclaimer.
julientiron 0:1cb50d31c3b5 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
julientiron 0:1cb50d31c3b5 18 * this list of conditions and the following disclaimer in the documentation
julientiron 0:1cb50d31c3b5 19 * and/or other materials provided with the distribution.
julientiron 0:1cb50d31c3b5 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
julientiron 0:1cb50d31c3b5 21 * may be used to endorse or promote products derived from this software
julientiron 0:1cb50d31c3b5 22 * without specific prior written permission.
julientiron 0:1cb50d31c3b5 23 *
julientiron 0:1cb50d31c3b5 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
julientiron 0:1cb50d31c3b5 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
julientiron 0:1cb50d31c3b5 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
julientiron 0:1cb50d31c3b5 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
julientiron 0:1cb50d31c3b5 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
julientiron 0:1cb50d31c3b5 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
julientiron 0:1cb50d31c3b5 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
julientiron 0:1cb50d31c3b5 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
julientiron 0:1cb50d31c3b5 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
julientiron 0:1cb50d31c3b5 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
julientiron 0:1cb50d31c3b5 34 *
julientiron 0:1cb50d31c3b5 35 ******************************************************************************
julientiron 0:1cb50d31c3b5 36 */
julientiron 0:1cb50d31c3b5 37
julientiron 0:1cb50d31c3b5 38 /* Includes */
julientiron 0:1cb50d31c3b5 39 #include "vl6180x_class.h"
julientiron 0:1cb50d31c3b5 40
julientiron 0:1cb50d31c3b5 41
julientiron 0:1cb50d31c3b5 42 #ifndef VL6180x_RANGE_STATUS_ERRSTRING
julientiron 0:1cb50d31c3b5 43 #warning "VL6180x_RANGE_STATUS_ERRSTRING not defined ?"
julientiron 0:1cb50d31c3b5 44 /* TODO you may remove or comment these #warning and keep the default below to keep compatibility
julientiron 0:1cb50d31c3b5 45 or update your vl6180x_platform.h file */
julientiron 0:1cb50d31c3b5 46 /**
julientiron 0:1cb50d31c3b5 47 * force VL6180x_RANGE_STATUS_ERRSTRING to not supported when not part of any cfg file
julientiron 0:1cb50d31c3b5 48 */
julientiron 0:1cb50d31c3b5 49 #define VL6180x_RANGE_STATUS_ERRSTRING 0
julientiron 0:1cb50d31c3b5 50 #endif
julientiron 0:1cb50d31c3b5 51
julientiron 0:1cb50d31c3b5 52 #ifndef VL6180X_SAFE_POLLING_ENTER
julientiron 0:1cb50d31c3b5 53 #warning "VL6180X_SAFE_POLLING_ENTER not defined, likely old vl6180x_cfg.h file ?"
julientiron 0:1cb50d31c3b5 54 /* TODO you may remove or comment these #warning and keep the default below to keep compatibility
julientiron 0:1cb50d31c3b5 55 or update your vl6180x_platform.h file */
julientiron 0:1cb50d31c3b5 56 /**
julientiron 0:1cb50d31c3b5 57 * force VL6180X_SAFE_POLLING_ENTER to off when not in cfg file
julientiron 0:1cb50d31c3b5 58 */
julientiron 0:1cb50d31c3b5 59 #define VL6180X_SAFE_POLLING_ENTER 0 /* off by default as in api 2.0 */
julientiron 0:1cb50d31c3b5 60 #endif
julientiron 0:1cb50d31c3b5 61
julientiron 0:1cb50d31c3b5 62 #ifndef VL6180X_LOG_ENABLE
julientiron 0:1cb50d31c3b5 63 /**
julientiron 0:1cb50d31c3b5 64 * Force VL6180X_LOG_ENABLE to none as default
julientiron 0:1cb50d31c3b5 65 */
julientiron 0:1cb50d31c3b5 66 #define VL6180X_LOG_ENABLE 0
julientiron 0:1cb50d31c3b5 67 #endif
julientiron 0:1cb50d31c3b5 68
julientiron 0:1cb50d31c3b5 69 #ifdef MY_LOG
julientiron 0:1cb50d31c3b5 70 Serial pc(USBTX, USBRX);
julientiron 0:1cb50d31c3b5 71 #endif
julientiron 0:1cb50d31c3b5 72
julientiron 0:1cb50d31c3b5 73 #if VL6180x_RANGE_STATUS_ERRSTRING
julientiron 0:1cb50d31c3b5 74 /**@def VL6180x_HAVE_RANGE_STATUS_ERRSTRING
julientiron 0:1cb50d31c3b5 75 * @brief is defined when @a #VL6180x_RANGE_STATUS_ERRSTRING is enable
julientiron 0:1cb50d31c3b5 76 */
julientiron 0:1cb50d31c3b5 77 #define VL6180x_HAVE_RANGE_STATUS_ERRSTRING
julientiron 0:1cb50d31c3b5 78 #endif
julientiron 0:1cb50d31c3b5 79
julientiron 0:1cb50d31c3b5 80
julientiron 0:1cb50d31c3b5 81 /** @brief Get API version as "hex integer" 0xMMnnss
julientiron 0:1cb50d31c3b5 82 */
julientiron 0:1cb50d31c3b5 83 #define VL6180x_ApiRevInt ((VL6180x_API_REV_MAJOR<<24)+(VL6180x_API_REV_MINOR<<16)+VL6180x_API_REV_SUB)
julientiron 0:1cb50d31c3b5 84
julientiron 0:1cb50d31c3b5 85 /** Get API version as string for exe "2.1.12" "
julientiron 0:1cb50d31c3b5 86 */
julientiron 0:1cb50d31c3b5 87 #define VL6180x_ApiRevStr VL6180X_STR(VL6180x_API_REV_MAJOR) "." VL6180X_STR(VL6180x_API_REV_MINOR) "." VL6180X_STR(VL6180x_API_REV_SUB)
julientiron 0:1cb50d31c3b5 88
julientiron 0:1cb50d31c3b5 89 /** @defgroup api_init Init functions
julientiron 0:1cb50d31c3b5 90 * @brief API init functions
julientiron 0:1cb50d31c3b5 91 * @ingroup api_hl
julientiron 0:1cb50d31c3b5 92 * @{
julientiron 0:1cb50d31c3b5 93 */
julientiron 0:1cb50d31c3b5 94
julientiron 0:1cb50d31c3b5 95
julientiron 0:1cb50d31c3b5 96 /****************** define for i2c configuration *******************************/
julientiron 0:1cb50d31c3b5 97
julientiron 0:1cb50d31c3b5 98 #define TEMP_BUF_SIZE 32
julientiron 0:1cb50d31c3b5 99
julientiron 0:1cb50d31c3b5 100 #define IsValidGPIOFunction(x) ((x)==GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT || (x)==GPIOx_SELECT_OFF)
julientiron 0:1cb50d31c3b5 101 /**
julientiron 0:1cb50d31c3b5 102 * @brief Clear All interrupt causes (als+range+error)
julientiron 0:1cb50d31c3b5 103 *
julientiron 0:1cb50d31c3b5 104 * @param dev The device
julientiron 0:1cb50d31c3b5 105 * @return 0 On success
julientiron 0:1cb50d31c3b5 106 */
julientiron 0:1cb50d31c3b5 107 #define VL6180x_ClearAllInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING|INTERRUPT_CLEAR_ALS)
julientiron 0:1cb50d31c3b5 108 /**
julientiron 0:1cb50d31c3b5 109 * @brief Clear ALS interrupt
julientiron 0:1cb50d31c3b5 110 *
julientiron 0:1cb50d31c3b5 111 * @param dev The device
julientiron 0:1cb50d31c3b5 112 * @return 0 On success
julientiron 0:1cb50d31c3b5 113 */
julientiron 0:1cb50d31c3b5 114 #define VL6180x_AlsClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ALS)
julientiron 0:1cb50d31c3b5 115 /**
julientiron 0:1cb50d31c3b5 116 * @brief Clear range interrupt
julientiron 0:1cb50d31c3b5 117 *
julientiron 0:1cb50d31c3b5 118 * @param dev The device
julientiron 0:1cb50d31c3b5 119 * @return 0 On success
julientiron 0:1cb50d31c3b5 120 */
julientiron 0:1cb50d31c3b5 121 #define VL6180x_RangeClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_RANGING)
julientiron 0:1cb50d31c3b5 122
julientiron 0:1cb50d31c3b5 123 /******************************************************************************/
julientiron 0:1cb50d31c3b5 124 /******************************* file api.c ***********************************/
julientiron 0:1cb50d31c3b5 125
julientiron 0:1cb50d31c3b5 126 #define VL6180x_9to7Conv(x) (x)
julientiron 0:1cb50d31c3b5 127
julientiron 0:1cb50d31c3b5 128 /* TODO when set all "cached" value with "default init" are updated after init from register read back */
julientiron 0:1cb50d31c3b5 129 #define REFRESH_CACHED_DATA_AFTER_INIT 1
julientiron 0:1cb50d31c3b5 130
julientiron 0:1cb50d31c3b5 131
julientiron 0:1cb50d31c3b5 132 #define IsValidGPIOFunction(x) ((x)==GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT || (x)==GPIOx_SELECT_OFF)
julientiron 0:1cb50d31c3b5 133
julientiron 0:1cb50d31c3b5 134
julientiron 0:1cb50d31c3b5 135 /** default value ECE factor Molecular */
julientiron 0:1cb50d31c3b5 136 #define DEF_ECE_FACTOR_M 85
julientiron 0:1cb50d31c3b5 137 /** default value ECE factor Denominator */
julientiron 0:1cb50d31c3b5 138 #define DEF_ECE_FACTOR_D 100
julientiron 0:1cb50d31c3b5 139 /** default value ALS integration time */
julientiron 0:1cb50d31c3b5 140 #define DEF_INT_PEFRIOD 100
julientiron 0:1cb50d31c3b5 141 /** default value ALS gain */
julientiron 0:1cb50d31c3b5 142 #define DEF_ALS_GAIN 1
julientiron 0:1cb50d31c3b5 143 /** default value ALS scaler */
julientiron 0:1cb50d31c3b5 144 #define DEF_ALS_SCALER 1
julientiron 0:1cb50d31c3b5 145 /** default value for DMAX Enbale */
julientiron 0:1cb50d31c3b5 146 #define DEF_DMAX_ENABLE 1
julientiron 0:1cb50d31c3b5 147 /** default ambient tuning factor %x1000 */
julientiron 0:1cb50d31c3b5 148 #define DEF_AMBIENT_TUNING 80
julientiron 0:1cb50d31c3b5 149
julientiron 0:1cb50d31c3b5 150 #if VL6180x_SINGLE_DEVICE_DRIVER
julientiron 0:1cb50d31c3b5 151 extern struct VL6180xDevData_t SingleVL6180xDevData;
julientiron 0:1cb50d31c3b5 152 #define VL6180xDevDataGet(dev, field) (SingleVL6180xDevData.field)
julientiron 0:1cb50d31c3b5 153 #define VL6180xDevDataSet(dev, field, data) SingleVL6180xDevData.field=(data)
julientiron 0:1cb50d31c3b5 154 #endif
julientiron 0:1cb50d31c3b5 155
julientiron 0:1cb50d31c3b5 156 #define LUXRES_FIX_PREC 8
julientiron 0:1cb50d31c3b5 157 #define GAIN_FIX_PREC 8 /* ! if not sme as LUX_PREC then :( adjust GetLux */
julientiron 0:1cb50d31c3b5 158 #define AN_GAIN_MULT (1<<GAIN_FIX_PREC)
julientiron 0:1cb50d31c3b5 159
julientiron 0:1cb50d31c3b5 160
julientiron 0:1cb50d31c3b5 161 //int32_t _GetAveTotalTime(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 162 //int VL6180x_RangeSetEarlyConvergenceEestimateThreshold(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 163
julientiron 0:1cb50d31c3b5 164 /**
julientiron 0:1cb50d31c3b5 165 * ScalerLookUP scaling factor-1 to register #RANGE_SCALER lookup
julientiron 0:1cb50d31c3b5 166 */
julientiron 0:1cb50d31c3b5 167 static const uint16_t ScalerLookUP[] ROMABLE_DATA ={ 253, 127, 84}; /* lookup table for scaling->scalar 1x2x 3x */
julientiron 0:1cb50d31c3b5 168 /**
julientiron 0:1cb50d31c3b5 169 * scaling factor to Upper limit look up
julientiron 0:1cb50d31c3b5 170 */
julientiron 0:1cb50d31c3b5 171 static const uint16_t UpperLimitLookUP[] ROMABLE_DATA ={ 185, 370, 580}; /* lookup table for scaling->limit 1x2x3x */
julientiron 0:1cb50d31c3b5 172 /**
julientiron 0:1cb50d31c3b5 173 * Als Code gain to fix point gain lookup
julientiron 0:1cb50d31c3b5 174 */
julientiron 0:1cb50d31c3b5 175 static const uint16_t AlsGainLookUp[8] ROMABLE_DATA = {
julientiron 0:1cb50d31c3b5 176 (uint16_t)(20.0f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 177 (uint16_t)(10.0f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 178 (uint16_t)(5.0f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 179 (uint16_t)(2.5f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 180 (uint16_t)(1.67f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 181 (uint16_t)(1.25f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 182 (uint16_t)(1.0f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 183 (uint16_t)(40.0f * AN_GAIN_MULT),
julientiron 0:1cb50d31c3b5 184 };
julientiron 0:1cb50d31c3b5 185
julientiron 0:1cb50d31c3b5 186
julientiron 0:1cb50d31c3b5 187 #if VL6180x_RANGE_STATUS_ERRSTRING
julientiron 0:1cb50d31c3b5 188 const char * ROMABLE_DATA VL6180x_RangeStatusErrString[]={
julientiron 0:1cb50d31c3b5 189 "No Error",
julientiron 0:1cb50d31c3b5 190 "VCSEL Continuity Test",
julientiron 0:1cb50d31c3b5 191 "VCSEL Watchdog Test",
julientiron 0:1cb50d31c3b5 192 "VCSEL Watchdog",
julientiron 0:1cb50d31c3b5 193 "PLL1 Lock",
julientiron 0:1cb50d31c3b5 194 "PLL2 Lock",
julientiron 0:1cb50d31c3b5 195 "Early Convergence Estimate",
julientiron 0:1cb50d31c3b5 196 "Max Convergence",
julientiron 0:1cb50d31c3b5 197 "No Target Ignore",
julientiron 0:1cb50d31c3b5 198 "Not used 9",
julientiron 0:1cb50d31c3b5 199 "Not used 10",
julientiron 0:1cb50d31c3b5 200 "Max Signal To Noise Ratio",
julientiron 0:1cb50d31c3b5 201 "Raw Ranging Algo Underflow",
julientiron 0:1cb50d31c3b5 202 "Raw Ranging Algo Overflow",
julientiron 0:1cb50d31c3b5 203 "Ranging Algo Underflow",
julientiron 0:1cb50d31c3b5 204 "Ranging Algo Overflow",
julientiron 0:1cb50d31c3b5 205
julientiron 0:1cb50d31c3b5 206 "Filtered by post processing"
julientiron 0:1cb50d31c3b5 207 };
julientiron 0:1cb50d31c3b5 208
julientiron 0:1cb50d31c3b5 209 const char * VL6180x_RangeGetStatusErrString(uint8_t RangeErrCode){
julientiron 0:1cb50d31c3b5 210 if( RangeErrCode > sizeof(VL6180x_RangeStatusErrString)/sizeof(VL6180x_RangeStatusErrString[0]) )
julientiron 0:1cb50d31c3b5 211 return NULL;
julientiron 0:1cb50d31c3b5 212 return VL6180x_RangeStatusErrString[RangeErrCode];
julientiron 0:1cb50d31c3b5 213 }
julientiron 0:1cb50d31c3b5 214 #endif
julientiron 0:1cb50d31c3b5 215
julientiron 0:1cb50d31c3b5 216 #if VL6180x_UPSCALE_SUPPORT == 1
julientiron 0:1cb50d31c3b5 217 #define _GetUpscale(dev, ... ) 1
julientiron 0:1cb50d31c3b5 218 #define _SetUpscale(...) -1
julientiron 0:1cb50d31c3b5 219 #define DEF_UPSCALE 1
julientiron 0:1cb50d31c3b5 220 #elif VL6180x_UPSCALE_SUPPORT == 2
julientiron 0:1cb50d31c3b5 221 #define _GetUpscale(dev, ... ) 2
julientiron 0:1cb50d31c3b5 222 #define _SetUpscale(...)
julientiron 0:1cb50d31c3b5 223 #define DEF_UPSCALE 2
julientiron 0:1cb50d31c3b5 224 #elif VL6180x_UPSCALE_SUPPORT == 3
julientiron 0:1cb50d31c3b5 225 #define _GetUpscale(dev, ... ) 3
julientiron 0:1cb50d31c3b5 226 #define _SetUpscale(...)
julientiron 0:1cb50d31c3b5 227 #define DEF_UPSCALE 3
julientiron 0:1cb50d31c3b5 228 #else
julientiron 0:1cb50d31c3b5 229 #define DEF_UPSCALE (-(VL6180x_UPSCALE_SUPPORT))
julientiron 0:1cb50d31c3b5 230 #define _GetUpscale(dev, ... ) VL6180xDevDataGet(dev, UpscaleFactor)
julientiron 0:1cb50d31c3b5 231 #define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
julientiron 0:1cb50d31c3b5 232 #endif
julientiron 0:1cb50d31c3b5 233
julientiron 0:1cb50d31c3b5 234
julientiron 0:1cb50d31c3b5 235 #if VL6180x_SINGLE_DEVICE_DRIVER
julientiron 0:1cb50d31c3b5 236 /**
julientiron 0:1cb50d31c3b5 237 * the unique driver data When single device driver is active
julientiron 0:1cb50d31c3b5 238 */
julientiron 0:1cb50d31c3b5 239 struct VL6180xDevData_t VL6180x_DEV_DATA_ATTR SingleVL6180xDevData={
julientiron 0:1cb50d31c3b5 240 .EceFactorM = DEF_ECE_FACTOR_M,
julientiron 0:1cb50d31c3b5 241 .EceFactorD = DEF_ECE_FACTOR_D,
julientiron 0:1cb50d31c3b5 242 #ifdef VL6180x_HAVE_UPSCALE_DATA
julientiron 0:1cb50d31c3b5 243 .UpscaleFactor = DEF_UPSCALE,
julientiron 0:1cb50d31c3b5 244 #endif
julientiron 0:1cb50d31c3b5 245 #ifdef VL6180x_HAVE_ALS_DATA
julientiron 0:1cb50d31c3b5 246 .IntegrationPeriod = DEF_INT_PEFRIOD,
julientiron 0:1cb50d31c3b5 247 .AlsGainCode = DEF_ALS_GAIN,
julientiron 0:1cb50d31c3b5 248 .AlsScaler = DEF_ALS_SCALER,
julientiron 0:1cb50d31c3b5 249 #endif
julientiron 0:1cb50d31c3b5 250 #ifdef VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 251 .DMaxEnable = DEF_DMAX_ENABLE,
julientiron 0:1cb50d31c3b5 252 #endif
julientiron 0:1cb50d31c3b5 253 };
julientiron 0:1cb50d31c3b5 254 #endif /* VL6180x_SINGLE_DEVICE_DRIVER */
julientiron 0:1cb50d31c3b5 255
julientiron 0:1cb50d31c3b5 256 #define Fix7_2_KCPs(x) ((((uint32_t)(x))*1000)>>7)
julientiron 0:1cb50d31c3b5 257
julientiron 0:1cb50d31c3b5 258 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 259 static int _filter_Init(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 260 #define _IsWrapArroundActive(dev) VL6180xDevDataGet(dev,WrapAroundFilterActive)
julientiron 0:1cb50d31c3b5 261 #else
julientiron 0:1cb50d31c3b5 262 #define _IsWrapArroundActive(dev) 0
julientiron 0:1cb50d31c3b5 263 #endif
julientiron 0:1cb50d31c3b5 264
julientiron 0:1cb50d31c3b5 265
julientiron 0:1cb50d31c3b5 266 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 267 void _DMax_OneTimeInit(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 268 // static int _DMax_InitData(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 269 static int _DMax_Compute(VL6180xDev_t dev, VL6180x_RangeData_t *pRange);
julientiron 0:1cb50d31c3b5 270 #define _IsDMaxActive(dev) VL6180xDevDataGet(dev,DMaxEnable)
julientiron 0:1cb50d31c3b5 271 #else
julientiron 0:1cb50d31c3b5 272 #define _DMax_InitData(...) 0 /* success */
julientiron 0:1cb50d31c3b5 273 #define _DMax_OneTimeInit(...) (void)0
julientiron 0:1cb50d31c3b5 274 #define _IsDMaxActive(...) 0
julientiron 0:1cb50d31c3b5 275 #endif
julientiron 0:1cb50d31c3b5 276
julientiron 0:1cb50d31c3b5 277 //static int VL6180x_RangeStaticInit(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 278 //static int VL6180x_UpscaleStaticInit(VL6180xDev_t dev);
julientiron 0:1cb50d31c3b5 279
julientiron 0:1cb50d31c3b5 280 int VL6180X::VL6180x_WaitDeviceBooted(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 281 uint8_t FreshOutReset;
julientiron 0:1cb50d31c3b5 282 int status;
julientiron 0:1cb50d31c3b5 283 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 284 do{
julientiron 0:1cb50d31c3b5 285 status = VL6180x_RdByte(dev,SYSTEM_FRESH_OUT_OF_RESET, &FreshOutReset);
julientiron 0:1cb50d31c3b5 286 }
julientiron 0:1cb50d31c3b5 287 while( FreshOutReset!=1 && status==0);
julientiron 0:1cb50d31c3b5 288 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 289 return status;
julientiron 0:1cb50d31c3b5 290 }
julientiron 0:1cb50d31c3b5 291
julientiron 0:1cb50d31c3b5 292 int VL6180X::VL6180x_InitData(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 293 int status, dmax_status ;
julientiron 0:1cb50d31c3b5 294 int8_t offset;
julientiron 0:1cb50d31c3b5 295 uint8_t FreshOutReset;
julientiron 0:1cb50d31c3b5 296 uint32_t CalValue;
julientiron 0:1cb50d31c3b5 297 uint16_t u16;
julientiron 0:1cb50d31c3b5 298 uint32_t XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 299
julientiron 0:1cb50d31c3b5 300 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 301
julientiron 0:1cb50d31c3b5 302 VL6180xDevDataSet(dev, EceFactorM , DEF_ECE_FACTOR_M);
julientiron 0:1cb50d31c3b5 303 VL6180xDevDataSet(dev, EceFactorD , DEF_ECE_FACTOR_D);
julientiron 0:1cb50d31c3b5 304
julientiron 0:1cb50d31c3b5 305 #ifdef VL6180x_HAVE_UPSCALE_DATA
julientiron 0:1cb50d31c3b5 306 VL6180xDevDataSet(dev, UpscaleFactor , DEF_UPSCALE);
julientiron 0:1cb50d31c3b5 307 #endif
julientiron 0:1cb50d31c3b5 308
julientiron 0:1cb50d31c3b5 309 #ifdef VL6180x_HAVE_ALS_DATA
julientiron 0:1cb50d31c3b5 310 VL6180xDevDataSet(dev, IntegrationPeriod, DEF_INT_PEFRIOD);
julientiron 0:1cb50d31c3b5 311 VL6180xDevDataSet(dev, AlsGainCode, DEF_ALS_GAIN);
julientiron 0:1cb50d31c3b5 312 VL6180xDevDataSet(dev, AlsScaler, DEF_ALS_SCALER);
julientiron 0:1cb50d31c3b5 313 #endif
julientiron 0:1cb50d31c3b5 314
julientiron 0:1cb50d31c3b5 315 #ifdef VL6180x_HAVE_WRAP_AROUND_DATA
julientiron 0:1cb50d31c3b5 316 VL6180xDevDataSet(dev, WrapAroundFilterActive, (VL6180x_WRAP_AROUND_FILTER_SUPPORT >0));
julientiron 0:1cb50d31c3b5 317 VL6180xDevDataSet(dev, DMaxEnable, DEF_DMAX_ENABLE);
julientiron 0:1cb50d31c3b5 318 #endif
julientiron 0:1cb50d31c3b5 319
julientiron 0:1cb50d31c3b5 320 _DMax_OneTimeInit(dev);
julientiron 0:1cb50d31c3b5 321 do{
julientiron 0:1cb50d31c3b5 322
julientiron 0:1cb50d31c3b5 323 /* backup offset initial value from nvm these must be done prior any over call that use offset */
julientiron 0:1cb50d31c3b5 324 status = VL6180X::VL6180x_RdByte(dev,SYSRANGE_PART_TO_PART_RANGE_OFFSET, (uint8_t*)&offset);
julientiron 0:1cb50d31c3b5 325 if( status ){
julientiron 0:1cb50d31c3b5 326 VL6180x_ErrLog("SYSRANGE_PART_TO_PART_RANGE_OFFSET rd fail");
julientiron 0:1cb50d31c3b5 327 break;
julientiron 0:1cb50d31c3b5 328 }
julientiron 0:1cb50d31c3b5 329 VL6180xDevDataSet(dev, Part2PartOffsetNVM, offset);
julientiron 0:1cb50d31c3b5 330
julientiron 0:1cb50d31c3b5 331 status=VL6180x_RdDWord( dev, SYSRANGE_RANGE_IGNORE_THRESHOLD, &CalValue);
julientiron 0:1cb50d31c3b5 332 if( status ){
julientiron 0:1cb50d31c3b5 333 VL6180x_ErrLog("Part2PartAmbNVM rd fail");
julientiron 0:1cb50d31c3b5 334 break;
julientiron 0:1cb50d31c3b5 335 }
julientiron 0:1cb50d31c3b5 336 if( (CalValue&0xFFFF0000) == 0 ){
julientiron 0:1cb50d31c3b5 337 CalValue=0x00CE03F8;
julientiron 0:1cb50d31c3b5 338 }
julientiron 0:1cb50d31c3b5 339 VL6180xDevDataSet(dev, Part2PartAmbNVM, CalValue);
julientiron 0:1cb50d31c3b5 340
julientiron 0:1cb50d31c3b5 341 status = VL6180x_RdWord(dev, SYSRANGE_CROSSTALK_COMPENSATION_RATE ,&u16);
julientiron 0:1cb50d31c3b5 342 if( status){
julientiron 0:1cb50d31c3b5 343 VL6180x_ErrLog("SYSRANGE_CROSSTALK_COMPENSATION_RATE rd fail ");
julientiron 0:1cb50d31c3b5 344 break;
julientiron 0:1cb50d31c3b5 345 }
julientiron 0:1cb50d31c3b5 346 XTalkCompRate_KCps = Fix7_2_KCPs(u16);
julientiron 0:1cb50d31c3b5 347 VL6180xDevDataSet(dev, XTalkCompRate_KCps , XTalkCompRate_KCps );
julientiron 0:1cb50d31c3b5 348
julientiron 0:1cb50d31c3b5 349 dmax_status = _DMax_InitData(dev);
julientiron 0:1cb50d31c3b5 350 if( dmax_status < 0 ){
julientiron 0:1cb50d31c3b5 351 VL6180x_ErrLog("DMax init failure");
julientiron 0:1cb50d31c3b5 352 break;
julientiron 0:1cb50d31c3b5 353 }
julientiron 0:1cb50d31c3b5 354
julientiron 0:1cb50d31c3b5 355 /* Read or wait for fresh out of reset */
julientiron 0:1cb50d31c3b5 356 status = VL6180x_RdByte(dev,SYSTEM_FRESH_OUT_OF_RESET, &FreshOutReset);
julientiron 0:1cb50d31c3b5 357 if( status ) {
julientiron 0:1cb50d31c3b5 358 VL6180x_ErrLog("SYSTEM_FRESH_OUT_OF_RESET rd fail");
julientiron 0:1cb50d31c3b5 359 break;
julientiron 0:1cb50d31c3b5 360 }
julientiron 0:1cb50d31c3b5 361 if( FreshOutReset!= 1 || dmax_status )
julientiron 0:1cb50d31c3b5 362 status = CALIBRATION_WARNING;
julientiron 0:1cb50d31c3b5 363
julientiron 0:1cb50d31c3b5 364 }
julientiron 0:1cb50d31c3b5 365 while(0);
julientiron 0:1cb50d31c3b5 366
julientiron 0:1cb50d31c3b5 367 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 368 return status;
julientiron 0:1cb50d31c3b5 369 }
julientiron 0:1cb50d31c3b5 370
julientiron 0:1cb50d31c3b5 371 int8_t VL6180X::VL6180x_GetOffsetCalibrationData(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 372 {
julientiron 0:1cb50d31c3b5 373 int8_t offset;
julientiron 0:1cb50d31c3b5 374 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 375 offset = VL6180xDevDataGet(dev, Part2PartOffsetNVM);
julientiron 0:1cb50d31c3b5 376 LOG_FUNCTION_END( offset );
julientiron 0:1cb50d31c3b5 377 return offset;
julientiron 0:1cb50d31c3b5 378 }
julientiron 0:1cb50d31c3b5 379
julientiron 0:1cb50d31c3b5 380 void VL6180X::VL6180x_SetOffsetCalibrationData(VL6180xDev_t dev, int8_t offset)
julientiron 0:1cb50d31c3b5 381 {
julientiron 0:1cb50d31c3b5 382 LOG_FUNCTION_START("%d", offset);
julientiron 0:1cb50d31c3b5 383 VL6180xDevDataSet(dev, Part2PartOffsetNVM, offset);
julientiron 0:1cb50d31c3b5 384 LOG_FUNCTION_END(0);
julientiron 0:1cb50d31c3b5 385 }
julientiron 0:1cb50d31c3b5 386
julientiron 0:1cb50d31c3b5 387 int VL6180X::VL6180x_SetXTalkCompensationRate(VL6180xDev_t dev, FixPoint97_t Rate)
julientiron 0:1cb50d31c3b5 388 {
julientiron 0:1cb50d31c3b5 389 int status;
julientiron 0:1cb50d31c3b5 390 LOG_FUNCTION_START("%d", Rate);
julientiron 0:1cb50d31c3b5 391 status = VL6180x_WrWord(dev, SYSRANGE_CROSSTALK_COMPENSATION_RATE, Rate);
julientiron 0:1cb50d31c3b5 392 if( status ==0 ){
julientiron 0:1cb50d31c3b5 393 uint32_t XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 394 XTalkCompRate_KCps = Fix7_2_KCPs(Rate);
julientiron 0:1cb50d31c3b5 395 VL6180xDevDataSet(dev, XTalkCompRate_KCps , XTalkCompRate_KCps );
julientiron 0:1cb50d31c3b5 396 /* update dmax whenever xtalk rate changes */
julientiron 0:1cb50d31c3b5 397 status = _DMax_InitData(dev);
julientiron 0:1cb50d31c3b5 398 }
julientiron 0:1cb50d31c3b5 399 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 400 return status;
julientiron 0:1cb50d31c3b5 401 }
julientiron 0:1cb50d31c3b5 402
julientiron 0:1cb50d31c3b5 403 int VL6180X::VL6180x_SetI2CAddress(VL6180xDev_t dev, uint8_t NewAddress){
julientiron 0:1cb50d31c3b5 404 int status;
julientiron 0:1cb50d31c3b5 405 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 406
julientiron 0:1cb50d31c3b5 407 status = VL6180x_WrByte(dev, I2C_SLAVE_DEVICE_ADDRESS, NewAddress);
julientiron 0:1cb50d31c3b5 408 if( status ){
julientiron 0:1cb50d31c3b5 409 VL6180x_ErrLog("new i2c addr Wr fail");
julientiron 0:1cb50d31c3b5 410 }
julientiron 0:1cb50d31c3b5 411 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 412 return status;
julientiron 0:1cb50d31c3b5 413 }
julientiron 0:1cb50d31c3b5 414
julientiron 0:1cb50d31c3b5 415
julientiron 0:1cb50d31c3b5 416 uint16_t VL6180X::VL6180x_GetUpperLimit(VL6180xDev_t dev) {
julientiron 0:1cb50d31c3b5 417 uint16_t limit;
julientiron 0:1cb50d31c3b5 418 int scaling;
julientiron 0:1cb50d31c3b5 419
julientiron 0:1cb50d31c3b5 420 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 421
julientiron 0:1cb50d31c3b5 422 scaling = _GetUpscale(dev);
julientiron 0:1cb50d31c3b5 423 /* FIXME we do assume here _GetUpscale is valid if user call us prior to init we may overflow the LUT mem area */
julientiron 0:1cb50d31c3b5 424 limit = UpperLimitLookUP[scaling - 1];
julientiron 0:1cb50d31c3b5 425
julientiron 0:1cb50d31c3b5 426 LOG_FUNCTION_END((int )limit);
julientiron 0:1cb50d31c3b5 427 return limit;
julientiron 0:1cb50d31c3b5 428 }
julientiron 0:1cb50d31c3b5 429
julientiron 0:1cb50d31c3b5 430
julientiron 0:1cb50d31c3b5 431
julientiron 0:1cb50d31c3b5 432 int VL6180X::VL6180x_StaticInit(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 433 int status=0, init_status;
julientiron 0:1cb50d31c3b5 434 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 435
julientiron 0:1cb50d31c3b5 436 /* TODO doc When using configurable scaling but using 1x as start condition
julientiron 0:1cb50d31c3b5 437 * load tunning upscale or not ??? */
julientiron 0:1cb50d31c3b5 438 if( _GetUpscale(dev) == 1 && !(VL6180x_UPSCALE_SUPPORT<0))
julientiron 0:1cb50d31c3b5 439 init_status=VL6180x_RangeStaticInit(dev);
julientiron 0:1cb50d31c3b5 440 else
julientiron 0:1cb50d31c3b5 441 init_status=VL6180x_UpscaleStaticInit(dev);
julientiron 0:1cb50d31c3b5 442
julientiron 0:1cb50d31c3b5 443 if( init_status <0 ){
julientiron 0:1cb50d31c3b5 444 VL6180x_ErrLog("StaticInit fail");
julientiron 0:1cb50d31c3b5 445 goto error;
julientiron 0:1cb50d31c3b5 446 }
julientiron 0:1cb50d31c3b5 447 else if(init_status > 0){
julientiron 0:1cb50d31c3b5 448 VL6180x_ErrLog("StaticInit warning");
julientiron 0:1cb50d31c3b5 449 }
julientiron 0:1cb50d31c3b5 450
julientiron 0:1cb50d31c3b5 451 #if REFRESH_CACHED_DATA_AFTER_INIT
julientiron 0:1cb50d31c3b5 452 /* update cached value after tuning applied */
julientiron 0:1cb50d31c3b5 453 do{
julientiron 0:1cb50d31c3b5 454 #ifdef VL6180x_HAVE_ALS_DATA
julientiron 0:1cb50d31c3b5 455 uint8_t data;
julientiron 0:1cb50d31c3b5 456 status= VL6180x_RdByte(dev, FW_ALS_RESULT_SCALER, &data);
julientiron 0:1cb50d31c3b5 457 if( status ) break;
julientiron 0:1cb50d31c3b5 458 VL6180xDevDataSet(dev, AlsScaler, data);
julientiron 0:1cb50d31c3b5 459
julientiron 0:1cb50d31c3b5 460 status= VL6180x_RdByte(dev, SYSALS_ANALOGUE_GAIN, &data);
julientiron 0:1cb50d31c3b5 461 if( status ) break;
julientiron 0:1cb50d31c3b5 462 VL6180x_AlsSetAnalogueGain(dev, data);
julientiron 0:1cb50d31c3b5 463 #endif
julientiron 0:1cb50d31c3b5 464 }
julientiron 0:1cb50d31c3b5 465 while(0);
julientiron 0:1cb50d31c3b5 466 #endif /* REFRESH_CACHED_DATA_AFTER_INIT */
julientiron 0:1cb50d31c3b5 467 if( status < 0 ){
julientiron 0:1cb50d31c3b5 468 VL6180x_ErrLog("StaticInit fail");
julientiron 0:1cb50d31c3b5 469 }
julientiron 0:1cb50d31c3b5 470 if( !status && init_status){
julientiron 0:1cb50d31c3b5 471 status = init_status;
julientiron 0:1cb50d31c3b5 472 }
julientiron 0:1cb50d31c3b5 473 error:
julientiron 0:1cb50d31c3b5 474 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 475 return status;
julientiron 0:1cb50d31c3b5 476 }
julientiron 0:1cb50d31c3b5 477
julientiron 0:1cb50d31c3b5 478
julientiron 0:1cb50d31c3b5 479 int VL6180X::VL6180x_SetGroupParamHold(VL6180xDev_t dev, int Hold)
julientiron 0:1cb50d31c3b5 480 {
julientiron 0:1cb50d31c3b5 481 int status;
julientiron 0:1cb50d31c3b5 482 uint8_t value;
julientiron 0:1cb50d31c3b5 483
julientiron 0:1cb50d31c3b5 484 LOG_FUNCTION_START("%d", Hold);
julientiron 0:1cb50d31c3b5 485 if( Hold )
julientiron 0:1cb50d31c3b5 486 value = 1;
julientiron 0:1cb50d31c3b5 487 else
julientiron 0:1cb50d31c3b5 488 value = 0;
julientiron 0:1cb50d31c3b5 489 status = VL6180x_WrByte(dev, SYSTEM_GROUPED_PARAMETER_HOLD, value);
julientiron 0:1cb50d31c3b5 490
julientiron 0:1cb50d31c3b5 491 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 492 return status;
julientiron 0:1cb50d31c3b5 493
julientiron 0:1cb50d31c3b5 494 }
julientiron 0:1cb50d31c3b5 495
julientiron 0:1cb50d31c3b5 496 int VL6180X::VL6180x_Prepare(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 497 {
julientiron 0:1cb50d31c3b5 498 int status;
julientiron 0:1cb50d31c3b5 499 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 500
julientiron 0:1cb50d31c3b5 501 do{
julientiron 0:1cb50d31c3b5 502 status=VL6180x_StaticInit(dev);
julientiron 0:1cb50d31c3b5 503 if( status<0) break;
julientiron 0:1cb50d31c3b5 504
julientiron 0:1cb50d31c3b5 505 /* set range InterruptMode to new sample */
julientiron 0:1cb50d31c3b5 506 status=VL6180x_RangeConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_DISABLED );
julientiron 0:1cb50d31c3b5 507 if( status)
julientiron 0:1cb50d31c3b5 508 break;
julientiron 0:1cb50d31c3b5 509
julientiron 0:1cb50d31c3b5 510 /* set default threshold */
julientiron 0:1cb50d31c3b5 511 status=VL6180x_RangeSetRawThresholds(dev, 10, 200);
julientiron 0:1cb50d31c3b5 512 if( status ){
julientiron 0:1cb50d31c3b5 513 VL6180x_ErrLog("VL6180x_RangeSetRawThresholds fail");
julientiron 0:1cb50d31c3b5 514 break;
julientiron 0:1cb50d31c3b5 515 }
julientiron 0:1cb50d31c3b5 516 #if VL6180x_ALS_SUPPORT
julientiron 0:1cb50d31c3b5 517 status =VL6180x_AlsSetIntegrationPeriod(dev, 100);
julientiron 0:1cb50d31c3b5 518 if( status ) break;
julientiron 0:1cb50d31c3b5 519 status = VL6180x_AlsSetInterMeasurementPeriod(dev, 200);
julientiron 0:1cb50d31c3b5 520 if( status ) break;
julientiron 0:1cb50d31c3b5 521 status = VL6180x_AlsSetAnalogueGain(dev, 0);
julientiron 0:1cb50d31c3b5 522 if( status ) break;
julientiron 0:1cb50d31c3b5 523 status = VL6180x_AlsSetThresholds(dev, 0, 0xFFFF);
julientiron 0:1cb50d31c3b5 524 if( status ) break;
julientiron 0:1cb50d31c3b5 525 /* set Als InterruptMode to new sample */
julientiron 0:1cb50d31c3b5 526 status=VL6180x_AlsConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 527 if( status ) {
julientiron 0:1cb50d31c3b5 528 VL6180x_ErrLog("VL6180x_AlsConfigInterrupt fail");
julientiron 0:1cb50d31c3b5 529 break;
julientiron 0:1cb50d31c3b5 530 }
julientiron 0:1cb50d31c3b5 531 #endif
julientiron 0:1cb50d31c3b5 532 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 533 _filter_Init(dev);
julientiron 0:1cb50d31c3b5 534 #endif
julientiron 0:1cb50d31c3b5 535 /* make sure to reset any left previous condition that can hangs first poll */
julientiron 0:1cb50d31c3b5 536 status=VL6180x_ClearAllInterrupt(dev);
julientiron 0:1cb50d31c3b5 537 }
julientiron 0:1cb50d31c3b5 538 while(0);
julientiron 0:1cb50d31c3b5 539 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 540
julientiron 0:1cb50d31c3b5 541 return status;
julientiron 0:1cb50d31c3b5 542 }
julientiron 0:1cb50d31c3b5 543
julientiron 0:1cb50d31c3b5 544 #if VL6180x_ALS_SUPPORT
julientiron 0:1cb50d31c3b5 545 int VL6180X::VL6180x_AlsGetLux(VL6180xDev_t dev, lux_t *pLux)
julientiron 0:1cb50d31c3b5 546 {
julientiron 0:1cb50d31c3b5 547 int status;
julientiron 0:1cb50d31c3b5 548 uint16_t RawAls;
julientiron 0:1cb50d31c3b5 549 uint32_t luxValue = 0;
julientiron 0:1cb50d31c3b5 550 uint32_t IntPeriod;
julientiron 0:1cb50d31c3b5 551 uint32_t AlsAnGain;
julientiron 0:1cb50d31c3b5 552 uint32_t GainFix;
julientiron 0:1cb50d31c3b5 553 uint32_t AlsScaler;
julientiron 0:1cb50d31c3b5 554
julientiron 0:1cb50d31c3b5 555 #if LUXRES_FIX_PREC != GAIN_FIX_PREC
julientiron 0:1cb50d31c3b5 556 #error "LUXRES_FIX_PREC != GAIN_FIX_PREC review these code to be correct"
julientiron 0:1cb50d31c3b5 557 #endif
julientiron 0:1cb50d31c3b5 558 const uint32_t LuxResxIntIme =(uint32_t)(0.56f* DEF_INT_PEFRIOD *(1<<LUXRES_FIX_PREC));
julientiron 0:1cb50d31c3b5 559
julientiron 0:1cb50d31c3b5 560 LOG_FUNCTION_START("%p", pLux);
julientiron 0:1cb50d31c3b5 561
julientiron 0:1cb50d31c3b5 562 status = VL6180x_RdWord( dev, RESULT_ALS_VAL, &RawAls);
julientiron 0:1cb50d31c3b5 563 if( !status){
julientiron 0:1cb50d31c3b5 564 /* wer are yet here at no fix point */
julientiron 0:1cb50d31c3b5 565 IntPeriod=VL6180xDevDataGet(dev, IntegrationPeriod);
julientiron 0:1cb50d31c3b5 566 AlsScaler=VL6180xDevDataGet(dev, AlsScaler);
julientiron 0:1cb50d31c3b5 567 IntPeriod++; /* what stored is real time ms -1 and it can be 0 for or 0 or 1ms */
julientiron 0:1cb50d31c3b5 568 luxValue = (uint32_t)RawAls * LuxResxIntIme; /* max # 16+8bits + 6bit (0.56*100) */
julientiron 0:1cb50d31c3b5 569 luxValue /= IntPeriod; /* max # 16+8bits + 6bit 16+8+1 to 9 bit */
julientiron 0:1cb50d31c3b5 570 /* between 29 - 21 bit */
julientiron 0:1cb50d31c3b5 571 AlsAnGain = VL6180xDevDataGet(dev, AlsGainCode);
julientiron 0:1cb50d31c3b5 572 GainFix = AlsGainLookUp[AlsAnGain];
julientiron 0:1cb50d31c3b5 573 luxValue = luxValue / (AlsScaler * GainFix);
julientiron 0:1cb50d31c3b5 574 *pLux=luxValue;
julientiron 0:1cb50d31c3b5 575 }
julientiron 0:1cb50d31c3b5 576
julientiron 0:1cb50d31c3b5 577 LOG_FUNCTION_END_FMT(status, "%x",(int)*pLux);
julientiron 0:1cb50d31c3b5 578 return status;
julientiron 0:1cb50d31c3b5 579 }
julientiron 0:1cb50d31c3b5 580
julientiron 0:1cb50d31c3b5 581 int VL6180X::VL6180x_AlsGetMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData)
julientiron 0:1cb50d31c3b5 582 {
julientiron 0:1cb50d31c3b5 583 int status;
julientiron 0:1cb50d31c3b5 584 uint8_t ErrStatus;
julientiron 0:1cb50d31c3b5 585
julientiron 0:1cb50d31c3b5 586 LOG_FUNCTION_START("%p", pAlsData);
julientiron 0:1cb50d31c3b5 587
julientiron 0:1cb50d31c3b5 588 status = VL6180x_AlsGetLux(dev, &pAlsData->lux);
julientiron 0:1cb50d31c3b5 589 if( !status ){
julientiron 0:1cb50d31c3b5 590 status = VL6180x_RdByte(dev, RESULT_ALS_STATUS, & ErrStatus);
julientiron 0:1cb50d31c3b5 591 pAlsData->errorStatus = ErrStatus>>4;
julientiron 0:1cb50d31c3b5 592 }
julientiron 0:1cb50d31c3b5 593 LOG_FUNCTION_END_FMT(status,"%d %d", (int)pAlsData->lux, (int)pAlsData->errorStatus);
julientiron 0:1cb50d31c3b5 594
julientiron 0:1cb50d31c3b5 595 return status;
julientiron 0:1cb50d31c3b5 596 }
julientiron 0:1cb50d31c3b5 597
julientiron 0:1cb50d31c3b5 598
julientiron 0:1cb50d31c3b5 599 int VL6180X::VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData) {
julientiron 0:1cb50d31c3b5 600 int status;
julientiron 0:1cb50d31c3b5 601 int ClrStatus;
julientiron 0:1cb50d31c3b5 602 uint8_t IntStatus;
julientiron 0:1cb50d31c3b5 603
julientiron 0:1cb50d31c3b5 604 LOG_FUNCTION_START("%p", pAlsData);
julientiron 0:1cb50d31c3b5 605 #if VL6180X_SAFE_POLLING_ENTER
julientiron 0:1cb50d31c3b5 606 /* if device get stopped with left interrupt uncleared , it is required to clear them now or poll for new condition will never occur*/
julientiron 0:1cb50d31c3b5 607 status=VL6180x_AlsClearInterrupt(dev);
julientiron 0:1cb50d31c3b5 608 if(status){
julientiron 0:1cb50d31c3b5 609 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 610 goto over;
julientiron 0:1cb50d31c3b5 611 }
julientiron 0:1cb50d31c3b5 612 #endif
julientiron 0:1cb50d31c3b5 613
julientiron 0:1cb50d31c3b5 614 status=VL6180x_AlsSetSystemMode(dev, MODE_START_STOP|MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 615 if( status){
julientiron 0:1cb50d31c3b5 616 VL6180x_ErrLog("VL6180x_AlsSetSystemMode fail");
julientiron 0:1cb50d31c3b5 617 goto over;
julientiron 0:1cb50d31c3b5 618 }
julientiron 0:1cb50d31c3b5 619
julientiron 0:1cb50d31c3b5 620 /* poll for new sample ready */
julientiron 0:1cb50d31c3b5 621 while (1 ) {
julientiron 0:1cb50d31c3b5 622 status = VL6180x_AlsGetInterruptStatus(dev, &IntStatus);
julientiron 0:1cb50d31c3b5 623 if (status) {
julientiron 0:1cb50d31c3b5 624 break;
julientiron 0:1cb50d31c3b5 625 }
julientiron 0:1cb50d31c3b5 626 if (IntStatus == RES_INT_STAT_GPIO_NEW_SAMPLE_READY) {
julientiron 0:1cb50d31c3b5 627 break; /* break on new data (status is 0) */
julientiron 0:1cb50d31c3b5 628 }
julientiron 0:1cb50d31c3b5 629 wait_ms(10);
julientiron 0:1cb50d31c3b5 630 };
julientiron 0:1cb50d31c3b5 631
julientiron 0:1cb50d31c3b5 632 if (!status) {
julientiron 0:1cb50d31c3b5 633 status = VL6180x_AlsGetMeasurement(dev, pAlsData);
julientiron 0:1cb50d31c3b5 634 }
julientiron 0:1cb50d31c3b5 635
julientiron 0:1cb50d31c3b5 636 ClrStatus = VL6180x_AlsClearInterrupt(dev);
julientiron 0:1cb50d31c3b5 637 if (ClrStatus) {
julientiron 0:1cb50d31c3b5 638 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 639 if (!status) {
julientiron 0:1cb50d31c3b5 640 status = ClrStatus; /* leave previous if already on error */
julientiron 0:1cb50d31c3b5 641 }
julientiron 0:1cb50d31c3b5 642 }
julientiron 0:1cb50d31c3b5 643 over:
julientiron 0:1cb50d31c3b5 644 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 645
julientiron 0:1cb50d31c3b5 646 return status;
julientiron 0:1cb50d31c3b5 647 }
julientiron 0:1cb50d31c3b5 648
julientiron 0:1cb50d31c3b5 649 int VL6180X::VL6180x_AlsGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus) {
julientiron 0:1cb50d31c3b5 650 int status;
julientiron 0:1cb50d31c3b5 651 uint8_t IntStatus;
julientiron 0:1cb50d31c3b5 652 LOG_FUNCTION_START("%p", pIntStatus);
julientiron 0:1cb50d31c3b5 653
julientiron 0:1cb50d31c3b5 654 status = VL6180x_RdByte(dev, RESULT_INTERRUPT_STATUS_GPIO, &IntStatus);
julientiron 0:1cb50d31c3b5 655 *pIntStatus= (IntStatus>>3)&0x07;
julientiron 0:1cb50d31c3b5 656
julientiron 0:1cb50d31c3b5 657 LOG_FUNCTION_END_FMT(status, "%d", (int)*pIntStatus);
julientiron 0:1cb50d31c3b5 658 return status;
julientiron 0:1cb50d31c3b5 659 }
julientiron 0:1cb50d31c3b5 660
julientiron 0:1cb50d31c3b5 661 int VL6180X::VL6180x_AlsWaitDeviceReady(VL6180xDev_t dev, int MaxLoop ){
julientiron 0:1cb50d31c3b5 662 int status;
julientiron 0:1cb50d31c3b5 663 int n;
julientiron 0:1cb50d31c3b5 664 uint8_t u8;
julientiron 0:1cb50d31c3b5 665 LOG_FUNCTION_START("%d", (int)MaxLoop);
julientiron 0:1cb50d31c3b5 666 if( MaxLoop<1){
julientiron 0:1cb50d31c3b5 667 status=INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 668 }
julientiron 0:1cb50d31c3b5 669 else{
julientiron 0:1cb50d31c3b5 670 for( n=0; n < MaxLoop ; n++){
julientiron 0:1cb50d31c3b5 671 status=VL6180x_RdByte(dev, RESULT_ALS_STATUS, &u8);
julientiron 0:1cb50d31c3b5 672 if( status)
julientiron 0:1cb50d31c3b5 673 break;
julientiron 0:1cb50d31c3b5 674 u8 = u8 & ALS_DEVICE_READY_MASK;
julientiron 0:1cb50d31c3b5 675 if( u8 )
julientiron 0:1cb50d31c3b5 676 break;
julientiron 0:1cb50d31c3b5 677
julientiron 0:1cb50d31c3b5 678 }
julientiron 0:1cb50d31c3b5 679 if( !status && !u8 ){
julientiron 0:1cb50d31c3b5 680 status = TIME_OUT;
julientiron 0:1cb50d31c3b5 681 }
julientiron 0:1cb50d31c3b5 682 }
julientiron 0:1cb50d31c3b5 683 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 684 return status;
julientiron 0:1cb50d31c3b5 685 }
julientiron 0:1cb50d31c3b5 686
julientiron 0:1cb50d31c3b5 687 int VL6180X::VL6180x_AlsSetSystemMode(VL6180xDev_t dev, uint8_t mode)
julientiron 0:1cb50d31c3b5 688 {
julientiron 0:1cb50d31c3b5 689 int status;
julientiron 0:1cb50d31c3b5 690 LOG_FUNCTION_START("%d", (int)mode);
julientiron 0:1cb50d31c3b5 691 /* FIXME if we are called back to back real fast we are not checking
julientiron 0:1cb50d31c3b5 692 * if previous mode "set" got absorbed => bit 0 must be 0 so that wr 1 work */
julientiron 0:1cb50d31c3b5 693 if( mode <= 3){
julientiron 0:1cb50d31c3b5 694 status=VL6180x_WrByte(dev, SYSALS_START, mode);
julientiron 0:1cb50d31c3b5 695 }
julientiron 0:1cb50d31c3b5 696 else{
julientiron 0:1cb50d31c3b5 697 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 698 }
julientiron 0:1cb50d31c3b5 699 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 700 return status;
julientiron 0:1cb50d31c3b5 701 }
julientiron 0:1cb50d31c3b5 702
julientiron 0:1cb50d31c3b5 703 int VL6180X::VL6180x_AlsConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt)
julientiron 0:1cb50d31c3b5 704 {
julientiron 0:1cb50d31c3b5 705 int status;
julientiron 0:1cb50d31c3b5 706
julientiron 0:1cb50d31c3b5 707 if( ConfigGpioInt<= CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY){
julientiron 0:1cb50d31c3b5 708 status = VL6180x_UpdateByte(dev, SYSTEM_INTERRUPT_CONFIG_GPIO, (uint8_t)(~CONFIG_GPIO_ALS_MASK), (ConfigGpioInt<<3));
julientiron 0:1cb50d31c3b5 709 }
julientiron 0:1cb50d31c3b5 710 else{
julientiron 0:1cb50d31c3b5 711 VL6180x_ErrLog("Invalid config mode param %d", (int)ConfigGpioInt);
julientiron 0:1cb50d31c3b5 712 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 713 }
julientiron 0:1cb50d31c3b5 714 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 715 return status;
julientiron 0:1cb50d31c3b5 716 }
julientiron 0:1cb50d31c3b5 717
julientiron 0:1cb50d31c3b5 718
julientiron 0:1cb50d31c3b5 719
julientiron 0:1cb50d31c3b5 720 int VL6180X::VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high) {
julientiron 0:1cb50d31c3b5 721 int status;
julientiron 0:1cb50d31c3b5 722
julientiron 0:1cb50d31c3b5 723 LOG_FUNCTION_START("%d %d", (int )low, (int)high);
julientiron 0:1cb50d31c3b5 724
julientiron 0:1cb50d31c3b5 725 status = VL6180x_WrWord(dev, SYSALS_THRESH_LOW, low);
julientiron 0:1cb50d31c3b5 726 if(!status ){
julientiron 0:1cb50d31c3b5 727 status = VL6180x_WrWord(dev, SYSALS_THRESH_HIGH, high);
julientiron 0:1cb50d31c3b5 728 }
julientiron 0:1cb50d31c3b5 729
julientiron 0:1cb50d31c3b5 730 LOG_FUNCTION_END(status) ;
julientiron 0:1cb50d31c3b5 731 return status;
julientiron 0:1cb50d31c3b5 732 }
julientiron 0:1cb50d31c3b5 733
julientiron 0:1cb50d31c3b5 734
julientiron 0:1cb50d31c3b5 735 int VL6180X::VL6180x_AlsSetAnalogueGain(VL6180xDev_t dev, uint8_t gain) {
julientiron 0:1cb50d31c3b5 736 int status;
julientiron 0:1cb50d31c3b5 737 uint8_t GainTotal;
julientiron 0:1cb50d31c3b5 738
julientiron 0:1cb50d31c3b5 739 LOG_FUNCTION_START("%d", (int )gain);
julientiron 0:1cb50d31c3b5 740 gain&=~0x40;
julientiron 0:1cb50d31c3b5 741 if (gain > 7) {
julientiron 0:1cb50d31c3b5 742 gain = 7;
julientiron 0:1cb50d31c3b5 743 }
julientiron 0:1cb50d31c3b5 744 GainTotal = gain|0x40;
julientiron 0:1cb50d31c3b5 745
julientiron 0:1cb50d31c3b5 746 status = VL6180x_WrByte(dev, SYSALS_ANALOGUE_GAIN, GainTotal);
julientiron 0:1cb50d31c3b5 747 if( !status){
julientiron 0:1cb50d31c3b5 748 VL6180xDevDataSet(dev, AlsGainCode, gain);
julientiron 0:1cb50d31c3b5 749 }
julientiron 0:1cb50d31c3b5 750
julientiron 0:1cb50d31c3b5 751 LOG_FUNCTION_END_FMT(status, "%d %d", (int ) gain, (int )GainTotal);
julientiron 0:1cb50d31c3b5 752 return status;
julientiron 0:1cb50d31c3b5 753 }
julientiron 0:1cb50d31c3b5 754
julientiron 0:1cb50d31c3b5 755 int VL6180X::VL6180x_AlsSetInterMeasurementPeriod(VL6180xDev_t dev, uint16_t intermeasurement_period_ms)
julientiron 0:1cb50d31c3b5 756 {
julientiron 0:1cb50d31c3b5 757 int status;
julientiron 0:1cb50d31c3b5 758
julientiron 0:1cb50d31c3b5 759 LOG_FUNCTION_START("%d",(int)intermeasurement_period_ms);
julientiron 0:1cb50d31c3b5 760 /* clipping: range is 0-2550ms */
julientiron 0:1cb50d31c3b5 761 if (intermeasurement_period_ms >= 255 *10)
julientiron 0:1cb50d31c3b5 762 intermeasurement_period_ms = 255 *10;
julientiron 0:1cb50d31c3b5 763 status=VL6180x_WrByte(dev, SYSALS_INTERMEASUREMENT_PERIOD, (uint8_t)(intermeasurement_period_ms/10));
julientiron 0:1cb50d31c3b5 764
julientiron 0:1cb50d31c3b5 765 LOG_FUNCTION_END_FMT(status, "%d", (int) intermeasurement_period_ms);
julientiron 0:1cb50d31c3b5 766 return status;
julientiron 0:1cb50d31c3b5 767 }
julientiron 0:1cb50d31c3b5 768
julientiron 0:1cb50d31c3b5 769
julientiron 0:1cb50d31c3b5 770 int VL6180X::VL6180x_AlsSetIntegrationPeriod(VL6180xDev_t dev, uint16_t period_ms)
julientiron 0:1cb50d31c3b5 771 {
julientiron 0:1cb50d31c3b5 772 int status;
julientiron 0:1cb50d31c3b5 773 uint16_t SetIntegrationPeriod;
julientiron 0:1cb50d31c3b5 774
julientiron 0:1cb50d31c3b5 775 LOG_FUNCTION_START("%d", (int)period_ms);
julientiron 0:1cb50d31c3b5 776
julientiron 0:1cb50d31c3b5 777 if( period_ms>=1 )
julientiron 0:1cb50d31c3b5 778 SetIntegrationPeriod = period_ms - 1;
julientiron 0:1cb50d31c3b5 779 else
julientiron 0:1cb50d31c3b5 780 SetIntegrationPeriod = period_ms;
julientiron 0:1cb50d31c3b5 781
julientiron 0:1cb50d31c3b5 782 if (SetIntegrationPeriod > 464) {
julientiron 0:1cb50d31c3b5 783 SetIntegrationPeriod = 464;
julientiron 0:1cb50d31c3b5 784 }
julientiron 0:1cb50d31c3b5 785 else if (SetIntegrationPeriod == 255) {
julientiron 0:1cb50d31c3b5 786 SetIntegrationPeriod++; /* can't write 255 since this causes the device to lock out.*/
julientiron 0:1cb50d31c3b5 787 }
julientiron 0:1cb50d31c3b5 788
julientiron 0:1cb50d31c3b5 789 status =VL6180x_WrWord(dev, SYSALS_INTEGRATION_PERIOD, SetIntegrationPeriod);
julientiron 0:1cb50d31c3b5 790 if( !status ){
julientiron 0:1cb50d31c3b5 791 VL6180xDevDataSet(dev, IntegrationPeriod, SetIntegrationPeriod) ;
julientiron 0:1cb50d31c3b5 792 }
julientiron 0:1cb50d31c3b5 793 LOG_FUNCTION_END_FMT(status, "%d", (int)SetIntegrationPeriod);
julientiron 0:1cb50d31c3b5 794 return status;
julientiron 0:1cb50d31c3b5 795 }
julientiron 0:1cb50d31c3b5 796
julientiron 0:1cb50d31c3b5 797 #endif /* HAVE_ALS_SUPPORT */
julientiron 0:1cb50d31c3b5 798
julientiron 0:1cb50d31c3b5 799
julientiron 0:1cb50d31c3b5 800 int VL6180X::VL6180x_RangePollMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData)
julientiron 0:1cb50d31c3b5 801 {
julientiron 0:1cb50d31c3b5 802 int status;
julientiron 0:1cb50d31c3b5 803 int ClrStatus;
julientiron 0:1cb50d31c3b5 804 IntrStatus_t IntStatus;
julientiron 0:1cb50d31c3b5 805
julientiron 0:1cb50d31c3b5 806 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 807 /* start single range measurement */
julientiron 0:1cb50d31c3b5 808
julientiron 0:1cb50d31c3b5 809
julientiron 0:1cb50d31c3b5 810 #if VL6180X_SAFE_POLLING_ENTER
julientiron 0:1cb50d31c3b5 811 /* if device get stopped with left interrupt uncleared , it is required to clear them now or poll for new condition will never occur*/
julientiron 0:1cb50d31c3b5 812 status=VL6180x_RangeClearInterrupt(dev);
julientiron 0:1cb50d31c3b5 813 if(status){
julientiron 0:1cb50d31c3b5 814 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 815 goto done;
julientiron 0:1cb50d31c3b5 816 }
julientiron 0:1cb50d31c3b5 817 #endif
julientiron 0:1cb50d31c3b5 818 /* //![single_shot_snipet] */
julientiron 0:1cb50d31c3b5 819 status=VL6180x_RangeSetSystemMode(dev, MODE_START_STOP|MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 820 if( status ){
julientiron 0:1cb50d31c3b5 821 VL6180x_ErrLog("VL6180x_RangeSetSystemMode fail");
julientiron 0:1cb50d31c3b5 822 goto done;
julientiron 0:1cb50d31c3b5 823 }
julientiron 0:1cb50d31c3b5 824
julientiron 0:1cb50d31c3b5 825 /* poll for new sample ready */
julientiron 0:1cb50d31c3b5 826 while(1 ){
julientiron 0:1cb50d31c3b5 827 status=VL6180x_RangeGetInterruptStatus(dev, &IntStatus.val);
julientiron 0:1cb50d31c3b5 828 if( status ){
julientiron 0:1cb50d31c3b5 829 break;
julientiron 0:1cb50d31c3b5 830 }
julientiron 0:1cb50d31c3b5 831 if( IntStatus.status.Error !=0 ){
julientiron 0:1cb50d31c3b5 832 VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val);
julientiron 0:1cb50d31c3b5 833 status = RANGE_ERROR;
julientiron 0:1cb50d31c3b5 834 break;
julientiron 0:1cb50d31c3b5 835 }
julientiron 0:1cb50d31c3b5 836 else
julientiron 0:1cb50d31c3b5 837 if( IntStatus.status.Range == RES_INT_STAT_GPIO_NEW_SAMPLE_READY){
julientiron 0:1cb50d31c3b5 838 break;
julientiron 0:1cb50d31c3b5 839 }
julientiron 0:1cb50d31c3b5 840 wait_ms(10);
julientiron 0:1cb50d31c3b5 841 }
julientiron 0:1cb50d31c3b5 842 /* //![single_shot_snipet] */
julientiron 0:1cb50d31c3b5 843
julientiron 0:1cb50d31c3b5 844 if ( !status ){
julientiron 0:1cb50d31c3b5 845 status = VL6180x_RangeGetMeasurement(dev, pRangeData);
julientiron 0:1cb50d31c3b5 846 }
julientiron 0:1cb50d31c3b5 847
julientiron 0:1cb50d31c3b5 848 /* clear range interrupt source */
julientiron 0:1cb50d31c3b5 849 ClrStatus = VL6180x_RangeClearInterrupt(dev);
julientiron 0:1cb50d31c3b5 850 if( ClrStatus ){
julientiron 0:1cb50d31c3b5 851 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 852 /* leave initial status if already in error */
julientiron 0:1cb50d31c3b5 853 if( !status ){
julientiron 0:1cb50d31c3b5 854 status=ClrStatus;
julientiron 0:1cb50d31c3b5 855 }
julientiron 0:1cb50d31c3b5 856 }
julientiron 0:1cb50d31c3b5 857 done:
julientiron 0:1cb50d31c3b5 858 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 859 return status;
julientiron 0:1cb50d31c3b5 860 }
julientiron 0:1cb50d31c3b5 861
julientiron 0:1cb50d31c3b5 862
julientiron 0:1cb50d31c3b5 863
julientiron 0:1cb50d31c3b5 864 int VL6180X::VL6180x_RangeGetMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData)
julientiron 0:1cb50d31c3b5 865 {
julientiron 0:1cb50d31c3b5 866 int status;
julientiron 0:1cb50d31c3b5 867 uint16_t RawRate;
julientiron 0:1cb50d31c3b5 868 uint8_t RawStatus;
julientiron 0:1cb50d31c3b5 869
julientiron 0:1cb50d31c3b5 870 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 871
julientiron 0:1cb50d31c3b5 872 status = VL6180x_RangeGetResult(dev, &pRangeData->range_mm);
julientiron 0:1cb50d31c3b5 873 if( !status ){
julientiron 0:1cb50d31c3b5 874 status = VL6180x_RdWord(dev,RESULT_RANGE_SIGNAL_RATE, &RawRate );
julientiron 0:1cb50d31c3b5 875 if( !status ){
julientiron 0:1cb50d31c3b5 876 pRangeData->signalRate_mcps = VL6180x_9to7Conv(RawRate);
julientiron 0:1cb50d31c3b5 877 status = VL6180x_RdByte(dev, RESULT_RANGE_STATUS, &RawStatus);
julientiron 0:1cb50d31c3b5 878 if( !status ){
julientiron 0:1cb50d31c3b5 879 pRangeData->errorStatus = RawStatus >>4;
julientiron 0:1cb50d31c3b5 880 }
julientiron 0:1cb50d31c3b5 881 else{
julientiron 0:1cb50d31c3b5 882 VL6180x_ErrLog("Rd RESULT_RANGE_STATUS fail");
julientiron 0:1cb50d31c3b5 883 }
julientiron 0:1cb50d31c3b5 884 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT || VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 885 status = _GetRateResult(dev, pRangeData);
julientiron 0:1cb50d31c3b5 886 if( status )
julientiron 0:1cb50d31c3b5 887 goto error;
julientiron 0:1cb50d31c3b5 888 #endif
julientiron 0:1cb50d31c3b5 889 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 890 /* if enabled run filter */
julientiron 0:1cb50d31c3b5 891 if( _IsWrapArroundActive(dev) ){
julientiron 0:1cb50d31c3b5 892 status=_filter_GetResult(dev, pRangeData);
julientiron 0:1cb50d31c3b5 893 if( !status){
julientiron 0:1cb50d31c3b5 894 /* patch the range status and measure if it is filtered */
julientiron 0:1cb50d31c3b5 895 if( pRangeData->range_mm != pRangeData->FilteredData.range_mm) {
julientiron 0:1cb50d31c3b5 896 pRangeData->errorStatus=RangingFiltered;
julientiron 0:1cb50d31c3b5 897 pRangeData->range_mm = pRangeData->FilteredData.range_mm;
julientiron 0:1cb50d31c3b5 898 }
julientiron 0:1cb50d31c3b5 899 }
julientiron 0:1cb50d31c3b5 900 }
julientiron 0:1cb50d31c3b5 901 #endif
julientiron 0:1cb50d31c3b5 902
julientiron 0:1cb50d31c3b5 903 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 904 if(_IsDMaxActive(dev) ){
julientiron 0:1cb50d31c3b5 905 _DMax_Compute(dev, pRangeData);
julientiron 0:1cb50d31c3b5 906 }
julientiron 0:1cb50d31c3b5 907 #endif
julientiron 0:1cb50d31c3b5 908 }
julientiron 0:1cb50d31c3b5 909 else{
julientiron 0:1cb50d31c3b5 910 VL6180x_ErrLog("Rd RESULT_RANGE_SIGNAL_RATE fail");
julientiron 0:1cb50d31c3b5 911 }
julientiron 0:1cb50d31c3b5 912 }
julientiron 0:1cb50d31c3b5 913 else{
julientiron 0:1cb50d31c3b5 914 VL6180x_ErrLog("VL6180x_GetRangeResult fail");
julientiron 0:1cb50d31c3b5 915 }
julientiron 0:1cb50d31c3b5 916 error:
julientiron 0:1cb50d31c3b5 917 LOG_FUNCTION_END_FMT(status, "%d %d %d", (int)pRangeData->range_mm, (int)pRangeData->signalRate_mcps, (int)pRangeData->errorStatus) ;
julientiron 0:1cb50d31c3b5 918 return status;
julientiron 0:1cb50d31c3b5 919 }
julientiron 0:1cb50d31c3b5 920
julientiron 0:1cb50d31c3b5 921
julientiron 0:1cb50d31c3b5 922 int VL6180X::VL6180x_RangeGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData)
julientiron 0:1cb50d31c3b5 923 {
julientiron 0:1cb50d31c3b5 924 int status;
julientiron 0:1cb50d31c3b5 925 IntrStatus_t IntStatus;
julientiron 0:1cb50d31c3b5 926
julientiron 0:1cb50d31c3b5 927 LOG_FUNCTION_START();
julientiron 0:1cb50d31c3b5 928
julientiron 0:1cb50d31c3b5 929 status = VL6180x_RangeGetInterruptStatus(dev, &IntStatus.val);
julientiron 0:1cb50d31c3b5 930 if( status ==0 ){
julientiron 0:1cb50d31c3b5 931 if( IntStatus.status.Error !=0 ){
julientiron 0:1cb50d31c3b5 932 VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val);
julientiron 0:1cb50d31c3b5 933 status = RANGE_ERROR;
julientiron 0:1cb50d31c3b5 934 }
julientiron 0:1cb50d31c3b5 935 else
julientiron 0:1cb50d31c3b5 936 if( IntStatus.status.Range == RES_INT_STAT_GPIO_NEW_SAMPLE_READY){
julientiron 0:1cb50d31c3b5 937 status = VL6180x_RangeGetMeasurement(dev,pRangeData );
julientiron 0:1cb50d31c3b5 938 if( status == 0){
julientiron 0:1cb50d31c3b5 939 /* clear range interrupt source */
julientiron 0:1cb50d31c3b5 940 status = VL6180x_RangeClearInterrupt(dev);
julientiron 0:1cb50d31c3b5 941 if( status ){
julientiron 0:1cb50d31c3b5 942 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 943 }
julientiron 0:1cb50d31c3b5 944 }
julientiron 0:1cb50d31c3b5 945 }
julientiron 0:1cb50d31c3b5 946 else{
julientiron 0:1cb50d31c3b5 947 status = NOT_READY;
julientiron 0:1cb50d31c3b5 948 }
julientiron 0:1cb50d31c3b5 949 }
julientiron 0:1cb50d31c3b5 950 else{
julientiron 0:1cb50d31c3b5 951 VL6180x_ErrLog("fail to get interrupt status");
julientiron 0:1cb50d31c3b5 952 }
julientiron 0:1cb50d31c3b5 953 LOG_FUNCTION_END(status) ;
julientiron 0:1cb50d31c3b5 954 return status;
julientiron 0:1cb50d31c3b5 955 }
julientiron 0:1cb50d31c3b5 956
julientiron 0:1cb50d31c3b5 957 int VL6180X::VL6180x_FilterSetState(VL6180xDev_t dev, int state){
julientiron 0:1cb50d31c3b5 958 int status;
julientiron 0:1cb50d31c3b5 959 LOG_FUNCTION_START("%d", state);
julientiron 0:1cb50d31c3b5 960 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 961 VL6180xDevDataSet(dev,WrapAroundFilterActive, state);
julientiron 0:1cb50d31c3b5 962 status = 0;
julientiron 0:1cb50d31c3b5 963 #else
julientiron 0:1cb50d31c3b5 964 status = NOT_SUPPORTED;
julientiron 0:1cb50d31c3b5 965 #endif
julientiron 0:1cb50d31c3b5 966 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 967 return status;
julientiron 0:1cb50d31c3b5 968 }
julientiron 0:1cb50d31c3b5 969
julientiron 0:1cb50d31c3b5 970 int VL6180X::VL6180x_FilterGetState(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 971 int status;
julientiron 0:1cb50d31c3b5 972 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 973 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 974 status = VL6180xDevDataGet(dev,WrapAroundFilterActive);
julientiron 0:1cb50d31c3b5 975 #else
julientiron 0:1cb50d31c3b5 976 status = 0;
julientiron 0:1cb50d31c3b5 977 #endif
julientiron 0:1cb50d31c3b5 978 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 979 return status;
julientiron 0:1cb50d31c3b5 980 }
julientiron 0:1cb50d31c3b5 981
julientiron 0:1cb50d31c3b5 982 int VL6180X::VL6180x_RangeGetResult(VL6180xDev_t dev, int32_t *pRange_mm) {
julientiron 0:1cb50d31c3b5 983 int status;
julientiron 0:1cb50d31c3b5 984 uint8_t RawRange;
julientiron 0:1cb50d31c3b5 985 int32_t Upscale;
julientiron 0:1cb50d31c3b5 986
julientiron 0:1cb50d31c3b5 987 LOG_FUNCTION_START("%p",pRange_mm);
julientiron 0:1cb50d31c3b5 988
julientiron 0:1cb50d31c3b5 989 status = VL6180x_RdByte(dev, RESULT_RANGE_VAL, &RawRange);
julientiron 0:1cb50d31c3b5 990 if( !status ){
julientiron 0:1cb50d31c3b5 991 Upscale = _GetUpscale(dev);
julientiron 0:1cb50d31c3b5 992 *pRange_mm= Upscale*(int32_t)RawRange;
julientiron 0:1cb50d31c3b5 993 }
julientiron 0:1cb50d31c3b5 994 LOG_FUNCTION_END_FMT(status, "%d", (int)*pRange_mm);
julientiron 0:1cb50d31c3b5 995 return status;
julientiron 0:1cb50d31c3b5 996 }
julientiron 0:1cb50d31c3b5 997
julientiron 0:1cb50d31c3b5 998 int VL6180X::VL6180x_RangeSetRawThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high)
julientiron 0:1cb50d31c3b5 999 {
julientiron 0:1cb50d31c3b5 1000 int status;
julientiron 0:1cb50d31c3b5 1001 LOG_FUNCTION_START("%d %d", (int) low, (int)high);
julientiron 0:1cb50d31c3b5 1002 /* TODO we can optimize here grouping high/low in a word but that's cpu endianness dependent */
julientiron 0:1cb50d31c3b5 1003 status=VL6180x_WrByte(dev, SYSRANGE_THRESH_HIGH,high);
julientiron 0:1cb50d31c3b5 1004 if( !status){
julientiron 0:1cb50d31c3b5 1005 status=VL6180x_WrByte(dev, SYSRANGE_THRESH_LOW, low);
julientiron 0:1cb50d31c3b5 1006 }
julientiron 0:1cb50d31c3b5 1007
julientiron 0:1cb50d31c3b5 1008 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1009 return status;
julientiron 0:1cb50d31c3b5 1010 }
julientiron 0:1cb50d31c3b5 1011
julientiron 0:1cb50d31c3b5 1012 int VL6180X::VL6180x_RangeSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high, int UseSafeParamHold)
julientiron 0:1cb50d31c3b5 1013 {
julientiron 0:1cb50d31c3b5 1014 int status;
julientiron 0:1cb50d31c3b5 1015 int scale;
julientiron 0:1cb50d31c3b5 1016 LOG_FUNCTION_START("%d %d", (int) low, (int)high);
julientiron 0:1cb50d31c3b5 1017 scale=_GetUpscale(dev,UpscaleFactor);
julientiron 0:1cb50d31c3b5 1018 if( low>scale*255 || high >scale*255){
julientiron 0:1cb50d31c3b5 1019 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1020 }
julientiron 0:1cb50d31c3b5 1021 else{
julientiron 0:1cb50d31c3b5 1022 do{
julientiron 0:1cb50d31c3b5 1023 if( UseSafeParamHold ){
julientiron 0:1cb50d31c3b5 1024 status=VL6180x_SetGroupParamHold(dev, 1);
julientiron 0:1cb50d31c3b5 1025 if( status )
julientiron 0:1cb50d31c3b5 1026 break;
julientiron 0:1cb50d31c3b5 1027 }
julientiron 0:1cb50d31c3b5 1028 status=VL6180x_RangeSetRawThresholds(dev, (uint8_t)(low/scale), (uint8_t)(high/scale));
julientiron 0:1cb50d31c3b5 1029 if( status ){
julientiron 0:1cb50d31c3b5 1030 VL6180x_ErrLog("VL6180x_RangeSetRawThresholds fail");
julientiron 0:1cb50d31c3b5 1031 }
julientiron 0:1cb50d31c3b5 1032 if( UseSafeParamHold ){
julientiron 0:1cb50d31c3b5 1033 int HoldStatus;
julientiron 0:1cb50d31c3b5 1034 /* tryt to unset param hold vene if previous fail */
julientiron 0:1cb50d31c3b5 1035 HoldStatus=VL6180x_SetGroupParamHold(dev, 0);
julientiron 0:1cb50d31c3b5 1036 if( !status)
julientiron 0:1cb50d31c3b5 1037 status=HoldStatus;
julientiron 0:1cb50d31c3b5 1038 }
julientiron 0:1cb50d31c3b5 1039 }
julientiron 0:1cb50d31c3b5 1040 while(0);
julientiron 0:1cb50d31c3b5 1041 }
julientiron 0:1cb50d31c3b5 1042
julientiron 0:1cb50d31c3b5 1043 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1044 return status;
julientiron 0:1cb50d31c3b5 1045 }
julientiron 0:1cb50d31c3b5 1046
julientiron 0:1cb50d31c3b5 1047
julientiron 0:1cb50d31c3b5 1048 int VL6180X::VL6180x_RangeGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high)
julientiron 0:1cb50d31c3b5 1049 {
julientiron 0:1cb50d31c3b5 1050 int status;
julientiron 0:1cb50d31c3b5 1051 uint8_t RawLow, RawHigh;
julientiron 0:1cb50d31c3b5 1052 int scale;
julientiron 0:1cb50d31c3b5 1053
julientiron 0:1cb50d31c3b5 1054 LOG_FUNCTION_START("%p %p", low , high);
julientiron 0:1cb50d31c3b5 1055
julientiron 0:1cb50d31c3b5 1056 scale=_GetUpscale(dev,UpscaleFactor);
julientiron 0:1cb50d31c3b5 1057 do{
julientiron 0:1cb50d31c3b5 1058 if( high != NULL ){
julientiron 0:1cb50d31c3b5 1059 status=VL6180x_RdByte(dev, SYSRANGE_THRESH_HIGH,&RawHigh);
julientiron 0:1cb50d31c3b5 1060 if( status ){
julientiron 0:1cb50d31c3b5 1061 VL6180x_ErrLog("rd SYSRANGE_THRESH_HIGH fail");
julientiron 0:1cb50d31c3b5 1062 break;
julientiron 0:1cb50d31c3b5 1063 }
julientiron 0:1cb50d31c3b5 1064 *high=(uint16_t)RawHigh*scale;
julientiron 0:1cb50d31c3b5 1065 }
julientiron 0:1cb50d31c3b5 1066 if( low != NULL ) {
julientiron 0:1cb50d31c3b5 1067 status=VL6180x_RdByte(dev, SYSRANGE_THRESH_LOW, &RawLow);
julientiron 0:1cb50d31c3b5 1068 if( status ){
julientiron 0:1cb50d31c3b5 1069 VL6180x_ErrLog("rd SYSRANGE_THRESH_LOW fail");
julientiron 0:1cb50d31c3b5 1070 break;
julientiron 0:1cb50d31c3b5 1071 }
julientiron 0:1cb50d31c3b5 1072 *low=(uint16_t)RawLow*scale;
julientiron 0:1cb50d31c3b5 1073 }
julientiron 0:1cb50d31c3b5 1074 }
julientiron 0:1cb50d31c3b5 1075 while(0);
julientiron 0:1cb50d31c3b5 1076 LOG_FUNCTION_END_FMT(status, "%d %d",(int)*low ,(int)*high);
julientiron 0:1cb50d31c3b5 1077 return status;
julientiron 0:1cb50d31c3b5 1078 }
julientiron 0:1cb50d31c3b5 1079
julientiron 0:1cb50d31c3b5 1080
julientiron 0:1cb50d31c3b5 1081 int VL6180X::VL6180x_RangeGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus) {
julientiron 0:1cb50d31c3b5 1082 int status;
julientiron 0:1cb50d31c3b5 1083 uint8_t IntStatus;
julientiron 0:1cb50d31c3b5 1084 LOG_FUNCTION_START("%p", pIntStatus);
julientiron 0:1cb50d31c3b5 1085 /* FIXME we are grouping "error" with over status the user must check implicitly for it
julientiron 0:1cb50d31c3b5 1086 * not just new sample or over status , that will nevr show up in case of error*/
julientiron 0:1cb50d31c3b5 1087 status = VL6180x_RdByte(dev, RESULT_INTERRUPT_STATUS_GPIO, &IntStatus);
julientiron 0:1cb50d31c3b5 1088 *pIntStatus= IntStatus&0xC7;
julientiron 0:1cb50d31c3b5 1089
julientiron 0:1cb50d31c3b5 1090 LOG_FUNCTION_END_FMT(status, "%d", (int)*pIntStatus);
julientiron 0:1cb50d31c3b5 1091 return status;
julientiron 0:1cb50d31c3b5 1092 }
julientiron 0:1cb50d31c3b5 1093
julientiron 0:1cb50d31c3b5 1094
julientiron 0:1cb50d31c3b5 1095 int VL6180X::VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *IntStatus)
julientiron 0:1cb50d31c3b5 1096 {
julientiron 0:1cb50d31c3b5 1097 int status;
julientiron 0:1cb50d31c3b5 1098 LOG_FUNCTION_START("%p" , IntStatus);
julientiron 0:1cb50d31c3b5 1099 status = VL6180x_RdByte(dev, RESULT_INTERRUPT_STATUS_GPIO, IntStatus);
julientiron 0:1cb50d31c3b5 1100 LOG_FUNCTION_END_FMT(status, "%d", (int)*IntStatus);
julientiron 0:1cb50d31c3b5 1101 return status;
julientiron 0:1cb50d31c3b5 1102 }
julientiron 0:1cb50d31c3b5 1103
julientiron 0:1cb50d31c3b5 1104 int VL6180X::VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear )
julientiron 0:1cb50d31c3b5 1105 {
julientiron 0:1cb50d31c3b5 1106 int status;
julientiron 0:1cb50d31c3b5 1107 LOG_FUNCTION_START("%d" ,(int)IntClear);
julientiron 0:1cb50d31c3b5 1108 if( IntClear <= 7 ){
julientiron 0:1cb50d31c3b5 1109 status=VL6180x_WrByte( dev, SYSTEM_INTERRUPT_CLEAR, IntClear);
julientiron 0:1cb50d31c3b5 1110 }
julientiron 0:1cb50d31c3b5 1111 else{
julientiron 0:1cb50d31c3b5 1112 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1113 }
julientiron 0:1cb50d31c3b5 1114 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1115 return status;
julientiron 0:1cb50d31c3b5 1116 }
julientiron 0:1cb50d31c3b5 1117
julientiron 0:1cb50d31c3b5 1118
julientiron 0:1cb50d31c3b5 1119 int VL6180X::VL6180x_RangeStaticInit(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1120 {
julientiron 0:1cb50d31c3b5 1121 int status;
julientiron 0:1cb50d31c3b5 1122 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1123
julientiron 0:1cb50d31c3b5 1124 /* REGISTER_TUNING_SR03_270514_CustomerView.txt */
julientiron 0:1cb50d31c3b5 1125 VL6180x_WrByte( dev, 0x0207, 0x01);
julientiron 0:1cb50d31c3b5 1126 VL6180x_WrByte( dev, 0x0208, 0x01);
julientiron 0:1cb50d31c3b5 1127 VL6180x_WrByte( dev, 0x0096, 0x00);
julientiron 0:1cb50d31c3b5 1128 VL6180x_WrByte( dev, 0x0097, 0xfd);
julientiron 0:1cb50d31c3b5 1129 VL6180x_WrByte( dev, 0x00e3, 0x00);
julientiron 0:1cb50d31c3b5 1130 VL6180x_WrByte( dev, 0x00e4, 0x04);
julientiron 0:1cb50d31c3b5 1131 VL6180x_WrByte( dev, 0x00e5, 0x02);
julientiron 0:1cb50d31c3b5 1132 VL6180x_WrByte( dev, 0x00e6, 0x01);
julientiron 0:1cb50d31c3b5 1133 VL6180x_WrByte( dev, 0x00e7, 0x03);
julientiron 0:1cb50d31c3b5 1134 VL6180x_WrByte( dev, 0x00f5, 0x02);
julientiron 0:1cb50d31c3b5 1135 VL6180x_WrByte( dev, 0x00d9, 0x05);
julientiron 0:1cb50d31c3b5 1136 VL6180x_WrByte( dev, 0x00db, 0xce);
julientiron 0:1cb50d31c3b5 1137 VL6180x_WrByte( dev, 0x00dc, 0x03);
julientiron 0:1cb50d31c3b5 1138 VL6180x_WrByte( dev, 0x00dd, 0xf8);
julientiron 0:1cb50d31c3b5 1139 VL6180x_WrByte( dev, 0x009f, 0x00);
julientiron 0:1cb50d31c3b5 1140 VL6180x_WrByte( dev, 0x00a3, 0x3c);
julientiron 0:1cb50d31c3b5 1141 VL6180x_WrByte( dev, 0x00b7, 0x00);
julientiron 0:1cb50d31c3b5 1142 VL6180x_WrByte( dev, 0x00bb, 0x3c);
julientiron 0:1cb50d31c3b5 1143 VL6180x_WrByte( dev, 0x00b2, 0x09);
julientiron 0:1cb50d31c3b5 1144 VL6180x_WrByte( dev, 0x00ca, 0x09);
julientiron 0:1cb50d31c3b5 1145 VL6180x_WrByte( dev, 0x0198, 0x01);
julientiron 0:1cb50d31c3b5 1146 VL6180x_WrByte( dev, 0x01b0, 0x17);
julientiron 0:1cb50d31c3b5 1147 VL6180x_WrByte( dev, 0x01ad, 0x00);
julientiron 0:1cb50d31c3b5 1148 VL6180x_WrByte( dev, 0x00ff, 0x05);
julientiron 0:1cb50d31c3b5 1149 VL6180x_WrByte( dev, 0x0100, 0x05);
julientiron 0:1cb50d31c3b5 1150 VL6180x_WrByte( dev, 0x0199, 0x05);
julientiron 0:1cb50d31c3b5 1151 VL6180x_WrByte( dev, 0x01a6, 0x1b);
julientiron 0:1cb50d31c3b5 1152 VL6180x_WrByte( dev, 0x01ac, 0x3e);
julientiron 0:1cb50d31c3b5 1153 VL6180x_WrByte( dev, 0x01a7, 0x1f);
julientiron 0:1cb50d31c3b5 1154 VL6180x_WrByte( dev, 0x0030, 0x00);
julientiron 0:1cb50d31c3b5 1155
julientiron 0:1cb50d31c3b5 1156 /* Recommended : Public registers - See data sheet for more detail */
julientiron 0:1cb50d31c3b5 1157 VL6180x_WrByte( dev, SYSTEM_MODE_GPIO1, 0x10); /* Enables polling for New Sample ready when measurement completes */
julientiron 0:1cb50d31c3b5 1158 VL6180x_WrByte( dev, READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); /* Set the averaging sample period (compromise between lower noise and increased execution time) */
julientiron 0:1cb50d31c3b5 1159 VL6180x_WrByte( dev, SYSALS_ANALOGUE_GAIN, 0x46); /* Sets the light and dark gain (upper nibble). Dark gain should not be changed.*/
julientiron 0:1cb50d31c3b5 1160 VL6180x_WrByte( dev, SYSRANGE_VHV_REPEAT_RATE, 0xFF); /* sets the # of range measurements after which auto calibration of system is performed */
julientiron 0:1cb50d31c3b5 1161 VL6180x_WrByte( dev, SYSALS_INTEGRATION_PERIOD, 0x63); /* Set ALS integration time to 100ms */
julientiron 0:1cb50d31c3b5 1162 VL6180x_WrByte( dev, SYSRANGE_VHV_RECALIBRATE, 0x01); /* perform a single temperature calibration of the ranging sensor */
julientiron 0:1cb50d31c3b5 1163
julientiron 0:1cb50d31c3b5 1164 /* Optional: Public registers - See data sheet for more detail */
julientiron 0:1cb50d31c3b5 1165 VL6180x_WrByte( dev, SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); /* Set default ranging inter-measurement period to 100ms */
julientiron 0:1cb50d31c3b5 1166 VL6180x_WrByte( dev, SYSALS_INTERMEASUREMENT_PERIOD, 0x31); /* Set default ALS inter-measurement period to 500ms */
julientiron 0:1cb50d31c3b5 1167 VL6180x_WrByte( dev, SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); /* Configures interrupt on New sample ready */
julientiron 0:1cb50d31c3b5 1168
julientiron 0:1cb50d31c3b5 1169
julientiron 0:1cb50d31c3b5 1170 status=VL6180x_RangeSetMaxConvergenceTime(dev, 50); /* Calculate ece value on initialization (use max conv) */
julientiron 0:1cb50d31c3b5 1171 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1172
julientiron 0:1cb50d31c3b5 1173 return status;
julientiron 0:1cb50d31c3b5 1174 }
julientiron 0:1cb50d31c3b5 1175
julientiron 0:1cb50d31c3b5 1176 #if VL6180x_UPSCALE_SUPPORT != 1
julientiron 0:1cb50d31c3b5 1177
julientiron 0:1cb50d31c3b5 1178 int VL6180X::_UpscaleInitPatch0(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 1179 int status;
julientiron 0:1cb50d31c3b5 1180 uint32_t CalValue=0;
julientiron 0:1cb50d31c3b5 1181 CalValue= VL6180xDevDataGet(dev, Part2PartAmbNVM);
julientiron 0:1cb50d31c3b5 1182 status=VL6180x_WrDWord( dev, 0xDA, CalValue);
julientiron 0:1cb50d31c3b5 1183 return status;
julientiron 0:1cb50d31c3b5 1184 }
julientiron 0:1cb50d31c3b5 1185
julientiron 0:1cb50d31c3b5 1186 /* only include up-scaling register setting when up-scale support is configured in */
julientiron 0:1cb50d31c3b5 1187 int VL6180X::VL6180x_UpscaleRegInit(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1188 {
julientiron 0:1cb50d31c3b5 1189 /* apply REGISTER_TUNING_ER02_100614_CustomerView.txt */
julientiron 0:1cb50d31c3b5 1190 VL6180x_WrByte( dev, 0x0207, 0x01);
julientiron 0:1cb50d31c3b5 1191 VL6180x_WrByte( dev, 0x0208, 0x01);
julientiron 0:1cb50d31c3b5 1192 VL6180x_WrByte( dev, 0x0096, 0x00);
julientiron 0:1cb50d31c3b5 1193 VL6180x_WrByte( dev, 0x0097, 0x54);
julientiron 0:1cb50d31c3b5 1194 VL6180x_WrByte( dev, 0x00e3, 0x00);
julientiron 0:1cb50d31c3b5 1195 VL6180x_WrByte( dev, 0x00e4, 0x04);
julientiron 0:1cb50d31c3b5 1196 VL6180x_WrByte( dev, 0x00e5, 0x02);
julientiron 0:1cb50d31c3b5 1197 VL6180x_WrByte( dev, 0x00e6, 0x01);
julientiron 0:1cb50d31c3b5 1198 VL6180x_WrByte( dev, 0x00e7, 0x03);
julientiron 0:1cb50d31c3b5 1199 VL6180x_WrByte( dev, 0x00f5, 0x02);
julientiron 0:1cb50d31c3b5 1200 VL6180x_WrByte( dev, 0x00d9, 0x05);
julientiron 0:1cb50d31c3b5 1201
julientiron 0:1cb50d31c3b5 1202 _UpscaleInitPatch0(dev);
julientiron 0:1cb50d31c3b5 1203
julientiron 0:1cb50d31c3b5 1204 VL6180x_WrByte( dev, 0x009f, 0x00);
julientiron 0:1cb50d31c3b5 1205 VL6180x_WrByte( dev, 0x00a3, 0x28);
julientiron 0:1cb50d31c3b5 1206 VL6180x_WrByte( dev, 0x00b7, 0x00);
julientiron 0:1cb50d31c3b5 1207 VL6180x_WrByte( dev, 0x00bb, 0x28);
julientiron 0:1cb50d31c3b5 1208 VL6180x_WrByte( dev, 0x00b2, 0x09);
julientiron 0:1cb50d31c3b5 1209 VL6180x_WrByte( dev, 0x00ca, 0x09);
julientiron 0:1cb50d31c3b5 1210 VL6180x_WrByte( dev, 0x0198, 0x01);
julientiron 0:1cb50d31c3b5 1211 VL6180x_WrByte( dev, 0x01b0, 0x17);
julientiron 0:1cb50d31c3b5 1212 VL6180x_WrByte( dev, 0x01ad, 0x00);
julientiron 0:1cb50d31c3b5 1213 VL6180x_WrByte( dev, 0x00ff, 0x05);
julientiron 0:1cb50d31c3b5 1214 VL6180x_WrByte( dev, 0x0100, 0x05);
julientiron 0:1cb50d31c3b5 1215 VL6180x_WrByte( dev, 0x0199, 0x05);
julientiron 0:1cb50d31c3b5 1216 VL6180x_WrByte( dev, 0x01a6, 0x1b);
julientiron 0:1cb50d31c3b5 1217 VL6180x_WrByte( dev, 0x01ac, 0x3e);
julientiron 0:1cb50d31c3b5 1218 VL6180x_WrByte( dev, 0x01a7, 0x1f);
julientiron 0:1cb50d31c3b5 1219 VL6180x_WrByte( dev, 0x0030, 0x00);
julientiron 0:1cb50d31c3b5 1220 VL6180x_WrByte( dev, SYSTEM_MODE_GPIO1, 0x10);
julientiron 0:1cb50d31c3b5 1221 VL6180x_WrByte( dev, READOUT_AVERAGING_SAMPLE_PERIOD, 0x30);
julientiron 0:1cb50d31c3b5 1222 VL6180x_WrByte( dev, SYSALS_ANALOGUE_GAIN, 0x46);
julientiron 0:1cb50d31c3b5 1223 VL6180x_WrByte( dev, SYSRANGE_VHV_REPEAT_RATE, 0xFF);
julientiron 0:1cb50d31c3b5 1224 VL6180x_WrByte( dev, SYSALS_INTEGRATION_PERIOD, 0x63);
julientiron 0:1cb50d31c3b5 1225 VL6180x_WrByte( dev, SYSRANGE_VHV_RECALIBRATE, 0x01);
julientiron 0:1cb50d31c3b5 1226 VL6180x_WrByte( dev, SYSRANGE_MAX_AMBIENT_LEVEL_MULT, 0xff);
julientiron 0:1cb50d31c3b5 1227 VL6180x_WrByte( dev, SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09);
julientiron 0:1cb50d31c3b5 1228 VL6180x_WrByte( dev, SYSALS_INTERMEASUREMENT_PERIOD, 0x31);
julientiron 0:1cb50d31c3b5 1229 VL6180x_WrByte( dev, SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24);
julientiron 0:1cb50d31c3b5 1230 #if VL6180x_EXTENDED_RANGE
julientiron 0:1cb50d31c3b5 1231 VL6180x_RangeSetMaxConvergenceTime(dev, 63);
julientiron 0:1cb50d31c3b5 1232 #else
julientiron 0:1cb50d31c3b5 1233 VL6180x_RangeSetMaxConvergenceTime(dev, 50);
julientiron 0:1cb50d31c3b5 1234 #endif
julientiron 0:1cb50d31c3b5 1235 return 0;
julientiron 0:1cb50d31c3b5 1236 }
julientiron 0:1cb50d31c3b5 1237 #else
julientiron 0:1cb50d31c3b5 1238 #define VL6180x_UpscaleRegInit(...) -1
julientiron 0:1cb50d31c3b5 1239 #endif
julientiron 0:1cb50d31c3b5 1240
julientiron 0:1cb50d31c3b5 1241 int VL6180X::VL6180x_UpscaleSetScaling(VL6180xDev_t dev, uint8_t scaling)
julientiron 0:1cb50d31c3b5 1242 {
julientiron 0:1cb50d31c3b5 1243 int status;
julientiron 0:1cb50d31c3b5 1244 uint16_t Scaler;
julientiron 0:1cb50d31c3b5 1245 int8_t Offset;
julientiron 0:1cb50d31c3b5 1246
julientiron 0:1cb50d31c3b5 1247 LOG_FUNCTION_START("%d",(int) scaling);
julientiron 0:1cb50d31c3b5 1248
julientiron 0:1cb50d31c3b5 1249 #ifdef VL6180x_HAVE_UPSCALE_DATA
julientiron 0:1cb50d31c3b5 1250 #define min_scaling 1
julientiron 0:1cb50d31c3b5 1251 #define max_scaling sizeof(ScalerLookUP)/sizeof(ScalerLookUP[0])
julientiron 0:1cb50d31c3b5 1252 #else
julientiron 0:1cb50d31c3b5 1253 /* we are in fixed config so only allow configured factor */
julientiron 0:1cb50d31c3b5 1254 #define min_scaling VL6180x_UPSCALE_SUPPORT
julientiron 0:1cb50d31c3b5 1255 #define max_scaling VL6180x_UPSCALE_SUPPORT
julientiron 0:1cb50d31c3b5 1256 #endif
julientiron 0:1cb50d31c3b5 1257
julientiron 0:1cb50d31c3b5 1258 if( scaling>=min_scaling && scaling<= max_scaling ){
julientiron 0:1cb50d31c3b5 1259
julientiron 0:1cb50d31c3b5 1260 Scaler = ScalerLookUP[scaling-1];
julientiron 0:1cb50d31c3b5 1261 status = VL6180x_WrWord(dev, RANGE_SCALER, Scaler);
julientiron 0:1cb50d31c3b5 1262 _SetUpscale(dev, scaling );
julientiron 0:1cb50d31c3b5 1263
julientiron 0:1cb50d31c3b5 1264 /* Apply scaling on part-2-part offset */
julientiron 0:1cb50d31c3b5 1265 Offset = VL6180xDevDataGet(dev, Part2PartOffsetNVM)/scaling;
julientiron 0:1cb50d31c3b5 1266 status = VL6180x_WrByte(dev, SYSRANGE_PART_TO_PART_RANGE_OFFSET, Offset);
julientiron 0:1cb50d31c3b5 1267 #if ! VL6180x_EXTENDED_RANGE
julientiron 0:1cb50d31c3b5 1268 if( status ==0 ){
julientiron 0:1cb50d31c3b5 1269 status = VL6180x_RangeSetEceState(dev, scaling == 1); /* enable ece only at 1x scaling */
julientiron 0:1cb50d31c3b5 1270 }
julientiron 0:1cb50d31c3b5 1271 if( status == 0 && !VL6180x_EXTENDED_RANGE && scaling!=1 ){
julientiron 0:1cb50d31c3b5 1272 status = NOT_GUARANTEED ;
julientiron 0:1cb50d31c3b5 1273 }
julientiron 0:1cb50d31c3b5 1274 #endif
julientiron 0:1cb50d31c3b5 1275 }
julientiron 0:1cb50d31c3b5 1276 else{
julientiron 0:1cb50d31c3b5 1277 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1278 }
julientiron 0:1cb50d31c3b5 1279 #undef min_scaling
julientiron 0:1cb50d31c3b5 1280 #undef max_scaling
julientiron 0:1cb50d31c3b5 1281 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1282 return status;
julientiron 0:1cb50d31c3b5 1283 }
julientiron 0:1cb50d31c3b5 1284
julientiron 0:1cb50d31c3b5 1285
julientiron 0:1cb50d31c3b5 1286 int VL6180X::VL6180x_UpscaleGetScaling(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1287 {
julientiron 0:1cb50d31c3b5 1288 int status;
julientiron 0:1cb50d31c3b5 1289 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1290 status=_GetUpscale(dev );
julientiron 0:1cb50d31c3b5 1291 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1292
julientiron 0:1cb50d31c3b5 1293 return status;
julientiron 0:1cb50d31c3b5 1294 }
julientiron 0:1cb50d31c3b5 1295
julientiron 0:1cb50d31c3b5 1296
julientiron 0:1cb50d31c3b5 1297 int VL6180X::VL6180x_UpscaleStaticInit(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1298 {
julientiron 0:1cb50d31c3b5 1299 /* todo make these a fail macro in case only 1x is suppoted */
julientiron 0:1cb50d31c3b5 1300 int status;
julientiron 0:1cb50d31c3b5 1301
julientiron 0:1cb50d31c3b5 1302 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1303 do{
julientiron 0:1cb50d31c3b5 1304 status=VL6180x_UpscaleRegInit(dev);
julientiron 0:1cb50d31c3b5 1305 if( status){
julientiron 0:1cb50d31c3b5 1306 VL6180x_ErrLog("regInit fail");
julientiron 0:1cb50d31c3b5 1307 break;
julientiron 0:1cb50d31c3b5 1308 }
julientiron 0:1cb50d31c3b5 1309 #if VL6180x_EXTENDED_RANGE
julientiron 0:1cb50d31c3b5 1310 status = VL6180x_RangeSetEceState(dev, 0);
julientiron 0:1cb50d31c3b5 1311 if( status){
julientiron 0:1cb50d31c3b5 1312 VL6180x_ErrLog("VL6180x_RangeSetEceState fail");
julientiron 0:1cb50d31c3b5 1313 break;
julientiron 0:1cb50d31c3b5 1314 }
julientiron 0:1cb50d31c3b5 1315 #endif
julientiron 0:1cb50d31c3b5 1316 } while(0);
julientiron 0:1cb50d31c3b5 1317 if( !status){
julientiron 0:1cb50d31c3b5 1318 /* must write the scaler at least once to the device to ensure the scaler is in a known state. */
julientiron 0:1cb50d31c3b5 1319 status=VL6180x_UpscaleSetScaling(dev, _GetUpscale(dev));
julientiron 0:1cb50d31c3b5 1320 VL6180x_WrByte( dev, SYSTEM_FRESH_OUT_OF_RESET, 0x00); /* change fresh out of set status to 0 */
julientiron 0:1cb50d31c3b5 1321 }
julientiron 0:1cb50d31c3b5 1322 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1323 return status;
julientiron 0:1cb50d31c3b5 1324 }
julientiron 0:1cb50d31c3b5 1325
julientiron 0:1cb50d31c3b5 1326
julientiron 0:1cb50d31c3b5 1327 int VL6180X::VL6180x_SetGPIOxPolarity(VL6180xDev_t dev, int pin, int active_high)
julientiron 0:1cb50d31c3b5 1328 {
julientiron 0:1cb50d31c3b5 1329 int status;
julientiron 0:1cb50d31c3b5 1330 LOG_FUNCTION_START("%d %d",(int) pin, (int)active_high);
julientiron 0:1cb50d31c3b5 1331
julientiron 0:1cb50d31c3b5 1332 if( pin ==0 || pin ==1 ){
julientiron 0:1cb50d31c3b5 1333 uint16_t RegIndex;
julientiron 0:1cb50d31c3b5 1334 uint8_t DataSet;
julientiron 0:1cb50d31c3b5 1335 if( pin==0 )
julientiron 0:1cb50d31c3b5 1336 RegIndex= SYSTEM_MODE_GPIO0;
julientiron 0:1cb50d31c3b5 1337 else
julientiron 0:1cb50d31c3b5 1338 RegIndex= SYSTEM_MODE_GPIO1;
julientiron 0:1cb50d31c3b5 1339
julientiron 0:1cb50d31c3b5 1340 if (active_high )
julientiron 0:1cb50d31c3b5 1341 DataSet = GPIOx_POLARITY_SELECT_MASK;
julientiron 0:1cb50d31c3b5 1342 else
julientiron 0:1cb50d31c3b5 1343 DataSet = 0;
julientiron 0:1cb50d31c3b5 1344
julientiron 0:1cb50d31c3b5 1345 status = VL6180x_UpdateByte(dev, RegIndex, (uint8_t)~GPIOx_POLARITY_SELECT_MASK, DataSet);
julientiron 0:1cb50d31c3b5 1346 }
julientiron 0:1cb50d31c3b5 1347 else{
julientiron 0:1cb50d31c3b5 1348 VL6180x_ErrLog("Invalid pin param %d", (int)pin);
julientiron 0:1cb50d31c3b5 1349 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1350 }
julientiron 0:1cb50d31c3b5 1351
julientiron 0:1cb50d31c3b5 1352 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1353
julientiron 0:1cb50d31c3b5 1354 return status;
julientiron 0:1cb50d31c3b5 1355 }
julientiron 0:1cb50d31c3b5 1356
julientiron 0:1cb50d31c3b5 1357 int VL6180X::VL6180x_SetGPIOxFunctionality(VL6180xDev_t dev, int pin, uint8_t functionality)
julientiron 0:1cb50d31c3b5 1358 {
julientiron 0:1cb50d31c3b5 1359 int status;
julientiron 0:1cb50d31c3b5 1360
julientiron 0:1cb50d31c3b5 1361 LOG_FUNCTION_START("%d %d",(int) pin, (int)functionality);
julientiron 0:1cb50d31c3b5 1362
julientiron 0:1cb50d31c3b5 1363 if( ((pin ==0) || (pin ==1)) && IsValidGPIOFunction(functionality) ){
julientiron 0:1cb50d31c3b5 1364 uint16_t RegIndex;
julientiron 0:1cb50d31c3b5 1365
julientiron 0:1cb50d31c3b5 1366 if( pin==0 )
julientiron 0:1cb50d31c3b5 1367 RegIndex= SYSTEM_MODE_GPIO0;
julientiron 0:1cb50d31c3b5 1368 else
julientiron 0:1cb50d31c3b5 1369 RegIndex= SYSTEM_MODE_GPIO1;
julientiron 0:1cb50d31c3b5 1370
julientiron 0:1cb50d31c3b5 1371 status = VL6180x_UpdateByte(dev, RegIndex, (uint8_t)~GPIOx_FUNCTIONALITY_SELECT_MASK, functionality<<GPIOx_FUNCTIONALITY_SELECT_SHIFT);
julientiron 0:1cb50d31c3b5 1372 if( status){
julientiron 0:1cb50d31c3b5 1373 VL6180x_ErrLog("Update SYSTEM_MODE_GPIO%d fail", (int)pin);
julientiron 0:1cb50d31c3b5 1374 }
julientiron 0:1cb50d31c3b5 1375 }
julientiron 0:1cb50d31c3b5 1376 else{
julientiron 0:1cb50d31c3b5 1377 VL6180x_ErrLog("Invalid pin %d or function %d", (int)pin, (int) functionality);
julientiron 0:1cb50d31c3b5 1378 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1379 }
julientiron 0:1cb50d31c3b5 1380
julientiron 0:1cb50d31c3b5 1381 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1382 return status;
julientiron 0:1cb50d31c3b5 1383 }
julientiron 0:1cb50d31c3b5 1384
julientiron 0:1cb50d31c3b5 1385
julientiron 0:1cb50d31c3b5 1386 int VL6180X::VL6180x_SetupGPIOx(VL6180xDev_t dev, int pin, uint8_t IntFunction, int ActiveHigh)
julientiron 0:1cb50d31c3b5 1387 {
julientiron 0:1cb50d31c3b5 1388 int status;
julientiron 0:1cb50d31c3b5 1389
julientiron 0:1cb50d31c3b5 1390 LOG_FUNCTION_START("%d %d",(int) pin, (int)IntFunction);
julientiron 0:1cb50d31c3b5 1391
julientiron 0:1cb50d31c3b5 1392 if( ((pin ==0) || (pin ==1)) && IsValidGPIOFunction(IntFunction) ){
julientiron 0:1cb50d31c3b5 1393 uint16_t RegIndex;
julientiron 0:1cb50d31c3b5 1394 uint8_t value=0;
julientiron 0:1cb50d31c3b5 1395
julientiron 0:1cb50d31c3b5 1396 if( pin==0 )
julientiron 0:1cb50d31c3b5 1397 RegIndex= SYSTEM_MODE_GPIO0;
julientiron 0:1cb50d31c3b5 1398 else
julientiron 0:1cb50d31c3b5 1399 RegIndex= SYSTEM_MODE_GPIO1;
julientiron 0:1cb50d31c3b5 1400
julientiron 0:1cb50d31c3b5 1401 if( ActiveHigh )
julientiron 0:1cb50d31c3b5 1402 value|=GPIOx_POLARITY_SELECT_MASK;
julientiron 0:1cb50d31c3b5 1403
julientiron 0:1cb50d31c3b5 1404 value |= IntFunction<<GPIOx_FUNCTIONALITY_SELECT_SHIFT;
julientiron 0:1cb50d31c3b5 1405 status = VL6180x_WrByte(dev, RegIndex, value);
julientiron 0:1cb50d31c3b5 1406 if( status ){
julientiron 0:1cb50d31c3b5 1407 VL6180x_ErrLog("SYSTEM_MODE_GPIO%d wr fail", (int)pin-SYSTEM_MODE_GPIO0);
julientiron 0:1cb50d31c3b5 1408 }
julientiron 0:1cb50d31c3b5 1409 }
julientiron 0:1cb50d31c3b5 1410 else{
julientiron 0:1cb50d31c3b5 1411 VL6180x_ErrLog("Invalid pin %d or function %d", (int)pin, (int) IntFunction);
julientiron 0:1cb50d31c3b5 1412 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1413 }
julientiron 0:1cb50d31c3b5 1414
julientiron 0:1cb50d31c3b5 1415 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1416 return status;
julientiron 0:1cb50d31c3b5 1417 }
julientiron 0:1cb50d31c3b5 1418
julientiron 0:1cb50d31c3b5 1419
julientiron 0:1cb50d31c3b5 1420 int VL6180X::VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin) {
julientiron 0:1cb50d31c3b5 1421 int status;
julientiron 0:1cb50d31c3b5 1422
julientiron 0:1cb50d31c3b5 1423 LOG_FUNCTION_START("%d",(int)pin);
julientiron 0:1cb50d31c3b5 1424
julientiron 0:1cb50d31c3b5 1425 status=VL6180x_SetGPIOxFunctionality(dev, pin, GPIOx_SELECT_OFF);
julientiron 0:1cb50d31c3b5 1426
julientiron 0:1cb50d31c3b5 1427 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1428 return status;
julientiron 0:1cb50d31c3b5 1429 }
julientiron 0:1cb50d31c3b5 1430
julientiron 0:1cb50d31c3b5 1431
julientiron 0:1cb50d31c3b5 1432 int VL6180X::VL6180x_SetupGPIO1(VL6180xDev_t dev, uint8_t IntFunction, int ActiveHigh)
julientiron 0:1cb50d31c3b5 1433 {
julientiron 0:1cb50d31c3b5 1434 int status;
julientiron 0:1cb50d31c3b5 1435 LOG_FUNCTION_START("%d %d",(int)IntFunction, (int)ActiveHigh );
julientiron 0:1cb50d31c3b5 1436 status=VL6180x_SetupGPIOx(dev, 1 , IntFunction, ActiveHigh);
julientiron 0:1cb50d31c3b5 1437 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1438 return status;
julientiron 0:1cb50d31c3b5 1439 }
julientiron 0:1cb50d31c3b5 1440
julientiron 0:1cb50d31c3b5 1441 int VL6180X::VL6180x_RangeConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt)
julientiron 0:1cb50d31c3b5 1442 {
julientiron 0:1cb50d31c3b5 1443 int status;
julientiron 0:1cb50d31c3b5 1444
julientiron 0:1cb50d31c3b5 1445 if( ConfigGpioInt<= CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY){
julientiron 0:1cb50d31c3b5 1446 status = VL6180x_UpdateByte(dev, SYSTEM_INTERRUPT_CONFIG_GPIO, (uint8_t)(~CONFIG_GPIO_RANGE_MASK), ConfigGpioInt);
julientiron 0:1cb50d31c3b5 1447 }
julientiron 0:1cb50d31c3b5 1448 else{
julientiron 0:1cb50d31c3b5 1449 VL6180x_ErrLog("Invalid config mode param %d", (int)ConfigGpioInt);
julientiron 0:1cb50d31c3b5 1450 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1451 }
julientiron 0:1cb50d31c3b5 1452 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1453 return status;
julientiron 0:1cb50d31c3b5 1454 }
julientiron 0:1cb50d31c3b5 1455
julientiron 0:1cb50d31c3b5 1456
julientiron 0:1cb50d31c3b5 1457 int VL6180X::VL6180x_RangeSetEceFactor(VL6180xDev_t dev, uint16_t FactorM, uint16_t FactorD){
julientiron 0:1cb50d31c3b5 1458 int status;
julientiron 0:1cb50d31c3b5 1459 uint8_t u8;
julientiron 0:1cb50d31c3b5 1460
julientiron 0:1cb50d31c3b5 1461 LOG_FUNCTION_START("%d %d", (int)FactorM, (int)FactorD );
julientiron 0:1cb50d31c3b5 1462 do{
julientiron 0:1cb50d31c3b5 1463 /* D cannot be 0 M must be <=D and >= 0 */
julientiron 0:1cb50d31c3b5 1464 if( FactorM <= FactorD && FactorD> 0){
julientiron 0:1cb50d31c3b5 1465 VL6180xDevDataSet(dev, EceFactorM, FactorM);
julientiron 0:1cb50d31c3b5 1466 VL6180xDevDataSet(dev, EceFactorD, FactorD);
julientiron 0:1cb50d31c3b5 1467 /* read and re-apply max conv time to get new ece factor set */
julientiron 0:1cb50d31c3b5 1468 status = VL6180x_RdByte(dev, SYSRANGE_MAX_CONVERGENCE_TIME, &u8);
julientiron 0:1cb50d31c3b5 1469 if( status){
julientiron 0:1cb50d31c3b5 1470 VL6180x_ErrLog("SYSRANGE_MAX_CONVERGENCE_TIME rd fail ");
julientiron 0:1cb50d31c3b5 1471 break;
julientiron 0:1cb50d31c3b5 1472 }
julientiron 0:1cb50d31c3b5 1473 status = VL6180x_RangeSetMaxConvergenceTime(dev, u8);
julientiron 0:1cb50d31c3b5 1474 if( status <0 ){
julientiron 0:1cb50d31c3b5 1475 VL6180x_ErrLog("fail to apply time after ece m/d change");
julientiron 0:1cb50d31c3b5 1476 break;
julientiron 0:1cb50d31c3b5 1477 }
julientiron 0:1cb50d31c3b5 1478 }
julientiron 0:1cb50d31c3b5 1479 else{
julientiron 0:1cb50d31c3b5 1480 VL6180x_ErrLog("invalid factor %d/%d", (int)FactorM, (int)FactorD );
julientiron 0:1cb50d31c3b5 1481 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1482 }
julientiron 0:1cb50d31c3b5 1483 }
julientiron 0:1cb50d31c3b5 1484 while(0);
julientiron 0:1cb50d31c3b5 1485 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1486 return status;
julientiron 0:1cb50d31c3b5 1487 }
julientiron 0:1cb50d31c3b5 1488
julientiron 0:1cb50d31c3b5 1489 int VL6180X::VL6180x_RangeSetEceState(VL6180xDev_t dev, int enable ){
julientiron 0:1cb50d31c3b5 1490 int status;
julientiron 0:1cb50d31c3b5 1491 uint8_t or_mask;
julientiron 0:1cb50d31c3b5 1492
julientiron 0:1cb50d31c3b5 1493 LOG_FUNCTION_START("%d", (int)enable);
julientiron 0:1cb50d31c3b5 1494 if( enable )
julientiron 0:1cb50d31c3b5 1495 or_mask = RANGE_CHECK_ECE_ENABLE_MASK;
julientiron 0:1cb50d31c3b5 1496 else
julientiron 0:1cb50d31c3b5 1497 or_mask = 0;
julientiron 0:1cb50d31c3b5 1498
julientiron 0:1cb50d31c3b5 1499 status =VL6180x_UpdateByte(dev, SYSRANGE_RANGE_CHECK_ENABLES, ~RANGE_CHECK_ECE_ENABLE_MASK, or_mask);
julientiron 0:1cb50d31c3b5 1500 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1501 return status;
julientiron 0:1cb50d31c3b5 1502 }
julientiron 0:1cb50d31c3b5 1503
julientiron 0:1cb50d31c3b5 1504
julientiron 0:1cb50d31c3b5 1505 int VL6180X::VL6180x_RangeSetMaxConvergenceTime(VL6180xDev_t dev, uint8_t MaxConTime_msec)
julientiron 0:1cb50d31c3b5 1506 {
julientiron 0:1cb50d31c3b5 1507 int status = 0;
julientiron 0:1cb50d31c3b5 1508 LOG_FUNCTION_START("%d",(int)MaxConTime_msec);
julientiron 0:1cb50d31c3b5 1509 do{
julientiron 0:1cb50d31c3b5 1510 status=VL6180x_WrByte(dev, SYSRANGE_MAX_CONVERGENCE_TIME, MaxConTime_msec);
julientiron 0:1cb50d31c3b5 1511 if( status ){
julientiron 0:1cb50d31c3b5 1512 break;
julientiron 0:1cb50d31c3b5 1513 }
julientiron 0:1cb50d31c3b5 1514 status=VL6180x_RangeSetEarlyConvergenceEestimateThreshold(dev);
julientiron 0:1cb50d31c3b5 1515 if( status){
julientiron 0:1cb50d31c3b5 1516 break;
julientiron 0:1cb50d31c3b5 1517 }
julientiron 0:1cb50d31c3b5 1518 status = _DMax_InitData(dev);
julientiron 0:1cb50d31c3b5 1519 }
julientiron 0:1cb50d31c3b5 1520 while(0);
julientiron 0:1cb50d31c3b5 1521 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1522 return status;
julientiron 0:1cb50d31c3b5 1523 }
julientiron 0:1cb50d31c3b5 1524
julientiron 0:1cb50d31c3b5 1525 int VL6180X::VL6180x_RangeSetInterMeasPeriod(VL6180xDev_t dev, uint32_t InterMeasTime_msec){
julientiron 0:1cb50d31c3b5 1526 uint8_t SetTime;
julientiron 0:1cb50d31c3b5 1527 int status;
julientiron 0:1cb50d31c3b5 1528
julientiron 0:1cb50d31c3b5 1529 LOG_FUNCTION_START("%d",(int)InterMeasTime_msec);
julientiron 0:1cb50d31c3b5 1530 do {
julientiron 0:1cb50d31c3b5 1531 if( InterMeasTime_msec > 2550 ){
julientiron 0:1cb50d31c3b5 1532 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1533 break;
julientiron 0:1cb50d31c3b5 1534 }
julientiron 0:1cb50d31c3b5 1535 /* doc in not 100% clear and confusing about the limit practically all value are OK but 0
julientiron 0:1cb50d31c3b5 1536 * that can hang device in continuous mode */
julientiron 0:1cb50d31c3b5 1537 if( InterMeasTime_msec < 10 ) {
julientiron 0:1cb50d31c3b5 1538 InterMeasTime_msec=10;
julientiron 0:1cb50d31c3b5 1539 }
julientiron 0:1cb50d31c3b5 1540 SetTime=(uint8_t)(InterMeasTime_msec/10);
julientiron 0:1cb50d31c3b5 1541 status=VL6180x_WrByte(dev, SYSRANGE_INTERMEASUREMENT_PERIOD, SetTime);
julientiron 0:1cb50d31c3b5 1542 if( status ){
julientiron 0:1cb50d31c3b5 1543 VL6180x_ErrLog("SYSRANGE_INTERMEASUREMENT_PERIOD wr fail");
julientiron 0:1cb50d31c3b5 1544 }
julientiron 0:1cb50d31c3b5 1545 else
julientiron 0:1cb50d31c3b5 1546 if( SetTime != InterMeasTime_msec /10 ) {
julientiron 0:1cb50d31c3b5 1547 status = MIN_CLIPED; /* on success change status to clip if it did */
julientiron 0:1cb50d31c3b5 1548 }
julientiron 0:1cb50d31c3b5 1549 }while(0);
julientiron 0:1cb50d31c3b5 1550 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1551 return status;
julientiron 0:1cb50d31c3b5 1552 }
julientiron 0:1cb50d31c3b5 1553
julientiron 0:1cb50d31c3b5 1554
julientiron 0:1cb50d31c3b5 1555 int VL6180X::VL6180x_RangeGetDeviceReady(VL6180xDev_t dev, int * Ready){
julientiron 0:1cb50d31c3b5 1556 int status;
julientiron 0:1cb50d31c3b5 1557 uint8_t u8;
julientiron 0:1cb50d31c3b5 1558 LOG_FUNCTION_START("%p", (int)Ready);
julientiron 0:1cb50d31c3b5 1559 status=VL6180x_RdByte(dev, RESULT_RANGE_STATUS, &u8);
julientiron 0:1cb50d31c3b5 1560 if( !status)
julientiron 0:1cb50d31c3b5 1561 *Ready = u8&RANGE_DEVICE_READY_MASK;
julientiron 0:1cb50d31c3b5 1562 LOG_FUNCTION_END_FMT(status,"%d", *Ready);
julientiron 0:1cb50d31c3b5 1563 return status;
julientiron 0:1cb50d31c3b5 1564 }
julientiron 0:1cb50d31c3b5 1565
julientiron 0:1cb50d31c3b5 1566
julientiron 0:1cb50d31c3b5 1567 int VL6180X::VL6180x_RangeWaitDeviceReady(VL6180xDev_t dev, int MaxLoop ){
julientiron 0:1cb50d31c3b5 1568 int status; /* if user specify an invalid <=0 loop count we'll return error */
julientiron 0:1cb50d31c3b5 1569 int n;
julientiron 0:1cb50d31c3b5 1570 uint8_t u8;
julientiron 0:1cb50d31c3b5 1571 LOG_FUNCTION_START("%d", (int)MaxLoop);
julientiron 0:1cb50d31c3b5 1572 if( MaxLoop<1){
julientiron 0:1cb50d31c3b5 1573 status=INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1574 }
julientiron 0:1cb50d31c3b5 1575 else{
julientiron 0:1cb50d31c3b5 1576 for( n=0; n < MaxLoop ; n++){
julientiron 0:1cb50d31c3b5 1577 status=VL6180x_RdByte(dev, RESULT_RANGE_STATUS, &u8);
julientiron 0:1cb50d31c3b5 1578 if( status)
julientiron 0:1cb50d31c3b5 1579 break;
julientiron 0:1cb50d31c3b5 1580 u8 = u8 & RANGE_DEVICE_READY_MASK;
julientiron 0:1cb50d31c3b5 1581 if( u8 )
julientiron 0:1cb50d31c3b5 1582 break;
julientiron 0:1cb50d31c3b5 1583
julientiron 0:1cb50d31c3b5 1584 }
julientiron 0:1cb50d31c3b5 1585 if( !status && !u8 ){
julientiron 0:1cb50d31c3b5 1586 status = TIME_OUT;
julientiron 0:1cb50d31c3b5 1587 }
julientiron 0:1cb50d31c3b5 1588 }
julientiron 0:1cb50d31c3b5 1589 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1590 return status;
julientiron 0:1cb50d31c3b5 1591 }
julientiron 0:1cb50d31c3b5 1592
julientiron 0:1cb50d31c3b5 1593 int VL6180X::VL6180x_RangeSetSystemMode(VL6180xDev_t dev, uint8_t mode)
julientiron 0:1cb50d31c3b5 1594 {
julientiron 0:1cb50d31c3b5 1595 int status;
julientiron 0:1cb50d31c3b5 1596 LOG_FUNCTION_START("%d", (int)mode);
julientiron 0:1cb50d31c3b5 1597 /* FIXME we are not checking device is ready via @a VL6180x_RangeWaitDeviceReady
julientiron 0:1cb50d31c3b5 1598 * so if called back to back real fast we are not checking
julientiron 0:1cb50d31c3b5 1599 * if previous mode "set" got absorbed => bit 0 must be 0 so that it work
julientiron 0:1cb50d31c3b5 1600 */
julientiron 0:1cb50d31c3b5 1601 if( mode <= 3){
julientiron 0:1cb50d31c3b5 1602 status=VL6180x_WrByte(dev, SYSRANGE_START, mode);
julientiron 0:1cb50d31c3b5 1603 if( status ){
julientiron 0:1cb50d31c3b5 1604 VL6180x_ErrLog("SYSRANGE_START wr fail");
julientiron 0:1cb50d31c3b5 1605 }
julientiron 0:1cb50d31c3b5 1606 }
julientiron 0:1cb50d31c3b5 1607 else{
julientiron 0:1cb50d31c3b5 1608 status = INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 1609 }
julientiron 0:1cb50d31c3b5 1610 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1611 return status;
julientiron 0:1cb50d31c3b5 1612 }
julientiron 0:1cb50d31c3b5 1613
julientiron 0:1cb50d31c3b5 1614
julientiron 0:1cb50d31c3b5 1615 int VL6180X::VL6180x_RangeStartContinuousMode(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1616 {
julientiron 0:1cb50d31c3b5 1617 int status;
julientiron 0:1cb50d31c3b5 1618 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1619 status= VL6180x_RangeSetSystemMode(dev, MODE_START_STOP | MODE_CONTINUOUS);
julientiron 0:1cb50d31c3b5 1620 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1621 return status;
julientiron 0:1cb50d31c3b5 1622 }
julientiron 0:1cb50d31c3b5 1623
julientiron 0:1cb50d31c3b5 1624 int VL6180X::VL6180x_RangeStartSingleShot(VL6180xDev_t dev) {
julientiron 0:1cb50d31c3b5 1625 int status;
julientiron 0:1cb50d31c3b5 1626 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1627 status = VL6180x_RangeSetSystemMode(dev, MODE_START_STOP|MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 1628 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1629 return status;
julientiron 0:1cb50d31c3b5 1630 }
julientiron 0:1cb50d31c3b5 1631
julientiron 0:1cb50d31c3b5 1632
julientiron 0:1cb50d31c3b5 1633 int VL6180X::VL6180x_RangeSetEarlyConvergenceEestimateThreshold(VL6180xDev_t dev)
julientiron 0:1cb50d31c3b5 1634 {
julientiron 0:1cb50d31c3b5 1635 int status;
julientiron 0:1cb50d31c3b5 1636
julientiron 0:1cb50d31c3b5 1637 const uint32_t cMicroSecPerMilliSec = 1000;
julientiron 0:1cb50d31c3b5 1638 const uint32_t cEceSampleTime_us = 500;
julientiron 0:1cb50d31c3b5 1639 uint32_t ece_factor_m = VL6180xDevDataGet(dev, EceFactorM);
julientiron 0:1cb50d31c3b5 1640 uint32_t ece_factor_d = VL6180xDevDataGet(dev, EceFactorD);
julientiron 0:1cb50d31c3b5 1641 uint32_t convergTime_us;
julientiron 0:1cb50d31c3b5 1642 uint32_t fineThresh;
julientiron 0:1cb50d31c3b5 1643 uint32_t eceThresh;
julientiron 0:1cb50d31c3b5 1644 uint8_t u8;
julientiron 0:1cb50d31c3b5 1645 uint32_t maxConv_ms;
julientiron 0:1cb50d31c3b5 1646 int32_t AveTime;
julientiron 0:1cb50d31c3b5 1647
julientiron 0:1cb50d31c3b5 1648 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1649
julientiron 0:1cb50d31c3b5 1650 do{
julientiron 0:1cb50d31c3b5 1651 status = VL6180x_RdByte(dev, SYSRANGE_MAX_CONVERGENCE_TIME, &u8);
julientiron 0:1cb50d31c3b5 1652 if( status ){
julientiron 0:1cb50d31c3b5 1653 VL6180x_ErrLog("SYSRANGE_MAX_CONVERGENCE_TIME rd fail");
julientiron 0:1cb50d31c3b5 1654 break;
julientiron 0:1cb50d31c3b5 1655 }
julientiron 0:1cb50d31c3b5 1656 maxConv_ms = u8;
julientiron 0:1cb50d31c3b5 1657 AveTime = _GetAveTotalTime(dev);
julientiron 0:1cb50d31c3b5 1658 if( AveTime <0 ){
julientiron 0:1cb50d31c3b5 1659 status=-1;
julientiron 0:1cb50d31c3b5 1660 break;
julientiron 0:1cb50d31c3b5 1661 }
julientiron 0:1cb50d31c3b5 1662
julientiron 0:1cb50d31c3b5 1663 convergTime_us = maxConv_ms * cMicroSecPerMilliSec - AveTime;
julientiron 0:1cb50d31c3b5 1664 status = VL6180x_RdDWord(dev, 0xB8, &fineThresh);
julientiron 0:1cb50d31c3b5 1665 if( status ) {
julientiron 0:1cb50d31c3b5 1666 VL6180x_ErrLog("reg 0xB8 rd fail");
julientiron 0:1cb50d31c3b5 1667 break;
julientiron 0:1cb50d31c3b5 1668 }
julientiron 0:1cb50d31c3b5 1669 fineThresh*=256;
julientiron 0:1cb50d31c3b5 1670 eceThresh = ece_factor_m * cEceSampleTime_us * fineThresh/(convergTime_us * ece_factor_d);
julientiron 0:1cb50d31c3b5 1671
julientiron 0:1cb50d31c3b5 1672 status=VL6180x_WrWord(dev, SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, (uint16_t)eceThresh);
julientiron 0:1cb50d31c3b5 1673 }
julientiron 0:1cb50d31c3b5 1674 while(0);
julientiron 0:1cb50d31c3b5 1675
julientiron 0:1cb50d31c3b5 1676 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 1677 return status;
julientiron 0:1cb50d31c3b5 1678 }
julientiron 0:1cb50d31c3b5 1679
julientiron 0:1cb50d31c3b5 1680 /*
julientiron 0:1cb50d31c3b5 1681 * Return >0 = time
julientiron 0:1cb50d31c3b5 1682 * <0 1 if fail to get read data from device to compute time
julientiron 0:1cb50d31c3b5 1683 */
julientiron 0:1cb50d31c3b5 1684 int32_t VL6180X::_GetAveTotalTime(VL6180xDev_t dev) {
julientiron 0:1cb50d31c3b5 1685 uint32_t cFwOverhead_us = 24;
julientiron 0:1cb50d31c3b5 1686 uint32_t cVcpSetupTime_us = 70;
julientiron 0:1cb50d31c3b5 1687 uint32_t cPLL2_StartupDelay_us = 200;
julientiron 0:1cb50d31c3b5 1688 uint8_t cMeasMask = 0x07;
julientiron 0:1cb50d31c3b5 1689 uint32_t Samples;
julientiron 0:1cb50d31c3b5 1690 uint32_t SamplePeriod;
julientiron 0:1cb50d31c3b5 1691 uint32_t SingleTime_us;
julientiron 0:1cb50d31c3b5 1692 int32_t TotalAveTime_us;
julientiron 0:1cb50d31c3b5 1693 uint8_t u8;
julientiron 0:1cb50d31c3b5 1694 int status;
julientiron 0:1cb50d31c3b5 1695
julientiron 0:1cb50d31c3b5 1696 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 1697
julientiron 0:1cb50d31c3b5 1698 status = VL6180x_RdByte(dev, 0x109, &u8);
julientiron 0:1cb50d31c3b5 1699 if (status) {
julientiron 0:1cb50d31c3b5 1700 VL6180x_ErrLog("rd 0x109 fail");
julientiron 0:1cb50d31c3b5 1701 return -1;
julientiron 0:1cb50d31c3b5 1702 }
julientiron 0:1cb50d31c3b5 1703 Samples = u8 & cMeasMask;
julientiron 0:1cb50d31c3b5 1704 status = VL6180x_RdByte(dev, READOUT_AVERAGING_SAMPLE_PERIOD, &u8);
julientiron 0:1cb50d31c3b5 1705 if (status) {
julientiron 0:1cb50d31c3b5 1706 VL6180x_ErrLog("i2c READOUT_AVERAGING_SAMPLE_PERIOD fail");
julientiron 0:1cb50d31c3b5 1707 return -1;
julientiron 0:1cb50d31c3b5 1708 }
julientiron 0:1cb50d31c3b5 1709 SamplePeriod = u8;
julientiron 0:1cb50d31c3b5 1710 SingleTime_us = cFwOverhead_us + cVcpSetupTime_us + (SamplePeriod * 10);
julientiron 0:1cb50d31c3b5 1711 TotalAveTime_us = (Samples + 1) * SingleTime_us + cPLL2_StartupDelay_us;
julientiron 0:1cb50d31c3b5 1712
julientiron 0:1cb50d31c3b5 1713 LOG_FUNCTION_END(TotalAveTime_us);
julientiron 0:1cb50d31c3b5 1714 return TotalAveTime_us;
julientiron 0:1cb50d31c3b5 1715 }
julientiron 0:1cb50d31c3b5 1716
julientiron 0:1cb50d31c3b5 1717 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 1718 #define _GetDMaxDataRetSignalAt400mm(dev) VL6180xDevDataGet(dev, DMaxData.retSignalAt400mm)
julientiron 0:1cb50d31c3b5 1719 #else
julientiron 0:1cb50d31c3b5 1720 #define _GetDMaxDataRetSignalAt400mm(dev) 375 // Use a default high value
julientiron 0:1cb50d31c3b5 1721 #endif
julientiron 0:1cb50d31c3b5 1722
julientiron 0:1cb50d31c3b5 1723
julientiron 0:1cb50d31c3b5 1724 #if VL6180x_WRAP_AROUND_FILTER_SUPPORT
julientiron 0:1cb50d31c3b5 1725
julientiron 0:1cb50d31c3b5 1726 #define FILTER_STDDEV_SAMPLES 6
julientiron 0:1cb50d31c3b5 1727 #define MIN_FILTER_STDDEV_SAMPLES 3
julientiron 0:1cb50d31c3b5 1728 #define MIN_FILTER_VALID_STDDEV_SAMPLES 3
julientiron 0:1cb50d31c3b5 1729 #define FILTER_INVALID_DISTANCE 65535
julientiron 0:1cb50d31c3b5 1730
julientiron 0:1cb50d31c3b5 1731 #define _FilterData(field) VL6180xDevDataGet(dev, FilterData.field)
julientiron 0:1cb50d31c3b5 1732 /*
julientiron 0:1cb50d31c3b5 1733 * One time init
julientiron 0:1cb50d31c3b5 1734 */
julientiron 0:1cb50d31c3b5 1735 static int _filter_Init( VL6180xDev_t dev) {
julientiron 0:1cb50d31c3b5 1736 int i;
julientiron 0:1cb50d31c3b5 1737 _FilterData(MeasurementIndex) = 0;
julientiron 0:1cb50d31c3b5 1738
julientiron 0:1cb50d31c3b5 1739 _FilterData(Default_ZeroVal) = 0;
julientiron 0:1cb50d31c3b5 1740 _FilterData(Default_VAVGVal) = 0;
julientiron 0:1cb50d31c3b5 1741 _FilterData(NoDelay_ZeroVal) = 0;
julientiron 0:1cb50d31c3b5 1742 _FilterData(NoDelay_VAVGVal) = 0;
julientiron 0:1cb50d31c3b5 1743 _FilterData(Previous_VAVGDiff) = 0;
julientiron 0:1cb50d31c3b5 1744
julientiron 0:1cb50d31c3b5 1745 _FilterData(StdFilteredReads) = 0;
julientiron 0:1cb50d31c3b5 1746
julientiron 0:1cb50d31c3b5 1747 for (i = 0; i < FILTER_NBOF_SAMPLES; i++) {
julientiron 0:1cb50d31c3b5 1748 _FilterData(LastTrueRange)[i] = FILTER_INVALID_DISTANCE;
julientiron 0:1cb50d31c3b5 1749 _FilterData(LastReturnRates)[i] = 0;
julientiron 0:1cb50d31c3b5 1750 }
julientiron 0:1cb50d31c3b5 1751 return 0;
julientiron 0:1cb50d31c3b5 1752 }
julientiron 0:1cb50d31c3b5 1753
julientiron 0:1cb50d31c3b5 1754
julientiron 0:1cb50d31c3b5 1755 static uint32_t _filter_StdDevDamper(uint32_t AmbientRate, uint32_t SignalRate, const uint32_t StdDevLimitLowLight, const uint32_t StdDevLimitLowLightSNR, const uint32_t StdDevLimitHighLight, const uint32_t StdDevLimitHighLightSNR) {
julientiron 0:1cb50d31c3b5 1756 uint32_t newStdDev;
julientiron 0:1cb50d31c3b5 1757 uint16_t SNR;
julientiron 0:1cb50d31c3b5 1758
julientiron 0:1cb50d31c3b5 1759 if (AmbientRate > 0)
julientiron 0:1cb50d31c3b5 1760 SNR = (uint16_t) ((100 * SignalRate) / AmbientRate);
julientiron 0:1cb50d31c3b5 1761 else
julientiron 0:1cb50d31c3b5 1762 SNR = 9999;
julientiron 0:1cb50d31c3b5 1763
julientiron 0:1cb50d31c3b5 1764 if (SNR >= StdDevLimitLowLightSNR) {
julientiron 0:1cb50d31c3b5 1765 newStdDev = StdDevLimitLowLight;
julientiron 0:1cb50d31c3b5 1766 } else {
julientiron 0:1cb50d31c3b5 1767 if (SNR <= StdDevLimitHighLightSNR)
julientiron 0:1cb50d31c3b5 1768 newStdDev = StdDevLimitHighLight;
julientiron 0:1cb50d31c3b5 1769 else {
julientiron 0:1cb50d31c3b5 1770 newStdDev = (uint32_t) (StdDevLimitHighLight + (SNR - StdDevLimitHighLightSNR) * (int) (StdDevLimitLowLight - StdDevLimitHighLight) / (StdDevLimitLowLightSNR - StdDevLimitHighLightSNR));
julientiron 0:1cb50d31c3b5 1771 }
julientiron 0:1cb50d31c3b5 1772 }
julientiron 0:1cb50d31c3b5 1773
julientiron 0:1cb50d31c3b5 1774 return newStdDev;
julientiron 0:1cb50d31c3b5 1775 }
julientiron 0:1cb50d31c3b5 1776
julientiron 0:1cb50d31c3b5 1777
julientiron 0:1cb50d31c3b5 1778 /*
julientiron 0:1cb50d31c3b5 1779 * Return <0 on error
julientiron 0:1cb50d31c3b5 1780 */
julientiron 0:1cb50d31c3b5 1781 int32_t VL6180X::_filter_Start(VL6180xDev_t dev, uint16_t m_trueRange_mm, uint16_t m_rawRange_mm, uint32_t m_rtnSignalRate, uint32_t m_rtnAmbientRate, uint16_t errorCode) {
julientiron 0:1cb50d31c3b5 1782 int status;
julientiron 0:1cb50d31c3b5 1783 uint16_t m_newTrueRange_mm = 0;
julientiron 0:1cb50d31c3b5 1784
julientiron 0:1cb50d31c3b5 1785 uint16_t i;
julientiron 0:1cb50d31c3b5 1786 uint16_t bypassFilter = 0;
julientiron 0:1cb50d31c3b5 1787
julientiron 0:1cb50d31c3b5 1788 uint16_t registerValue;
julientiron 0:1cb50d31c3b5 1789
julientiron 0:1cb50d31c3b5 1790 uint32_t register32BitsValue1;
julientiron 0:1cb50d31c3b5 1791 uint32_t register32BitsValue2;
julientiron 0:1cb50d31c3b5 1792
julientiron 0:1cb50d31c3b5 1793 uint16_t ValidDistance = 0;
julientiron 0:1cb50d31c3b5 1794
julientiron 0:1cb50d31c3b5 1795 uint16_t WrapAroundFlag = 0;
julientiron 0:1cb50d31c3b5 1796 uint16_t NoWrapAroundFlag = 0;
julientiron 0:1cb50d31c3b5 1797 uint16_t NoWrapAroundHighConfidenceFlag = 0;
julientiron 0:1cb50d31c3b5 1798
julientiron 0:1cb50d31c3b5 1799 uint16_t FlushFilter = 0;
julientiron 0:1cb50d31c3b5 1800 uint32_t RateChange = 0;
julientiron 0:1cb50d31c3b5 1801
julientiron 0:1cb50d31c3b5 1802 uint16_t StdDevSamples = 0;
julientiron 0:1cb50d31c3b5 1803 uint32_t StdDevDistanceSum = 0;
julientiron 0:1cb50d31c3b5 1804 uint32_t StdDevDistanceMean = 0;
julientiron 0:1cb50d31c3b5 1805 uint32_t StdDevDistance = 0;
julientiron 0:1cb50d31c3b5 1806 uint32_t StdDevRateSum = 0;
julientiron 0:1cb50d31c3b5 1807 uint32_t StdDevRateMean = 0;
julientiron 0:1cb50d31c3b5 1808 uint32_t StdDevRate = 0;
julientiron 0:1cb50d31c3b5 1809 uint32_t StdDevLimitWithTargetMove = 0;
julientiron 0:1cb50d31c3b5 1810
julientiron 0:1cb50d31c3b5 1811 uint32_t VAVGDiff;
julientiron 0:1cb50d31c3b5 1812 uint32_t IdealVAVGDiff;
julientiron 0:1cb50d31c3b5 1813 uint32_t MinVAVGDiff;
julientiron 0:1cb50d31c3b5 1814 uint32_t MaxVAVGDiff;
julientiron 0:1cb50d31c3b5 1815
julientiron 0:1cb50d31c3b5 1816 /* Filter Parameters */
julientiron 0:1cb50d31c3b5 1817 static const uint16_t ROMABLE_DATA WrapAroundLowRawRangeLimit = 60;
julientiron 0:1cb50d31c3b5 1818 static const uint32_t ROMABLE_DATA WrapAroundLowReturnRateLimit_ROM = 800; // Shall be adapted depending on crossTalk
julientiron 0:1cb50d31c3b5 1819 static const uint16_t ROMABLE_DATA WrapAroundLowRawRangeLimit2 = 165;
julientiron 0:1cb50d31c3b5 1820 static const uint32_t ROMABLE_DATA WrapAroundLowReturnRateLimit2_ROM = 180; // Shall be adapted depending on crossTalk and device sensitivity
julientiron 0:1cb50d31c3b5 1821
julientiron 0:1cb50d31c3b5 1822 static const uint32_t ROMABLE_DATA WrapAroundLowReturnRateFilterLimit_ROM = 850; // Shall be adapted depending on crossTalk and device sensitivity
julientiron 0:1cb50d31c3b5 1823 static const uint16_t ROMABLE_DATA WrapAroundHighRawRangeFilterLimit = 350;
julientiron 0:1cb50d31c3b5 1824 static const uint32_t ROMABLE_DATA WrapAroundHighReturnRateFilterLimit_ROM = 1400; // Shall be adapted depending on crossTalk and device sensitivity
julientiron 0:1cb50d31c3b5 1825
julientiron 0:1cb50d31c3b5 1826 static const uint32_t ROMABLE_DATA WrapAroundMaximumAmbientRateFilterLimit = 7500;
julientiron 0:1cb50d31c3b5 1827
julientiron 0:1cb50d31c3b5 1828 /* Temporal filter data and flush values */
julientiron 0:1cb50d31c3b5 1829 static const uint32_t ROMABLE_DATA MinReturnRateFilterFlush = 75;
julientiron 0:1cb50d31c3b5 1830 static const uint32_t ROMABLE_DATA MaxReturnRateChangeFilterFlush = 50;
julientiron 0:1cb50d31c3b5 1831
julientiron 0:1cb50d31c3b5 1832 /* STDDEV values and damper values */
julientiron 0:1cb50d31c3b5 1833
julientiron 0:1cb50d31c3b5 1834 static const uint32_t ROMABLE_DATA StdDevLimitLowLight = 300;
julientiron 0:1cb50d31c3b5 1835 static const uint32_t ROMABLE_DATA StdDevLimitLowLightSNR = 30; /* 0.3 */
julientiron 0:1cb50d31c3b5 1836 static const uint32_t ROMABLE_DATA StdDevLimitHighLight = 2500;
julientiron 0:1cb50d31c3b5 1837 static const uint32_t ROMABLE_DATA StdDevLimitHighLightSNR = 5; /* 0.05 */
julientiron 0:1cb50d31c3b5 1838
julientiron 0:1cb50d31c3b5 1839 static const uint32_t ROMABLE_DATA StdDevHighConfidenceSNRLimit = 8;
julientiron 0:1cb50d31c3b5 1840
julientiron 0:1cb50d31c3b5 1841 static const uint32_t ROMABLE_DATA StdDevMovingTargetStdDevLimit = 90000;
julientiron 0:1cb50d31c3b5 1842
julientiron 0:1cb50d31c3b5 1843 static const uint32_t ROMABLE_DATA StdDevMovingTargetReturnRateLimit = 3500;
julientiron 0:1cb50d31c3b5 1844 static const uint32_t ROMABLE_DATA StdDevMovingTargetStdDevForReturnRateLimit = 5000;
julientiron 0:1cb50d31c3b5 1845
julientiron 0:1cb50d31c3b5 1846 static const uint32_t ROMABLE_DATA MAX_VAVGDiff = 1800;
julientiron 0:1cb50d31c3b5 1847
julientiron 0:1cb50d31c3b5 1848 /* WrapAroundDetection variables */
julientiron 0:1cb50d31c3b5 1849 static const uint16_t ROMABLE_DATA WrapAroundNoDelayCheckPeriod = 2;
julientiron 0:1cb50d31c3b5 1850 static const uint16_t ROMABLE_DATA StdFilteredReadsIncrement = 2;
julientiron 0:1cb50d31c3b5 1851 static const uint16_t ROMABLE_DATA StdMaxFilteredReads = 4;
julientiron 0:1cb50d31c3b5 1852
julientiron 0:1cb50d31c3b5 1853 uint32_t SignalRateDMax;
julientiron 0:1cb50d31c3b5 1854 uint32_t WrapAroundLowReturnRateLimit;
julientiron 0:1cb50d31c3b5 1855 uint32_t WrapAroundLowReturnRateLimit2;
julientiron 0:1cb50d31c3b5 1856 uint32_t WrapAroundLowReturnRateFilterLimit;
julientiron 0:1cb50d31c3b5 1857 uint32_t WrapAroundHighReturnRateFilterLimit;
julientiron 0:1cb50d31c3b5 1858
julientiron 0:1cb50d31c3b5 1859 uint8_t u8, u8_2;
julientiron 0:1cb50d31c3b5 1860 uint32_t XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1861 uint32_t StdDevLimit = 300;
julientiron 0:1cb50d31c3b5 1862 uint32_t MaxOrInvalidDistance = 255*_GetUpscale(dev);
julientiron 0:1cb50d31c3b5 1863 /* #define MaxOrInvalidDistance (uint16_t) (255 * 3) */
julientiron 0:1cb50d31c3b5 1864
julientiron 0:1cb50d31c3b5 1865 /* Check if distance is Valid or not */
julientiron 0:1cb50d31c3b5 1866 switch (errorCode) {
julientiron 0:1cb50d31c3b5 1867 case 0x0C:
julientiron 0:1cb50d31c3b5 1868 m_trueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 1869 ValidDistance = 0;
julientiron 0:1cb50d31c3b5 1870 break;
julientiron 0:1cb50d31c3b5 1871 case 0x0D:
julientiron 0:1cb50d31c3b5 1872 m_trueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 1873 ValidDistance = 1;
julientiron 0:1cb50d31c3b5 1874 break;
julientiron 0:1cb50d31c3b5 1875 case 0x0F:
julientiron 0:1cb50d31c3b5 1876 m_trueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 1877 ValidDistance = 1;
julientiron 0:1cb50d31c3b5 1878 break;
julientiron 0:1cb50d31c3b5 1879 default:
julientiron 0:1cb50d31c3b5 1880 if (m_rawRange_mm >= MaxOrInvalidDistance) {
julientiron 0:1cb50d31c3b5 1881 ValidDistance = 0;
julientiron 0:1cb50d31c3b5 1882 } else {
julientiron 0:1cb50d31c3b5 1883 ValidDistance = 1;
julientiron 0:1cb50d31c3b5 1884 }
julientiron 0:1cb50d31c3b5 1885 break;
julientiron 0:1cb50d31c3b5 1886 }
julientiron 0:1cb50d31c3b5 1887 m_newTrueRange_mm = m_trueRange_mm;
julientiron 0:1cb50d31c3b5 1888
julientiron 0:1cb50d31c3b5 1889 XTalkCompRate_KCps = VL6180xDevDataGet(dev, XTalkCompRate_KCps );
julientiron 0:1cb50d31c3b5 1890
julientiron 0:1cb50d31c3b5 1891
julientiron 0:1cb50d31c3b5 1892 //Update signal rate limits depending on crosstalk
julientiron 0:1cb50d31c3b5 1893 SignalRateDMax = (uint32_t)_GetDMaxDataRetSignalAt400mm(dev) + XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1894 WrapAroundLowReturnRateLimit = WrapAroundLowReturnRateLimit_ROM + XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1895 WrapAroundLowReturnRateLimit2 = ((WrapAroundLowReturnRateLimit2_ROM * SignalRateDMax) / 312) + XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1896 WrapAroundLowReturnRateFilterLimit = ((WrapAroundLowReturnRateFilterLimit_ROM * SignalRateDMax) / 312) + XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1897 WrapAroundHighReturnRateFilterLimit = ((WrapAroundHighReturnRateFilterLimit_ROM * SignalRateDMax) / 312) + XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 1898
julientiron 0:1cb50d31c3b5 1899
julientiron 0:1cb50d31c3b5 1900 /* Checks on low range data */
julientiron 0:1cb50d31c3b5 1901 if ((m_rawRange_mm < WrapAroundLowRawRangeLimit) && (m_rtnSignalRate < WrapAroundLowReturnRateLimit)) {
julientiron 0:1cb50d31c3b5 1902 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 1903 bypassFilter = 1;
julientiron 0:1cb50d31c3b5 1904 }
julientiron 0:1cb50d31c3b5 1905 if ((m_rawRange_mm < WrapAroundLowRawRangeLimit2) && (m_rtnSignalRate < WrapAroundLowReturnRateLimit2)) {
julientiron 0:1cb50d31c3b5 1906 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 1907 bypassFilter = 1;
julientiron 0:1cb50d31c3b5 1908 }
julientiron 0:1cb50d31c3b5 1909
julientiron 0:1cb50d31c3b5 1910 /* Checks on Ambient rate level */
julientiron 0:1cb50d31c3b5 1911 if (m_rtnAmbientRate > WrapAroundMaximumAmbientRateFilterLimit) {
julientiron 0:1cb50d31c3b5 1912 /* Too high ambient rate */
julientiron 0:1cb50d31c3b5 1913 FlushFilter = 1;
julientiron 0:1cb50d31c3b5 1914 bypassFilter = 1;
julientiron 0:1cb50d31c3b5 1915 }
julientiron 0:1cb50d31c3b5 1916 /* Checks on Filter flush */
julientiron 0:1cb50d31c3b5 1917 if (m_rtnSignalRate < MinReturnRateFilterFlush) {
julientiron 0:1cb50d31c3b5 1918 /* Completely lost target, so flush the filter */
julientiron 0:1cb50d31c3b5 1919 FlushFilter = 1;
julientiron 0:1cb50d31c3b5 1920 bypassFilter = 1;
julientiron 0:1cb50d31c3b5 1921 }
julientiron 0:1cb50d31c3b5 1922 if (_FilterData(LastReturnRates)[0] != 0) {
julientiron 0:1cb50d31c3b5 1923 if (m_rtnSignalRate > _FilterData(LastReturnRates)[0])
julientiron 0:1cb50d31c3b5 1924 RateChange = (100 * (m_rtnSignalRate - _FilterData(LastReturnRates)[0])) / _FilterData(LastReturnRates)[0];
julientiron 0:1cb50d31c3b5 1925 else
julientiron 0:1cb50d31c3b5 1926 RateChange = (100 * (_FilterData(LastReturnRates)[0] - m_rtnSignalRate)) / _FilterData(LastReturnRates)[0];
julientiron 0:1cb50d31c3b5 1927 } else
julientiron 0:1cb50d31c3b5 1928 RateChange = 0;
julientiron 0:1cb50d31c3b5 1929 if (RateChange > MaxReturnRateChangeFilterFlush) {
julientiron 0:1cb50d31c3b5 1930 FlushFilter = 1;
julientiron 0:1cb50d31c3b5 1931 }
julientiron 0:1cb50d31c3b5 1932 /* TODO optimize filter using circular buffer */
julientiron 0:1cb50d31c3b5 1933 if (FlushFilter == 1) {
julientiron 0:1cb50d31c3b5 1934 _FilterData(MeasurementIndex) = 0;
julientiron 0:1cb50d31c3b5 1935 for (i = 0; i < FILTER_NBOF_SAMPLES; i++) {
julientiron 0:1cb50d31c3b5 1936 _FilterData(LastTrueRange)[i] = FILTER_INVALID_DISTANCE;
julientiron 0:1cb50d31c3b5 1937 _FilterData(LastReturnRates)[i] = 0;
julientiron 0:1cb50d31c3b5 1938 }
julientiron 0:1cb50d31c3b5 1939 } else {
julientiron 0:1cb50d31c3b5 1940 for (i = (uint16_t) (FILTER_NBOF_SAMPLES - 1); i > 0; i--) {
julientiron 0:1cb50d31c3b5 1941 _FilterData(LastTrueRange)[i] = _FilterData(LastTrueRange)[i - 1];
julientiron 0:1cb50d31c3b5 1942 _FilterData(LastReturnRates)[i] = _FilterData(LastReturnRates)[i - 1];
julientiron 0:1cb50d31c3b5 1943 }
julientiron 0:1cb50d31c3b5 1944 }
julientiron 0:1cb50d31c3b5 1945 if (ValidDistance == 1)
julientiron 0:1cb50d31c3b5 1946 _FilterData(LastTrueRange)[0] = m_trueRange_mm;
julientiron 0:1cb50d31c3b5 1947 else
julientiron 0:1cb50d31c3b5 1948 _FilterData(LastTrueRange)[0] = FILTER_INVALID_DISTANCE;
julientiron 0:1cb50d31c3b5 1949 _FilterData(LastReturnRates)[0] = m_rtnSignalRate;
julientiron 0:1cb50d31c3b5 1950
julientiron 0:1cb50d31c3b5 1951 /* Check if we need to go through the filter or not */
julientiron 0:1cb50d31c3b5 1952 if (!(((m_rawRange_mm < WrapAroundHighRawRangeFilterLimit) && (m_rtnSignalRate < WrapAroundLowReturnRateFilterLimit)) || ((m_rawRange_mm >= WrapAroundHighRawRangeFilterLimit) && (m_rtnSignalRate < WrapAroundHighReturnRateFilterLimit))))
julientiron 0:1cb50d31c3b5 1953 bypassFilter = 1;
julientiron 0:1cb50d31c3b5 1954
julientiron 0:1cb50d31c3b5 1955 /* Check which kind of measurement has been made */
julientiron 0:1cb50d31c3b5 1956 status = VL6180x_RdByte(dev, 0x01AC, &u8 );
julientiron 0:1cb50d31c3b5 1957 if( status ){
julientiron 0:1cb50d31c3b5 1958 VL6180x_ErrLog("0x01AC rd fail");
julientiron 0:1cb50d31c3b5 1959 goto done_err;
julientiron 0:1cb50d31c3b5 1960 }
julientiron 0:1cb50d31c3b5 1961 registerValue =u8;
julientiron 0:1cb50d31c3b5 1962
julientiron 0:1cb50d31c3b5 1963 /* Read data for filtering */
julientiron 0:1cb50d31c3b5 1964 status = VL6180x_RdByte(dev, 0x10C, &u8 ); /* read only 8 lsb bits */
julientiron 0:1cb50d31c3b5 1965 if( status ){
julientiron 0:1cb50d31c3b5 1966 VL6180x_ErrLog("0x010C rd fail");
julientiron 0:1cb50d31c3b5 1967 goto done_err;
julientiron 0:1cb50d31c3b5 1968 }
julientiron 0:1cb50d31c3b5 1969 register32BitsValue1=u8;
julientiron 0:1cb50d31c3b5 1970 status = VL6180x_RdByte(dev, 0x0110, &u8); /* read only 8 lsb bits */
julientiron 0:1cb50d31c3b5 1971 if( status ){
julientiron 0:1cb50d31c3b5 1972 VL6180x_ErrLog("0x0110 rd fail");
julientiron 0:1cb50d31c3b5 1973 goto done_err;
julientiron 0:1cb50d31c3b5 1974 }
julientiron 0:1cb50d31c3b5 1975 register32BitsValue2 = u8;
julientiron 0:1cb50d31c3b5 1976
julientiron 0:1cb50d31c3b5 1977 if (registerValue == 0x3E) {
julientiron 0:1cb50d31c3b5 1978 _FilterData(Default_ZeroVal) = register32BitsValue1;
julientiron 0:1cb50d31c3b5 1979 _FilterData(Default_VAVGVal) = register32BitsValue2;
julientiron 0:1cb50d31c3b5 1980 } else {
julientiron 0:1cb50d31c3b5 1981 _FilterData(NoDelay_ZeroVal) = register32BitsValue1;
julientiron 0:1cb50d31c3b5 1982 _FilterData(NoDelay_VAVGVal) = register32BitsValue2;
julientiron 0:1cb50d31c3b5 1983 }
julientiron 0:1cb50d31c3b5 1984
julientiron 0:1cb50d31c3b5 1985 if (bypassFilter == 1) {
julientiron 0:1cb50d31c3b5 1986 /* Do not go through the filter */
julientiron 0:1cb50d31c3b5 1987 if (registerValue != 0x3E) {
julientiron 0:1cb50d31c3b5 1988 status = VL6180x_WrByte(dev, 0x1AC, 0x3E);
julientiron 0:1cb50d31c3b5 1989 if( status ){
julientiron 0:1cb50d31c3b5 1990 VL6180x_ErrLog("0x01AC bypass wr fail");
julientiron 0:1cb50d31c3b5 1991 goto done_err;
julientiron 0:1cb50d31c3b5 1992 }
julientiron 0:1cb50d31c3b5 1993 status = VL6180x_WrByte(dev, 0x0F2, 0x01);
julientiron 0:1cb50d31c3b5 1994 if( status ){
julientiron 0:1cb50d31c3b5 1995 VL6180x_ErrLog("0x0F2 bypass wr fail");
julientiron 0:1cb50d31c3b5 1996 goto done_err;
julientiron 0:1cb50d31c3b5 1997 }
julientiron 0:1cb50d31c3b5 1998 }
julientiron 0:1cb50d31c3b5 1999 /* Set both Default and NoDelay To same value */
julientiron 0:1cb50d31c3b5 2000 _FilterData(Default_ZeroVal) = register32BitsValue1;
julientiron 0:1cb50d31c3b5 2001 _FilterData(Default_VAVGVal) = register32BitsValue2;
julientiron 0:1cb50d31c3b5 2002 _FilterData(NoDelay_ZeroVal) = register32BitsValue1;
julientiron 0:1cb50d31c3b5 2003 _FilterData(NoDelay_VAVGVal) = register32BitsValue2;
julientiron 0:1cb50d31c3b5 2004 _FilterData(MeasurementIndex) = 0;
julientiron 0:1cb50d31c3b5 2005
julientiron 0:1cb50d31c3b5 2006 return m_newTrueRange_mm;
julientiron 0:1cb50d31c3b5 2007 }
julientiron 0:1cb50d31c3b5 2008
julientiron 0:1cb50d31c3b5 2009 if (_FilterData(MeasurementIndex) % WrapAroundNoDelayCheckPeriod == 0) {
julientiron 0:1cb50d31c3b5 2010 u8=0x3C;
julientiron 0:1cb50d31c3b5 2011 u8_2 = 0x05;
julientiron 0:1cb50d31c3b5 2012 } else {
julientiron 0:1cb50d31c3b5 2013 u8=0x3E;
julientiron 0:1cb50d31c3b5 2014 u8_2 = 0x01;
julientiron 0:1cb50d31c3b5 2015 }
julientiron 0:1cb50d31c3b5 2016 status = VL6180x_WrByte(dev, 0x01AC, u8);
julientiron 0:1cb50d31c3b5 2017 if( status ){
julientiron 0:1cb50d31c3b5 2018 VL6180x_ErrLog("0x01AC wr fail");
julientiron 0:1cb50d31c3b5 2019 goto done_err;
julientiron 0:1cb50d31c3b5 2020 }
julientiron 0:1cb50d31c3b5 2021 status = VL6180x_WrByte(dev, 0x0F2, u8_2);
julientiron 0:1cb50d31c3b5 2022 if( status ){
julientiron 0:1cb50d31c3b5 2023 VL6180x_ErrLog("0x0F2 wr fail");
julientiron 0:1cb50d31c3b5 2024 goto done_err;
julientiron 0:1cb50d31c3b5 2025 }
julientiron 0:1cb50d31c3b5 2026
julientiron 0:1cb50d31c3b5 2027
julientiron 0:1cb50d31c3b5 2028 _FilterData(MeasurementIndex)++;
julientiron 0:1cb50d31c3b5 2029
julientiron 0:1cb50d31c3b5 2030 /* Computes current VAVGDiff */
julientiron 0:1cb50d31c3b5 2031 if (_FilterData(Default_VAVGVal) > _FilterData(NoDelay_VAVGVal))
julientiron 0:1cb50d31c3b5 2032 VAVGDiff = _FilterData(Default_VAVGVal) - _FilterData(NoDelay_VAVGVal);
julientiron 0:1cb50d31c3b5 2033 else
julientiron 0:1cb50d31c3b5 2034 VAVGDiff = 0;
julientiron 0:1cb50d31c3b5 2035 _FilterData(Previous_VAVGDiff) = VAVGDiff;
julientiron 0:1cb50d31c3b5 2036
julientiron 0:1cb50d31c3b5 2037 /* Check the VAVGDiff */
julientiron 0:1cb50d31c3b5 2038 if (_FilterData(Default_ZeroVal) > _FilterData(NoDelay_ZeroVal))
julientiron 0:1cb50d31c3b5 2039 IdealVAVGDiff = _FilterData(Default_ZeroVal) - _FilterData(NoDelay_ZeroVal);
julientiron 0:1cb50d31c3b5 2040 else
julientiron 0:1cb50d31c3b5 2041 IdealVAVGDiff = _FilterData(NoDelay_ZeroVal) - _FilterData(Default_ZeroVal);
julientiron 0:1cb50d31c3b5 2042 if (IdealVAVGDiff > MAX_VAVGDiff)
julientiron 0:1cb50d31c3b5 2043 MinVAVGDiff = IdealVAVGDiff - MAX_VAVGDiff;
julientiron 0:1cb50d31c3b5 2044 else
julientiron 0:1cb50d31c3b5 2045 MinVAVGDiff = 0;
julientiron 0:1cb50d31c3b5 2046 MaxVAVGDiff = IdealVAVGDiff + MAX_VAVGDiff;
julientiron 0:1cb50d31c3b5 2047 if (VAVGDiff < MinVAVGDiff || VAVGDiff > MaxVAVGDiff) {
julientiron 0:1cb50d31c3b5 2048 WrapAroundFlag = 1;
julientiron 0:1cb50d31c3b5 2049 } else {
julientiron 0:1cb50d31c3b5 2050 /* Go through filtering check */
julientiron 0:1cb50d31c3b5 2051
julientiron 0:1cb50d31c3b5 2052 /* StdDevLimit Damper on SNR */
julientiron 0:1cb50d31c3b5 2053 StdDevLimit = _filter_StdDevDamper(m_rtnAmbientRate, m_rtnSignalRate, StdDevLimitLowLight, StdDevLimitLowLightSNR, StdDevLimitHighLight, StdDevLimitHighLightSNR);
julientiron 0:1cb50d31c3b5 2054
julientiron 0:1cb50d31c3b5 2055 /* Standard deviations computations */
julientiron 0:1cb50d31c3b5 2056 StdDevSamples = 0;
julientiron 0:1cb50d31c3b5 2057 StdDevDistanceSum = 0;
julientiron 0:1cb50d31c3b5 2058 StdDevDistanceMean = 0;
julientiron 0:1cb50d31c3b5 2059 StdDevDistance = 0;
julientiron 0:1cb50d31c3b5 2060 StdDevRateSum = 0;
julientiron 0:1cb50d31c3b5 2061 StdDevRateMean = 0;
julientiron 0:1cb50d31c3b5 2062 StdDevRate = 0;
julientiron 0:1cb50d31c3b5 2063 for (i = 0; (i < FILTER_NBOF_SAMPLES) && (StdDevSamples < FILTER_STDDEV_SAMPLES); i++) {
julientiron 0:1cb50d31c3b5 2064 if (_FilterData(LastTrueRange)[i] != FILTER_INVALID_DISTANCE) {
julientiron 0:1cb50d31c3b5 2065 StdDevSamples = (uint16_t) (StdDevSamples + 1);
julientiron 0:1cb50d31c3b5 2066 StdDevDistanceSum = (uint32_t) (StdDevDistanceSum + _FilterData(LastTrueRange)[i]);
julientiron 0:1cb50d31c3b5 2067 StdDevRateSum = (uint32_t) (StdDevRateSum + _FilterData(LastReturnRates)[i]);
julientiron 0:1cb50d31c3b5 2068 }
julientiron 0:1cb50d31c3b5 2069 }
julientiron 0:1cb50d31c3b5 2070 if (StdDevSamples > 0) {
julientiron 0:1cb50d31c3b5 2071 StdDevDistanceMean = (uint32_t) (StdDevDistanceSum / StdDevSamples);
julientiron 0:1cb50d31c3b5 2072 StdDevRateMean = (uint32_t) (StdDevRateSum / StdDevSamples);
julientiron 0:1cb50d31c3b5 2073 }
julientiron 0:1cb50d31c3b5 2074 /* TODO optimize shorten Std dev in aisngle loop computation using sum of x2 - (sum of x)2 */
julientiron 0:1cb50d31c3b5 2075 StdDevSamples = 0;
julientiron 0:1cb50d31c3b5 2076 StdDevDistanceSum = 0;
julientiron 0:1cb50d31c3b5 2077 StdDevRateSum = 0;
julientiron 0:1cb50d31c3b5 2078 for (i = 0; (i < FILTER_NBOF_SAMPLES) && (StdDevSamples < FILTER_STDDEV_SAMPLES); i++) {
julientiron 0:1cb50d31c3b5 2079 if (_FilterData(LastTrueRange)[i] != FILTER_INVALID_DISTANCE) {
julientiron 0:1cb50d31c3b5 2080 StdDevSamples = (uint16_t) (StdDevSamples + 1);
julientiron 0:1cb50d31c3b5 2081 StdDevDistanceSum = (uint32_t) (StdDevDistanceSum + (int) (_FilterData(LastTrueRange)[i] - StdDevDistanceMean) * (int) (_FilterData(LastTrueRange)[i] - StdDevDistanceMean));
julientiron 0:1cb50d31c3b5 2082 StdDevRateSum = (uint32_t) (StdDevRateSum + (int) (_FilterData(LastReturnRates)[i] - StdDevRateMean) * (int) (_FilterData(LastReturnRates)[i] - StdDevRateMean));
julientiron 0:1cb50d31c3b5 2083 }
julientiron 0:1cb50d31c3b5 2084 }
julientiron 0:1cb50d31c3b5 2085 if (StdDevSamples >= MIN_FILTER_STDDEV_SAMPLES) {
julientiron 0:1cb50d31c3b5 2086 StdDevDistance = (uint16_t) (StdDevDistanceSum / StdDevSamples);
julientiron 0:1cb50d31c3b5 2087 StdDevRate = (uint16_t) (StdDevRateSum / StdDevSamples);
julientiron 0:1cb50d31c3b5 2088 } else {
julientiron 0:1cb50d31c3b5 2089 StdDevDistance = 0;
julientiron 0:1cb50d31c3b5 2090 StdDevRate = 0;
julientiron 0:1cb50d31c3b5 2091 }
julientiron 0:1cb50d31c3b5 2092
julientiron 0:1cb50d31c3b5 2093 /* Check Return rate standard deviation */
julientiron 0:1cb50d31c3b5 2094 if (StdDevRate < StdDevMovingTargetStdDevLimit) {
julientiron 0:1cb50d31c3b5 2095 if (StdDevSamples < MIN_FILTER_VALID_STDDEV_SAMPLES) {
julientiron 0:1cb50d31c3b5 2096 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 2097 } else {
julientiron 0:1cb50d31c3b5 2098 /* Check distance standard deviation */
julientiron 0:1cb50d31c3b5 2099 if (StdDevRate < StdDevMovingTargetReturnRateLimit)
julientiron 0:1cb50d31c3b5 2100 StdDevLimitWithTargetMove = StdDevLimit + (((StdDevMovingTargetStdDevForReturnRateLimit - StdDevLimit) * StdDevRate) / StdDevMovingTargetReturnRateLimit);
julientiron 0:1cb50d31c3b5 2101 else
julientiron 0:1cb50d31c3b5 2102 StdDevLimitWithTargetMove = StdDevMovingTargetStdDevForReturnRateLimit;
julientiron 0:1cb50d31c3b5 2103
julientiron 0:1cb50d31c3b5 2104 if ((StdDevDistance * StdDevHighConfidenceSNRLimit) < StdDevLimitWithTargetMove) {
julientiron 0:1cb50d31c3b5 2105 NoWrapAroundHighConfidenceFlag = 1;
julientiron 0:1cb50d31c3b5 2106 } else {
julientiron 0:1cb50d31c3b5 2107 if (StdDevDistance < StdDevLimitWithTargetMove) {
julientiron 0:1cb50d31c3b5 2108 if (StdDevSamples >= MIN_FILTER_VALID_STDDEV_SAMPLES) {
julientiron 0:1cb50d31c3b5 2109 NoWrapAroundFlag = 1;
julientiron 0:1cb50d31c3b5 2110 } else {
julientiron 0:1cb50d31c3b5 2111 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 2112 }
julientiron 0:1cb50d31c3b5 2113 } else {
julientiron 0:1cb50d31c3b5 2114 WrapAroundFlag = 1;
julientiron 0:1cb50d31c3b5 2115 }
julientiron 0:1cb50d31c3b5 2116 }
julientiron 0:1cb50d31c3b5 2117 }
julientiron 0:1cb50d31c3b5 2118 } else {
julientiron 0:1cb50d31c3b5 2119 WrapAroundFlag = 1;
julientiron 0:1cb50d31c3b5 2120 }
julientiron 0:1cb50d31c3b5 2121 }
julientiron 0:1cb50d31c3b5 2122
julientiron 0:1cb50d31c3b5 2123 if (m_newTrueRange_mm == MaxOrInvalidDistance) {
julientiron 0:1cb50d31c3b5 2124 if (_FilterData(StdFilteredReads) > 0)
julientiron 0:1cb50d31c3b5 2125 _FilterData(StdFilteredReads) = (uint16_t) (_FilterData(StdFilteredReads) - 1);
julientiron 0:1cb50d31c3b5 2126 } else {
julientiron 0:1cb50d31c3b5 2127 if (WrapAroundFlag == 1) {
julientiron 0:1cb50d31c3b5 2128 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 2129 _FilterData(StdFilteredReads) = (uint16_t) (_FilterData(StdFilteredReads) + StdFilteredReadsIncrement);
julientiron 0:1cb50d31c3b5 2130 if (_FilterData(StdFilteredReads) > StdMaxFilteredReads)
julientiron 0:1cb50d31c3b5 2131 _FilterData(StdFilteredReads) = StdMaxFilteredReads;
julientiron 0:1cb50d31c3b5 2132 } else {
julientiron 0:1cb50d31c3b5 2133 if (NoWrapAroundFlag == 1) {
julientiron 0:1cb50d31c3b5 2134 if (_FilterData(StdFilteredReads) > 0) {
julientiron 0:1cb50d31c3b5 2135 m_newTrueRange_mm = MaxOrInvalidDistance;
julientiron 0:1cb50d31c3b5 2136 if (_FilterData(StdFilteredReads) > StdFilteredReadsIncrement)
julientiron 0:1cb50d31c3b5 2137 _FilterData(StdFilteredReads) = (uint16_t) (_FilterData(StdFilteredReads) - StdFilteredReadsIncrement);
julientiron 0:1cb50d31c3b5 2138 else
julientiron 0:1cb50d31c3b5 2139 _FilterData(StdFilteredReads) = 0;
julientiron 0:1cb50d31c3b5 2140 }
julientiron 0:1cb50d31c3b5 2141 } else {
julientiron 0:1cb50d31c3b5 2142 if (NoWrapAroundHighConfidenceFlag == 1) {
julientiron 0:1cb50d31c3b5 2143 _FilterData(StdFilteredReads) = 0;
julientiron 0:1cb50d31c3b5 2144 }
julientiron 0:1cb50d31c3b5 2145 }
julientiron 0:1cb50d31c3b5 2146 }
julientiron 0:1cb50d31c3b5 2147 }
julientiron 0:1cb50d31c3b5 2148
julientiron 0:1cb50d31c3b5 2149 return m_newTrueRange_mm;
julientiron 0:1cb50d31c3b5 2150 done_err:
julientiron 0:1cb50d31c3b5 2151 return -1;
julientiron 0:1cb50d31c3b5 2152
julientiron 0:1cb50d31c3b5 2153 #undef MaxOrInvalidDistance
julientiron 0:1cb50d31c3b5 2154 }
julientiron 0:1cb50d31c3b5 2155
julientiron 0:1cb50d31c3b5 2156
julientiron 0:1cb50d31c3b5 2157 int VL6180X::_filter_GetResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData) {
julientiron 0:1cb50d31c3b5 2158 uint32_t m_rawRange_mm = 0;
julientiron 0:1cb50d31c3b5 2159 int32_t FilteredRange;
julientiron 0:1cb50d31c3b5 2160 const uint8_t scaler = _GetUpscale(dev);
julientiron 0:1cb50d31c3b5 2161 uint8_t u8;
julientiron 0:1cb50d31c3b5 2162 int status;
julientiron 0:1cb50d31c3b5 2163
julientiron 0:1cb50d31c3b5 2164 do {
julientiron 0:1cb50d31c3b5 2165 status = VL6180x_RdByte(dev, RESULT_RANGE_RAW, &u8);
julientiron 0:1cb50d31c3b5 2166 if (status) {
julientiron 0:1cb50d31c3b5 2167 VL6180x_ErrLog("RESULT_RANGE_RAW rd fail");
julientiron 0:1cb50d31c3b5 2168 break;
julientiron 0:1cb50d31c3b5 2169 }
julientiron 0:1cb50d31c3b5 2170 m_rawRange_mm = u8;
julientiron 0:1cb50d31c3b5 2171
julientiron 0:1cb50d31c3b5 2172 FilteredRange = _filter_Start(dev, pRangeData->range_mm, (m_rawRange_mm * scaler), pRangeData->rtnRate, pRangeData->rtnAmbRate, pRangeData->errorStatus);
julientiron 0:1cb50d31c3b5 2173 if( FilteredRange<0 ){
julientiron 0:1cb50d31c3b5 2174 status = -1;
julientiron 0:1cb50d31c3b5 2175 break;
julientiron 0:1cb50d31c3b5 2176 }
julientiron 0:1cb50d31c3b5 2177 pRangeData->FilteredData.range_mm= FilteredRange;
julientiron 0:1cb50d31c3b5 2178 pRangeData->FilteredData.rawRange_mm = m_rawRange_mm * scaler;
julientiron 0:1cb50d31c3b5 2179 } while (0);
julientiron 0:1cb50d31c3b5 2180 return status;
julientiron 0:1cb50d31c3b5 2181 }
julientiron 0:1cb50d31c3b5 2182
julientiron 0:1cb50d31c3b5 2183 #undef _FilterData
julientiron 0:1cb50d31c3b5 2184 #undef FILTER_STDDEV_SAMPLES
julientiron 0:1cb50d31c3b5 2185 #undef MIN_FILTER_STDDEV_SAMPLES
julientiron 0:1cb50d31c3b5 2186 #undef MIN_FILTER_VALID_STDDEV_SAMPLES
julientiron 0:1cb50d31c3b5 2187 #undef FILTER_INVALID_DISTANCE
julientiron 0:1cb50d31c3b5 2188
julientiron 0:1cb50d31c3b5 2189 #endif /* VL6180x_WRAP_AROUND_FILTER_SUPPORT */
julientiron 0:1cb50d31c3b5 2190
julientiron 0:1cb50d31c3b5 2191 #ifdef VL6180x_HAVE_RATE_DATA
julientiron 0:1cb50d31c3b5 2192
julientiron 0:1cb50d31c3b5 2193 int VL6180X::_GetRateResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData) {
julientiron 0:1cb50d31c3b5 2194 uint32_t m_rtnConvTime = 0;
julientiron 0:1cb50d31c3b5 2195 uint32_t m_rtnSignalRate = 0;
julientiron 0:1cb50d31c3b5 2196 uint32_t m_rtnAmbientRate = 0;
julientiron 0:1cb50d31c3b5 2197 uint32_t m_rtnSignalCount = 0;
julientiron 0:1cb50d31c3b5 2198 uint32_t m_rtnAmbientCount = 0;
julientiron 0:1cb50d31c3b5 2199 uint32_t m_refConvTime = 0;
julientiron 0:1cb50d31c3b5 2200 uint32_t cRtnSignalCountMax = 0x7FFFFFFF;
julientiron 0:1cb50d31c3b5 2201 uint32_t cDllPeriods = 6;
julientiron 0:1cb50d31c3b5 2202 uint32_t calcConvTime = 0;
julientiron 0:1cb50d31c3b5 2203
julientiron 0:1cb50d31c3b5 2204 int status;
julientiron 0:1cb50d31c3b5 2205
julientiron 0:1cb50d31c3b5 2206 do {
julientiron 0:1cb50d31c3b5 2207
julientiron 0:1cb50d31c3b5 2208 status = VL6180x_RdDWord(dev, RESULT_RANGE_RETURN_SIGNAL_COUNT, &m_rtnSignalCount);
julientiron 0:1cb50d31c3b5 2209 if (status) {
julientiron 0:1cb50d31c3b5 2210 VL6180x_ErrLog("RESULT_RANGE_RETURN_SIGNAL_COUNT rd fail");
julientiron 0:1cb50d31c3b5 2211 break;
julientiron 0:1cb50d31c3b5 2212 }
julientiron 0:1cb50d31c3b5 2213 if (m_rtnSignalCount > cRtnSignalCountMax) {
julientiron 0:1cb50d31c3b5 2214 m_rtnSignalCount = 0;
julientiron 0:1cb50d31c3b5 2215 }
julientiron 0:1cb50d31c3b5 2216
julientiron 0:1cb50d31c3b5 2217 status = VL6180x_RdDWord(dev, RESULT_RANGE_RETURN_AMB_COUNT, &m_rtnAmbientCount);
julientiron 0:1cb50d31c3b5 2218 if (status) {
julientiron 0:1cb50d31c3b5 2219 VL6180x_ErrLog("RESULT_RANGE_RETURN_AMB_COUNTrd fail");
julientiron 0:1cb50d31c3b5 2220 break;
julientiron 0:1cb50d31c3b5 2221 }
julientiron 0:1cb50d31c3b5 2222
julientiron 0:1cb50d31c3b5 2223
julientiron 0:1cb50d31c3b5 2224 status = VL6180x_RdDWord(dev, RESULT_RANGE_RETURN_CONV_TIME, &m_rtnConvTime);
julientiron 0:1cb50d31c3b5 2225 if (status) {
julientiron 0:1cb50d31c3b5 2226 VL6180x_ErrLog("RESULT_RANGE_RETURN_CONV_TIME rd fail");
julientiron 0:1cb50d31c3b5 2227 break;
julientiron 0:1cb50d31c3b5 2228 }
julientiron 0:1cb50d31c3b5 2229
julientiron 0:1cb50d31c3b5 2230 status = VL6180x_RdDWord(dev, RESULT_RANGE_REFERENCE_CONV_TIME, &m_refConvTime);
julientiron 0:1cb50d31c3b5 2231 if (status) {
julientiron 0:1cb50d31c3b5 2232 VL6180x_ErrLog("RESULT_RANGE_REFERENCE_CONV_TIME rd fail");
julientiron 0:1cb50d31c3b5 2233 break;
julientiron 0:1cb50d31c3b5 2234 }
julientiron 0:1cb50d31c3b5 2235
julientiron 0:1cb50d31c3b5 2236 pRangeData->rtnConvTime = m_rtnConvTime;
julientiron 0:1cb50d31c3b5 2237 pRangeData->refConvTime = m_refConvTime;
julientiron 0:1cb50d31c3b5 2238
julientiron 0:1cb50d31c3b5 2239 calcConvTime = m_refConvTime;
julientiron 0:1cb50d31c3b5 2240 if (m_rtnConvTime > m_refConvTime) {
julientiron 0:1cb50d31c3b5 2241 calcConvTime = m_rtnConvTime;
julientiron 0:1cb50d31c3b5 2242 }
julientiron 0:1cb50d31c3b5 2243 if (calcConvTime == 0)
julientiron 0:1cb50d31c3b5 2244 calcConvTime = 63000;
julientiron 0:1cb50d31c3b5 2245
julientiron 0:1cb50d31c3b5 2246 m_rtnSignalRate = (m_rtnSignalCount * 1000) / calcConvTime;
julientiron 0:1cb50d31c3b5 2247 m_rtnAmbientRate = (m_rtnAmbientCount * cDllPeriods * 1000) / calcConvTime;
julientiron 0:1cb50d31c3b5 2248
julientiron 0:1cb50d31c3b5 2249 pRangeData->rtnRate = m_rtnSignalRate;
julientiron 0:1cb50d31c3b5 2250 pRangeData->rtnAmbRate = m_rtnAmbientRate;
julientiron 0:1cb50d31c3b5 2251
julientiron 0:1cb50d31c3b5 2252
julientiron 0:1cb50d31c3b5 2253 } while (0);
julientiron 0:1cb50d31c3b5 2254 return status;
julientiron 0:1cb50d31c3b5 2255 }
julientiron 0:1cb50d31c3b5 2256 #endif /* VL6180x_HAVE_RATE_DATA */
julientiron 0:1cb50d31c3b5 2257
julientiron 0:1cb50d31c3b5 2258
julientiron 0:1cb50d31c3b5 2259 int VL6180X::VL6180x_DMaxSetState(VL6180xDev_t dev, int state){
julientiron 0:1cb50d31c3b5 2260 int status;
julientiron 0:1cb50d31c3b5 2261 LOG_FUNCTION_START("%d", state);
julientiron 0:1cb50d31c3b5 2262 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 2263 VL6180xDevDataSet(dev,DMaxEnable, state);
julientiron 0:1cb50d31c3b5 2264 if( state ){
julientiron 0:1cb50d31c3b5 2265 status = _DMax_InitData(dev);
julientiron 0:1cb50d31c3b5 2266 }
julientiron 0:1cb50d31c3b5 2267 else {
julientiron 0:1cb50d31c3b5 2268 status = 0;
julientiron 0:1cb50d31c3b5 2269 }
julientiron 0:1cb50d31c3b5 2270 #else
julientiron 0:1cb50d31c3b5 2271 status = NOT_SUPPORTED;
julientiron 0:1cb50d31c3b5 2272 #endif
julientiron 0:1cb50d31c3b5 2273 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 2274 return status;
julientiron 0:1cb50d31c3b5 2275 }
julientiron 0:1cb50d31c3b5 2276
julientiron 0:1cb50d31c3b5 2277 int VL6180X::VL6180x_DMaxGetState(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 2278 int status;
julientiron 0:1cb50d31c3b5 2279 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 2280 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 2281 status = VL6180xDevDataGet(dev,DMaxEnable);
julientiron 0:1cb50d31c3b5 2282 #else
julientiron 0:1cb50d31c3b5 2283 status = 0;
julientiron 0:1cb50d31c3b5 2284 #endif
julientiron 0:1cb50d31c3b5 2285 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 2286 return status;
julientiron 0:1cb50d31c3b5 2287 }
julientiron 0:1cb50d31c3b5 2288
julientiron 0:1cb50d31c3b5 2289
julientiron 0:1cb50d31c3b5 2290 #if VL6180x_HAVE_DMAX_RANGING
julientiron 0:1cb50d31c3b5 2291
julientiron 0:1cb50d31c3b5 2292 #define _DMaxData(field) VL6180xDevDataGet(dev, DMaxData.field)
julientiron 0:1cb50d31c3b5 2293 /*
julientiron 0:1cb50d31c3b5 2294 * Convert fix point x.7 to KCpount per sec
julientiron 0:1cb50d31c3b5 2295 */
julientiron 0:1cb50d31c3b5 2296
julientiron 0:1cb50d31c3b5 2297 #ifndef VL6180x_PLATFORM_PROVIDE_SQRT
julientiron 0:1cb50d31c3b5 2298
julientiron 0:1cb50d31c3b5 2299 /*
julientiron 0:1cb50d31c3b5 2300 * 32 bit integer square root with not so bad precision (integer result) and is quite fast
julientiron 0:1cb50d31c3b5 2301 * see http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
julientiron 0:1cb50d31c3b5 2302 */
julientiron 0:1cb50d31c3b5 2303 uint32_t VL6180x_SqrtUint32(uint32_t num) {
julientiron 0:1cb50d31c3b5 2304 uint32_t res = 0;
julientiron 0:1cb50d31c3b5 2305 uint32_t bit = 1 << 30; /* The second-to-top bit is set: 1 << 30 for 32 bits */
julientiron 0:1cb50d31c3b5 2306
julientiron 0:1cb50d31c3b5 2307 /* "bit" starts at the highest power of four <= the argument. */
julientiron 0:1cb50d31c3b5 2308 while (bit > num)
julientiron 0:1cb50d31c3b5 2309 bit >>= 2;
julientiron 0:1cb50d31c3b5 2310
julientiron 0:1cb50d31c3b5 2311 while (bit != 0) {
julientiron 0:1cb50d31c3b5 2312 if (num >= res + bit) {
julientiron 0:1cb50d31c3b5 2313 num -= res + bit;
julientiron 0:1cb50d31c3b5 2314 res = (res >> 1) + bit;
julientiron 0:1cb50d31c3b5 2315 }
julientiron 0:1cb50d31c3b5 2316 else
julientiron 0:1cb50d31c3b5 2317 res >>= 1;
julientiron 0:1cb50d31c3b5 2318 bit >>= 2;
julientiron 0:1cb50d31c3b5 2319 }
julientiron 0:1cb50d31c3b5 2320 return res;
julientiron 0:1cb50d31c3b5 2321 }
julientiron 0:1cb50d31c3b5 2322 #endif
julientiron 0:1cb50d31c3b5 2323
julientiron 0:1cb50d31c3b5 2324
julientiron 0:1cb50d31c3b5 2325 /* DMax one time init */
julientiron 0:1cb50d31c3b5 2326 void _DMax_OneTimeInit(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 2327 _DMaxData(ambTuningWindowFactor_K)=DEF_AMBIENT_TUNING;
julientiron 0:1cb50d31c3b5 2328 }
julientiron 0:1cb50d31c3b5 2329
julientiron 0:1cb50d31c3b5 2330
julientiron 0:1cb50d31c3b5 2331 static uint32_t _DMax_RawValueAtRateKCps(VL6180xDev_t dev, int32_t rate){
julientiron 0:1cb50d31c3b5 2332 uint32_t snrLimit_K;
julientiron 0:1cb50d31c3b5 2333 int32_t DMaxSq;
julientiron 0:1cb50d31c3b5 2334 uint32_t RawDMax;
julientiron 0:1cb50d31c3b5 2335 DMaxFix_t retSignalAt400mm;
julientiron 0:1cb50d31c3b5 2336 uint32_t ambTuningWindowFactor_K;
julientiron 0:1cb50d31c3b5 2337
julientiron 0:1cb50d31c3b5 2338
julientiron 0:1cb50d31c3b5 2339 ambTuningWindowFactor_K = _DMaxData(ambTuningWindowFactor_K);
julientiron 0:1cb50d31c3b5 2340 snrLimit_K = _DMaxData(snrLimit_K);
julientiron 0:1cb50d31c3b5 2341 retSignalAt400mm = _DMaxData(retSignalAt400mm); /* 12 to 18 bits Kcps */
julientiron 0:1cb50d31c3b5 2342 if( rate > 0 ){
julientiron 0:1cb50d31c3b5 2343 DMaxSq = 400*400*1000 / rate -(400*400/330); /* K of (1/RtnAmb -1/330 )=> 30bit- (12-18)bit => 12-18 bits*/
julientiron 0:1cb50d31c3b5 2344 if( DMaxSq<= 0){
julientiron 0:1cb50d31c3b5 2345 RawDMax = 0;
julientiron 0:1cb50d31c3b5 2346 }
julientiron 0:1cb50d31c3b5 2347 else{
julientiron 0:1cb50d31c3b5 2348 /* value can be more 32 bit so base on raneg apply *retSignalAt400mm before or after division to presevr accuracy */
julientiron 0:1cb50d31c3b5 2349 if( DMaxSq< (2<<12) ){
julientiron 0:1cb50d31c3b5 2350 DMaxSq = DMaxSq*retSignalAt400mm/(snrLimit_K+ambTuningWindowFactor_K); /* max 12 + 12 to 18 -10 => 12-26 bit */
julientiron 0:1cb50d31c3b5 2351 }else{
julientiron 0:1cb50d31c3b5 2352 DMaxSq = DMaxSq/(snrLimit_K+ambTuningWindowFactor_K)*retSignalAt400mm; /* 12 to 18 -10 + 12 to 18 *=> 12-26 bit */
julientiron 0:1cb50d31c3b5 2353 }
julientiron 0:1cb50d31c3b5 2354 RawDMax=VL6180x_SqrtUint32(DMaxSq);
julientiron 0:1cb50d31c3b5 2355 }
julientiron 0:1cb50d31c3b5 2356 }
julientiron 0:1cb50d31c3b5 2357 else{
julientiron 0:1cb50d31c3b5 2358 RawDMax = 0x7FFFFFFF; /* bigest possibmle 32bit signed value */
julientiron 0:1cb50d31c3b5 2359 }
julientiron 0:1cb50d31c3b5 2360 return RawDMax;
julientiron 0:1cb50d31c3b5 2361 }
julientiron 0:1cb50d31c3b5 2362
julientiron 0:1cb50d31c3b5 2363 /*
julientiron 0:1cb50d31c3b5 2364 * fetch static data from register to avoid re-read
julientiron 0:1cb50d31c3b5 2365 * precompute all intermediate constant and cliipings
julientiron 0:1cb50d31c3b5 2366 *
julientiron 0:1cb50d31c3b5 2367 * to be re-used/call on changes of :
julientiron 0:1cb50d31c3b5 2368 * 0x2A
julientiron 0:1cb50d31c3b5 2369 * SYSRANGE_MAX_AMBIENT_LEVEL_MULT
julientiron 0:1cb50d31c3b5 2370 * Dev Data XtalkComRate_KCPs
julientiron 0:1cb50d31c3b5 2371 * SYSRANGE_MAX_CONVERGENCE_TIME
julientiron 0:1cb50d31c3b5 2372 * SYSRANGE_RANGE_CHECK_ENABLES mask RANGE_CHECK_RANGE_ENABLE_MASK
julientiron 0:1cb50d31c3b5 2373 * range 0xb8-0xbb (0xbb)
julientiron 0:1cb50d31c3b5 2374 */
julientiron 0:1cb50d31c3b5 2375 int VL6180X::_DMax_InitData(VL6180xDev_t dev){
julientiron 0:1cb50d31c3b5 2376 int status, warning;
julientiron 0:1cb50d31c3b5 2377 uint8_t u8;
julientiron 0:1cb50d31c3b5 2378 uint16_t u16;
julientiron 0:1cb50d31c3b5 2379 uint32_t u32;
julientiron 0:1cb50d31c3b5 2380 uint32_t Reg2A_KCps;
julientiron 0:1cb50d31c3b5 2381 uint32_t RegB8;
julientiron 0:1cb50d31c3b5 2382 uint8_t MaxConvTime;
julientiron 0:1cb50d31c3b5 2383 uint32_t XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 2384 uint32_t RangeIgnoreThreshold;
julientiron 0:1cb50d31c3b5 2385 int32_t minSignalNeeded;
julientiron 0:1cb50d31c3b5 2386 uint8_t SysRangeCheckEn;
julientiron 0:1cb50d31c3b5 2387 uint8_t snrLimit;
julientiron 0:1cb50d31c3b5 2388 warning=0;
julientiron 0:1cb50d31c3b5 2389
julientiron 0:1cb50d31c3b5 2390 static const int ROMABLE_DATA MaxConvTimeAdjust=-4;
julientiron 0:1cb50d31c3b5 2391
julientiron 0:1cb50d31c3b5 2392 LOG_FUNCTION_START("");
julientiron 0:1cb50d31c3b5 2393 do{
julientiron 0:1cb50d31c3b5 2394 status = VL6180x_RdByte(dev, 0x02A ,&u8);
julientiron 0:1cb50d31c3b5 2395 if( status ){
julientiron 0:1cb50d31c3b5 2396 VL6180x_ErrLog("Reg 0x02A rd fail");
julientiron 0:1cb50d31c3b5 2397 break;
julientiron 0:1cb50d31c3b5 2398 }
julientiron 0:1cb50d31c3b5 2399
julientiron 0:1cb50d31c3b5 2400 if( u8 == 0 ) {
julientiron 0:1cb50d31c3b5 2401 warning = CALIBRATION_WARNING;
julientiron 0:1cb50d31c3b5 2402 u8 = 40; /* use a default average value */
julientiron 0:1cb50d31c3b5 2403 }
julientiron 0:1cb50d31c3b5 2404 Reg2A_KCps = Fix7_2_KCPs(u8); /* convert to KCPs */
julientiron 0:1cb50d31c3b5 2405
julientiron 0:1cb50d31c3b5 2406 status = VL6180x_RdByte(dev, SYSRANGE_RANGE_CHECK_ENABLES, &SysRangeCheckEn);
julientiron 0:1cb50d31c3b5 2407 if (status) {
julientiron 0:1cb50d31c3b5 2408 VL6180x_ErrLog("SYSRANGE_RANGE_CHECK_ENABLES rd fail ");
julientiron 0:1cb50d31c3b5 2409 break;
julientiron 0:1cb50d31c3b5 2410 }
julientiron 0:1cb50d31c3b5 2411
julientiron 0:1cb50d31c3b5 2412 status = VL6180x_RdByte(dev, SYSRANGE_MAX_CONVERGENCE_TIME, &MaxConvTime);
julientiron 0:1cb50d31c3b5 2413 if( status){
julientiron 0:1cb50d31c3b5 2414 VL6180x_ErrLog("SYSRANGE_MAX_CONVERGENCE_TIME rd fail ");
julientiron 0:1cb50d31c3b5 2415 break;
julientiron 0:1cb50d31c3b5 2416 }
julientiron 0:1cb50d31c3b5 2417
julientiron 0:1cb50d31c3b5 2418 status = VL6180x_RdDWord(dev, 0x0B8, &RegB8);
julientiron 0:1cb50d31c3b5 2419 if( status ){
julientiron 0:1cb50d31c3b5 2420 VL6180x_ErrLog("reg 0x0B8 rd fail ");
julientiron 0:1cb50d31c3b5 2421 break;
julientiron 0:1cb50d31c3b5 2422 }
julientiron 0:1cb50d31c3b5 2423
julientiron 0:1cb50d31c3b5 2424 status = VL6180x_RdByte(dev, SYSRANGE_MAX_AMBIENT_LEVEL_MULT, &snrLimit);
julientiron 0:1cb50d31c3b5 2425 if( status){
julientiron 0:1cb50d31c3b5 2426 VL6180x_ErrLog("SYSRANGE_MAX_AMBIENT_LEVEL_MULT rd fail ");
julientiron 0:1cb50d31c3b5 2427 break;
julientiron 0:1cb50d31c3b5 2428 }
julientiron 0:1cb50d31c3b5 2429 _DMaxData(snrLimit_K) = (int32_t)16*1000/snrLimit;
julientiron 0:1cb50d31c3b5 2430 XTalkCompRate_KCps = VL6180xDevDataGet(dev, XTalkCompRate_KCps );
julientiron 0:1cb50d31c3b5 2431
julientiron 0:1cb50d31c3b5 2432 if( Reg2A_KCps >= XTalkCompRate_KCps){
julientiron 0:1cb50d31c3b5 2433 _DMaxData(retSignalAt400mm)=( Reg2A_KCps - XTalkCompRate_KCps);
julientiron 0:1cb50d31c3b5 2434 }
julientiron 0:1cb50d31c3b5 2435 else{
julientiron 0:1cb50d31c3b5 2436 _DMaxData(retSignalAt400mm)=0; /* Reg2A_K - XTalkCompRate_KCp <0 is invalid */
julientiron 0:1cb50d31c3b5 2437 }
julientiron 0:1cb50d31c3b5 2438
julientiron 0:1cb50d31c3b5 2439 /* if xtalk range check is off omit it in snr clipping */
julientiron 0:1cb50d31c3b5 2440 if( SysRangeCheckEn&RANGE_CHECK_RANGE_ENABLE_MASK ){
julientiron 0:1cb50d31c3b5 2441 status = VL6180x_RdWord(dev, SYSRANGE_RANGE_IGNORE_THRESHOLD, &u16);
julientiron 0:1cb50d31c3b5 2442 if( status){
julientiron 0:1cb50d31c3b5 2443 VL6180x_ErrLog("SYSRANGE_RANGE_IGNORE_THRESHOLD rd fail ");
julientiron 0:1cb50d31c3b5 2444 break;
julientiron 0:1cb50d31c3b5 2445 }
julientiron 0:1cb50d31c3b5 2446 RangeIgnoreThreshold = Fix7_2_KCPs(u16);
julientiron 0:1cb50d31c3b5 2447 }
julientiron 0:1cb50d31c3b5 2448 else{
julientiron 0:1cb50d31c3b5 2449 RangeIgnoreThreshold = 0;
julientiron 0:1cb50d31c3b5 2450 }
julientiron 0:1cb50d31c3b5 2451
julientiron 0:1cb50d31c3b5 2452 minSignalNeeded = (RegB8*256)/((int32_t)MaxConvTime+(int32_t)MaxConvTimeAdjust); /* KCps 8+8 bit -(1 to 6 bit) => 15-10 bit */
julientiron 0:1cb50d31c3b5 2453 /* minSignalNeeded = max ( minSignalNeeded, RangeIgnoreThreshold - XTalkCompRate_KCps) */
julientiron 0:1cb50d31c3b5 2454 if( minSignalNeeded <= RangeIgnoreThreshold - XTalkCompRate_KCps )
julientiron 0:1cb50d31c3b5 2455 minSignalNeeded = RangeIgnoreThreshold - XTalkCompRate_KCps;
julientiron 0:1cb50d31c3b5 2456
julientiron 0:1cb50d31c3b5 2457 u32 = (minSignalNeeded*(uint32_t)snrLimit)/16;
julientiron 0:1cb50d31c3b5 2458 _DMaxData(ClipSnrLimit ) = _DMax_RawValueAtRateKCps(dev, u32 ); /* clip to dmax to min signal snr limit rate*/
julientiron 0:1cb50d31c3b5 2459 }
julientiron 0:1cb50d31c3b5 2460 while(0);
julientiron 0:1cb50d31c3b5 2461 if( !status )
julientiron 0:1cb50d31c3b5 2462 status = warning;
julientiron 0:1cb50d31c3b5 2463 LOG_FUNCTION_END(status);
julientiron 0:1cb50d31c3b5 2464 return status;
julientiron 0:1cb50d31c3b5 2465 }
julientiron 0:1cb50d31c3b5 2466
julientiron 0:1cb50d31c3b5 2467 static int _DMax_Compute(VL6180xDev_t dev, VL6180x_RangeData_t *pRange){
julientiron 0:1cb50d31c3b5 2468 uint32_t rtnAmbRate;
julientiron 0:1cb50d31c3b5 2469 int32_t DMax;
julientiron 0:1cb50d31c3b5 2470 int scaling;
julientiron 0:1cb50d31c3b5 2471 uint16_t HwLimitAtScale;
julientiron 0:1cb50d31c3b5 2472 static const int ROMABLE_DATA rtnAmbLowLimit_KCps=330*1000;
julientiron 0:1cb50d31c3b5 2473
julientiron 0:1cb50d31c3b5 2474 rtnAmbRate = pRange->rtnAmbRate;
julientiron 0:1cb50d31c3b5 2475 if( rtnAmbRate < rtnAmbLowLimit_KCps ){
julientiron 0:1cb50d31c3b5 2476 DMax = _DMax_RawValueAtRateKCps( dev, rtnAmbRate);
julientiron 0:1cb50d31c3b5 2477 scaling = _GetUpscale(dev);
julientiron 0:1cb50d31c3b5 2478 HwLimitAtScale=UpperLimitLookUP[scaling - 1];
julientiron 0:1cb50d31c3b5 2479
julientiron 0:1cb50d31c3b5 2480 if( DMax > _DMaxData(ClipSnrLimit) ){
julientiron 0:1cb50d31c3b5 2481 DMax=_DMaxData(ClipSnrLimit);
julientiron 0:1cb50d31c3b5 2482 }
julientiron 0:1cb50d31c3b5 2483 if( DMax > HwLimitAtScale ){
julientiron 0:1cb50d31c3b5 2484 DMax=HwLimitAtScale;
julientiron 0:1cb50d31c3b5 2485 }
julientiron 0:1cb50d31c3b5 2486 pRange->DMax=DMax;
julientiron 0:1cb50d31c3b5 2487 }
julientiron 0:1cb50d31c3b5 2488 else{
julientiron 0:1cb50d31c3b5 2489 pRange->DMax = 0;
julientiron 0:1cb50d31c3b5 2490 }
julientiron 0:1cb50d31c3b5 2491 return 0;
julientiron 0:1cb50d31c3b5 2492 }
julientiron 0:1cb50d31c3b5 2493
julientiron 0:1cb50d31c3b5 2494 #undef _DMaxData
julientiron 0:1cb50d31c3b5 2495 #undef Fix7_2_KCPs
julientiron 0:1cb50d31c3b5 2496
julientiron 0:1cb50d31c3b5 2497 #endif /* VL6180x_HAVE_DMAX_RANGING */
julientiron 0:1cb50d31c3b5 2498
julientiron 0:1cb50d31c3b5 2499
julientiron 0:1cb50d31c3b5 2500 /******************************************************************************/
julientiron 0:1cb50d31c3b5 2501 /******************************************************************************/
julientiron 0:1cb50d31c3b5 2502
julientiron 0:1cb50d31c3b5 2503
julientiron 0:1cb50d31c3b5 2504
julientiron 0:1cb50d31c3b5 2505 /****************** Write and read functions from I2C *************************/
julientiron 0:1cb50d31c3b5 2506
julientiron 0:1cb50d31c3b5 2507 int VL6180X::VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data)
julientiron 0:1cb50d31c3b5 2508 {
julientiron 0:1cb50d31c3b5 2509 int status;
julientiron 0:1cb50d31c3b5 2510
julientiron 0:1cb50d31c3b5 2511 status=VL6180x_I2CWrite(Device->I2cAddr, index, &data,(uint8_t)1);
julientiron 0:1cb50d31c3b5 2512 return status;
julientiron 0:1cb50d31c3b5 2513 }
julientiron 0:1cb50d31c3b5 2514
julientiron 0:1cb50d31c3b5 2515 int VL6180X::VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data)
julientiron 0:1cb50d31c3b5 2516 {
julientiron 0:1cb50d31c3b5 2517 int status;
julientiron 0:1cb50d31c3b5 2518
julientiron 0:1cb50d31c3b5 2519 status=VL6180x_I2CWrite(Device->I2cAddr, index, (uint8_t *)&data,(uint8_t)2);
julientiron 0:1cb50d31c3b5 2520 return status;
julientiron 0:1cb50d31c3b5 2521 }
julientiron 0:1cb50d31c3b5 2522
julientiron 0:1cb50d31c3b5 2523 int VL6180X::VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data)
julientiron 0:1cb50d31c3b5 2524 {
julientiron 0:1cb50d31c3b5 2525 int status;
julientiron 0:1cb50d31c3b5 2526
julientiron 0:1cb50d31c3b5 2527 status=VL6180x_I2CWrite(Device->I2cAddr, index, (uint8_t *)&data,(uint8_t)4);
julientiron 0:1cb50d31c3b5 2528 return status;
julientiron 0:1cb50d31c3b5 2529 }
julientiron 0:1cb50d31c3b5 2530
julientiron 0:1cb50d31c3b5 2531 int VL6180X::VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data)
julientiron 0:1cb50d31c3b5 2532 {
julientiron 0:1cb50d31c3b5 2533 int status;
julientiron 0:1cb50d31c3b5 2534
julientiron 0:1cb50d31c3b5 2535 uint8_t buffer;
julientiron 0:1cb50d31c3b5 2536 status=VL6180x_I2CRead(Device->I2cAddr, index, &buffer,1);
julientiron 0:1cb50d31c3b5 2537 if(!status)
julientiron 0:1cb50d31c3b5 2538 {
julientiron 0:1cb50d31c3b5 2539 *data=buffer;
julientiron 0:1cb50d31c3b5 2540 }
julientiron 0:1cb50d31c3b5 2541 return status;
julientiron 0:1cb50d31c3b5 2542 }
julientiron 0:1cb50d31c3b5 2543
julientiron 0:1cb50d31c3b5 2544 int VL6180X::VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data)
julientiron 0:1cb50d31c3b5 2545 {
julientiron 0:1cb50d31c3b5 2546 int status;
julientiron 0:1cb50d31c3b5 2547
julientiron 0:1cb50d31c3b5 2548 uint8_t buffer[2];
julientiron 0:1cb50d31c3b5 2549 status=VL6180x_I2CRead(Device->I2cAddr, index, buffer, 2);
julientiron 0:1cb50d31c3b5 2550 if(!status)
julientiron 0:1cb50d31c3b5 2551 {
julientiron 0:1cb50d31c3b5 2552 memcpy(data, buffer, 2);
julientiron 0:1cb50d31c3b5 2553 }
julientiron 0:1cb50d31c3b5 2554 return status;
julientiron 0:1cb50d31c3b5 2555 }
julientiron 0:1cb50d31c3b5 2556
julientiron 0:1cb50d31c3b5 2557 int VL6180X::VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data)
julientiron 0:1cb50d31c3b5 2558 {
julientiron 0:1cb50d31c3b5 2559 int status;
julientiron 0:1cb50d31c3b5 2560 uint8_t buffer[4];
julientiron 0:1cb50d31c3b5 2561 status=VL6180x_I2CRead(Device->I2cAddr, index, buffer,4);
julientiron 0:1cb50d31c3b5 2562 if(!status)
julientiron 0:1cb50d31c3b5 2563 {
julientiron 0:1cb50d31c3b5 2564 memcpy(data, buffer, 4);
julientiron 0:1cb50d31c3b5 2565 }
julientiron 0:1cb50d31c3b5 2566 return status;
julientiron 0:1cb50d31c3b5 2567 }
julientiron 0:1cb50d31c3b5 2568
julientiron 0:1cb50d31c3b5 2569 int VL6180X::VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData)
julientiron 0:1cb50d31c3b5 2570 {
julientiron 0:1cb50d31c3b5 2571 int status;
julientiron 0:1cb50d31c3b5 2572 uint8_t buffer;
julientiron 0:1cb50d31c3b5 2573
julientiron 0:1cb50d31c3b5 2574 status=VL6180x_I2CWrite(Device->I2cAddr, index, (uint8_t *)buffer,(uint8_t)0);
julientiron 0:1cb50d31c3b5 2575 if(!status)
julientiron 0:1cb50d31c3b5 2576 {
julientiron 0:1cb50d31c3b5 2577 /* read data direct onto buffer */
julientiron 0:1cb50d31c3b5 2578 status=VL6180x_I2CRead(Device->I2cAddr, index, &buffer,1);
julientiron 0:1cb50d31c3b5 2579 if(!status)
julientiron 0:1cb50d31c3b5 2580 {
julientiron 0:1cb50d31c3b5 2581 buffer=(buffer & AndData)|OrData;
julientiron 0:1cb50d31c3b5 2582 status=VL6180x_I2CWrite(Device->I2cAddr, index, &buffer, (uint8_t)1);
julientiron 0:1cb50d31c3b5 2583 }
julientiron 0:1cb50d31c3b5 2584 }
julientiron 0:1cb50d31c3b5 2585 return status;
julientiron 0:1cb50d31c3b5 2586 }
julientiron 0:1cb50d31c3b5 2587
julientiron 0:1cb50d31c3b5 2588 int VL6180X::VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
julientiron 0:1cb50d31c3b5 2589 {
julientiron 0:1cb50d31c3b5 2590 int ret;
julientiron 0:1cb50d31c3b5 2591 int i;
julientiron 0:1cb50d31c3b5 2592 uint8_t tmp[TEMP_BUF_SIZE];
julientiron 0:1cb50d31c3b5 2593 uint16_t myRegisterAddr = RegisterAddr;
julientiron 0:1cb50d31c3b5 2594 uint16_t WriteDeviceAddr=0;
julientiron 0:1cb50d31c3b5 2595
julientiron 0:1cb50d31c3b5 2596 /* First, prepare 8 bits device address in 7bits i2ci format */
julientiron 0:1cb50d31c3b5 2597 WriteDeviceAddr=DeviceAddr*2;
julientiron 0:1cb50d31c3b5 2598 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
julientiron 0:1cb50d31c3b5 2599
julientiron 0:1cb50d31c3b5 2600 /* then prepare 16 bits register address in BE format. Then, send data and STOP condition */
julientiron 0:1cb50d31c3b5 2601 tmp[0] = *(((uint8_t*)&myRegisterAddr)+1);
julientiron 0:1cb50d31c3b5 2602 tmp[1] = (uint8_t)RegisterAddr;
julientiron 0:1cb50d31c3b5 2603
julientiron 0:1cb50d31c3b5 2604 if(NumByteToWrite>1) /* swap data endianess */
julientiron 0:1cb50d31c3b5 2605 {
julientiron 0:1cb50d31c3b5 2606 for(i=0;i<NumByteToWrite;i++)
julientiron 0:1cb50d31c3b5 2607 {
julientiron 0:1cb50d31c3b5 2608 tmp[NumByteToWrite+sizeof(RegisterAddr)-1-i]=pBuffer[i];
julientiron 0:1cb50d31c3b5 2609 }
julientiron 0:1cb50d31c3b5 2610 }
julientiron 0:1cb50d31c3b5 2611 else
julientiron 0:1cb50d31c3b5 2612 {
julientiron 0:1cb50d31c3b5 2613 memcpy(tmp+sizeof(RegisterAddr), pBuffer, NumByteToWrite);
julientiron 0:1cb50d31c3b5 2614 }
julientiron 0:1cb50d31c3b5 2615 ret = dev_i2c.write(WriteDeviceAddr, (const char*)tmp, NumByteToWrite+sizeof(RegisterAddr), false);
julientiron 0:1cb50d31c3b5 2616
julientiron 0:1cb50d31c3b5 2617 if(ret)
julientiron 0:1cb50d31c3b5 2618 return -1;
julientiron 0:1cb50d31c3b5 2619 return 0;
julientiron 0:1cb50d31c3b5 2620 }
julientiron 0:1cb50d31c3b5 2621
julientiron 0:1cb50d31c3b5 2622 int VL6180X::VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
julientiron 0:1cb50d31c3b5 2623 {
julientiron 0:1cb50d31c3b5 2624 int ret,i;
julientiron 0:1cb50d31c3b5 2625 uint8_t tmp[TEMP_BUF_SIZE];
julientiron 0:1cb50d31c3b5 2626 uint16_t myRegisterAddr = RegisterAddr;
julientiron 0:1cb50d31c3b5 2627 uint16_t myRegisterAddrBE;
julientiron 0:1cb50d31c3b5 2628 uint16_t ReadDeviceAddr=DeviceAddr;
julientiron 0:1cb50d31c3b5 2629
julientiron 0:1cb50d31c3b5 2630 ReadDeviceAddr=DeviceAddr*2;
julientiron 0:1cb50d31c3b5 2631 myRegisterAddrBE = *(((uint8_t*)&myRegisterAddr)+1);
julientiron 0:1cb50d31c3b5 2632 *(((uint8_t*)&myRegisterAddrBE)+1) = (uint8_t)myRegisterAddr;
julientiron 0:1cb50d31c3b5 2633
julientiron 0:1cb50d31c3b5 2634 /* Send 8 bits device address and 16 bits register address in BE format, with no STOP condition */
julientiron 0:1cb50d31c3b5 2635 ret = dev_i2c.write(ReadDeviceAddr, (const char*)&myRegisterAddrBE, sizeof(RegisterAddr), true);
julientiron 0:1cb50d31c3b5 2636 if(!ret)
julientiron 0:1cb50d31c3b5 2637 {
julientiron 0:1cb50d31c3b5 2638 ReadDeviceAddr|=0x001;
julientiron 0:1cb50d31c3b5 2639 /* Read data, with STOP condition */
julientiron 0:1cb50d31c3b5 2640 ret = dev_i2c.read(ReadDeviceAddr, (char*)tmp, NumByteToRead, false);
julientiron 0:1cb50d31c3b5 2641 }
julientiron 0:1cb50d31c3b5 2642 if(ret)
julientiron 0:1cb50d31c3b5 2643 return -1;
julientiron 0:1cb50d31c3b5 2644
julientiron 0:1cb50d31c3b5 2645 if(NumByteToRead>1) /* swap data endianess */
julientiron 0:1cb50d31c3b5 2646 {
julientiron 0:1cb50d31c3b5 2647 for(i=0;i<NumByteToRead;i++)
julientiron 0:1cb50d31c3b5 2648 {
julientiron 0:1cb50d31c3b5 2649 pBuffer[i] = tmp[NumByteToRead-1-i];
julientiron 0:1cb50d31c3b5 2650 }
julientiron 0:1cb50d31c3b5 2651 }
julientiron 0:1cb50d31c3b5 2652 else
julientiron 0:1cb50d31c3b5 2653 {
julientiron 0:1cb50d31c3b5 2654 memcpy(pBuffer, tmp, NumByteToRead);
julientiron 0:1cb50d31c3b5 2655 }
julientiron 0:1cb50d31c3b5 2656 return 0;
julientiron 0:1cb50d31c3b5 2657 }
julientiron 0:1cb50d31c3b5 2658
julientiron 0:1cb50d31c3b5 2659 /******************************************************************************/
julientiron 0:1cb50d31c3b5 2660
julientiron 0:1cb50d31c3b5 2661 int VL6180X::AlsSetThresholds(uint16_t lux_threshold_low, uint16_t lux_threshold_high)
julientiron 0:1cb50d31c3b5 2662 {
julientiron 0:1cb50d31c3b5 2663 uint32_t AlsAnGain, IntPeriod, AlsScaler, GainFix, RawAlsHigh, RawAlsLow;
julientiron 0:1cb50d31c3b5 2664 uint16_t RawThreshLow, RawThreshHigh;
julientiron 0:1cb50d31c3b5 2665 const uint32_t LuxResxIntIme =(uint32_t)(0.56f* DEF_INT_PEFRIOD *(1<<LUXRES_FIX_PREC));
julientiron 0:1cb50d31c3b5 2666 void *p_low;
julientiron 0:1cb50d31c3b5 2667 void *p_high;
julientiron 0:1cb50d31c3b5 2668
julientiron 0:1cb50d31c3b5 2669 AlsAnGain=VL6180xDevDataGet(Device, AlsGainCode);
julientiron 0:1cb50d31c3b5 2670 IntPeriod=VL6180xDevDataGet(Device, IntegrationPeriod);
julientiron 0:1cb50d31c3b5 2671 AlsScaler=VL6180xDevDataGet(Device, AlsScaler);
julientiron 0:1cb50d31c3b5 2672 GainFix=AlsGainLookUp[AlsAnGain];
julientiron 0:1cb50d31c3b5 2673 IntPeriod++;
julientiron 0:1cb50d31c3b5 2674 RawAlsLow=lux_threshold_low*AlsScaler*GainFix;
julientiron 0:1cb50d31c3b5 2675 RawAlsLow=RawAlsLow*IntPeriod;
julientiron 0:1cb50d31c3b5 2676 RawAlsLow=RawAlsLow/LuxResxIntIme;
julientiron 0:1cb50d31c3b5 2677 RawAlsHigh=lux_threshold_high*(AlsScaler*GainFix);
julientiron 0:1cb50d31c3b5 2678 RawAlsHigh=RawAlsHigh*IntPeriod;
julientiron 0:1cb50d31c3b5 2679 RawAlsHigh=RawAlsHigh/LuxResxIntIme;
julientiron 0:1cb50d31c3b5 2680 p_low=&RawAlsLow;
julientiron 0:1cb50d31c3b5 2681 RawThreshLow=*(uint16_t*)p_low;
julientiron 0:1cb50d31c3b5 2682 p_high=&RawAlsHigh;
julientiron 0:1cb50d31c3b5 2683 RawThreshHigh=*(uint16_t*)p_high;
julientiron 0:1cb50d31c3b5 2684 return VL6180x_AlsSetThresholds(Device, RawThreshLow, RawThreshHigh);
julientiron 0:1cb50d31c3b5 2685 }
julientiron 0:1cb50d31c3b5 2686
julientiron 0:1cb50d31c3b5 2687
julientiron 0:1cb50d31c3b5 2688 int VL6180X::ReadID()
julientiron 0:1cb50d31c3b5 2689 {
julientiron 0:1cb50d31c3b5 2690 int status;
julientiron 0:1cb50d31c3b5 2691 uint8_t rl_id=0;
julientiron 0:1cb50d31c3b5 2692
julientiron 0:1cb50d31c3b5 2693 status=VL6180x_RdByte(Device, IDENTIFICATION_MODEL_ID, &rl_id);
julientiron 0:1cb50d31c3b5 2694 if((status==0)&&(rl_id==0xB4))
julientiron 0:1cb50d31c3b5 2695 return status;
julientiron 0:1cb50d31c3b5 2696 else
julientiron 0:1cb50d31c3b5 2697 return INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 2698 }
julientiron 0:1cb50d31c3b5 2699
julientiron 0:1cb50d31c3b5 2700
julientiron 0:1cb50d31c3b5 2701 int VL6180X::InitSensor(uint8_t NewAddr)
julientiron 0:1cb50d31c3b5 2702 {
julientiron 0:1cb50d31c3b5 2703 int status;
julientiron 0:1cb50d31c3b5 2704
julientiron 0:1cb50d31c3b5 2705 VL6180x_Off();
julientiron 0:1cb50d31c3b5 2706 VL6180x_On();
julientiron 0:1cb50d31c3b5 2707 status=VL6180x_WaitDeviceBooted(Device);
julientiron 0:1cb50d31c3b5 2708 if(status)
julientiron 0:1cb50d31c3b5 2709 VL6180x_ErrLog("WaitDeviceBooted fail\n\r");
julientiron 0:1cb50d31c3b5 2710 status=IsPresent();
julientiron 0:1cb50d31c3b5 2711 if(!status)
julientiron 0:1cb50d31c3b5 2712 {
julientiron 0:1cb50d31c3b5 2713 Device->Present=1;
julientiron 0:1cb50d31c3b5 2714 status=Init();
julientiron 0:1cb50d31c3b5 2715 if(status)
julientiron 0:1cb50d31c3b5 2716 {
julientiron 0:1cb50d31c3b5 2717 printf("Failed to init VL6180X sensor!\n\r");
julientiron 0:1cb50d31c3b5 2718 return status;
julientiron 0:1cb50d31c3b5 2719 }
julientiron 0:1cb50d31c3b5 2720 status=Prepare();
julientiron 0:1cb50d31c3b5 2721 if(status)
julientiron 0:1cb50d31c3b5 2722 {
julientiron 0:1cb50d31c3b5 2723 printf("Failed to prepare VL6180X!\n\r");
julientiron 0:1cb50d31c3b5 2724 return status;
julientiron 0:1cb50d31c3b5 2725 }
julientiron 0:1cb50d31c3b5 2726 if(NewAddr!=DEFAULT_DEVICE_ADDRESS)
julientiron 0:1cb50d31c3b5 2727 {
julientiron 0:1cb50d31c3b5 2728 status=SetI2CAddress(NewAddr);
julientiron 0:1cb50d31c3b5 2729 if(status)
julientiron 0:1cb50d31c3b5 2730 {
julientiron 0:1cb50d31c3b5 2731 printf("Failed to change I2C address!\n\r");
julientiron 0:1cb50d31c3b5 2732 return status;
julientiron 0:1cb50d31c3b5 2733 }
julientiron 0:1cb50d31c3b5 2734 }
julientiron 0:1cb50d31c3b5 2735 else
julientiron 0:1cb50d31c3b5 2736 {
julientiron 0:1cb50d31c3b5 2737 printf("Invalid new address!\n\r");
julientiron 0:1cb50d31c3b5 2738 return INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 2739 }
julientiron 0:1cb50d31c3b5 2740 Device->Ready=1;
julientiron 0:1cb50d31c3b5 2741 }
julientiron 0:1cb50d31c3b5 2742 return status;
julientiron 0:1cb50d31c3b5 2743 }
julientiron 0:1cb50d31c3b5 2744
julientiron 0:1cb50d31c3b5 2745
julientiron 0:1cb50d31c3b5 2746 int VL6180X::StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), uint16_t low, uint16_t high)
julientiron 0:1cb50d31c3b5 2747 {
julientiron 0:1cb50d31c3b5 2748 int status, r_status, l_status;
julientiron 0:1cb50d31c3b5 2749
julientiron 0:1cb50d31c3b5 2750 switch(operating_mode)
julientiron 0:1cb50d31c3b5 2751 {
julientiron 0:1cb50d31c3b5 2752 case(range_single_shot_polling):
julientiron 0:1cb50d31c3b5 2753 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2754 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2755 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2756 return RangeMeasPollSingleShot();
julientiron 0:1cb50d31c3b5 2757 else
julientiron 0:1cb50d31c3b5 2758 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2759
julientiron 0:1cb50d31c3b5 2760 case(als_single_shot_polling):
julientiron 0:1cb50d31c3b5 2761 r_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2762 l_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2763 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2764 return AlsMeasPollSingleShot();
julientiron 0:1cb50d31c3b5 2765 else
julientiron 0:1cb50d31c3b5 2766 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2767
julientiron 0:1cb50d31c3b5 2768 case(range_continuous_polling):
julientiron 0:1cb50d31c3b5 2769 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2770 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2771 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2772 return RangeMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2773 else
julientiron 0:1cb50d31c3b5 2774 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2775
julientiron 0:1cb50d31c3b5 2776 case(als_continuous_polling):
julientiron 0:1cb50d31c3b5 2777 r_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2778 l_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2779 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2780 return AlsMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2781 else
julientiron 0:1cb50d31c3b5 2782 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2783
julientiron 0:1cb50d31c3b5 2784 case(range_continuous_interrupt):
julientiron 0:1cb50d31c3b5 2785 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2786 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2787 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2788 return RangeMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2789 else
julientiron 0:1cb50d31c3b5 2790 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2791
julientiron 0:1cb50d31c3b5 2792 case(als_continuous_interrupt):
julientiron 0:1cb50d31c3b5 2793 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2794 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2795 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2796 return AlsMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2797 else
julientiron 0:1cb50d31c3b5 2798 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2799
julientiron 0:1cb50d31c3b5 2800 case(interleaved_mode_interrupt):
julientiron 0:1cb50d31c3b5 2801 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
julientiron 0:1cb50d31c3b5 2802 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2803 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2804 return InterleavedMode(fptr);
julientiron 0:1cb50d31c3b5 2805 else
julientiron 0:1cb50d31c3b5 2806 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2807
julientiron 0:1cb50d31c3b5 2808 case(range_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 2809 r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW);
julientiron 0:1cb50d31c3b5 2810 l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2811 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2812 {
julientiron 0:1cb50d31c3b5 2813 status=RangeSetLowThreshold(low);
julientiron 0:1cb50d31c3b5 2814 if(!status)
julientiron 0:1cb50d31c3b5 2815 return RangeMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2816 else
julientiron 0:1cb50d31c3b5 2817 return status;
julientiron 0:1cb50d31c3b5 2818 }
julientiron 0:1cb50d31c3b5 2819 else
julientiron 0:1cb50d31c3b5 2820 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2821
julientiron 0:1cb50d31c3b5 2822 case(range_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 2823 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH);
julientiron 0:1cb50d31c3b5 2824 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2825 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2826 {
julientiron 0:1cb50d31c3b5 2827 status=RangeSetHighThreshold(high);
julientiron 0:1cb50d31c3b5 2828 if(!status)
julientiron 0:1cb50d31c3b5 2829 return RangeMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2830 else
julientiron 0:1cb50d31c3b5 2831 return status;
julientiron 0:1cb50d31c3b5 2832 }
julientiron 0:1cb50d31c3b5 2833 else
julientiron 0:1cb50d31c3b5 2834 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2835
julientiron 0:1cb50d31c3b5 2836 case(range_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 2837 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW);
julientiron 0:1cb50d31c3b5 2838 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2839 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2840 {
julientiron 0:1cb50d31c3b5 2841 status=VL6180x_RangeSetThresholds(Device,low,high,1);
julientiron 0:1cb50d31c3b5 2842 if(!status)
julientiron 0:1cb50d31c3b5 2843 return RangeMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2844 else
julientiron 0:1cb50d31c3b5 2845 return status;
julientiron 0:1cb50d31c3b5 2846 }
julientiron 0:1cb50d31c3b5 2847 else
julientiron 0:1cb50d31c3b5 2848 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2849
julientiron 0:1cb50d31c3b5 2850 case(als_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 2851 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW);
julientiron 0:1cb50d31c3b5 2852 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2853 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2854 {
julientiron 0:1cb50d31c3b5 2855 status=AlsSetLowThreshold(low);
julientiron 0:1cb50d31c3b5 2856 if(!status)
julientiron 0:1cb50d31c3b5 2857 return AlsMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2858 else
julientiron 0:1cb50d31c3b5 2859 return status;
julientiron 0:1cb50d31c3b5 2860 }
julientiron 0:1cb50d31c3b5 2861 else
julientiron 0:1cb50d31c3b5 2862 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2863
julientiron 0:1cb50d31c3b5 2864 case(als_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 2865 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH);
julientiron 0:1cb50d31c3b5 2866 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2867 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2868 {
julientiron 0:1cb50d31c3b5 2869 status=AlsSetHighThreshold(high);
julientiron 0:1cb50d31c3b5 2870 if(!status)
julientiron 0:1cb50d31c3b5 2871 return AlsMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2872 else
julientiron 0:1cb50d31c3b5 2873 return status;
julientiron 0:1cb50d31c3b5 2874 }
julientiron 0:1cb50d31c3b5 2875 else
julientiron 0:1cb50d31c3b5 2876 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2877
julientiron 0:1cb50d31c3b5 2878 case(als_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 2879 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW);
julientiron 0:1cb50d31c3b5 2880 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2881 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2882 {
julientiron 0:1cb50d31c3b5 2883 status=VL6180x_AlsSetThresholds(Device,low,high);
julientiron 0:1cb50d31c3b5 2884 if(!status)
julientiron 0:1cb50d31c3b5 2885 return AlsMeasPollContinuousMode();
julientiron 0:1cb50d31c3b5 2886 else
julientiron 0:1cb50d31c3b5 2887 return status;
julientiron 0:1cb50d31c3b5 2888 }
julientiron 0:1cb50d31c3b5 2889 else
julientiron 0:1cb50d31c3b5 2890 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2891
julientiron 0:1cb50d31c3b5 2892 case(range_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 2893 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW);
julientiron 0:1cb50d31c3b5 2894 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2895 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2896 {
julientiron 0:1cb50d31c3b5 2897 status=RangeSetLowThreshold(low);
julientiron 0:1cb50d31c3b5 2898 if(!status)
julientiron 0:1cb50d31c3b5 2899 return RangeMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2900 else
julientiron 0:1cb50d31c3b5 2901 return status;
julientiron 0:1cb50d31c3b5 2902 }
julientiron 0:1cb50d31c3b5 2903 else
julientiron 0:1cb50d31c3b5 2904 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2905
julientiron 0:1cb50d31c3b5 2906 case(range_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 2907 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH);
julientiron 0:1cb50d31c3b5 2908 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2909 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2910 {
julientiron 0:1cb50d31c3b5 2911 status=RangeSetHighThreshold(high);
julientiron 0:1cb50d31c3b5 2912 if(!status)
julientiron 0:1cb50d31c3b5 2913 return RangeMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2914 else
julientiron 0:1cb50d31c3b5 2915 return status;
julientiron 0:1cb50d31c3b5 2916 }
julientiron 0:1cb50d31c3b5 2917 else
julientiron 0:1cb50d31c3b5 2918 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2919
julientiron 0:1cb50d31c3b5 2920 case(range_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 2921 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW);
julientiron 0:1cb50d31c3b5 2922 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2923 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2924 {
julientiron 0:1cb50d31c3b5 2925 status=VL6180x_RangeSetThresholds(Device,low,high,1);
julientiron 0:1cb50d31c3b5 2926 if(!status)
julientiron 0:1cb50d31c3b5 2927 return RangeMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2928 else
julientiron 0:1cb50d31c3b5 2929 return status;
julientiron 0:1cb50d31c3b5 2930 }
julientiron 0:1cb50d31c3b5 2931 else
julientiron 0:1cb50d31c3b5 2932 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2933
julientiron 0:1cb50d31c3b5 2934 case(als_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 2935 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW);
julientiron 0:1cb50d31c3b5 2936 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2937 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2938 {
julientiron 0:1cb50d31c3b5 2939 status=AlsSetLowThreshold(low);
julientiron 0:1cb50d31c3b5 2940 if(!status)
julientiron 0:1cb50d31c3b5 2941 return AlsMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2942 else
julientiron 0:1cb50d31c3b5 2943 return status;
julientiron 0:1cb50d31c3b5 2944 }
julientiron 0:1cb50d31c3b5 2945 else
julientiron 0:1cb50d31c3b5 2946 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2947
julientiron 0:1cb50d31c3b5 2948 case(als_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 2949 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH);
julientiron 0:1cb50d31c3b5 2950 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2951 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2952 {
julientiron 0:1cb50d31c3b5 2953 status=AlsSetHighThreshold(high);
julientiron 0:1cb50d31c3b5 2954 if(!status)
julientiron 0:1cb50d31c3b5 2955 return AlsMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2956 else
julientiron 0:1cb50d31c3b5 2957 return status;
julientiron 0:1cb50d31c3b5 2958 }
julientiron 0:1cb50d31c3b5 2959 else
julientiron 0:1cb50d31c3b5 2960 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2961
julientiron 0:1cb50d31c3b5 2962 case(als_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 2963 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW);
julientiron 0:1cb50d31c3b5 2964 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 2965 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 2966 {
julientiron 0:1cb50d31c3b5 2967 status=VL6180x_AlsSetThresholds(Device,low,high);
julientiron 0:1cb50d31c3b5 2968 if(!status)
julientiron 0:1cb50d31c3b5 2969 return AlsMeasIntContinuousMode(fptr);
julientiron 0:1cb50d31c3b5 2970 else
julientiron 0:1cb50d31c3b5 2971 return status;
julientiron 0:1cb50d31c3b5 2972 }
julientiron 0:1cb50d31c3b5 2973 else
julientiron 0:1cb50d31c3b5 2974 return (r_status|l_status);
julientiron 0:1cb50d31c3b5 2975
julientiron 0:1cb50d31c3b5 2976 default:
julientiron 0:1cb50d31c3b5 2977 return INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 2978 }
julientiron 0:1cb50d31c3b5 2979 }
julientiron 0:1cb50d31c3b5 2980
julientiron 0:1cb50d31c3b5 2981
julientiron 0:1cb50d31c3b5 2982 int VL6180X::GetRangeError(MeasureData_t *Data, VL6180x_RangeData_t RangeData)
julientiron 0:1cb50d31c3b5 2983 {
julientiron 0:1cb50d31c3b5 2984 Data->range_error=RangeData.errorStatus;
julientiron 0:1cb50d31c3b5 2985 if(Data->range_error!=0)
julientiron 0:1cb50d31c3b5 2986 {
julientiron 0:1cb50d31c3b5 2987 VL6180x_ErrLog("Range error %d",Data->range_error);
julientiron 0:1cb50d31c3b5 2988 return RANGE_ERROR;
julientiron 0:1cb50d31c3b5 2989 }
julientiron 0:1cb50d31c3b5 2990 return NoError_;
julientiron 0:1cb50d31c3b5 2991 }
julientiron 0:1cb50d31c3b5 2992
julientiron 0:1cb50d31c3b5 2993
julientiron 0:1cb50d31c3b5 2994 int VL6180X::GetAlsError(MeasureData_t *Data, VL6180x_AlsData_t AlsData)
julientiron 0:1cb50d31c3b5 2995 {
julientiron 0:1cb50d31c3b5 2996 Data->als_error=AlsData.errorStatus;
julientiron 0:1cb50d31c3b5 2997 if(Data->als_error!=0)
julientiron 0:1cb50d31c3b5 2998 {
julientiron 0:1cb50d31c3b5 2999 VL6180x_ErrLog("Light error %d",Data->light_error);
julientiron 0:1cb50d31c3b5 3000 return API_ERROR;
julientiron 0:1cb50d31c3b5 3001 }
julientiron 0:1cb50d31c3b5 3002 return NoError_;
julientiron 0:1cb50d31c3b5 3003 }
julientiron 0:1cb50d31c3b5 3004
julientiron 0:1cb50d31c3b5 3005
julientiron 0:1cb50d31c3b5 3006 int VL6180X::RangeMeasPollSingleShot()
julientiron 0:1cb50d31c3b5 3007 {
julientiron 0:1cb50d31c3b5 3008 int status;
julientiron 0:1cb50d31c3b5 3009
julientiron 0:1cb50d31c3b5 3010 status=VL6180x_RangeClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3011 if(status)
julientiron 0:1cb50d31c3b5 3012 {
julientiron 0:1cb50d31c3b5 3013 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3014 return status;
julientiron 0:1cb50d31c3b5 3015 }
julientiron 0:1cb50d31c3b5 3016 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3017 if(status)
julientiron 0:1cb50d31c3b5 3018 {
julientiron 0:1cb50d31c3b5 3019 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3020 return status;
julientiron 0:1cb50d31c3b5 3021 }
julientiron 0:1cb50d31c3b5 3022 return RangeStartSingleShot();
julientiron 0:1cb50d31c3b5 3023 }
julientiron 0:1cb50d31c3b5 3024
julientiron 0:1cb50d31c3b5 3025
julientiron 0:1cb50d31c3b5 3026 int VL6180X::AlsMeasPollSingleShot()
julientiron 0:1cb50d31c3b5 3027 {
julientiron 0:1cb50d31c3b5 3028 int status;
julientiron 0:1cb50d31c3b5 3029
julientiron 0:1cb50d31c3b5 3030 status=VL6180x_AlsClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3031 if(status)
julientiron 0:1cb50d31c3b5 3032 {
julientiron 0:1cb50d31c3b5 3033 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3034 return status;
julientiron 0:1cb50d31c3b5 3035 }
julientiron 0:1cb50d31c3b5 3036 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3037 if(status)
julientiron 0:1cb50d31c3b5 3038 {
julientiron 0:1cb50d31c3b5 3039 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3040 return status;
julientiron 0:1cb50d31c3b5 3041 }
julientiron 0:1cb50d31c3b5 3042 return AlsStartSingleShot();
julientiron 0:1cb50d31c3b5 3043 }
julientiron 0:1cb50d31c3b5 3044
julientiron 0:1cb50d31c3b5 3045
julientiron 0:1cb50d31c3b5 3046 int VL6180X::RangeMeasPollContinuousMode()
julientiron 0:1cb50d31c3b5 3047 {
julientiron 0:1cb50d31c3b5 3048 int status;
julientiron 0:1cb50d31c3b5 3049
julientiron 0:1cb50d31c3b5 3050 status=VL6180x_RangeClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3051 if(status)
julientiron 0:1cb50d31c3b5 3052 {
julientiron 0:1cb50d31c3b5 3053 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3054 return status;
julientiron 0:1cb50d31c3b5 3055 }
julientiron 0:1cb50d31c3b5 3056 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3057 if(status)
julientiron 0:1cb50d31c3b5 3058 {
julientiron 0:1cb50d31c3b5 3059 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3060 return status;
julientiron 0:1cb50d31c3b5 3061 }
julientiron 0:1cb50d31c3b5 3062 return RangeStartContinuousMode();
julientiron 0:1cb50d31c3b5 3063 }
julientiron 0:1cb50d31c3b5 3064
julientiron 0:1cb50d31c3b5 3065
julientiron 0:1cb50d31c3b5 3066 int VL6180X::AlsMeasPollContinuousMode()
julientiron 0:1cb50d31c3b5 3067 {
julientiron 0:1cb50d31c3b5 3068 int status;
julientiron 0:1cb50d31c3b5 3069
julientiron 0:1cb50d31c3b5 3070 status=VL6180x_AlsClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3071 if(status)
julientiron 0:1cb50d31c3b5 3072 {
julientiron 0:1cb50d31c3b5 3073 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3074 return status;
julientiron 0:1cb50d31c3b5 3075 }
julientiron 0:1cb50d31c3b5 3076 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3077 if(status)
julientiron 0:1cb50d31c3b5 3078 {
julientiron 0:1cb50d31c3b5 3079 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3080 return status;
julientiron 0:1cb50d31c3b5 3081 }
julientiron 0:1cb50d31c3b5 3082 return AlsStartContinuousMode();
julientiron 0:1cb50d31c3b5 3083 }
julientiron 0:1cb50d31c3b5 3084
julientiron 0:1cb50d31c3b5 3085
julientiron 0:1cb50d31c3b5 3086 int VL6180X::AlsGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData)
julientiron 0:1cb50d31c3b5 3087 {
julientiron 0:1cb50d31c3b5 3088 int status;
julientiron 0:1cb50d31c3b5 3089 uint8_t IntStatus;
julientiron 0:1cb50d31c3b5 3090
julientiron 0:1cb50d31c3b5 3091 status=VL6180x_AlsGetInterruptStatus(dev, &IntStatus);
julientiron 0:1cb50d31c3b5 3092 if(!status)
julientiron 0:1cb50d31c3b5 3093 {
julientiron 0:1cb50d31c3b5 3094 if(IntStatus==RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
julientiron 0:1cb50d31c3b5 3095 {
julientiron 0:1cb50d31c3b5 3096 status = VL6180x_AlsGetMeasurement(dev, pAlsData);
julientiron 0:1cb50d31c3b5 3097 if(!status)
julientiron 0:1cb50d31c3b5 3098 {
julientiron 0:1cb50d31c3b5 3099 status=VL6180x_AlsClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3100 if(status)
julientiron 0:1cb50d31c3b5 3101 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3102 }
julientiron 0:1cb50d31c3b5 3103 }
julientiron 0:1cb50d31c3b5 3104 else
julientiron 0:1cb50d31c3b5 3105 status=NOT_READY;
julientiron 0:1cb50d31c3b5 3106 }
julientiron 0:1cb50d31c3b5 3107 else
julientiron 0:1cb50d31c3b5 3108 VL6180x_ErrLog("Failed to get interrupt status");
julientiron 0:1cb50d31c3b5 3109 return status;
julientiron 0:1cb50d31c3b5 3110 }
julientiron 0:1cb50d31c3b5 3111
julientiron 0:1cb50d31c3b5 3112
julientiron 0:1cb50d31c3b5 3113 int VL6180X::RangeMeasIntContinuousMode(void (*fptr)(void))
julientiron 0:1cb50d31c3b5 3114 {
julientiron 0:1cb50d31c3b5 3115 int status, ClrStatus;
julientiron 0:1cb50d31c3b5 3116
julientiron 0:1cb50d31c3b5 3117 EnableInterruptMeasureDetectionIRQ();
julientiron 0:1cb50d31c3b5 3118 AttachInterruptMeasureDetectionIRQ(fptr);
julientiron 0:1cb50d31c3b5 3119 status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1);
julientiron 0:1cb50d31c3b5 3120 ClrStatus=VL6180x_ClearAllInterrupt(Device);
julientiron 0:1cb50d31c3b5 3121 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3122 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3123 if(!status)
julientiron 0:1cb50d31c3b5 3124 status=RangeStartContinuousMode();
julientiron 0:1cb50d31c3b5 3125 return status;
julientiron 0:1cb50d31c3b5 3126 }
julientiron 0:1cb50d31c3b5 3127
julientiron 0:1cb50d31c3b5 3128
julientiron 0:1cb50d31c3b5 3129 int VL6180X::AlsMeasIntContinuousMode(void (*fptr)(void))
julientiron 0:1cb50d31c3b5 3130 {
julientiron 0:1cb50d31c3b5 3131 int status, ClrStatus;
julientiron 0:1cb50d31c3b5 3132
julientiron 0:1cb50d31c3b5 3133 EnableInterruptMeasureDetectionIRQ();
julientiron 0:1cb50d31c3b5 3134 AttachInterruptMeasureDetectionIRQ(fptr);
julientiron 0:1cb50d31c3b5 3135 status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1);
julientiron 0:1cb50d31c3b5 3136 ClrStatus=VL6180x_ClearAllInterrupt(Device);
julientiron 0:1cb50d31c3b5 3137 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3138 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3139 if(!status)
julientiron 0:1cb50d31c3b5 3140 status=AlsStartContinuousMode();
julientiron 0:1cb50d31c3b5 3141 return status;
julientiron 0:1cb50d31c3b5 3142 }
julientiron 0:1cb50d31c3b5 3143
julientiron 0:1cb50d31c3b5 3144
julientiron 0:1cb50d31c3b5 3145 int VL6180X::StartInterleavedMode()
julientiron 0:1cb50d31c3b5 3146 {
julientiron 0:1cb50d31c3b5 3147 int status;
julientiron 0:1cb50d31c3b5 3148 uint16_t integration_period, intermeasurement_period_ms;
julientiron 0:1cb50d31c3b5 3149 uint8_t max_convergence_time;
julientiron 0:1cb50d31c3b5 3150 uint8_t buf;
julientiron 0:1cb50d31c3b5 3151
julientiron 0:1cb50d31c3b5 3152 status=VL6180x_WrByte(Device, 0x2A3, 1);
julientiron 0:1cb50d31c3b5 3153 if(status)
julientiron 0:1cb50d31c3b5 3154 {
julientiron 0:1cb50d31c3b5 3155 VL6180x_ErrLog("Failed to write INTERLEAVED_MODE_ENABLE!\n\r");
julientiron 0:1cb50d31c3b5 3156 return status;
julientiron 0:1cb50d31c3b5 3157 }
julientiron 0:1cb50d31c3b5 3158 status=VL6180x_RdByte(Device, SYSRANGE_MAX_CONVERGENCE_TIME, &max_convergence_time);
julientiron 0:1cb50d31c3b5 3159 if(status)
julientiron 0:1cb50d31c3b5 3160 {
julientiron 0:1cb50d31c3b5 3161 VL6180x_ErrLog("Failed to read SYSRANGE_MAX_CONVERGENCE_TIME!\n\r");
julientiron 0:1cb50d31c3b5 3162 return status;
julientiron 0:1cb50d31c3b5 3163 }
julientiron 0:1cb50d31c3b5 3164 status=VL6180x_RdWord(Device, SYSALS_INTEGRATION_PERIOD, &integration_period);
julientiron 0:1cb50d31c3b5 3165 if(status)
julientiron 0:1cb50d31c3b5 3166 {
julientiron 0:1cb50d31c3b5 3167 VL6180x_ErrLog("Failed to read SYSALS_INTEGRATION_PERIOD!\n\r");
julientiron 0:1cb50d31c3b5 3168 return status;
julientiron 0:1cb50d31c3b5 3169 }
julientiron 0:1cb50d31c3b5 3170 max_convergence_time&=0x3F;
julientiron 0:1cb50d31c3b5 3171 integration_period&=0x01FF;
julientiron 0:1cb50d31c3b5 3172 intermeasurement_period_ms=((max_convergence_time+5)+(integration_period*1.1));
julientiron 0:1cb50d31c3b5 3173 intermeasurement_period_ms=(intermeasurement_period_ms/0.9)+10;
julientiron 0:1cb50d31c3b5 3174 intermeasurement_period_ms=200;
julientiron 0:1cb50d31c3b5 3175 status=VL6180x_AlsSetInterMeasurementPeriod(Device, intermeasurement_period_ms);
julientiron 0:1cb50d31c3b5 3176 VL6180x_RdByte(Device, 0x03E, &buf);
julientiron 0:1cb50d31c3b5 3177 if(status)
julientiron 0:1cb50d31c3b5 3178 {
julientiron 0:1cb50d31c3b5 3179 VL6180x_ErrLog("Failed to write SYSALS_INTERMEASUREMENT_PERIOD!\n\r");
julientiron 0:1cb50d31c3b5 3180 return status;
julientiron 0:1cb50d31c3b5 3181 }
julientiron 0:1cb50d31c3b5 3182 return AlsStartContinuousMode();
julientiron 0:1cb50d31c3b5 3183 }
julientiron 0:1cb50d31c3b5 3184
julientiron 0:1cb50d31c3b5 3185
julientiron 0:1cb50d31c3b5 3186 int VL6180X::InterleavedMode(void (*fptr)(void))
julientiron 0:1cb50d31c3b5 3187 {
julientiron 0:1cb50d31c3b5 3188 int status, ClrStatus;
julientiron 0:1cb50d31c3b5 3189
julientiron 0:1cb50d31c3b5 3190 EnableInterruptMeasureDetectionIRQ();
julientiron 0:1cb50d31c3b5 3191 AttachInterruptMeasureDetectionIRQ(fptr);
julientiron 0:1cb50d31c3b5 3192 status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1);
julientiron 0:1cb50d31c3b5 3193 ClrStatus=VL6180x_ClearAllInterrupt(Device);
julientiron 0:1cb50d31c3b5 3194 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3195 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3196 if(!status)
julientiron 0:1cb50d31c3b5 3197 status=StartInterleavedMode();
julientiron 0:1cb50d31c3b5 3198 return status;
julientiron 0:1cb50d31c3b5 3199 }
julientiron 0:1cb50d31c3b5 3200
julientiron 0:1cb50d31c3b5 3201
julientiron 0:1cb50d31c3b5 3202 int VL6180X::HandleIRQ(OperatingMode operating_mode, MeasureData_t *Data)
julientiron 0:1cb50d31c3b5 3203 {
julientiron 0:1cb50d31c3b5 3204 int status;
julientiron 0:1cb50d31c3b5 3205
julientiron 0:1cb50d31c3b5 3206 EnableInterruptMeasureDetectionIRQ();
julientiron 0:1cb50d31c3b5 3207 status=GetMeasurement(operating_mode, Data);
julientiron 0:1cb50d31c3b5 3208 return status;
julientiron 0:1cb50d31c3b5 3209 }
julientiron 0:1cb50d31c3b5 3210
julientiron 0:1cb50d31c3b5 3211
julientiron 0:1cb50d31c3b5 3212 int VL6180X::RangeSetLowThreshold(uint16_t threshold)
julientiron 0:1cb50d31c3b5 3213 {
julientiron 0:1cb50d31c3b5 3214 int status;
julientiron 0:1cb50d31c3b5 3215 uint16_t low, high;
julientiron 0:1cb50d31c3b5 3216
julientiron 0:1cb50d31c3b5 3217 status=VL6180x_RangeGetThresholds(Device, &low, &high);
julientiron 0:1cb50d31c3b5 3218 if(!status)
julientiron 0:1cb50d31c3b5 3219 status=VL6180x_RangeSetThresholds(Device, threshold, high, 1);
julientiron 0:1cb50d31c3b5 3220 return status;
julientiron 0:1cb50d31c3b5 3221 }
julientiron 0:1cb50d31c3b5 3222
julientiron 0:1cb50d31c3b5 3223
julientiron 0:1cb50d31c3b5 3224 int VL6180X::RangeSetHighThreshold(uint16_t threshold)
julientiron 0:1cb50d31c3b5 3225 {
julientiron 0:1cb50d31c3b5 3226 int status;
julientiron 0:1cb50d31c3b5 3227 uint16_t low, high;
julientiron 0:1cb50d31c3b5 3228
julientiron 0:1cb50d31c3b5 3229 status=VL6180x_RangeGetThresholds(Device, &low, &high);
julientiron 0:1cb50d31c3b5 3230 if(!status)
julientiron 0:1cb50d31c3b5 3231 status=VL6180x_RangeSetThresholds(Device, low, threshold, 1);
julientiron 0:1cb50d31c3b5 3232 return status;
julientiron 0:1cb50d31c3b5 3233 }
julientiron 0:1cb50d31c3b5 3234
julientiron 0:1cb50d31c3b5 3235
julientiron 0:1cb50d31c3b5 3236 int VL6180X::AlsSetLowThreshold(uint16_t threshold)
julientiron 0:1cb50d31c3b5 3237 {
julientiron 0:1cb50d31c3b5 3238 int status;
julientiron 0:1cb50d31c3b5 3239 lux_t low, high;
julientiron 0:1cb50d31c3b5 3240
julientiron 0:1cb50d31c3b5 3241 status=AlsGetThresholds(Device, &low, &high);
julientiron 0:1cb50d31c3b5 3242 if(!status)
julientiron 0:1cb50d31c3b5 3243 status=VL6180x_AlsSetThresholds(Device, threshold, high);
julientiron 0:1cb50d31c3b5 3244 return status;
julientiron 0:1cb50d31c3b5 3245 }
julientiron 0:1cb50d31c3b5 3246
julientiron 0:1cb50d31c3b5 3247
julientiron 0:1cb50d31c3b5 3248 int VL6180X::AlsSetHighThreshold(uint16_t threshold)
julientiron 0:1cb50d31c3b5 3249 {
julientiron 0:1cb50d31c3b5 3250 int status;
julientiron 0:1cb50d31c3b5 3251 lux_t low, high;
julientiron 0:1cb50d31c3b5 3252
julientiron 0:1cb50d31c3b5 3253 status=AlsGetThresholds(Device, &low, &high);
julientiron 0:1cb50d31c3b5 3254 if(!status)
julientiron 0:1cb50d31c3b5 3255 status=VL6180x_AlsSetThresholds(Device, low, threshold);
julientiron 0:1cb50d31c3b5 3256 return status;
julientiron 0:1cb50d31c3b5 3257 }
julientiron 0:1cb50d31c3b5 3258
julientiron 0:1cb50d31c3b5 3259
julientiron 0:1cb50d31c3b5 3260 int VL6180X::AlsGetThresholds(VL6180xDev_t dev, lux_t *low, lux_t *high)
julientiron 0:1cb50d31c3b5 3261 {
julientiron 0:1cb50d31c3b5 3262 int status;
julientiron 0:1cb50d31c3b5 3263 uint16_t RawAlsLow, RawAlsHigh;
julientiron 0:1cb50d31c3b5 3264 uint32_t luxLowValue, luxHighValue, IntPeriod, AlsAnGain, GainFix, AlsScaler;
julientiron 0:1cb50d31c3b5 3265 const uint32_t LuxResxIntIme =(uint32_t)(0.56f* DEF_INT_PEFRIOD *(1<<LUXRES_FIX_PREC));
julientiron 0:1cb50d31c3b5 3266
julientiron 0:1cb50d31c3b5 3267 status=VL6180x_RdWord(dev, SYSALS_THRESH_LOW, &RawAlsLow);
julientiron 0:1cb50d31c3b5 3268 if(status)
julientiron 0:1cb50d31c3b5 3269 {
julientiron 0:1cb50d31c3b5 3270 VL6180x_ErrLog("rd SYSALS_THRESH_LOW fail");
julientiron 0:1cb50d31c3b5 3271 return status;
julientiron 0:1cb50d31c3b5 3272 }
julientiron 0:1cb50d31c3b5 3273 status=VL6180x_RdWord(dev, SYSALS_THRESH_HIGH, &RawAlsHigh);
julientiron 0:1cb50d31c3b5 3274 if(status)
julientiron 0:1cb50d31c3b5 3275 {
julientiron 0:1cb50d31c3b5 3276 VL6180x_ErrLog("rd SYSALS_THRESH_HIGH fail");
julientiron 0:1cb50d31c3b5 3277 return status;
julientiron 0:1cb50d31c3b5 3278 }
julientiron 0:1cb50d31c3b5 3279 AlsAnGain=VL6180xDevDataGet(dev, AlsGainCode);
julientiron 0:1cb50d31c3b5 3280 IntPeriod=VL6180xDevDataGet(dev, IntegrationPeriod);
julientiron 0:1cb50d31c3b5 3281 AlsScaler=VL6180xDevDataGet(dev, AlsScaler);
julientiron 0:1cb50d31c3b5 3282 GainFix=AlsGainLookUp[AlsAnGain];
julientiron 0:1cb50d31c3b5 3283 IntPeriod++;
julientiron 0:1cb50d31c3b5 3284 luxLowValue=(uint32_t)RawAlsLow*LuxResxIntIme;
julientiron 0:1cb50d31c3b5 3285 luxLowValue=luxLowValue/IntPeriod;
julientiron 0:1cb50d31c3b5 3286 luxLowValue=luxLowValue/(AlsScaler*GainFix);
julientiron 0:1cb50d31c3b5 3287 luxHighValue=(uint32_t)RawAlsHigh*LuxResxIntIme;
julientiron 0:1cb50d31c3b5 3288 luxHighValue=luxHighValue/IntPeriod;
julientiron 0:1cb50d31c3b5 3289 luxHighValue=luxHighValue/(AlsScaler*GainFix);
julientiron 0:1cb50d31c3b5 3290 *low=luxLowValue;
julientiron 0:1cb50d31c3b5 3291 *high=luxHighValue;
julientiron 0:1cb50d31c3b5 3292 return status;
julientiron 0:1cb50d31c3b5 3293 }
julientiron 0:1cb50d31c3b5 3294
julientiron 0:1cb50d31c3b5 3295
julientiron 0:1cb50d31c3b5 3296 int VL6180X::GetMeasurement(OperatingMode operating_mode, MeasureData_t *Data)
julientiron 0:1cb50d31c3b5 3297 {
julientiron 0:1cb50d31c3b5 3298 switch(operating_mode)
julientiron 0:1cb50d31c3b5 3299 {
julientiron 0:1cb50d31c3b5 3300 case(range_single_shot_polling):
julientiron 0:1cb50d31c3b5 3301 case(range_continuous_polling):
julientiron 0:1cb50d31c3b5 3302 case(range_continuous_interrupt):
julientiron 0:1cb50d31c3b5 3303 case(range_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 3304 case(range_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 3305 case(range_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 3306 case(range_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 3307 case(range_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 3308 case(range_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 3309 return GetRangeMeas(operating_mode, Data);
julientiron 0:1cb50d31c3b5 3310
julientiron 0:1cb50d31c3b5 3311 case(als_single_shot_polling):
julientiron 0:1cb50d31c3b5 3312 case(als_continuous_polling):
julientiron 0:1cb50d31c3b5 3313 case(als_continuous_interrupt):
julientiron 0:1cb50d31c3b5 3314 case(als_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 3315 case(als_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 3316 case(als_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 3317 case(als_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 3318 case(als_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 3319 case(als_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 3320 return GetAlsMeas(operating_mode, Data);
julientiron 0:1cb50d31c3b5 3321
julientiron 0:1cb50d31c3b5 3322 case(interleaved_mode_interrupt):
julientiron 0:1cb50d31c3b5 3323 return GetRangeAlsMeas(Data);
julientiron 0:1cb50d31c3b5 3324
julientiron 0:1cb50d31c3b5 3325 default:
julientiron 0:1cb50d31c3b5 3326 return INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 3327 }
julientiron 0:1cb50d31c3b5 3328 }
julientiron 0:1cb50d31c3b5 3329
julientiron 0:1cb50d31c3b5 3330
julientiron 0:1cb50d31c3b5 3331 int VL6180X::GetRangeMeas(OperatingMode operating_mode, MeasureData_t *Data)
julientiron 0:1cb50d31c3b5 3332 {
julientiron 0:1cb50d31c3b5 3333 VL6180x_RangeData_t RangeData;
julientiron 0:1cb50d31c3b5 3334 int status, ClrStatus;
julientiron 0:1cb50d31c3b5 3335 IntrStatus_t IntStatus;
julientiron 0:1cb50d31c3b5 3336
julientiron 0:1cb50d31c3b5 3337 status=VL6180x_RangeGetInterruptStatus(Device, &IntStatus.val);
julientiron 0:1cb50d31c3b5 3338 if(!status)
julientiron 0:1cb50d31c3b5 3339 {
julientiron 0:1cb50d31c3b5 3340 Data->int_error=IntStatus.status.Error;
julientiron 0:1cb50d31c3b5 3341 if(IntStatus.status.Error!=0)
julientiron 0:1cb50d31c3b5 3342 {
julientiron 0:1cb50d31c3b5 3343 VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val);
julientiron 0:1cb50d31c3b5 3344 status=RANGE_ERROR;
julientiron 0:1cb50d31c3b5 3345 }
julientiron 0:1cb50d31c3b5 3346 }
julientiron 0:1cb50d31c3b5 3347 else
julientiron 0:1cb50d31c3b5 3348 {
julientiron 0:1cb50d31c3b5 3349 VL6180x_ErrLog("Failed to read RESULT_INTERRUPT_STATUS_GPIO");
julientiron 0:1cb50d31c3b5 3350 }
julientiron 0:1cb50d31c3b5 3351 ClrStatus=VL6180x_RangeClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3352 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3353 {
julientiron 0:1cb50d31c3b5 3354 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3355 }
julientiron 0:1cb50d31c3b5 3356 ClrStatus=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3357 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3358 {
julientiron 0:1cb50d31c3b5 3359 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3360 }
julientiron 0:1cb50d31c3b5 3361 if(status)
julientiron 0:1cb50d31c3b5 3362 return status;
julientiron 0:1cb50d31c3b5 3363 if((operating_mode==range_single_shot_polling)||(operating_mode==range_continuous_polling)||(operating_mode==range_continuous_interrupt))
julientiron 0:1cb50d31c3b5 3364 {
julientiron 0:1cb50d31c3b5 3365 if(IntStatus.status.Range==RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
julientiron 0:1cb50d31c3b5 3366 status=VL6180x_RangeGetMeasurement(Device, &RangeData);
julientiron 0:1cb50d31c3b5 3367 else
julientiron 0:1cb50d31c3b5 3368 return NOT_READY;
julientiron 0:1cb50d31c3b5 3369 }
julientiron 0:1cb50d31c3b5 3370 else if((operating_mode==range_continuous_polling_low_threshold)||(operating_mode==range_continuous_interrupt_low_threshold))
julientiron 0:1cb50d31c3b5 3371 {
julientiron 0:1cb50d31c3b5 3372 if(IntStatus.status.Range==RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD)
julientiron 0:1cb50d31c3b5 3373 status=VL6180x_RangeGetMeasurement(Device, &RangeData);
julientiron 0:1cb50d31c3b5 3374 else
julientiron 0:1cb50d31c3b5 3375 return NOT_READY;
julientiron 0:1cb50d31c3b5 3376 }
julientiron 0:1cb50d31c3b5 3377 else if((operating_mode==range_continuous_polling_high_threshold)||(operating_mode==range_continuous_interrupt_high_threshold))
julientiron 0:1cb50d31c3b5 3378 {
julientiron 0:1cb50d31c3b5 3379 if(IntStatus.status.Range==RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD)
julientiron 0:1cb50d31c3b5 3380 status=VL6180x_RangeGetMeasurement(Device, &RangeData);
julientiron 0:1cb50d31c3b5 3381 else
julientiron 0:1cb50d31c3b5 3382 return NOT_READY;
julientiron 0:1cb50d31c3b5 3383 }
julientiron 0:1cb50d31c3b5 3384 else if((operating_mode==range_continuous_polling_out_of_window)||(operating_mode==range_continuous_interrupt_out_of_window))
julientiron 0:1cb50d31c3b5 3385 {
julientiron 0:1cb50d31c3b5 3386 if(IntStatus.status.Range==RES_INT_STAT_GPIO_OUT_OF_WINDOW)
julientiron 0:1cb50d31c3b5 3387 status=VL6180x_RangeGetMeasurement(Device, &RangeData);
julientiron 0:1cb50d31c3b5 3388 else
julientiron 0:1cb50d31c3b5 3389 return NOT_READY;
julientiron 0:1cb50d31c3b5 3390 }
julientiron 0:1cb50d31c3b5 3391 if(!status)
julientiron 0:1cb50d31c3b5 3392 {
julientiron 0:1cb50d31c3b5 3393 status=GetRangeError(Data, RangeData);
julientiron 0:1cb50d31c3b5 3394 if(!status)
julientiron 0:1cb50d31c3b5 3395 Data->range_mm=RangeData.range_mm;
julientiron 0:1cb50d31c3b5 3396 else
julientiron 0:1cb50d31c3b5 3397 Data->range_mm=0xFFFFFFFF;
julientiron 0:1cb50d31c3b5 3398 }
julientiron 0:1cb50d31c3b5 3399 return status;
julientiron 0:1cb50d31c3b5 3400 }
julientiron 0:1cb50d31c3b5 3401
julientiron 0:1cb50d31c3b5 3402
julientiron 0:1cb50d31c3b5 3403 int VL6180X::GetAlsMeas(OperatingMode operating_mode, MeasureData_t *Data)
julientiron 0:1cb50d31c3b5 3404 {
julientiron 0:1cb50d31c3b5 3405 VL6180x_AlsData_t AlsData;
julientiron 0:1cb50d31c3b5 3406 int status, ClrStatus;
julientiron 0:1cb50d31c3b5 3407 uint8_t IntStatus;
julientiron 0:1cb50d31c3b5 3408
julientiron 0:1cb50d31c3b5 3409 status=VL6180x_AlsGetInterruptStatus(Device, &IntStatus);
julientiron 0:1cb50d31c3b5 3410 if(status)
julientiron 0:1cb50d31c3b5 3411 {
julientiron 0:1cb50d31c3b5 3412 VL6180x_ErrLog("Failed to read RESULT_INTERRUPT_STATUS_GPIO");
julientiron 0:1cb50d31c3b5 3413 }
julientiron 0:1cb50d31c3b5 3414 ClrStatus=VL6180x_AlsClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3415 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3416 {
julientiron 0:1cb50d31c3b5 3417 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3418 }
julientiron 0:1cb50d31c3b5 3419 ClrStatus=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3420 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3421 {
julientiron 0:1cb50d31c3b5 3422 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3423 }
julientiron 0:1cb50d31c3b5 3424 if(status)
julientiron 0:1cb50d31c3b5 3425 return status;
julientiron 0:1cb50d31c3b5 3426 if((operating_mode==als_single_shot_polling)||(operating_mode==als_continuous_polling)||(operating_mode==als_continuous_interrupt))
julientiron 0:1cb50d31c3b5 3427 {
julientiron 0:1cb50d31c3b5 3428 if(IntStatus==RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
julientiron 0:1cb50d31c3b5 3429 status=VL6180x_AlsGetMeasurement(Device, &AlsData);
julientiron 0:1cb50d31c3b5 3430 else
julientiron 0:1cb50d31c3b5 3431 return NOT_READY;
julientiron 0:1cb50d31c3b5 3432 }
julientiron 0:1cb50d31c3b5 3433 else if((operating_mode==als_continuous_polling_low_threshold)||(operating_mode==als_continuous_interrupt_low_threshold))
julientiron 0:1cb50d31c3b5 3434 {
julientiron 0:1cb50d31c3b5 3435 if(IntStatus==RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD)
julientiron 0:1cb50d31c3b5 3436 status=VL6180x_AlsGetMeasurement(Device, &AlsData);
julientiron 0:1cb50d31c3b5 3437 else
julientiron 0:1cb50d31c3b5 3438 return NOT_READY;
julientiron 0:1cb50d31c3b5 3439 }
julientiron 0:1cb50d31c3b5 3440 else if((operating_mode==als_continuous_polling_high_threshold)||(operating_mode==als_continuous_interrupt_high_threshold))
julientiron 0:1cb50d31c3b5 3441 {
julientiron 0:1cb50d31c3b5 3442 if(IntStatus==RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD)
julientiron 0:1cb50d31c3b5 3443 status=VL6180x_AlsGetMeasurement(Device, &AlsData);
julientiron 0:1cb50d31c3b5 3444 else
julientiron 0:1cb50d31c3b5 3445 return NOT_READY;
julientiron 0:1cb50d31c3b5 3446 }
julientiron 0:1cb50d31c3b5 3447 else if((operating_mode==als_continuous_polling_out_of_window)||(operating_mode==als_continuous_interrupt_out_of_window))
julientiron 0:1cb50d31c3b5 3448 {
julientiron 0:1cb50d31c3b5 3449 if(IntStatus==RES_INT_STAT_GPIO_OUT_OF_WINDOW)
julientiron 0:1cb50d31c3b5 3450 status=VL6180x_AlsGetMeasurement(Device, &AlsData);
julientiron 0:1cb50d31c3b5 3451 else
julientiron 0:1cb50d31c3b5 3452 return NOT_READY;
julientiron 0:1cb50d31c3b5 3453 }
julientiron 0:1cb50d31c3b5 3454 if(!status)
julientiron 0:1cb50d31c3b5 3455 {
julientiron 0:1cb50d31c3b5 3456 status=GetAlsError(Data, AlsData);
julientiron 0:1cb50d31c3b5 3457 if(!status)
julientiron 0:1cb50d31c3b5 3458 Data->lux=AlsData.lux;
julientiron 0:1cb50d31c3b5 3459 else
julientiron 0:1cb50d31c3b5 3460 Data->lux=0xFFFFFFFF;
julientiron 0:1cb50d31c3b5 3461 }
julientiron 0:1cb50d31c3b5 3462 return status;
julientiron 0:1cb50d31c3b5 3463 }
julientiron 0:1cb50d31c3b5 3464
julientiron 0:1cb50d31c3b5 3465
julientiron 0:1cb50d31c3b5 3466 int VL6180X::GetRangeAlsMeas(MeasureData_t *Data)
julientiron 0:1cb50d31c3b5 3467 {
julientiron 0:1cb50d31c3b5 3468 int status, ClrStatus, r_status, l_status;
julientiron 0:1cb50d31c3b5 3469 IntrStatus_t IntStatus;
julientiron 0:1cb50d31c3b5 3470 VL6180x_RangeData_t RangeData;
julientiron 0:1cb50d31c3b5 3471 VL6180x_AlsData_t AlsData;
julientiron 0:1cb50d31c3b5 3472
julientiron 0:1cb50d31c3b5 3473 status=VL6180x_RdByte(Device, RESULT_INTERRUPT_STATUS_GPIO, &IntStatus.val);
julientiron 0:1cb50d31c3b5 3474 if(!status)
julientiron 0:1cb50d31c3b5 3475 {
julientiron 0:1cb50d31c3b5 3476 Data->int_error=IntStatus.status.Error;
julientiron 0:1cb50d31c3b5 3477 if(IntStatus.status.Error!=0)
julientiron 0:1cb50d31c3b5 3478 {
julientiron 0:1cb50d31c3b5 3479 VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val);
julientiron 0:1cb50d31c3b5 3480 status=RANGE_ERROR;
julientiron 0:1cb50d31c3b5 3481 }
julientiron 0:1cb50d31c3b5 3482 }
julientiron 0:1cb50d31c3b5 3483 else
julientiron 0:1cb50d31c3b5 3484 {
julientiron 0:1cb50d31c3b5 3485 VL6180x_ErrLog("Failed to read RESULT_INTERRUPT_STATUS_GPIO");
julientiron 0:1cb50d31c3b5 3486 }
julientiron 0:1cb50d31c3b5 3487 ClrStatus=VL6180x_ClearAllInterrupt(Device);
julientiron 0:1cb50d31c3b5 3488 if(ClrStatus)
julientiron 0:1cb50d31c3b5 3489 VL6180x_ErrLog("VL6180x_ClearAllInterrupt fail");
julientiron 0:1cb50d31c3b5 3490 if(status)
julientiron 0:1cb50d31c3b5 3491 return status;
julientiron 0:1cb50d31c3b5 3492
julientiron 0:1cb50d31c3b5 3493 if(IntStatus.status.Als==RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
julientiron 0:1cb50d31c3b5 3494 {
julientiron 0:1cb50d31c3b5 3495 r_status=VL6180x_RangeGetMeasurement(Device, &RangeData);
julientiron 0:1cb50d31c3b5 3496 l_status=VL6180x_AlsGetMeasurement(Device, &AlsData);
julientiron 0:1cb50d31c3b5 3497 if((!r_status)&&(!l_status))
julientiron 0:1cb50d31c3b5 3498 {
julientiron 0:1cb50d31c3b5 3499 r_status=GetRangeError(Data, RangeData);
julientiron 0:1cb50d31c3b5 3500 l_status=GetAlsError(Data, AlsData);
julientiron 0:1cb50d31c3b5 3501 if(!r_status)
julientiron 0:1cb50d31c3b5 3502 Data->range_mm=RangeData.range_mm;
julientiron 0:1cb50d31c3b5 3503 else
julientiron 0:1cb50d31c3b5 3504 Data->range_mm=0xFFFFFFFF;
julientiron 0:1cb50d31c3b5 3505 if(!l_status)
julientiron 0:1cb50d31c3b5 3506 Data->lux=AlsData.lux;
julientiron 0:1cb50d31c3b5 3507 else
julientiron 0:1cb50d31c3b5 3508 Data->lux=0xFFFFFFFF;
julientiron 0:1cb50d31c3b5 3509 status=r_status|l_status;
julientiron 0:1cb50d31c3b5 3510 }
julientiron 0:1cb50d31c3b5 3511 else
julientiron 0:1cb50d31c3b5 3512 {
julientiron 0:1cb50d31c3b5 3513 status=r_status|l_status;
julientiron 0:1cb50d31c3b5 3514 }
julientiron 0:1cb50d31c3b5 3515 }
julientiron 0:1cb50d31c3b5 3516 else
julientiron 0:1cb50d31c3b5 3517 return NOT_READY;
julientiron 0:1cb50d31c3b5 3518 return status;
julientiron 0:1cb50d31c3b5 3519 }
julientiron 0:1cb50d31c3b5 3520
julientiron 0:1cb50d31c3b5 3521
julientiron 0:1cb50d31c3b5 3522 int VL6180X::StopMeasurement(OperatingMode operating_mode)
julientiron 0:1cb50d31c3b5 3523 {
julientiron 0:1cb50d31c3b5 3524 int status;
julientiron 0:1cb50d31c3b5 3525
julientiron 0:1cb50d31c3b5 3526 switch(operating_mode)
julientiron 0:1cb50d31c3b5 3527 {
julientiron 0:1cb50d31c3b5 3528 case(range_single_shot_polling):
julientiron 0:1cb50d31c3b5 3529 case(range_continuous_polling):
julientiron 0:1cb50d31c3b5 3530 case(range_continuous_interrupt):
julientiron 0:1cb50d31c3b5 3531 case(range_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 3532 case(range_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 3533 case(range_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 3534 case(range_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 3535 case(range_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 3536 case(range_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 3537 return StopRangeMeasurement(operating_mode);
julientiron 0:1cb50d31c3b5 3538
julientiron 0:1cb50d31c3b5 3539 case(als_single_shot_polling):
julientiron 0:1cb50d31c3b5 3540 case(als_continuous_polling):
julientiron 0:1cb50d31c3b5 3541 case(als_continuous_interrupt):
julientiron 0:1cb50d31c3b5 3542 case(als_continuous_polling_low_threshold):
julientiron 0:1cb50d31c3b5 3543 case(als_continuous_polling_high_threshold):
julientiron 0:1cb50d31c3b5 3544 case(als_continuous_polling_out_of_window):
julientiron 0:1cb50d31c3b5 3545 case(als_continuous_interrupt_low_threshold):
julientiron 0:1cb50d31c3b5 3546 case(als_continuous_interrupt_high_threshold):
julientiron 0:1cb50d31c3b5 3547 case(als_continuous_interrupt_out_of_window):
julientiron 0:1cb50d31c3b5 3548 return StopAlsMeasurement(operating_mode);
julientiron 0:1cb50d31c3b5 3549
julientiron 0:1cb50d31c3b5 3550 case(interleaved_mode_interrupt):
julientiron 0:1cb50d31c3b5 3551 status=StopRangeMeasurement(range_continuous_interrupt);
julientiron 0:1cb50d31c3b5 3552 if(!status)
julientiron 0:1cb50d31c3b5 3553 return StopAlsMeasurement(als_continuous_interrupt);
julientiron 0:1cb50d31c3b5 3554 else return status;
julientiron 0:1cb50d31c3b5 3555
julientiron 0:1cb50d31c3b5 3556 default:
julientiron 0:1cb50d31c3b5 3557 return INVALID_PARAMS;
julientiron 0:1cb50d31c3b5 3558 }
julientiron 0:1cb50d31c3b5 3559 }
julientiron 0:1cb50d31c3b5 3560
julientiron 0:1cb50d31c3b5 3561
julientiron 0:1cb50d31c3b5 3562 int VL6180X::StopRangeMeasurement(OperatingMode operating_mode)
julientiron 0:1cb50d31c3b5 3563 {
julientiron 0:1cb50d31c3b5 3564 int status;
julientiron 0:1cb50d31c3b5 3565
julientiron 0:1cb50d31c3b5 3566 if(operating_mode==range_single_shot_polling)
julientiron 0:1cb50d31c3b5 3567 status=VL6180x_RangeSetSystemMode(Device, MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 3568 else
julientiron 0:1cb50d31c3b5 3569 status=VL6180x_RangeSetSystemMode(Device, MODE_START_STOP|MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 3570 if(status)
julientiron 0:1cb50d31c3b5 3571 return status;
julientiron 0:1cb50d31c3b5 3572 status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 3573 if(status)
julientiron 0:1cb50d31c3b5 3574 {
julientiron 0:1cb50d31c3b5 3575 VL6180x_ErrLog("VL6180x_RangeConfigInterrupt fail"\n\r);
julientiron 0:1cb50d31c3b5 3576 return status;
julientiron 0:1cb50d31c3b5 3577 }
julientiron 0:1cb50d31c3b5 3578 status=VL6180x_RangeClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3579 if(status)
julientiron 0:1cb50d31c3b5 3580 {
julientiron 0:1cb50d31c3b5 3581 VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3582 return status;
julientiron 0:1cb50d31c3b5 3583 }
julientiron 0:1cb50d31c3b5 3584 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3585 if(status)
julientiron 0:1cb50d31c3b5 3586 {
julientiron 0:1cb50d31c3b5 3587 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3588 return status;
julientiron 0:1cb50d31c3b5 3589 }
julientiron 0:1cb50d31c3b5 3590 status=VL6180x_RangeSetRawThresholds(Device, 10, 200);
julientiron 0:1cb50d31c3b5 3591 if(status)
julientiron 0:1cb50d31c3b5 3592 VL6180x_ErrLog("VL6180x_RangeSetThresholds fail");
julientiron 0:1cb50d31c3b5 3593 return status;
julientiron 0:1cb50d31c3b5 3594 }
julientiron 0:1cb50d31c3b5 3595
julientiron 0:1cb50d31c3b5 3596
julientiron 0:1cb50d31c3b5 3597 int VL6180X::StopAlsMeasurement(OperatingMode operating_mode)
julientiron 0:1cb50d31c3b5 3598 {
julientiron 0:1cb50d31c3b5 3599 int status;
julientiron 0:1cb50d31c3b5 3600
julientiron 0:1cb50d31c3b5 3601 if(operating_mode==als_single_shot_polling)
julientiron 0:1cb50d31c3b5 3602 status=VL6180x_AlsSetSystemMode(Device, MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 3603 else
julientiron 0:1cb50d31c3b5 3604 status=VL6180x_AlsSetSystemMode(Device, MODE_START_STOP|MODE_SINGLESHOT);
julientiron 0:1cb50d31c3b5 3605 if(status)
julientiron 0:1cb50d31c3b5 3606 return status;
julientiron 0:1cb50d31c3b5 3607 status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
julientiron 0:1cb50d31c3b5 3608 if(status)
julientiron 0:1cb50d31c3b5 3609 {
julientiron 0:1cb50d31c3b5 3610 VL6180x_ErrLog("VL6180x_AlsConfigInterrupt fail"\n\r);
julientiron 0:1cb50d31c3b5 3611 return status;
julientiron 0:1cb50d31c3b5 3612 }
julientiron 0:1cb50d31c3b5 3613 status=VL6180x_AlsClearInterrupt(Device);
julientiron 0:1cb50d31c3b5 3614 if(status)
julientiron 0:1cb50d31c3b5 3615 {
julientiron 0:1cb50d31c3b5 3616 VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail");
julientiron 0:1cb50d31c3b5 3617 return status;
julientiron 0:1cb50d31c3b5 3618 }
julientiron 0:1cb50d31c3b5 3619 status=VL6180x_ClearErrorInterrupt(Device);
julientiron 0:1cb50d31c3b5 3620 if(status)
julientiron 0:1cb50d31c3b5 3621 {
julientiron 0:1cb50d31c3b5 3622 VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
julientiron 0:1cb50d31c3b5 3623 return status;
julientiron 0:1cb50d31c3b5 3624 }
julientiron 0:1cb50d31c3b5 3625 status=VL6180x_AlsSetThresholds(Device, 0x0, 1800);
julientiron 0:1cb50d31c3b5 3626 if(status)
julientiron 0:1cb50d31c3b5 3627 VL6180x_ErrLog("VL6180x_AlsSetThresholds fail");
julientiron 0:1cb50d31c3b5 3628 return status;
julientiron 0:1cb50d31c3b5 3629 }
julientiron 0:1cb50d31c3b5 3630
julientiron 0:1cb50d31c3b5 3631
julientiron 0:1cb50d31c3b5 3632
julientiron 0:1cb50d31c3b5 3633
julientiron 0:1cb50d31c3b5 3634 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/