INSAT Mini Project

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Committer:
gallonm
Date:
Mon Nov 02 14:02:53 2015 +0100
Revision:
24:025afc3fe30f
Parent:
23:dfb5ccc7b780
Child:
26:db0cdc5ecc0a
Modified file cfg.h by introducing #define EXTENDED_RANGE_50CM.
Settled StartMeasurement by removing MeasureData_t from parameters.
Settled range single shot measure and als single shot measure.
Settled GetMeasurement by introducing range_single_shot case and als_single_shot case.
Settled VL6180x_AlsSetThresholds by introducing the computation in order to pass
the arguments in lux and write the same arguments in raw device value into registers.

Who changed what in which revision?

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