ST / X_NUCLEO_6180XA1

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   HelloWorld_6180XA1 SunTracker_BLE Servo_6180XA1 BLE_HR_Light ... more

Fork of X_NUCLEO_6180XA1 by ST Expansion SW Team

X-NUCLEO-6180XA1 Proximity and Ambient Light Sensor Expansion Board Firmware Package

Introduction

This firmware package includes Components Device Drivers and Board Support Package for STMicroelectronics' X-NUCLEO-6180XA1 Proximity and ambient light sensor expansion board based on VL6180X.

Firmware Library

Class X_NUCLEO_6180XA1 is intended to represent the Proximity and ambient light sensor expansion board with the same name.

The expansion board is providing the support of the following components:

  1. on-board VL6180X proximity and ambient light sensor,
  2. up to three additional VL6180X Satellites,
  3. on-board 4-digit display

It is intentionally implemented as a singleton because only one X-NUCLEO-VL6180XA1 at a time might be deployed in a HW component stack. In order to get the singleton instance you have to call class method `Instance()`, e.g.:

// Sensors expansion board singleton instance
static X_NUCLEO_6180XA1 *6180X_expansion_board = X_NUCLEO_6180XA1::Instance();

Arduino Connector Compatibility Warning

Using the X-NUCLEO-6180XA1 expansion board with the NUCLEO-F429ZI requires adopting the following patch:

  • to remove R46 resistor connected to A3 pin;
  • to solder R47 resistor connected to A5 pin.

Alternatively, you can route the Nucleo board’s A5 pin directly to the expansion board’s A3 pin with a wire. In case you patch your expansion board or route the pin, the interrupt signal for the front sensor will be driven on A5 pin rather than on A3 pin.


Example Applications

Committer:
gallonm
Date:
Thu Sep 17 10:41:21 2015 +0200
Revision:
4:a5abf7757947
Parent:
3:454541a079f4
Child:
7:2dc81120c917
First compile of Init function and ReadID function

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 1:1de1ea2994d9 37
gallonm 1:1de1ea2994d9 38 /* Includes ------------------------------------------------------------------*/
gallonm 1:1de1ea2994d9 39 #include "vl6180x_class.h"
gallonm 1:1de1ea2994d9 40
gallonm 4:a5abf7757947 41 /* define for inizialization -------------------------------------------------*/
gallonm 4:a5abf7757947 42
gallonm 4:a5abf7757947 43 #if VL6180x_UPSCALE_SUPPORT == 1
gallonm 4:a5abf7757947 44 #define _GetUpscale(dev, ... ) 1
gallonm 4:a5abf7757947 45 #define _SetUpscale(...) -1
gallonm 4:a5abf7757947 46 #define DEF_UPSCALE 1
gallonm 4:a5abf7757947 47 #elif VL6180x_UPSCALE_SUPPORT == 2
gallonm 4:a5abf7757947 48 #define _GetUpscale(dev, ... ) 2
gallonm 4:a5abf7757947 49 #define _SetUpscale(...)
gallonm 4:a5abf7757947 50 #define DEF_UPSCALE 2
gallonm 4:a5abf7757947 51 #elif VL6180x_UPSCALE_SUPPORT == 3
gallonm 4:a5abf7757947 52 #define _GetUpscale(dev, ... ) 3
gallonm 4:a5abf7757947 53 #define _SetUpscale(...)
gallonm 4:a5abf7757947 54 #define DEF_UPSCALE 3
gallonm 4:a5abf7757947 55 #else
gallonm 4:a5abf7757947 56 #define DEF_UPSCALE (-(VL6180x_UPSCALE_SUPPORT))
gallonm 4:a5abf7757947 57 #define _GetUpscale(dev, ... ) VL6180xDevDataGet(dev, UpscaleFactor)
gallonm 4:a5abf7757947 58 #define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
gallonm 4:a5abf7757947 59 #endif
gallonm 4:a5abf7757947 60
gallonm 4:a5abf7757947 61
gallonm 4:a5abf7757947 62 #if VL6180x_SINGLE_DEVICE_DRIVER
gallonm 4:a5abf7757947 63 extern struct VL6180xDevData_t SingleVL6180xDevData;
gallonm 4:a5abf7757947 64 #define VL6180xDevDataGet(dev, field) (SingleVL6180xDevData.field)
gallonm 4:a5abf7757947 65 #define VL6180xDevDataSet(dev, field, data) SingleVL6180xDevData.field=(data)
gallonm 4:a5abf7757947 66 #endif
gallonm 4:a5abf7757947 67
gallonm 4:a5abf7757947 68
gallonm 4:a5abf7757947 69 #if VL6180x_UPSCALE_SUPPORT == 1
gallonm 4:a5abf7757947 70 #define _GetUpscale(dev, ... ) 1
gallonm 4:a5abf7757947 71 #define _SetUpscale(...) -1
gallonm 4:a5abf7757947 72 #define DEF_UPSCALE 1
gallonm 4:a5abf7757947 73 #elif VL6180x_UPSCALE_SUPPORT == 2
gallonm 4:a5abf7757947 74 #define _GetUpscale(dev, ... ) 2
gallonm 4:a5abf7757947 75 #define _SetUpscale(...)
gallonm 4:a5abf7757947 76 #define DEF_UPSCALE 2
gallonm 4:a5abf7757947 77 #elif VL6180x_UPSCALE_SUPPORT == 3
gallonm 4:a5abf7757947 78 #define _GetUpscale(dev, ... ) 3
gallonm 4:a5abf7757947 79 #define _SetUpscale(...)
gallonm 4:a5abf7757947 80 #define DEF_UPSCALE 3
gallonm 4:a5abf7757947 81 #else
gallonm 4:a5abf7757947 82 #define DEF_UPSCALE (-(VL6180x_UPSCALE_SUPPORT))
gallonm 4:a5abf7757947 83 #define _GetUpscale(dev, ... ) VL6180xDevDataGet(dev, UpscaleFactor)
gallonm 4:a5abf7757947 84 #define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
gallonm 4:a5abf7757947 85 #endif
gallonm 4:a5abf7757947 86
gallonm 4:a5abf7757947 87
gallonm 4:a5abf7757947 88 struct VL6180xDevData_t VL6180x_DEV_DATA_ATTR SingleVL6180xDevData={
gallonm 4:a5abf7757947 89 .EceFactorM = DEF_ECE_FACTOR_M,
gallonm 4:a5abf7757947 90 .EceFactorD = DEF_ECE_FACTOR_D,
gallonm 4:a5abf7757947 91 #ifdef VL6180x_HAVE_UPSCALE_DATA
gallonm 4:a5abf7757947 92 .UpscaleFactor = DEF_UPSCALE,
gallonm 4:a5abf7757947 93 #endif
gallonm 4:a5abf7757947 94 #ifdef VL6180x_HAVE_ALS_DATA
gallonm 4:a5abf7757947 95 .IntegrationPeriod = DEF_INT_PEFRIOD,
gallonm 4:a5abf7757947 96 .AlsGainCode = DEF_ALS_GAIN,
gallonm 4:a5abf7757947 97 .AlsScaler = DEF_ALS_SCALER,
gallonm 4:a5abf7757947 98 #endif
gallonm 4:a5abf7757947 99 #ifdef VL6180x_HAVE_DMAX_RANGING
gallonm 4:a5abf7757947 100 .DMaxEnable = DEF_DMAX_ENABLE,
gallonm 4:a5abf7757947 101 #endif
gallonm 4:a5abf7757947 102 };
gallonm 4:a5abf7757947 103
gallonm 4:a5abf7757947 104
gallonm 3:454541a079f4 105 #define Fix7_2_KCPs(x) ((((uint32_t)(x))*1000)>>7)
gallonm 1:1de1ea2994d9 106
gallonm 4:a5abf7757947 107 /* define for i2c configuration ----------------------------------------------*/
gallonm 4:a5abf7757947 108
gallonm 4:a5abf7757947 109 #define I2C_BUFFER_CONFIG 1
gallonm 4:a5abf7757947 110 #define VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 111 #define TEMP_BUF_SIZE 32
gallonm 4:a5abf7757947 112
gallonm 4:a5abf7757947 113 #ifndef I2C_BUFFER_CONFIG
gallonm 4:a5abf7757947 114 #error "I2C_BUFFER_CONFIG not defined"
gallonm 4:a5abf7757947 115 /* TODO you must define value for I2C_BUFFER_CONFIG in configuration or platform h */
gallonm 4:a5abf7757947 116 #endif
gallonm 4:a5abf7757947 117
gallonm 4:a5abf7757947 118
gallonm 4:a5abf7757947 119 #if I2C_BUFFER_CONFIG == 0
gallonm 4:a5abf7757947 120 /* GLOBAL config buffer */
gallonm 4:a5abf7757947 121 uint8_t i2c_global_buffer[VL6180x_MAX_I2C_XFER_SIZE];
gallonm 4:a5abf7757947 122
gallonm 4:a5abf7757947 123 #define DECL_I2C_BUFFER
gallonm 4:a5abf7757947 124 #define VL6180x_GetI2cBuffer(dev, n_byte) i2c_global_buffer
gallonm 4:a5abf7757947 125
gallonm 4:a5abf7757947 126 #elif I2C_BUFFER_CONFIG == 1
gallonm 4:a5abf7757947 127 /* ON STACK */
gallonm 4:a5abf7757947 128 #define DECL_I2C_BUFFER uint8_t LocBuffer[VL6180x_MAX_I2C_XFER_SIZE];
gallonm 4:a5abf7757947 129 #define VL6180x_GetI2cBuffer(dev, n_byte) LocBuffer
gallonm 4:a5abf7757947 130 #elif I2C_BUFFER_CONFIG == 2
gallonm 4:a5abf7757947 131 /* user define buffer type declare DECL_I2C_BUFFER as access via VL6180x_GetI2cBuffer */
gallonm 4:a5abf7757947 132 #define DECL_I2C_BUFFER
gallonm 4:a5abf7757947 133 #else
gallonm 4:a5abf7757947 134 #error "invalid I2C_BUFFER_CONFIG "
gallonm 4:a5abf7757947 135 #endif
gallonm 4:a5abf7757947 136
gallonm 4:a5abf7757947 137 /* Initialization functions --------------------------------------------------*/
gallonm 4:a5abf7757947 138
gallonm 3:454541a079f4 139 int VL6180X::VL6180x_InitData(VL6180xDev_t dev)
gallonm 1:1de1ea2994d9 140 {
gallonm 3:454541a079f4 141 int status, dmax_status ;
gallonm 3:454541a079f4 142 int8_t offset;
gallonm 3:454541a079f4 143 uint8_t FreshOutReset;
gallonm 3:454541a079f4 144 uint32_t CalValue;
gallonm 3:454541a079f4 145 uint16_t u16;
gallonm 3:454541a079f4 146 uint32_t XTalkCompRate_KCps;
gallonm 3:454541a079f4 147
gallonm 3:454541a079f4 148 LOG_FUNCTION_START("");
gallonm 3:454541a079f4 149
gallonm 3:454541a079f4 150 VL6180xDevDataSet(dev, EceFactorM , DEF_ECE_FACTOR_M);
gallonm 3:454541a079f4 151 VL6180xDevDataSet(dev, EceFactorD , DEF_ECE_FACTOR_D);
gallonm 3:454541a079f4 152
gallonm 3:454541a079f4 153 #ifdef VL6180x_HAVE_UPSCALE_DATA
gallonm 3:454541a079f4 154 VL6180xDevDataSet(dev, UpscaleFactor , DEF_UPSCALE);
gallonm 3:454541a079f4 155 #endif
gallonm 3:454541a079f4 156
gallonm 3:454541a079f4 157 #ifdef VL6180x_HAVE_ALS_DATA
gallonm 3:454541a079f4 158 VL6180xDevDataSet(dev, IntegrationPeriod, DEF_INT_PEFRIOD);
gallonm 3:454541a079f4 159 VL6180xDevDataSet(dev, AlsGainCode, DEF_ALS_GAIN);
gallonm 3:454541a079f4 160 VL6180xDevDataSet(dev, AlsScaler, DEF_ALS_SCALER);
gallonm 3:454541a079f4 161 #endif
gallonm 3:454541a079f4 162
gallonm 3:454541a079f4 163 #ifdef VL6180x_HAVE_WRAP_AROUND_DATA
gallonm 3:454541a079f4 164 VL6180xDevDataSet(dev, WrapAroundFilterActive, (VL6180x_WRAP_AROUND_FILTER_SUPPORT >0));
gallonm 3:454541a079f4 165 VL6180xDevDataSet(dev, DMaxEnable, DEF_DMAX_ENABLE);
gallonm 3:454541a079f4 166 #endif
gallonm 3:454541a079f4 167
gallonm 3:454541a079f4 168 _DMax_OneTimeInit(dev);
gallonm 3:454541a079f4 169 do{
gallonm 3:454541a079f4 170
gallonm 3:454541a079f4 171 /* backup offset initial value from nvm these must be done prior any over call that use offset */
gallonm 4:a5abf7757947 172 status = VL6180x_RdByte(MyDevice,SYSRANGE_PART_TO_PART_RANGE_OFFSET, (uint8_t*)&offset);
gallonm 3:454541a079f4 173 if( status ){
gallonm 3:454541a079f4 174 VL6180x_ErrLog("SYSRANGE_PART_TO_PART_RANGE_OFFSET rd fail");
gallonm 3:454541a079f4 175 break;
gallonm 3:454541a079f4 176 }
gallonm 3:454541a079f4 177 VL6180xDevDataSet(dev, Part2PartOffsetNVM, offset);
gallonm 1:1de1ea2994d9 178
gallonm 4:a5abf7757947 179 status=VL6180x_RdDWord(MyDevice, SYSRANGE_RANGE_IGNORE_THRESHOLD, &CalValue);
gallonm 3:454541a079f4 180 if( status ){
gallonm 3:454541a079f4 181 VL6180x_ErrLog("Part2PartAmbNVM rd fail");
gallonm 3:454541a079f4 182 break;
gallonm 3:454541a079f4 183 }
gallonm 3:454541a079f4 184 if( (CalValue&0xFFFF0000) == 0 ){
gallonm 3:454541a079f4 185 CalValue=0x00CE03F8;
gallonm 3:454541a079f4 186 }
gallonm 3:454541a079f4 187 VL6180xDevDataSet(dev, Part2PartAmbNVM, CalValue);
gallonm 3:454541a079f4 188
gallonm 4:a5abf7757947 189 status = VL6180x_RdWord(MyDevice, SYSRANGE_CROSSTALK_COMPENSATION_RATE ,&u16);
gallonm 3:454541a079f4 190 if( status){
gallonm 3:454541a079f4 191 VL6180x_ErrLog("SYSRANGE_CROSSTALK_COMPENSATION_RATE rd fail ");
gallonm 3:454541a079f4 192 break;
gallonm 3:454541a079f4 193 }
gallonm 3:454541a079f4 194 XTalkCompRate_KCps = Fix7_2_KCPs(u16);
gallonm 3:454541a079f4 195 VL6180xDevDataSet(dev, XTalkCompRate_KCps , XTalkCompRate_KCps );
gallonm 1:1de1ea2994d9 196
gallonm 3:454541a079f4 197 dmax_status = _DMax_InitData(dev);
gallonm 3:454541a079f4 198 if( dmax_status < 0 ){
gallonm 3:454541a079f4 199 VL6180x_ErrLog("DMax init failure");
gallonm 3:454541a079f4 200 break;
gallonm 3:454541a079f4 201 }
gallonm 3:454541a079f4 202
gallonm 3:454541a079f4 203 /* Read or wait for fresh out of reset */
gallonm 4:a5abf7757947 204 status = VL6180x_RdByte(MyDevice,SYSTEM_FRESH_OUT_OF_RESET, &FreshOutReset);
gallonm 3:454541a079f4 205 if( status ) {
gallonm 3:454541a079f4 206 VL6180x_ErrLog("SYSTEM_FRESH_OUT_OF_RESET rd fail");
gallonm 3:454541a079f4 207 break;
gallonm 3:454541a079f4 208 }
gallonm 3:454541a079f4 209 if( FreshOutReset!= 1 || dmax_status )
gallonm 3:454541a079f4 210 status = CALIBRATION_WARNING;
gallonm 3:454541a079f4 211
gallonm 3:454541a079f4 212 }
gallonm 3:454541a079f4 213 while(0);
gallonm 3:454541a079f4 214
gallonm 3:454541a079f4 215 LOG_FUNCTION_END(status);
gallonm 3:454541a079f4 216 return status;
gallonm 1:1de1ea2994d9 217 }
gallonm 1:1de1ea2994d9 218
gallonm 1:1de1ea2994d9 219
gallonm 4:a5abf7757947 220 #define _DMaxData(field) VL6180xDevDataGet(dev, DMaxData.field)
gallonm 4:a5abf7757947 221
gallonm 4:a5abf7757947 222
gallonm 4:a5abf7757947 223 #ifndef VL6180x_PLATFORM_PROVIDE_SQRT
gallonm 4:a5abf7757947 224
gallonm 4:a5abf7757947 225 uint32_t VL6180X::VL6180x_SqrtUint32(uint32_t num) {
gallonm 4:a5abf7757947 226 uint32_t res = 0;
gallonm 4:a5abf7757947 227 uint32_t bit = 1 << 30; /* The second-to-top bit is set: 1 << 30 for 32 bits */
gallonm 4:a5abf7757947 228
gallonm 4:a5abf7757947 229 /* "bit" starts at the highest power of four <= the argument. */
gallonm 4:a5abf7757947 230 while (bit > num)
gallonm 4:a5abf7757947 231 bit >>= 2;
gallonm 1:1de1ea2994d9 232
gallonm 4:a5abf7757947 233 while (bit != 0) {
gallonm 4:a5abf7757947 234 if (num >= res + bit) {
gallonm 4:a5abf7757947 235 num -= res + bit;
gallonm 4:a5abf7757947 236 res = (res >> 1) + bit;
gallonm 4:a5abf7757947 237 }
gallonm 4:a5abf7757947 238 else
gallonm 4:a5abf7757947 239 res >>= 1;
gallonm 4:a5abf7757947 240 bit >>= 2;
gallonm 1:1de1ea2994d9 241 }
gallonm 4:a5abf7757947 242 return res;
gallonm 4:a5abf7757947 243 }
gallonm 4:a5abf7757947 244 #endif
gallonm 4:a5abf7757947 245
gallonm 4:a5abf7757947 246
gallonm 4:a5abf7757947 247 void VL6180X::_DMax_OneTimeInit(VL6180xDev_t dev){
gallonm 4:a5abf7757947 248 _DMaxData(ambTuningWindowFactor_K)=DEF_AMBIENT_TUNING;
gallonm 1:1de1ea2994d9 249 }
gallonm 1:1de1ea2994d9 250
gallonm 1:1de1ea2994d9 251
gallonm 4:a5abf7757947 252 uint32_t VL6180X::_DMax_RawValueAtRateKCps(VL6180xDev_t dev, int32_t rate){
gallonm 4:a5abf7757947 253 uint32_t snrLimit_K;
gallonm 4:a5abf7757947 254 int32_t DMaxSq;
gallonm 4:a5abf7757947 255 uint32_t RawDMax;
gallonm 4:a5abf7757947 256 DMaxFix_t retSignalAt400mm;
gallonm 4:a5abf7757947 257 uint32_t ambTuningWindowFactor_K;
gallonm 3:454541a079f4 258
gallonm 3:454541a079f4 259
gallonm 4:a5abf7757947 260 ambTuningWindowFactor_K = _DMaxData(ambTuningWindowFactor_K);
gallonm 4:a5abf7757947 261 snrLimit_K = _DMaxData(snrLimit_K);
gallonm 4:a5abf7757947 262 retSignalAt400mm = _DMaxData(retSignalAt400mm); /* 12 to 18 bits Kcps */
gallonm 4:a5abf7757947 263 if( rate > 0 ){
gallonm 4:a5abf7757947 264 DMaxSq = 400*400*1000 / rate -(400*400/330); /* K of (1/RtnAmb -1/330 )=> 30bit- (12-18)bit => 12-18 bits*/
gallonm 4:a5abf7757947 265 if( DMaxSq<= 0){
gallonm 4:a5abf7757947 266 RawDMax = 0;
gallonm 4:a5abf7757947 267 }
gallonm 4:a5abf7757947 268 else{
gallonm 4:a5abf7757947 269 /* value can be more 32 bit so base on raneg apply *retSignalAt400mm before or after division to presevr accuracy */
gallonm 4:a5abf7757947 270 if( DMaxSq< (2<<12) ){
gallonm 4:a5abf7757947 271 DMaxSq = DMaxSq*retSignalAt400mm/(snrLimit_K+ambTuningWindowFactor_K); /* max 12 + 12 to 18 -10 => 12-26 bit */
gallonm 4:a5abf7757947 272 }else{
gallonm 4:a5abf7757947 273 DMaxSq = DMaxSq/(snrLimit_K+ambTuningWindowFactor_K)*retSignalAt400mm; /* 12 to 18 -10 + 12 to 18 *=> 12-26 bit */
gallonm 4:a5abf7757947 274 }
gallonm 4:a5abf7757947 275 RawDMax=VL6180x_SqrtUint32(DMaxSq);
gallonm 4:a5abf7757947 276 }
gallonm 4:a5abf7757947 277 }
gallonm 4:a5abf7757947 278 else{
gallonm 4:a5abf7757947 279 RawDMax = 0x7FFFFFFF; /* bigest possibmle 32bit signed value */
gallonm 4:a5abf7757947 280 }
gallonm 4:a5abf7757947 281 return RawDMax;
gallonm 4:a5abf7757947 282 }
gallonm 3:454541a079f4 283
gallonm 3:454541a079f4 284
gallonm 4:a5abf7757947 285 int VL6180X::_DMax_InitData(VL6180xDev_t dev)
gallonm 4:a5abf7757947 286 {
gallonm 3:454541a079f4 287 int status, warning;
gallonm 3:454541a079f4 288 uint8_t u8;
gallonm 3:454541a079f4 289 uint16_t u16;
gallonm 3:454541a079f4 290 uint32_t u32;
gallonm 3:454541a079f4 291 uint32_t Reg2A_KCps;
gallonm 3:454541a079f4 292 uint32_t RegB8;
gallonm 3:454541a079f4 293 uint8_t MaxConvTime;
gallonm 3:454541a079f4 294 uint32_t XTalkCompRate_KCps;
gallonm 3:454541a079f4 295 uint32_t RangeIgnoreThreshold;
gallonm 3:454541a079f4 296 int32_t minSignalNeeded;
gallonm 3:454541a079f4 297 uint8_t SysRangeCheckEn;
gallonm 3:454541a079f4 298 uint8_t snrLimit;
gallonm 3:454541a079f4 299 warning=0;
gallonm 3:454541a079f4 300
gallonm 3:454541a079f4 301 static const int ROMABLE_DATA MaxConvTimeAdjust=-4;
gallonm 3:454541a079f4 302
gallonm 3:454541a079f4 303 LOG_FUNCTION_START("");
gallonm 3:454541a079f4 304 do{
gallonm 4:a5abf7757947 305 status = VL6180x_RdByte(MyDevice, 0x02A ,&u8);
gallonm 3:454541a079f4 306 if( status ){
gallonm 3:454541a079f4 307 VL6180x_ErrLog("Reg 0x02A rd fail");
gallonm 3:454541a079f4 308 break;
gallonm 3:454541a079f4 309 }
gallonm 3:454541a079f4 310
gallonm 3:454541a079f4 311 if( u8 == 0 ) {
gallonm 3:454541a079f4 312 warning = CALIBRATION_WARNING;
gallonm 3:454541a079f4 313 u8 = 40; /* use a default average value */
gallonm 3:454541a079f4 314 }
gallonm 3:454541a079f4 315 Reg2A_KCps = Fix7_2_KCPs(u8); /* convert to KCPs */
gallonm 3:454541a079f4 316
gallonm 4:a5abf7757947 317 status = VL6180x_RdByte(MyDevice, SYSRANGE_RANGE_CHECK_ENABLES, &SysRangeCheckEn);
gallonm 3:454541a079f4 318 if (status) {
gallonm 3:454541a079f4 319 VL6180x_ErrLog("SYSRANGE_RANGE_CHECK_ENABLES rd fail ");
gallonm 3:454541a079f4 320 break;
gallonm 3:454541a079f4 321 }
gallonm 3:454541a079f4 322
gallonm 4:a5abf7757947 323 status = VL6180x_RdByte(MyDevice, SYSRANGE_MAX_CONVERGENCE_TIME, &MaxConvTime);
gallonm 3:454541a079f4 324 if( status){
gallonm 3:454541a079f4 325 VL6180x_ErrLog("SYSRANGE_MAX_CONVERGENCE_TIME rd fail ");
gallonm 3:454541a079f4 326 break;
gallonm 3:454541a079f4 327 }
gallonm 3:454541a079f4 328
gallonm 4:a5abf7757947 329 status = VL6180x_RdDWord(MyDevice, 0x0B8, &RegB8);
gallonm 3:454541a079f4 330 if( status ){
gallonm 3:454541a079f4 331 VL6180x_ErrLog("reg 0x0B8 rd fail ");
gallonm 3:454541a079f4 332 break;
gallonm 3:454541a079f4 333 }
gallonm 3:454541a079f4 334
gallonm 4:a5abf7757947 335 status = VL6180x_RdByte(MyDevice, SYSRANGE_MAX_AMBIENT_LEVEL_MULT, &snrLimit);
gallonm 3:454541a079f4 336 if( status){
gallonm 3:454541a079f4 337 VL6180x_ErrLog("SYSRANGE_MAX_AMBIENT_LEVEL_MULT rd fail ");
gallonm 3:454541a079f4 338 break;
gallonm 3:454541a079f4 339 }
gallonm 3:454541a079f4 340 _DMaxData(snrLimit_K) = (int32_t)16*1000/snrLimit;
gallonm 3:454541a079f4 341 XTalkCompRate_KCps = VL6180xDevDataGet(dev, XTalkCompRate_KCps );
gallonm 3:454541a079f4 342
gallonm 3:454541a079f4 343 if( Reg2A_KCps >= XTalkCompRate_KCps){
gallonm 3:454541a079f4 344 _DMaxData(retSignalAt400mm)=( Reg2A_KCps - XTalkCompRate_KCps);
gallonm 3:454541a079f4 345 }
gallonm 3:454541a079f4 346 else{
gallonm 3:454541a079f4 347 _DMaxData(retSignalAt400mm)=0; /* Reg2A_K - XTalkCompRate_KCp <0 is invalid */
gallonm 3:454541a079f4 348 }
gallonm 3:454541a079f4 349
gallonm 3:454541a079f4 350 /* if xtalk range check is off omit it in snr clipping */
gallonm 3:454541a079f4 351 if( SysRangeCheckEn&RANGE_CHECK_RANGE_ENABLE_MASK ){
gallonm 4:a5abf7757947 352 status = VL6180x_RdWord(MyDevice, SYSRANGE_RANGE_IGNORE_THRESHOLD, &u16);
gallonm 3:454541a079f4 353 if( status){
gallonm 3:454541a079f4 354 VL6180x_ErrLog("SYSRANGE_RANGE_IGNORE_THRESHOLD rd fail ");
gallonm 3:454541a079f4 355 break;
gallonm 3:454541a079f4 356 }
gallonm 3:454541a079f4 357 RangeIgnoreThreshold = Fix7_2_KCPs(u16);
gallonm 3:454541a079f4 358 }
gallonm 3:454541a079f4 359 else{
gallonm 3:454541a079f4 360 RangeIgnoreThreshold = 0;
gallonm 3:454541a079f4 361 }
gallonm 3:454541a079f4 362
gallonm 3:454541a079f4 363 minSignalNeeded = (RegB8*256)/((int32_t)MaxConvTime+(int32_t)MaxConvTimeAdjust); /* KCps 8+8 bit -(1 to 6 bit) => 15-10 bit */
gallonm 3:454541a079f4 364 /* minSignalNeeded = max ( minSignalNeeded, RangeIgnoreThreshold - XTalkCompRate_KCps) */
gallonm 3:454541a079f4 365 if( minSignalNeeded <= RangeIgnoreThreshold - XTalkCompRate_KCps )
gallonm 3:454541a079f4 366 minSignalNeeded = RangeIgnoreThreshold - XTalkCompRate_KCps;
gallonm 3:454541a079f4 367
gallonm 3:454541a079f4 368 u32 = (minSignalNeeded*(uint32_t)snrLimit)/16;
gallonm 3:454541a079f4 369 _DMaxData(ClipSnrLimit ) = _DMax_RawValueAtRateKCps(dev, u32 ); /* clip to dmax to min signal snr limit rate*/
gallonm 3:454541a079f4 370 }
gallonm 3:454541a079f4 371 while(0);
gallonm 3:454541a079f4 372 if( !status )
gallonm 3:454541a079f4 373 status = warning;
gallonm 3:454541a079f4 374 LOG_FUNCTION_END(status);
gallonm 3:454541a079f4 375 return status;
gallonm 1:1de1ea2994d9 376 }
gallonm 1:1de1ea2994d9 377
gallonm 4:a5abf7757947 378
gallonm 3:454541a079f4 379 #undef Fix7_2_KCPs
gallonm 1:1de1ea2994d9 380
gallonm 4:a5abf7757947 381 /* Write and read functions from I2C -----------------------------------------*/
gallonm 4:a5abf7757947 382
gallonm 4:a5abf7757947 383 int VL6180X::VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data)
gallonm 4:a5abf7757947 384 {
gallonm 4:a5abf7757947 385 int status;
gallonm 4:a5abf7757947 386 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 387 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 388
gallonm 4:a5abf7757947 389 status=VL6180x_I2CWrite(dev, index, &data,(uint8_t)3);
gallonm 4:a5abf7757947 390 return status;
gallonm 4:a5abf7757947 391 }
gallonm 4:a5abf7757947 392
gallonm 4:a5abf7757947 393 int VL6180X::VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data)
gallonm 4:a5abf7757947 394 {
gallonm 4:a5abf7757947 395 int status;
gallonm 4:a5abf7757947 396 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 397 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 398
gallonm 4:a5abf7757947 399 status=VL6180x_I2CWrite(dev, index, (uint8_t *)&data,(uint8_t)4);
gallonm 4:a5abf7757947 400 return status;
gallonm 4:a5abf7757947 401 }
gallonm 4:a5abf7757947 402
gallonm 4:a5abf7757947 403 int VL6180X::VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data)
gallonm 4:a5abf7757947 404 {
gallonm 4:a5abf7757947 405 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 406 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 407 int status;
gallonm 4:a5abf7757947 408
gallonm 4:a5abf7757947 409 status=VL6180x_I2CWrite(dev, index, (uint8_t *)&data,(uint8_t)6);
gallonm 4:a5abf7757947 410 return status;
gallonm 4:a5abf7757947 411 }
gallonm 4:a5abf7757947 412
gallonm 4:a5abf7757947 413 int VL6180X::VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data)
gallonm 4:a5abf7757947 414 {
gallonm 4:a5abf7757947 415 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 416 int status;
gallonm 4:a5abf7757947 417 uint8_t buffer;
gallonm 4:a5abf7757947 418 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 419
gallonm 4:a5abf7757947 420 status=VL6180x_I2CRead(dev, index, &buffer,1);
gallonm 4:a5abf7757947 421 if( !status ){
gallonm 4:a5abf7757947 422 *data=buffer;
gallonm 4:a5abf7757947 423 }
gallonm 4:a5abf7757947 424 return status;
gallonm 4:a5abf7757947 425 }
gallonm 4:a5abf7757947 426
gallonm 4:a5abf7757947 427 int VL6180X::VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data)
gallonm 4:a5abf7757947 428 {
gallonm 4:a5abf7757947 429 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 430 int status;
gallonm 4:a5abf7757947 431 uint8_t *buffer;
gallonm 4:a5abf7757947 432 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 433
gallonm 4:a5abf7757947 434 status=VL6180x_I2CRead(dev, index, buffer,2);
gallonm 4:a5abf7757947 435 if( !status ){
gallonm 4:a5abf7757947 436 /* VL6180x register are Big endian if cpu is be direct read direct into *data is possible */
gallonm 4:a5abf7757947 437 *data=((uint16_t)buffer[0]<<8)|(uint16_t)buffer[1];
gallonm 4:a5abf7757947 438 }
gallonm 4:a5abf7757947 439 return status;
gallonm 4:a5abf7757947 440 }
gallonm 4:a5abf7757947 441
gallonm 4:a5abf7757947 442 int VL6180X::VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data)
gallonm 4:a5abf7757947 443 {
gallonm 4:a5abf7757947 444 VL6180x_I2C_USER_VAR
gallonm 4:a5abf7757947 445 int status;
gallonm 4:a5abf7757947 446 uint8_t *buffer;
gallonm 4:a5abf7757947 447 DECL_I2C_BUFFER
gallonm 4:a5abf7757947 448
gallonm 4:a5abf7757947 449
gallonm 4:a5abf7757947 450 status=VL6180x_I2CRead(dev, index, buffer,4);
gallonm 4:a5abf7757947 451 if( !status ){
gallonm 4:a5abf7757947 452 /* VL6180x register are Big endian if cpu is be direct read direct into data is possible */
gallonm 4:a5abf7757947 453 *data=((uint32_t)buffer[0]<<24)|((uint32_t)buffer[1]<<16)|((uint32_t)buffer[2]<<8)|((uint32_t)buffer[3]);
gallonm 4:a5abf7757947 454 }
gallonm 4:a5abf7757947 455 return status;
gallonm 4:a5abf7757947 456 }
gallonm 4:a5abf7757947 457
gallonm 4:a5abf7757947 458 int VL6180X::VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
gallonm 4:a5abf7757947 459 {
gallonm 4:a5abf7757947 460 int ret;
gallonm 4:a5abf7757947 461 int i;
gallonm 4:a5abf7757947 462 uint8_t tmp[TEMP_BUF_SIZE];
gallonm 4:a5abf7757947 463 uint16_t myRegisterAddr = RegisterAddr;
gallonm 4:a5abf7757947 464 uint8_t *array;
gallonm 4:a5abf7757947 465
gallonm 4:a5abf7757947 466 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
gallonm 4:a5abf7757947 467
gallonm 4:a5abf7757947 468 /* First, send 8 bits device address and 16 bits register address in BE format. Then, send data and STOP condition */
gallonm 4:a5abf7757947 469 tmp[0] = *(((uint8_t*)&myRegisterAddr)+1);
gallonm 4:a5abf7757947 470 tmp[1] = (uint8_t)RegisterAddr;
gallonm 4:a5abf7757947 471
gallonm 4:a5abf7757947 472 if(NumByteToWrite>1)
gallonm 4:a5abf7757947 473 {
gallonm 4:a5abf7757947 474 array=new uint8_t[NumByteToWrite];
gallonm 4:a5abf7757947 475 for(i=0;i<NumByteToWrite;i++)
gallonm 4:a5abf7757947 476 {
gallonm 4:a5abf7757947 477 array[NumByteToWrite-1-i]=pBuffer[i];
gallonm 4:a5abf7757947 478 }
gallonm 4:a5abf7757947 479 }
gallonm 4:a5abf7757947 480
gallonm 4:a5abf7757947 481 memcpy(tmp+2, array, NumByteToWrite);
gallonm 4:a5abf7757947 482
gallonm 4:a5abf7757947 483 ret = dev_i2c.write(DeviceAddr, (const char*)tmp, NumByteToWrite+sizeof(RegisterAddr), false);
gallonm 4:a5abf7757947 484
gallonm 4:a5abf7757947 485 if(ret) return -1;
gallonm 4:a5abf7757947 486 return 0;
gallonm 4:a5abf7757947 487 }
gallonm 4:a5abf7757947 488
gallonm 4:a5abf7757947 489 int VL6180X::VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
gallonm 4:a5abf7757947 490 {
gallonm 4:a5abf7757947 491 int ret;
gallonm 4:a5abf7757947 492 uint16_t myRegisterAddr = RegisterAddr;
gallonm 4:a5abf7757947 493 uint16_t myRegisterAddrBE;
gallonm 4:a5abf7757947 494
gallonm 4:a5abf7757947 495 myRegisterAddrBE = *(((uint8_t*)&myRegisterAddr)+1);
gallonm 4:a5abf7757947 496 *(((uint8_t*)&myRegisterAddrBE)+1) = (uint8_t)myRegisterAddr;
gallonm 4:a5abf7757947 497
gallonm 4:a5abf7757947 498 /* Send 8 bits device address and 16 bits register address in BE format, with no STOP condition */
gallonm 4:a5abf7757947 499 ret = dev_i2c.write(DeviceAddr, (const char*)&myRegisterAddrBE, sizeof(RegisterAddr), true);
gallonm 4:a5abf7757947 500 if(!ret) {
gallonm 4:a5abf7757947 501 /* Read data, with STOP condition */
gallonm 4:a5abf7757947 502 ret = dev_i2c.read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
gallonm 4:a5abf7757947 503 }
gallonm 4:a5abf7757947 504
gallonm 4:a5abf7757947 505 if(ret) return -1;
gallonm 4:a5abf7757947 506 return 0;
gallonm 4:a5abf7757947 507 }
gallonm 4:a5abf7757947 508
gallonm 4:a5abf7757947 509 /* IO read funcitons ---------------------------------------------------------*/
gallonm 4:a5abf7757947 510
gallonm 4:a5abf7757947 511 int VL6180X::VL6180X_ReadID(uint8_t *rl_id)
gallonm 4:a5abf7757947 512 {
gallonm 4:a5abf7757947 513 if(!rl_id)
gallonm 4:a5abf7757947 514 {
gallonm 4:a5abf7757947 515 return API_ERROR; // DA DEFINIRE IL TIPO DI ERRORE!!
gallonm 4:a5abf7757947 516 }
gallonm 4:a5abf7757947 517 return VL6180X_IO_Read(rl_id, IDENTIFICATION_MODEL_ID, 1);
gallonm 4:a5abf7757947 518 }
gallonm 4:a5abf7757947 519
gallonm 4:a5abf7757947 520
gallonm 4:a5abf7757947 521 int VL6180X::VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16_t NumByteToRead)
gallonm 4:a5abf7757947 522 {
gallonm 4:a5abf7757947 523 int lecture;
gallonm 4:a5abf7757947 524
gallonm 4:a5abf7757947 525 lecture=dev_i2c.i2c_read(pBuffer, MyDeviceAddress, RegisterAddress, NumByteToRead);
gallonm 4:a5abf7757947 526 if(lecture!=0)
gallonm 4:a5abf7757947 527 {
gallonm 4:a5abf7757947 528 return INVALID_PARAMS; // DA DEFINIRE IL TIPO DI ERRORE!!
gallonm 4:a5abf7757947 529 }
gallonm 4:a5abf7757947 530 return API_NO_ERROR; // DA DEFINIRE IL TIPO DI ERRORE!!
gallonm 4:a5abf7757947 531 }
gallonm 4:a5abf7757947 532
gallonm 4:a5abf7757947 533
gallonm 1:1de1ea2994d9 534 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/