INSAT Mini Project

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Committer:
licio.mapelli@st.com
Date:
Tue Sep 29 14:50:15 2015 +0200
Revision:
8:f943a1fca15f
Parent:
7:2dc81120c917
Child:
9:9cbc1736f148
fixed i2c functions and code cleanup

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.h
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 Header file for component VL6180X
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 #ifndef __VL6180X_CLASS_H
gallonm 1:1de1ea2994d9 39 #define __VL6180X_CLASS_H
gallonm 1:1de1ea2994d9 40
gallonm 1:1de1ea2994d9 41 /* Includes ------------------------------------------------------------------*/
gallonm 1:1de1ea2994d9 42 #include "RangeSensor.h"
gallonm 1:1de1ea2994d9 43 #include "LightSensor.h"
gallonm 4:a5abf7757947 44 #include "DevI2C.h" //temporaneo fino alla creazione della classe x_nucleo_6180xa1
licio.mapelli@st.com 8:f943a1fca15f 45 //#include "vl6180x_api.h"
licio.mapelli@st.com 8:f943a1fca15f 46 //#include "vl6180x_cfg.h"
licio.mapelli@st.com 8:f943a1fca15f 47 //#include "vl6180x_def.h"
licio.mapelli@st.com 8:f943a1fca15f 48 //#include "vl6180x_types.h"
gallonm 1:1de1ea2994d9 49 #include "vl6180x_platform.h"
licio.mapelli@st.com 8:f943a1fca15f 50 //#include "vl6180x_appcfg.h"
gallonm 7:2dc81120c917 51 #include "STMPE1600_class.h"
gallonm 1:1de1ea2994d9 52
gallonm 4:a5abf7757947 53 /** default device address */
gallonm 4:a5abf7757947 54 #define DEFAULT_DEVICE_ADDRESS 0x29
gallonm 4:a5abf7757947 55
gallonm 1:1de1ea2994d9 56 /* Classes -------------------------------------------------------------------*/
gallonm 1:1de1ea2994d9 57 /** Class representing a VL6180X sensor component
gallonm 1:1de1ea2994d9 58 */
gallonm 7:2dc81120c917 59 class VL6180X : public RangeSensor, public LightSensor
gallonm 7:2dc81120c917 60 {
gallonm 1:1de1ea2994d9 61 public:
gallonm 1:1de1ea2994d9 62 /** Constructor
gallonm 1:1de1ea2994d9 63 * @param[in] i2c device I2C to be used for communication
gallonm 7:2dc81120c917 64 * @param[in] digital out pin/STMPE1600DigiOut pin to be used for GPIO expander communication
gallonm 4:a5abf7757947 65 * @param[in] device address, 0x29 by default
gallonm 7:2dc81120c917 66 * @param[in] device id, 0 by default
gallonm 1:1de1ea2994d9 67 */
gallonm 7:2dc81120c917 68
gallonm 7:2dc81120c917 69
gallonm 7:2dc81120c917 70 VL6180X(DevI2C &i2c, DigitalOut &pin, uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), gpio0(&pin)
gallonm 7:2dc81120c917 71 {
licio.mapelli@st.com 8:f943a1fca15f 72 // MyDeviceAddress=DevAddr;
licio.mapelli@st.com 8:f943a1fca15f 73 DevData.I2cAddr = DevAddr;
licio.mapelli@st.com 8:f943a1fca15f 74 DevData.Present=0;
licio.mapelli@st.com 8:f943a1fca15f 75 DevData.Ready=0;
licio.mapelli@st.com 8:f943a1fca15f 76 expgpio0 = NULL;
gallonm 7:2dc81120c917 77 }
gallonm 7:2dc81120c917 78
gallonm 7:2dc81120c917 79 VL6180X(DevI2C &i2c, STMPE1600DigiOut &pin, uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), expgpio0(&pin)
gallonm 7:2dc81120c917 80 {
licio.mapelli@st.com 8:f943a1fca15f 81 // MyDeviceAddress=DevAddr;
licio.mapelli@st.com 8:f943a1fca15f 82 DevData.I2cAddr = DevAddr;
licio.mapelli@st.com 8:f943a1fca15f 83 DevData.Present=0;
licio.mapelli@st.com 8:f943a1fca15f 84 DevData.Ready=0;
licio.mapelli@st.com 8:f943a1fca15f 85 gpio0 = NULL;
gallonm 7:2dc81120c917 86 }
gallonm 7:2dc81120c917 87
gallonm 4:a5abf7757947 88
gallonm 7:2dc81120c917 89 /** Destructor
gallonm 7:2dc81120c917 90 */
gallonm 7:2dc81120c917 91 //~VL6180X(){}
gallonm 7:2dc81120c917 92
gallonm 7:2dc81120c917 93
gallonm 7:2dc81120c917 94 void VL6180x_On(void) {
gallonm 7:2dc81120c917 95 if (gpio0) *gpio0=1;
gallonm 7:2dc81120c917 96 else if (expgpio0) *expgpio0=1;
gallonm 7:2dc81120c917 97 }
gallonm 7:2dc81120c917 98
gallonm 7:2dc81120c917 99 void VL6180x_Off(void) {
gallonm 7:2dc81120c917 100 if (gpio0) *gpio0=0;
gallonm 7:2dc81120c917 101 else if (expgpio0) *expgpio0=0;
gallonm 7:2dc81120c917 102 }
gallonm 7:2dc81120c917 103 //Init sensor accende, legge id, inizializza il dispositivo e prepara per le misure (range, als)
gallonm 7:2dc81120c917 104 //altrimenti errore e cancello dalla board
gallonm 7:2dc81120c917 105
gallonm 1:1de1ea2994d9 106 /*** Interface Methods ***/
gallonm 7:2dc81120c917 107 /*virtual*/ int Init()
gallonm 4:a5abf7757947 108 {
licio.mapelli@st.com 8:f943a1fca15f 109 return VL6180x_InitData(DevData); // ritorna 0 se corretto
gallonm 1:1de1ea2994d9 110 }
gallonm 3:454541a079f4 111
gallonm 7:2dc81120c917 112 /*virtual*/ int ReadID()
gallonm 4:a5abf7757947 113 {
gallonm 7:2dc81120c917 114 return VL6180X_ReadID(); // ritorna 0 se corretto e -2 se errore
gallonm 1:1de1ea2994d9 115 }
gallonm 7:2dc81120c917 116 /*
gallonm 7:2dc81120c917 117
gallonm 7:2dc81120c917 118 void SetPresent()
gallonm 7:2dc81120c917 119 {
gallonm 7:2dc81120c917 120 MyDevice.Present=1;
gallonm 7:2dc81120c917 121 }
gallonm 7:2dc81120c917 122
gallonm 7:2dc81120c917 123 void SetDeviceID(int n)
gallonm 7:2dc81120c917 124 {
gallonm 7:2dc81120c917 125 MyDevice.DevID=n;
gallonm 7:2dc81120c917 126 }
gallonm 7:2dc81120c917 127
gallonm 7:2dc81120c917 128 void SetReady()
gallonm 7:2dc81120c917 129 {
gallonm 7:2dc81120c917 130 MyDevice.Ready=1;
gallonm 7:2dc81120c917 131 }
gallonm 7:2dc81120c917 132
licio.mapelli@st.com 8:f943a1fca15f 133 */
licio.mapelli@st.com 8:f943a1fca15f 134 /*
gallonm 3:454541a079f4 135 virtual int GetRange(int *piData) {
gallonm 3:454541a079f4 136 return VL6180X_GetRange(piData);
gallonm 1:1de1ea2994d9 137 }
gallonm 1:1de1ea2994d9 138
gallonm 3:454541a079f4 139 virtual int GetLight(int *piData) {
gallonm 3:454541a079f4 140 return VL6180X_GetLight(piData);
gallonm 3:454541a079f4 141 }
licio.mapelli@st.com 8:f943a1fca15f 142 */
gallonm 7:2dc81120c917 143 /* api.c functions */
gallonm 7:2dc81120c917 144 int VL6180x_WaitDeviceBooted(VL6180xDev_t dev);
gallonm 7:2dc81120c917 145 int VL6180x_InitData(VL6180xDev_t dev );
gallonm 7:2dc81120c917 146 int VL6180x_SetupGPIO1(VL6180xDev_t dev, uint8_t IntFunction, int ActiveHigh);
gallonm 7:2dc81120c917 147 int VL6180x_Prepare(VL6180xDev_t dev);
gallonm 7:2dc81120c917 148 int VL6180x_RangeStartContinuousMode(VL6180xDev_t dev);
gallonm 7:2dc81120c917 149 int VL6180x_RangeStartSingleShot(VL6180xDev_t dev);
gallonm 7:2dc81120c917 150 int VL6180x_RangeSetMaxConvergenceTime(VL6180xDev_t dev, uint8_t MaxConTime_msec);
gallonm 7:2dc81120c917 151 int VL6180x_RangePollMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
gallonm 7:2dc81120c917 152 int VL6180x_RangeGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
gallonm 7:2dc81120c917 153 int VL6180x_RangeGetMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
gallonm 7:2dc81120c917 154 int VL6180x_RangeGetResult(VL6180xDev_t dev, int32_t *pRange_mm);
gallonm 7:2dc81120c917 155 int VL6180x_RangeConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
gallonm 7:2dc81120c917 156 int VL6180x_RangeGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
gallonm 7:2dc81120c917 157 int VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
gallonm 7:2dc81120c917 158 int VL6180x_AlsGetMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
gallonm 7:2dc81120c917 159 int VL6180x_AlsConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
gallonm 7:2dc81120c917 160 int VL6180x_AlsSetIntegrationPeriod(VL6180xDev_t dev, uint16_t period_ms);
gallonm 7:2dc81120c917 161 int VL6180x_AlsSetInterMeasurementPeriod(VL6180xDev_t dev, uint16_t intermeasurement_period_ms);
gallonm 7:2dc81120c917 162 int VL6180x_AlsSetAnalogueGain(VL6180xDev_t dev, uint8_t gain);
gallonm 7:2dc81120c917 163 int VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
gallonm 7:2dc81120c917 164 int VL6180x_AlsGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
gallonm 7:2dc81120c917 165 int VL6180x_StaticInit(VL6180xDev_t dev);
gallonm 7:2dc81120c917 166 int VL6180x_RangeWaitDeviceReady(VL6180xDev_t dev, int MaxLoop );
gallonm 7:2dc81120c917 167 int VL6180x_RangeSetInterMeasPeriod(VL6180xDev_t dev, uint32_t InterMeasTime_msec);
gallonm 7:2dc81120c917 168 int VL6180x_UpscaleSetScaling(VL6180xDev_t dev, uint8_t scaling);
gallonm 7:2dc81120c917 169 int VL6180x_UpscaleGetScaling(VL6180xDev_t dev);
gallonm 7:2dc81120c917 170 uint16_t VL6180x_GetUpperLimit(VL6180xDev_t dev);
gallonm 7:2dc81120c917 171 int VL6180x_RangeSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high, int SafeHold);
gallonm 7:2dc81120c917 172 int VL6180x_RangeGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high);
gallonm 7:2dc81120c917 173 int VL6180x_RangeSetRawThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
gallonm 7:2dc81120c917 174 int VL6180x_RangeSetEceFactor(VL6180xDev_t dev, uint16_t FactorM, uint16_t FactorD);
gallonm 7:2dc81120c917 175 int VL6180x_RangeSetEceState(VL6180xDev_t dev, int enable );
gallonm 7:2dc81120c917 176 int VL6180x_FilterSetState(VL6180xDev_t dev, int state);
gallonm 7:2dc81120c917 177 int VL6180x_FilterGetState(VL6180xDev_t dev);
gallonm 7:2dc81120c917 178 int VL6180x_DMaxSetState(VL6180xDev_t dev, int state);
gallonm 7:2dc81120c917 179 int VL6180x_DMaxGetState(VL6180xDev_t dev);
gallonm 7:2dc81120c917 180 int VL6180x_RangeSetSystemMode(VL6180xDev_t dev, uint8_t mode);
gallonm 7:2dc81120c917 181 int8_t VL6180x_GetOffsetCalibrationData(VL6180xDev_t dev);
gallonm 7:2dc81120c917 182 void VL6180x_SetOffsetCalibrationData(VL6180xDev_t dev, int8_t offset);
gallonm 7:2dc81120c917 183 int VL6180x_SetXTalkCompensationRate(VL6180xDev_t dev, FixPoint97_t Rate);
gallonm 7:2dc81120c917 184 int VL6180x_AlsWaitDeviceReady(VL6180xDev_t dev, int MaxLoop );
gallonm 7:2dc81120c917 185 int VL6180x_AlsSetSystemMode(VL6180xDev_t dev, uint8_t mode);
gallonm 7:2dc81120c917 186 int VL6180x_SetGroupParamHold(VL6180xDev_t dev, int Hold);
gallonm 7:2dc81120c917 187 int VL6180x_SetI2CAddress(VL6180xDev_t dev, uint8_t NewAddr);
gallonm 7:2dc81120c917 188 int VL6180x_SetupGPIOx(VL6180xDev_t dev, int pin, uint8_t IntFunction, int ActiveHigh);
gallonm 7:2dc81120c917 189 int VL6180x_SetGPIOxPolarity(VL6180xDev_t dev, int pin, int active_high);
gallonm 7:2dc81120c917 190 int VL6180x_SetGPIOxFunctionality(VL6180xDev_t dev, int pin, uint8_t functionality);
gallonm 7:2dc81120c917 191 int VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin);
gallonm 7:2dc81120c917 192 int VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *status);
gallonm 7:2dc81120c917 193 int VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear );
gallonm 7:2dc81120c917 194 int VL6180x_RangeStaticInit(VL6180xDev_t dev);
gallonm 7:2dc81120c917 195 int VL6180x_UpscaleRegInit(VL6180xDev_t dev);
gallonm 7:2dc81120c917 196 int VL6180x_UpscaleStaticInit(VL6180xDev_t dev);
gallonm 7:2dc81120c917 197 int VL6180x_AlsGetLux(VL6180xDev_t dev, lux_t *pLux);
gallonm 7:2dc81120c917 198 int _UpscaleInitPatch0(VL6180xDev_t dev);
gallonm 7:2dc81120c917 199 int VL6180x_RangeGetDeviceReady(VL6180xDev_t dev, int * Ready);
gallonm 7:2dc81120c917 200 int VL6180x_RangeSetEarlyConvergenceEestimateThreshold(VL6180xDev_t dev);
gallonm 7:2dc81120c917 201 int32_t _GetAveTotalTime(VL6180xDev_t dev);
gallonm 7:2dc81120c917 202 int32_t _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 203 int _filter_GetResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
gallonm 7:2dc81120c917 204 int _GetRateResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
gallonm 7:2dc81120c917 205 int _DMax_InitData(VL6180xDev_t dev);
gallonm 7:2dc81120c917 206
gallonm 7:2dc81120c917 207 /* Read functions from registers and write functions into registers */
gallonm 7:2dc81120c917 208 int VL6180X_ReadID();
licio.mapelli@st.com 8:f943a1fca15f 209 int VL6180X_IO_Read(uint8_t *pBuffer, uint16_t RegisterAddress, uint16_t NumByteToRead);
licio.mapelli@st.com 8:f943a1fca15f 210 int VL6180X_IO_Write(uint8_t *pBuffer, uint16_t RegisterAddress, uint16_t NumByteToWrite);
gallonm 4:a5abf7757947 211
gallonm 4:a5abf7757947 212 /* Write and read functions from I2C */
gallonm 4:a5abf7757947 213 int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data);
gallonm 4:a5abf7757947 214 int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data);
gallonm 4:a5abf7757947 215 int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data);
gallonm 4:a5abf7757947 216 int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data);
gallonm 4:a5abf7757947 217 int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data);
gallonm 4:a5abf7757947 218 int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data);
gallonm 7:2dc81120c917 219 int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData);
gallonm 4:a5abf7757947 220 int VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite);
gallonm 4:a5abf7757947 221 int VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead);
gallonm 7:2dc81120c917 222
gallonm 4:a5abf7757947 223
gallonm 3:454541a079f4 224 private:
gallonm 1:1de1ea2994d9 225 /*** Instance Variables ***/
gallonm 1:1de1ea2994d9 226 /* IO Device */
gallonm 7:2dc81120c917 227 DevI2C &dev_i2c;
gallonm 7:2dc81120c917 228 DigitalOut *gpio0;
gallonm 7:2dc81120c917 229 STMPE1600DigiOut *expgpio0;
licio.mapelli@st.com 8:f943a1fca15f 230
licio.mapelli@st.com 8:f943a1fca15f 231 VL6180xDev_t DevData;
licio.mapelli@st.com 8:f943a1fca15f 232 // VL6180xDev_t MyDeviceAddress;
licio.mapelli@st.com 8:f943a1fca15f 233 // unsigned Present;
licio.mapelli@st.com 8:f943a1fca15f 234 // unsigned Ready;
gallonm 1:1de1ea2994d9 235
gallonm 1:1de1ea2994d9 236 };
gallonm 1:1de1ea2994d9 237
gallonm 4:a5abf7757947 238 #endif // __VL6180X_CLASS_H