vlx lib

Committer:
vijaynvr
Date:
Sun Feb 08 14:26:51 2015 +0000
Revision:
0:bc9f26b5dadf
working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vijaynvr 0:bc9f26b5dadf 1 /*******************************************************************************
vijaynvr 0:bc9f26b5dadf 2 ################################################################################
vijaynvr 0:bc9f26b5dadf 3 # (C) STMicroelectronics 2014
vijaynvr 0:bc9f26b5dadf 4 #
vijaynvr 0:bc9f26b5dadf 5 # This program is free software; you can redistribute it and/or modify it under
vijaynvr 0:bc9f26b5dadf 6 # the terms of the GNU General Public License version 2 and only version 2 as
vijaynvr 0:bc9f26b5dadf 7 # published by the Free Software Foundation.
vijaynvr 0:bc9f26b5dadf 8 #
vijaynvr 0:bc9f26b5dadf 9 # This program is distributed in the hope that it will be useful, but WITHOUT
vijaynvr 0:bc9f26b5dadf 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
vijaynvr 0:bc9f26b5dadf 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
vijaynvr 0:bc9f26b5dadf 12 # details.
vijaynvr 0:bc9f26b5dadf 13 #
vijaynvr 0:bc9f26b5dadf 14 # You should have received a copy of the GNU General Public License along with
vijaynvr 0:bc9f26b5dadf 15 # this program; if not, write to the Free Software Foundation, Inc.,
vijaynvr 0:bc9f26b5dadf 16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
vijaynvr 0:bc9f26b5dadf 17 #
vijaynvr 0:bc9f26b5dadf 18 #------------------------------------------------------------------------------
vijaynvr 0:bc9f26b5dadf 19 # Imaging Division
vijaynvr 0:bc9f26b5dadf 20 ################################################################################
vijaynvr 0:bc9f26b5dadf 21 ********************************************************************************/
vijaynvr 0:bc9f26b5dadf 22
vijaynvr 0:bc9f26b5dadf 23 /*
vijaynvr 0:bc9f26b5dadf 24 '''
vijaynvr 0:bc9f26b5dadf 25 Application-level methods used by VL6180X for ALS operations.
vijaynvr 0:bc9f26b5dadf 26 '''
vijaynvr 0:bc9f26b5dadf 27 */
vijaynvr 0:bc9f26b5dadf 28
vijaynvr 0:bc9f26b5dadf 29 //# ST libraries
vijaynvr 0:bc9f26b5dadf 30 #include "als_driver.h"
vijaynvr 0:bc9f26b5dadf 31 #include "debug.h"
vijaynvr 0:bc9f26b5dadf 32 #include "platform.h"
vijaynvr 0:bc9f26b5dadf 33 #include "utilities.h"
vijaynvr 0:bc9f26b5dadf 34
vijaynvr 0:bc9f26b5dadf 35 //-----------------------------------------------------------------------------
vijaynvr 0:bc9f26b5dadf 36 // global variable declarations
vijaynvr 0:bc9f26b5dadf 37 //-----------------------------------------------------------------------------
vijaynvr 0:bc9f26b5dadf 38 static uint16_t _integration_period = DEFAULT_INTEGRATION_PERIOD;
vijaynvr 0:bc9f26b5dadf 39 static float_t _lux_resolution = DEFAULT_LUX_RESOLUTION;
vijaynvr 0:bc9f26b5dadf 40 static uint32_t _als_scaler = DEFAULT_ALS_SCALER;
vijaynvr 0:bc9f26b5dadf 41 static uint32_t _als_real_gain_val = DEFAULT_ALS_GAIN;
vijaynvr 0:bc9f26b5dadf 42 //-----------------------------------------------------------------------------
vijaynvr 0:bc9f26b5dadf 43 // method definitions
vijaynvr 0:bc9f26b5dadf 44 //-----------------------------------------------------------------------------
vijaynvr 0:bc9f26b5dadf 45
vijaynvr 0:bc9f26b5dadf 46 sensor_error als_set_dynamic_config(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 47 {
vijaynvr 0:bc9f26b5dadf 48 /*
vijaynvr 0:bc9f26b5dadf 49 '''
vijaynvr 0:bc9f26b5dadf 50 Device setup for ALS parameters. These settings can be applied at any time. The status of operation bit (bit 0) of the SYSALS_START is not important.
vijaynvr 0:bc9f26b5dadf 51
vijaynvr 0:bc9f26b5dadf 52 :rtype: none
vijaynvr 0:bc9f26b5dadf 53 '''
vijaynvr 0:bc9f26b5dadf 54 */
vijaynvr 0:bc9f26b5dadf 55
vijaynvr 0:bc9f26b5dadf 56 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 57
vijaynvr 0:bc9f26b5dadf 58 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 59
vijaynvr 0:bc9f26b5dadf 60 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 61 }
vijaynvr 0:bc9f26b5dadf 62
vijaynvr 0:bc9f26b5dadf 63 sensor_error als_set_systemMode(uint8_t device_base_address, uint8_t mode)
vijaynvr 0:bc9f26b5dadf 64 {
vijaynvr 0:bc9f26b5dadf 65 uint8_t startRegVal;
vijaynvr 0:bc9f26b5dadf 66 sensor_error ret = SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 67
vijaynvr 0:bc9f26b5dadf 68 LOG_FUNCTION_START((void*)&device_base_address, (void*)&mode);
vijaynvr 0:bc9f26b5dadf 69 switch (mode)
vijaynvr 0:bc9f26b5dadf 70 {
vijaynvr 0:bc9f26b5dadf 71 case ALS_START_SINGLESHOT:
vijaynvr 0:bc9f26b5dadf 72 i2c_write_byte(SYSALS_START, mode, device_base_address);
vijaynvr 0:bc9f26b5dadf 73 // i2c_write_byte(SYSALS_START, (uint8_t)ALS_START_SINGLESHOT, I2C_ADDR);
vijaynvr 0:bc9f26b5dadf 74 break;
vijaynvr 0:bc9f26b5dadf 75 case ALS_START_CONTINUOUS:
vijaynvr 0:bc9f26b5dadf 76 // ensure mode bit is set!
vijaynvr 0:bc9f26b5dadf 77 startRegVal = i2c_read_byte(SYSALS_START, device_base_address);
vijaynvr 0:bc9f26b5dadf 78 startRegVal |= 0x03;
vijaynvr 0:bc9f26b5dadf 79 i2c_write_byte(SYSALS_START, startRegVal, device_base_address);
vijaynvr 0:bc9f26b5dadf 80 break;
vijaynvr 0:bc9f26b5dadf 81 case ALS_STOP:
vijaynvr 0:bc9f26b5dadf 82 // ensure mode bit is left unaffected!
vijaynvr 0:bc9f26b5dadf 83 startRegVal = i2c_read_byte(SYSALS_START, device_base_address);
vijaynvr 0:bc9f26b5dadf 84 startRegVal |= 0x01;
vijaynvr 0:bc9f26b5dadf 85 // set the bit, as it is a toggle state, not a 0=Off, 1=ON!
vijaynvr 0:bc9f26b5dadf 86 i2c_write_byte(SYSALS_START, startRegVal, device_base_address);
vijaynvr 0:bc9f26b5dadf 87 break;
vijaynvr 0:bc9f26b5dadf 88 default:
vijaynvr 0:bc9f26b5dadf 89 ret = COMMON_INVALID_PARAMS;
vijaynvr 0:bc9f26b5dadf 90 }
vijaynvr 0:bc9f26b5dadf 91 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 92
vijaynvr 0:bc9f26b5dadf 93 return ret;
vijaynvr 0:bc9f26b5dadf 94 }
vijaynvr 0:bc9f26b5dadf 95
vijaynvr 0:bc9f26b5dadf 96 uint8_t als_get_systemMode(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 97 {
vijaynvr 0:bc9f26b5dadf 98 uint8_t ret=0;
vijaynvr 0:bc9f26b5dadf 99
vijaynvr 0:bc9f26b5dadf 100 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 101 ret = i2c_read_byte(SYSALS_START, device_base_address);
vijaynvr 0:bc9f26b5dadf 102 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 103
vijaynvr 0:bc9f26b5dadf 104 return ret;
vijaynvr 0:bc9f26b5dadf 105 }
vijaynvr 0:bc9f26b5dadf 106
vijaynvr 0:bc9f26b5dadf 107 uint16_t als_get_result(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 108 {
vijaynvr 0:bc9f26b5dadf 109 uint16_t ret=0;
vijaynvr 0:bc9f26b5dadf 110
vijaynvr 0:bc9f26b5dadf 111 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 112 ret = i2c_read_word(RESULT_ALS_VAL, device_base_address);
vijaynvr 0:bc9f26b5dadf 113 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 114
vijaynvr 0:bc9f26b5dadf 115 return ret;
vijaynvr 0:bc9f26b5dadf 116 }
vijaynvr 0:bc9f26b5dadf 117
vijaynvr 0:bc9f26b5dadf 118 uint16_t als_get_lux(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 119 {
vijaynvr 0:bc9f26b5dadf 120 //uint16_t ret=0;
vijaynvr 0:bc9f26b5dadf 121 float rawValue = 0.0f;
vijaynvr 0:bc9f26b5dadf 122 float integrationTimeRatio = DEFAULT_INTEGRATION_PERIOD/_integration_period;
vijaynvr 0:bc9f26b5dadf 123 uint32_t luxValue = 0;
vijaynvr 0:bc9f26b5dadf 124
vijaynvr 0:bc9f26b5dadf 125 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 126
vijaynvr 0:bc9f26b5dadf 127 rawValue = (float_t)als_get_result(device_base_address);
vijaynvr 0:bc9f26b5dadf 128 luxValue = roundFloatToInt(rawValue * integrationTimeRatio * _lux_resolution);
vijaynvr 0:bc9f26b5dadf 129 luxValue /= float(_als_scaler * _als_real_gain_val);
vijaynvr 0:bc9f26b5dadf 130
vijaynvr 0:bc9f26b5dadf 131 LOG_FUNCTION_END((uint16_t)luxValue);
vijaynvr 0:bc9f26b5dadf 132
vijaynvr 0:bc9f26b5dadf 133 return luxValue;
vijaynvr 0:bc9f26b5dadf 134 }
vijaynvr 0:bc9f26b5dadf 135
vijaynvr 0:bc9f26b5dadf 136 sensor_error als_set_thresholds(uint8_t device_base_address, uint16_t low_threshold, uint16_t high_threshold)
vijaynvr 0:bc9f26b5dadf 137 {
vijaynvr 0:bc9f26b5dadf 138 LOG_FUNCTION_START((void*)&device_base_address,(void*)&low_threshold, (void*)&high_threshold);
vijaynvr 0:bc9f26b5dadf 139 i2c_write_word(SYSALS_THRESH_LOW, low_threshold, device_base_address);
vijaynvr 0:bc9f26b5dadf 140 i2c_write_word(SYSALS_THRESH_HIGH, high_threshold, device_base_address);
vijaynvr 0:bc9f26b5dadf 141 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 142
vijaynvr 0:bc9f26b5dadf 143 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 144 }
vijaynvr 0:bc9f26b5dadf 145
vijaynvr 0:bc9f26b5dadf 146 sensor_error als_set_high_threshold(uint8_t device_base_address, uint16_t threshold)
vijaynvr 0:bc9f26b5dadf 147 {
vijaynvr 0:bc9f26b5dadf 148 LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
vijaynvr 0:bc9f26b5dadf 149 i2c_write_word(SYSALS_THRESH_HIGH, threshold, device_base_address);
vijaynvr 0:bc9f26b5dadf 150 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 151
vijaynvr 0:bc9f26b5dadf 152 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 153 }
vijaynvr 0:bc9f26b5dadf 154
vijaynvr 0:bc9f26b5dadf 155 uint16_t als_get_high_threshold(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 156 {
vijaynvr 0:bc9f26b5dadf 157 uint16_t ret = 0;
vijaynvr 0:bc9f26b5dadf 158
vijaynvr 0:bc9f26b5dadf 159 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 160 ret = i2c_read_word(SYSALS_THRESH_HIGH, device_base_address);
vijaynvr 0:bc9f26b5dadf 161 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 162
vijaynvr 0:bc9f26b5dadf 163 return ret;
vijaynvr 0:bc9f26b5dadf 164 }
vijaynvr 0:bc9f26b5dadf 165
vijaynvr 0:bc9f26b5dadf 166 sensor_error als_set_low_threshold(uint8_t device_base_address, uint16_t threshold)
vijaynvr 0:bc9f26b5dadf 167 {
vijaynvr 0:bc9f26b5dadf 168 LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
vijaynvr 0:bc9f26b5dadf 169 i2c_write_word(SYSALS_THRESH_LOW, threshold, device_base_address);
vijaynvr 0:bc9f26b5dadf 170 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 171
vijaynvr 0:bc9f26b5dadf 172 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 173 }
vijaynvr 0:bc9f26b5dadf 174
vijaynvr 0:bc9f26b5dadf 175 uint16_t als_get_low_threshold(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 176 {
vijaynvr 0:bc9f26b5dadf 177 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 178 uint16_t ret = i2c_read_word(SYSALS_THRESH_LOW, device_base_address);
vijaynvr 0:bc9f26b5dadf 179 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 180
vijaynvr 0:bc9f26b5dadf 181 return ret;
vijaynvr 0:bc9f26b5dadf 182 }
vijaynvr 0:bc9f26b5dadf 183
vijaynvr 0:bc9f26b5dadf 184 sensor_error als_set_interMeasurement_period(uint8_t device_base_address, uint16_t intermeasurement_period)
vijaynvr 0:bc9f26b5dadf 185 {
vijaynvr 0:bc9f26b5dadf 186 LOG_FUNCTION_START((void*)&device_base_address, (void*)&intermeasurement_period);
vijaynvr 0:bc9f26b5dadf 187 //clipping: range is 0-2550ms
vijaynvr 0:bc9f26b5dadf 188 if (intermeasurement_period >= 255 *10)
vijaynvr 0:bc9f26b5dadf 189 intermeasurement_period = 255 *10;
vijaynvr 0:bc9f26b5dadf 190 i2c_write_byte(SYSALS_INTERMEASUREMENT_PERIOD, (uint8_t)(intermeasurement_period/10), device_base_address);
vijaynvr 0:bc9f26b5dadf 191 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 192
vijaynvr 0:bc9f26b5dadf 193 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 194 }
vijaynvr 0:bc9f26b5dadf 195
vijaynvr 0:bc9f26b5dadf 196 uint16_t als_get_interMeasurement_period(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 197 {
vijaynvr 0:bc9f26b5dadf 198 uint16_t ret=0;
vijaynvr 0:bc9f26b5dadf 199
vijaynvr 0:bc9f26b5dadf 200 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 201 ret = i2c_read_byte(SYSALS_INTERMEASUREMENT_PERIOD, device_base_address);
vijaynvr 0:bc9f26b5dadf 202 ret *=10; //retun as time in ms
vijaynvr 0:bc9f26b5dadf 203 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 204
vijaynvr 0:bc9f26b5dadf 205 return ret;
vijaynvr 0:bc9f26b5dadf 206 }
vijaynvr 0:bc9f26b5dadf 207
vijaynvr 0:bc9f26b5dadf 208 sensor_error als_set_analogue_gain(uint8_t device_base_address, uint8_t light_analogue_gain)
vijaynvr 0:bc9f26b5dadf 209 {
vijaynvr 0:bc9f26b5dadf 210 const uint8_t GainDark = 0x40;
vijaynvr 0:bc9f26b5dadf 211 uint8_t GainTotal;
vijaynvr 0:bc9f26b5dadf 212
vijaynvr 0:bc9f26b5dadf 213 LOG_FUNCTION_START((void*)&device_base_address, (void*)&light_analogue_gain);
vijaynvr 0:bc9f26b5dadf 214 if (light_analogue_gain > 7)
vijaynvr 0:bc9f26b5dadf 215 {
vijaynvr 0:bc9f26b5dadf 216 // ALS_SetAnalogueGainLight: Clipping value to 7
vijaynvr 0:bc9f26b5dadf 217 light_analogue_gain = 7;
vijaynvr 0:bc9f26b5dadf 218 }
vijaynvr 0:bc9f26b5dadf 219
vijaynvr 0:bc9f26b5dadf 220 GainTotal = GainDark | light_analogue_gain; // add both together
vijaynvr 0:bc9f26b5dadf 221 i2c_write_byte(SYSALS_ANALOGUE_GAIN, GainTotal, device_base_address);
vijaynvr 0:bc9f26b5dadf 222
vijaynvr 0:bc9f26b5dadf 223 if(light_analogue_gain == 0)
vijaynvr 0:bc9f26b5dadf 224 {
vijaynvr 0:bc9f26b5dadf 225 _als_real_gain_val = 20.0f;
vijaynvr 0:bc9f26b5dadf 226 }
vijaynvr 0:bc9f26b5dadf 227 else if(light_analogue_gain == 1)
vijaynvr 0:bc9f26b5dadf 228 {
vijaynvr 0:bc9f26b5dadf 229 _als_real_gain_val = 10.0f;
vijaynvr 0:bc9f26b5dadf 230 }
vijaynvr 0:bc9f26b5dadf 231 else if(light_analogue_gain == 2)
vijaynvr 0:bc9f26b5dadf 232 {
vijaynvr 0:bc9f26b5dadf 233 _als_real_gain_val = 5.0f;
vijaynvr 0:bc9f26b5dadf 234 }
vijaynvr 0:bc9f26b5dadf 235 else if(light_analogue_gain == 3)
vijaynvr 0:bc9f26b5dadf 236 {
vijaynvr 0:bc9f26b5dadf 237 _als_real_gain_val = 2.5f;
vijaynvr 0:bc9f26b5dadf 238 }
vijaynvr 0:bc9f26b5dadf 239 else if(light_analogue_gain == 4)
vijaynvr 0:bc9f26b5dadf 240 {
vijaynvr 0:bc9f26b5dadf 241 _als_real_gain_val = 1.67f;
vijaynvr 0:bc9f26b5dadf 242 }
vijaynvr 0:bc9f26b5dadf 243 else if(light_analogue_gain == 5)
vijaynvr 0:bc9f26b5dadf 244 {
vijaynvr 0:bc9f26b5dadf 245 _als_real_gain_val = 1.25;
vijaynvr 0:bc9f26b5dadf 246 }
vijaynvr 0:bc9f26b5dadf 247 else if(light_analogue_gain == 6)
vijaynvr 0:bc9f26b5dadf 248 {
vijaynvr 0:bc9f26b5dadf 249 _als_real_gain_val = 1.0;
vijaynvr 0:bc9f26b5dadf 250 }
vijaynvr 0:bc9f26b5dadf 251
vijaynvr 0:bc9f26b5dadf 252 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 253
vijaynvr 0:bc9f26b5dadf 254 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 255 }
vijaynvr 0:bc9f26b5dadf 256
vijaynvr 0:bc9f26b5dadf 257 uint8_t als_get_analogue_gain(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 258 {
vijaynvr 0:bc9f26b5dadf 259 int8_t ret=0;
vijaynvr 0:bc9f26b5dadf 260
vijaynvr 0:bc9f26b5dadf 261 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 262 ret = i2c_read_byte(SYSALS_ANALOGUE_GAIN, device_base_address);
vijaynvr 0:bc9f26b5dadf 263 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 264
vijaynvr 0:bc9f26b5dadf 265 return ret;
vijaynvr 0:bc9f26b5dadf 266 }
vijaynvr 0:bc9f26b5dadf 267
vijaynvr 0:bc9f26b5dadf 268 sensor_error als_set_integration_period(uint8_t device_base_address, uint16_t integration_period)
vijaynvr 0:bc9f26b5dadf 269 {
vijaynvr 0:bc9f26b5dadf 270 int myTime;
vijaynvr 0:bc9f26b5dadf 271
vijaynvr 0:bc9f26b5dadf 272 LOG_FUNCTION_START((void*)&device_base_address, (void*)&integration_period);
vijaynvr 0:bc9f26b5dadf 273
vijaynvr 0:bc9f26b5dadf 274 myTime = integration_period - 1;
vijaynvr 0:bc9f26b5dadf 275
vijaynvr 0:bc9f26b5dadf 276 if (myTime < 0)
vijaynvr 0:bc9f26b5dadf 277 {
vijaynvr 0:bc9f26b5dadf 278 myTime = 0;
vijaynvr 0:bc9f26b5dadf 279 }
vijaynvr 0:bc9f26b5dadf 280 else if (myTime > 464)
vijaynvr 0:bc9f26b5dadf 281 {
vijaynvr 0:bc9f26b5dadf 282 // ALS_SetIntegrationPeriod: Clipping value to 465ms
vijaynvr 0:bc9f26b5dadf 283 myTime = 464;
vijaynvr 0:bc9f26b5dadf 284 }
vijaynvr 0:bc9f26b5dadf 285 else if (myTime == 255)
vijaynvr 0:bc9f26b5dadf 286 {
vijaynvr 0:bc9f26b5dadf 287 myTime++;
vijaynvr 0:bc9f26b5dadf 288 // can't write 255 since this causes the device to lock out.
vijaynvr 0:bc9f26b5dadf 289 }
vijaynvr 0:bc9f26b5dadf 290
vijaynvr 0:bc9f26b5dadf 291 i2c_write_word(SYSALS_INTEGRATION_PERIOD, myTime, device_base_address);
vijaynvr 0:bc9f26b5dadf 292
vijaynvr 0:bc9f26b5dadf 293 _integration_period = myTime;
vijaynvr 0:bc9f26b5dadf 294
vijaynvr 0:bc9f26b5dadf 295 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 296
vijaynvr 0:bc9f26b5dadf 297 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 298 }
vijaynvr 0:bc9f26b5dadf 299
vijaynvr 0:bc9f26b5dadf 300 uint16_t als_get_integration_period(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 301 {
vijaynvr 0:bc9f26b5dadf 302 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 303 uint16_t intTime = i2c_read_word(SYSALS_INTEGRATION_PERIOD, device_base_address);
vijaynvr 0:bc9f26b5dadf 304 intTime +=1;
vijaynvr 0:bc9f26b5dadf 305
vijaynvr 0:bc9f26b5dadf 306 _integration_period = intTime;
vijaynvr 0:bc9f26b5dadf 307
vijaynvr 0:bc9f26b5dadf 308 LOG_FUNCTION_END(intTime);
vijaynvr 0:bc9f26b5dadf 309
vijaynvr 0:bc9f26b5dadf 310 return intTime;
vijaynvr 0:bc9f26b5dadf 311 }
vijaynvr 0:bc9f26b5dadf 312
vijaynvr 0:bc9f26b5dadf 313
vijaynvr 0:bc9f26b5dadf 314 uint8_t als_get_result_status(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 315 {
vijaynvr 0:bc9f26b5dadf 316 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 317 uint8_t resultStatus = i2c_read_byte(RESULT_ALS_STATUS, device_base_address);
vijaynvr 0:bc9f26b5dadf 318 LOG_FUNCTION_END(resultStatus);
vijaynvr 0:bc9f26b5dadf 319
vijaynvr 0:bc9f26b5dadf 320 return resultStatus;
vijaynvr 0:bc9f26b5dadf 321 }
vijaynvr 0:bc9f26b5dadf 322
vijaynvr 0:bc9f26b5dadf 323 bool_t als_get_device_ready(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 324 {
vijaynvr 0:bc9f26b5dadf 325 bool_t deviceReady = FALSE;
vijaynvr 0:bc9f26b5dadf 326
vijaynvr 0:bc9f26b5dadf 327 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 328 if ((i2c_read_byte(RESULT_ALS_STATUS, device_base_address) & ALS_DEVICE_READY) == ALS_DEVICE_READY)
vijaynvr 0:bc9f26b5dadf 329 {
vijaynvr 0:bc9f26b5dadf 330 deviceReady = TRUE;
vijaynvr 0:bc9f26b5dadf 331 }
vijaynvr 0:bc9f26b5dadf 332 LOG_FUNCTION_END(deviceReady);
vijaynvr 0:bc9f26b5dadf 333
vijaynvr 0:bc9f26b5dadf 334 return deviceReady;
vijaynvr 0:bc9f26b5dadf 335 }
vijaynvr 0:bc9f26b5dadf 336
vijaynvr 0:bc9f26b5dadf 337 uint8_t als_get_result_error_codes(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 338 {
vijaynvr 0:bc9f26b5dadf 339 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 340 uint8_t errorCode = (i2c_read_byte(RESULT_ALS_STATUS, device_base_address) & 0xF0) >> 4;
vijaynvr 0:bc9f26b5dadf 341 LOG_FUNCTION_END(errorCode);
vijaynvr 0:bc9f26b5dadf 342
vijaynvr 0:bc9f26b5dadf 343 return errorCode;
vijaynvr 0:bc9f26b5dadf 344 }
vijaynvr 0:bc9f26b5dadf 345
vijaynvr 0:bc9f26b5dadf 346
vijaynvr 0:bc9f26b5dadf 347 sensor_error als_set_interleaved_mode(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 348 {
vijaynvr 0:bc9f26b5dadf 349 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 350 i2c_write_byte(INTERLEAVED_MODE_ENABLE, 1, device_base_address);
vijaynvr 0:bc9f26b5dadf 351 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 352
vijaynvr 0:bc9f26b5dadf 353 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 354 }
vijaynvr 0:bc9f26b5dadf 355
vijaynvr 0:bc9f26b5dadf 356 sensor_error als_clear_interleaved_mode(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 357 {
vijaynvr 0:bc9f26b5dadf 358 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 359 i2c_write_byte(INTERLEAVED_MODE_ENABLE, 0, device_base_address);
vijaynvr 0:bc9f26b5dadf 360 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 361
vijaynvr 0:bc9f26b5dadf 362 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 363 }
vijaynvr 0:bc9f26b5dadf 364
vijaynvr 0:bc9f26b5dadf 365 uint8_t als_get_interleaved_mode(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 366 {
vijaynvr 0:bc9f26b5dadf 367 uint8_t ret=0;
vijaynvr 0:bc9f26b5dadf 368
vijaynvr 0:bc9f26b5dadf 369 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 370 ret = i2c_read_byte(INTERLEAVED_MODE_ENABLE, device_base_address);
vijaynvr 0:bc9f26b5dadf 371 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 372
vijaynvr 0:bc9f26b5dadf 373 return ret;
vijaynvr 0:bc9f26b5dadf 374 }
vijaynvr 0:bc9f26b5dadf 375
vijaynvr 0:bc9f26b5dadf 376 ///////////////////////
vijaynvr 0:bc9f26b5dadf 377
vijaynvr 0:bc9f26b5dadf 378 sensor_error als_set_system_interrupt_config_gpio(uint8_t device_base_address, uint8_t ALS_GPIO_interrupt_config)
vijaynvr 0:bc9f26b5dadf 379 {
vijaynvr 0:bc9f26b5dadf 380 uint8_t cmd, reg;
vijaynvr 0:bc9f26b5dadf 381 sensor_error ret = SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 382
vijaynvr 0:bc9f26b5dadf 383 LOG_FUNCTION_START((void*)&device_base_address, (void*)&ALS_GPIO_interrupt_config);
vijaynvr 0:bc9f26b5dadf 384 switch (ALS_GPIO_interrupt_config)
vijaynvr 0:bc9f26b5dadf 385 {
vijaynvr 0:bc9f26b5dadf 386 case CONFIG_GPIO_INTERRUPT_DISABLED:
vijaynvr 0:bc9f26b5dadf 387 case CONFIG_GPIO_INTERRUPT_LEVEL_LOW:
vijaynvr 0:bc9f26b5dadf 388 case CONFIG_GPIO_INTERRUPT_LEVEL_HIGH:
vijaynvr 0:bc9f26b5dadf 389 case CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW:
vijaynvr 0:bc9f26b5dadf 390 case CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY:
vijaynvr 0:bc9f26b5dadf 391 cmd = ALS_GPIO_interrupt_config << 3; // shift command upto bits [5:3]
vijaynvr 0:bc9f26b5dadf 392 reg = i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address);
vijaynvr 0:bc9f26b5dadf 393 reg &= 0x07; // preserve only the range part of the reg
vijaynvr 0:bc9f26b5dadf 394 i2c_write_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, (reg | cmd), device_base_address);
vijaynvr 0:bc9f26b5dadf 395 _integration_period = als_get_integration_period(device_base_address);
vijaynvr 0:bc9f26b5dadf 396 break;
vijaynvr 0:bc9f26b5dadf 397 default :
vijaynvr 0:bc9f26b5dadf 398 ret = COMMON_INVALID_PARAMS;
vijaynvr 0:bc9f26b5dadf 399 break;
vijaynvr 0:bc9f26b5dadf 400 }
vijaynvr 0:bc9f26b5dadf 401 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 402
vijaynvr 0:bc9f26b5dadf 403 return ret;
vijaynvr 0:bc9f26b5dadf 404 }
vijaynvr 0:bc9f26b5dadf 405
vijaynvr 0:bc9f26b5dadf 406 uint8_t als_get_system_interrupt_config_gpio(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 407 {
vijaynvr 0:bc9f26b5dadf 408 uint8_t ret = 0;
vijaynvr 0:bc9f26b5dadf 409
vijaynvr 0:bc9f26b5dadf 410 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 411 //# expose only bits [5:3], then shift down 3 places
vijaynvr 0:bc9f26b5dadf 412 ret = ( (i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address) & 0x38) >> 3 );
vijaynvr 0:bc9f26b5dadf 413 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 414
vijaynvr 0:bc9f26b5dadf 415 return ret;
vijaynvr 0:bc9f26b5dadf 416 }
vijaynvr 0:bc9f26b5dadf 417
vijaynvr 0:bc9f26b5dadf 418 uint8_t als_get_result_interrupt_status_gpio(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 419 {
vijaynvr 0:bc9f26b5dadf 420 uint8_t ret = 0;
vijaynvr 0:bc9f26b5dadf 421
vijaynvr 0:bc9f26b5dadf 422 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 423 ret = ( (i2c_read_byte(RESULT_INTERRUPT_STATUS_GPIO, device_base_address) & 0x38) >> 3 );
vijaynvr 0:bc9f26b5dadf 424 LOG_FUNCTION_END(ret);
vijaynvr 0:bc9f26b5dadf 425
vijaynvr 0:bc9f26b5dadf 426 return ret;
vijaynvr 0:bc9f26b5dadf 427 }
vijaynvr 0:bc9f26b5dadf 428
vijaynvr 0:bc9f26b5dadf 429 sensor_error als_set_system_interrupt_clear(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 430 {
vijaynvr 0:bc9f26b5dadf 431
vijaynvr 0:bc9f26b5dadf 432 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 433 i2c_write_byte(SYSTEM_INTERRUPT_CLEAR, INTERRUPT_CLEAR_ALS, device_base_address);
vijaynvr 0:bc9f26b5dadf 434 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 435
vijaynvr 0:bc9f26b5dadf 436 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 437 }
vijaynvr 0:bc9f26b5dadf 438
vijaynvr 0:bc9f26b5dadf 439 sensor_error als_set_history_buffer_mode_enable(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 440 {
vijaynvr 0:bc9f26b5dadf 441 uint8_t mode;
vijaynvr 0:bc9f26b5dadf 442 sensor_error ret = SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 443
vijaynvr 0:bc9f26b5dadf 444 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 445
vijaynvr 0:bc9f26b5dadf 446 mode = i2c_read_byte(SYSTEM_HISTORY_CTRL, device_base_address);
vijaynvr 0:bc9f26b5dadf 447 mode = mode | 0x03; // set bits 0 and 1 to set to ALS mode and enable
vijaynvr 0:bc9f26b5dadf 448 i2c_write_byte(SYSTEM_HISTORY_CTRL, mode, device_base_address);
vijaynvr 0:bc9f26b5dadf 449
vijaynvr 0:bc9f26b5dadf 450 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 451
vijaynvr 0:bc9f26b5dadf 452 return ret;
vijaynvr 0:bc9f26b5dadf 453 }
vijaynvr 0:bc9f26b5dadf 454
vijaynvr 0:bc9f26b5dadf 455 sensor_error als_set_scaler(uint8_t device_base_address, uint8_t scaler)
vijaynvr 0:bc9f26b5dadf 456 {
vijaynvr 0:bc9f26b5dadf 457 const uint32_t cMask = 0x0f;
vijaynvr 0:bc9f26b5dadf 458
vijaynvr 0:bc9f26b5dadf 459 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 460
vijaynvr 0:bc9f26b5dadf 461 _als_scaler = (uint32_t)scaler & cMask;
vijaynvr 0:bc9f26b5dadf 462 i2c_write_byte(FW_ALS_RESULT_SCALER, (uint8_t)_als_scaler, device_base_address);
vijaynvr 0:bc9f26b5dadf 463
vijaynvr 0:bc9f26b5dadf 464 LOG_FUNCTION_END(NULL);
vijaynvr 0:bc9f26b5dadf 465
vijaynvr 0:bc9f26b5dadf 466 return SENSOR_ERROR_NONE;
vijaynvr 0:bc9f26b5dadf 467 }
vijaynvr 0:bc9f26b5dadf 468
vijaynvr 0:bc9f26b5dadf 469 uint32_t als_get_scaler(uint8_t device_base_address)
vijaynvr 0:bc9f26b5dadf 470 {
vijaynvr 0:bc9f26b5dadf 471 const uint32_t cMask = 0x0f;
vijaynvr 0:bc9f26b5dadf 472
vijaynvr 0:bc9f26b5dadf 473 LOG_FUNCTION_START((void*)&device_base_address);
vijaynvr 0:bc9f26b5dadf 474
vijaynvr 0:bc9f26b5dadf 475 _als_scaler = (uint32_t)i2c_read_byte(FW_ALS_RESULT_SCALER, device_base_address) & cMask;
vijaynvr 0:bc9f26b5dadf 476
vijaynvr 0:bc9f26b5dadf 477 LOG_FUNCTION_END(_als_scaler);
vijaynvr 0:bc9f26b5dadf 478
vijaynvr 0:bc9f26b5dadf 479 return _als_scaler;
vijaynvr 0:bc9f26b5dadf 480 }
vijaynvr 0:bc9f26b5dadf 481
vijaynvr 0:bc9f26b5dadf 482