Vijayaraghavan Narayanan / VLX6180X_API
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers als_driver.cpp Source File

als_driver.cpp

00001 /*******************************************************************************
00002 ################################################################################
00003 #                             (C) STMicroelectronics 2014
00004 #
00005 # This program is free software; you can redistribute it and/or modify it under
00006 # the terms of the GNU General Public License version 2 and only version 2 as
00007 # published by the Free Software Foundation.
00008 #
00009 # This program is distributed in the hope that it will be useful, but WITHOUT
00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00012 # details.
00013 #
00014 # You should have received a copy of the GNU General Public License along with
00015 # this program; if not, write to the Free Software Foundation, Inc.,
00016 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 #
00018 #------------------------------------------------------------------------------
00019 #                             Imaging Division
00020 ################################################################################
00021 ********************************************************************************/
00022 
00023 /*
00024 '''
00025 Application-level methods used by VL6180X for ALS operations.
00026 '''
00027 */
00028 
00029 //# ST libraries
00030 #include "als_driver.h"
00031 #include "debug.h"
00032 #include "platform.h"
00033 #include "utilities.h"
00034 
00035 //-----------------------------------------------------------------------------
00036 // global variable declarations
00037 //-----------------------------------------------------------------------------
00038 static uint16_t _integration_period = DEFAULT_INTEGRATION_PERIOD;
00039 static float_t _lux_resolution      = DEFAULT_LUX_RESOLUTION;
00040 static uint32_t _als_scaler         = DEFAULT_ALS_SCALER;
00041 static uint32_t _als_real_gain_val  = DEFAULT_ALS_GAIN;
00042 //-----------------------------------------------------------------------------
00043 // method definitions
00044 //-----------------------------------------------------------------------------
00045 
00046 sensor_error als_set_dynamic_config(uint8_t device_base_address)
00047 {
00048 /*
00049   '''
00050   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.
00051 
00052   :rtype: none
00053   '''
00054 */
00055 
00056     LOG_FUNCTION_START((void*)&device_base_address);
00057 
00058     LOG_FUNCTION_END(NULL);
00059 
00060     return SENSOR_ERROR_NONE;
00061 }
00062 
00063 sensor_error als_set_systemMode(uint8_t device_base_address, uint8_t mode)
00064 {
00065     uint8_t startRegVal;
00066     sensor_error ret = SENSOR_ERROR_NONE;
00067 
00068     LOG_FUNCTION_START((void*)&device_base_address, (void*)&mode);
00069     switch (mode)
00070     {
00071         case ALS_START_SINGLESHOT:
00072             i2c_write_byte(SYSALS_START, mode, device_base_address);
00073 //            i2c_write_byte(SYSALS_START, (uint8_t)ALS_START_SINGLESHOT, I2C_ADDR);
00074             break;
00075         case ALS_START_CONTINUOUS:
00076             // ensure mode bit is set!
00077             startRegVal = i2c_read_byte(SYSALS_START, device_base_address);
00078             startRegVal |= 0x03;
00079             i2c_write_byte(SYSALS_START, startRegVal, device_base_address);
00080             break;
00081         case ALS_STOP:
00082             // ensure mode bit is left unaffected!
00083             startRegVal = i2c_read_byte(SYSALS_START, device_base_address);
00084             startRegVal |= 0x01;
00085             // set the bit, as it is a toggle state, not a 0=Off, 1=ON!
00086             i2c_write_byte(SYSALS_START, startRegVal, device_base_address);
00087             break;
00088         default:
00089             ret = COMMON_INVALID_PARAMS;
00090     }
00091     LOG_FUNCTION_END(ret);
00092 
00093     return ret;
00094 }
00095 
00096 uint8_t als_get_systemMode(uint8_t device_base_address)
00097 {
00098     uint8_t ret=0;
00099 
00100     LOG_FUNCTION_START((void*)&device_base_address);
00101     ret = i2c_read_byte(SYSALS_START, device_base_address);
00102     LOG_FUNCTION_END(ret);
00103 
00104     return ret;
00105 }
00106 
00107 uint16_t als_get_result(uint8_t device_base_address)
00108 {
00109     uint16_t ret=0;
00110 
00111     LOG_FUNCTION_START((void*)&device_base_address);
00112     ret = i2c_read_word(RESULT_ALS_VAL, device_base_address);
00113     LOG_FUNCTION_END(ret);
00114 
00115     return ret;
00116 }
00117 
00118 uint16_t als_get_lux(uint8_t device_base_address)
00119 {
00120     //uint16_t ret=0;
00121     float rawValue = 0.0f;
00122     float integrationTimeRatio = DEFAULT_INTEGRATION_PERIOD/_integration_period;
00123     uint32_t luxValue = 0;
00124 
00125     LOG_FUNCTION_START((void*)&device_base_address);
00126 
00127     rawValue = (float_t)als_get_result(device_base_address);
00128     luxValue = roundFloatToInt(rawValue * integrationTimeRatio * _lux_resolution);
00129     luxValue /= float(_als_scaler * _als_real_gain_val);
00130 
00131     LOG_FUNCTION_END((uint16_t)luxValue);
00132 
00133     return luxValue;
00134 }
00135 
00136 sensor_error als_set_thresholds(uint8_t device_base_address, uint16_t low_threshold, uint16_t high_threshold)
00137 {
00138     LOG_FUNCTION_START((void*)&device_base_address,(void*)&low_threshold, (void*)&high_threshold);
00139     i2c_write_word(SYSALS_THRESH_LOW, low_threshold, device_base_address);
00140     i2c_write_word(SYSALS_THRESH_HIGH, high_threshold, device_base_address);
00141     LOG_FUNCTION_END(NULL);
00142 
00143     return SENSOR_ERROR_NONE;
00144 }
00145 
00146 sensor_error als_set_high_threshold(uint8_t device_base_address, uint16_t threshold)
00147 {
00148     LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
00149     i2c_write_word(SYSALS_THRESH_HIGH, threshold, device_base_address);
00150     LOG_FUNCTION_END(NULL);
00151 
00152     return SENSOR_ERROR_NONE;
00153 }
00154 
00155 uint16_t als_get_high_threshold(uint8_t device_base_address)
00156 {
00157     uint16_t ret = 0;
00158 
00159     LOG_FUNCTION_START((void*)&device_base_address);
00160     ret = i2c_read_word(SYSALS_THRESH_HIGH, device_base_address);
00161     LOG_FUNCTION_END(ret);
00162 
00163     return ret;
00164 }
00165 
00166 sensor_error als_set_low_threshold(uint8_t device_base_address, uint16_t threshold)
00167 {
00168     LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
00169     i2c_write_word(SYSALS_THRESH_LOW, threshold, device_base_address);
00170     LOG_FUNCTION_END(NULL);
00171 
00172     return SENSOR_ERROR_NONE;
00173 }
00174 
00175 uint16_t als_get_low_threshold(uint8_t device_base_address)
00176 {
00177     LOG_FUNCTION_START((void*)&device_base_address);
00178     uint16_t ret = i2c_read_word(SYSALS_THRESH_LOW, device_base_address);
00179     LOG_FUNCTION_END(ret);
00180 
00181     return ret;
00182 }
00183 
00184 sensor_error als_set_interMeasurement_period(uint8_t device_base_address, uint16_t intermeasurement_period)
00185 {
00186     LOG_FUNCTION_START((void*)&device_base_address, (void*)&intermeasurement_period);
00187         //clipping: range is 0-2550ms
00188         if (intermeasurement_period >= 255 *10)
00189                 intermeasurement_period = 255 *10;
00190     i2c_write_byte(SYSALS_INTERMEASUREMENT_PERIOD, (uint8_t)(intermeasurement_period/10), device_base_address);
00191     LOG_FUNCTION_END(NULL);
00192 
00193     return SENSOR_ERROR_NONE;
00194 }
00195 
00196 uint16_t als_get_interMeasurement_period(uint8_t device_base_address)
00197 {
00198     uint16_t ret=0;
00199 
00200     LOG_FUNCTION_START((void*)&device_base_address);
00201     ret = i2c_read_byte(SYSALS_INTERMEASUREMENT_PERIOD, device_base_address);
00202         ret *=10; //retun as time in ms
00203     LOG_FUNCTION_END(ret);
00204 
00205         return ret;
00206 }
00207 
00208 sensor_error als_set_analogue_gain(uint8_t device_base_address, uint8_t light_analogue_gain)
00209 {
00210     const uint8_t GainDark = 0x40;
00211     uint8_t GainTotal;
00212 
00213     LOG_FUNCTION_START((void*)&device_base_address, (void*)&light_analogue_gain);
00214     if (light_analogue_gain > 7)
00215     {
00216         // ALS_SetAnalogueGainLight: Clipping value to 7
00217         light_analogue_gain = 7;
00218     }
00219 
00220     GainTotal = GainDark | light_analogue_gain; // add both together
00221     i2c_write_byte(SYSALS_ANALOGUE_GAIN, GainTotal, device_base_address);
00222 
00223     if(light_analogue_gain == 0)
00224     {
00225         _als_real_gain_val = 20.0f;
00226     }
00227     else if(light_analogue_gain == 1)
00228     {
00229         _als_real_gain_val = 10.0f;
00230     }
00231     else if(light_analogue_gain == 2)
00232     {
00233         _als_real_gain_val = 5.0f;
00234     }
00235     else if(light_analogue_gain == 3)
00236     {
00237         _als_real_gain_val = 2.5f;
00238     }
00239     else if(light_analogue_gain == 4)
00240     {
00241         _als_real_gain_val = 1.67f;
00242     }
00243     else if(light_analogue_gain == 5)
00244     {
00245         _als_real_gain_val = 1.25;
00246     }
00247     else if(light_analogue_gain == 6)
00248     {
00249         _als_real_gain_val = 1.0;
00250     }
00251 
00252     LOG_FUNCTION_END(NULL);
00253 
00254     return SENSOR_ERROR_NONE;
00255 }
00256 
00257 uint8_t als_get_analogue_gain(uint8_t device_base_address)
00258 {
00259     int8_t ret=0;
00260 
00261     LOG_FUNCTION_START((void*)&device_base_address);
00262     ret = i2c_read_byte(SYSALS_ANALOGUE_GAIN, device_base_address);
00263     LOG_FUNCTION_END(ret);
00264 
00265     return ret;
00266 }
00267 
00268 sensor_error als_set_integration_period(uint8_t device_base_address, uint16_t integration_period)
00269 {
00270     int myTime;
00271 
00272     LOG_FUNCTION_START((void*)&device_base_address, (void*)&integration_period);
00273 
00274     myTime = integration_period - 1;
00275 
00276     if (myTime < 0)
00277     {
00278         myTime = 0;
00279     }
00280     else if (myTime > 464)
00281     {
00282         // ALS_SetIntegrationPeriod: Clipping value to 465ms
00283         myTime = 464;
00284     }
00285     else if (myTime == 255)
00286     {
00287         myTime++;
00288         // can't write 255 since this causes the device to lock out.
00289     }
00290 
00291     i2c_write_word(SYSALS_INTEGRATION_PERIOD, myTime, device_base_address);
00292 
00293     _integration_period = myTime;
00294 
00295     LOG_FUNCTION_END(NULL);
00296 
00297     return SENSOR_ERROR_NONE;
00298 }
00299 
00300 uint16_t als_get_integration_period(uint8_t device_base_address)
00301 {
00302     LOG_FUNCTION_START((void*)&device_base_address);
00303     uint16_t intTime = i2c_read_word(SYSALS_INTEGRATION_PERIOD, device_base_address);
00304     intTime +=1;
00305     
00306     _integration_period = intTime;
00307 
00308     LOG_FUNCTION_END(intTime);
00309 
00310     return intTime;
00311 }
00312 
00313 
00314 uint8_t als_get_result_status(uint8_t device_base_address)
00315 {
00316     LOG_FUNCTION_START((void*)&device_base_address);
00317     uint8_t resultStatus = i2c_read_byte(RESULT_ALS_STATUS, device_base_address);
00318     LOG_FUNCTION_END(resultStatus);
00319 
00320     return resultStatus;
00321 }
00322 
00323 bool_t als_get_device_ready(uint8_t device_base_address)
00324 {
00325     bool_t deviceReady = FALSE;
00326 
00327     LOG_FUNCTION_START((void*)&device_base_address);
00328     if ((i2c_read_byte(RESULT_ALS_STATUS, device_base_address) & ALS_DEVICE_READY) == ALS_DEVICE_READY)
00329     {
00330         deviceReady = TRUE;
00331     }
00332     LOG_FUNCTION_END(deviceReady);
00333 
00334     return deviceReady;
00335 }
00336 
00337 uint8_t als_get_result_error_codes(uint8_t device_base_address)
00338 {
00339     LOG_FUNCTION_START((void*)&device_base_address);
00340     uint8_t errorCode = (i2c_read_byte(RESULT_ALS_STATUS, device_base_address) & 0xF0) >> 4;
00341     LOG_FUNCTION_END(errorCode);
00342 
00343     return errorCode;
00344 }
00345 
00346 
00347 sensor_error als_set_interleaved_mode(uint8_t device_base_address)
00348 {
00349     LOG_FUNCTION_START((void*)&device_base_address);
00350     i2c_write_byte(INTERLEAVED_MODE_ENABLE, 1, device_base_address);
00351     LOG_FUNCTION_END(NULL);
00352 
00353     return SENSOR_ERROR_NONE;
00354 }
00355 
00356 sensor_error als_clear_interleaved_mode(uint8_t device_base_address)
00357 {
00358     LOG_FUNCTION_START((void*)&device_base_address);
00359     i2c_write_byte(INTERLEAVED_MODE_ENABLE, 0, device_base_address);
00360     LOG_FUNCTION_END(NULL);
00361 
00362     return SENSOR_ERROR_NONE;
00363 }
00364 
00365 uint8_t als_get_interleaved_mode(uint8_t device_base_address)
00366 {
00367     uint8_t ret=0;
00368 
00369     LOG_FUNCTION_START((void*)&device_base_address);
00370     ret = i2c_read_byte(INTERLEAVED_MODE_ENABLE, device_base_address);
00371     LOG_FUNCTION_END(ret);
00372 
00373     return ret;
00374 }
00375 
00376 ///////////////////////
00377 
00378 sensor_error als_set_system_interrupt_config_gpio(uint8_t device_base_address, uint8_t ALS_GPIO_interrupt_config)
00379 {
00380     uint8_t cmd, reg;
00381     sensor_error ret = SENSOR_ERROR_NONE;
00382 
00383     LOG_FUNCTION_START((void*)&device_base_address, (void*)&ALS_GPIO_interrupt_config);
00384     switch (ALS_GPIO_interrupt_config)
00385     {
00386         case CONFIG_GPIO_INTERRUPT_DISABLED:
00387         case CONFIG_GPIO_INTERRUPT_LEVEL_LOW:
00388         case CONFIG_GPIO_INTERRUPT_LEVEL_HIGH:
00389         case CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW:
00390         case CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY:
00391             cmd = ALS_GPIO_interrupt_config << 3; // shift command upto bits [5:3]
00392             reg = i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address);
00393             reg &= 0x07; // preserve only the range part of the reg
00394             i2c_write_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, (reg | cmd), device_base_address);
00395             _integration_period = als_get_integration_period(device_base_address);
00396             break;
00397         default :
00398             ret =  COMMON_INVALID_PARAMS;
00399             break;
00400     }
00401     LOG_FUNCTION_END(ret);
00402 
00403     return ret;
00404 }
00405 
00406 uint8_t als_get_system_interrupt_config_gpio(uint8_t device_base_address)
00407 {
00408     uint8_t ret = 0;
00409 
00410     LOG_FUNCTION_START((void*)&device_base_address);
00411     //# expose only bits [5:3], then shift down 3 places
00412     ret = ( (i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address) & 0x38) >> 3 );
00413     LOG_FUNCTION_END(ret);
00414 
00415     return ret;
00416 }
00417 
00418 uint8_t als_get_result_interrupt_status_gpio(uint8_t device_base_address)
00419 {
00420     uint8_t ret = 0;
00421 
00422     LOG_FUNCTION_START((void*)&device_base_address);
00423     ret = ( (i2c_read_byte(RESULT_INTERRUPT_STATUS_GPIO, device_base_address) & 0x38) >> 3 );
00424     LOG_FUNCTION_END(ret);
00425 
00426     return ret;
00427 }
00428 
00429 sensor_error als_set_system_interrupt_clear(uint8_t device_base_address)
00430 {
00431 
00432     LOG_FUNCTION_START((void*)&device_base_address);
00433     i2c_write_byte(SYSTEM_INTERRUPT_CLEAR, INTERRUPT_CLEAR_ALS, device_base_address);
00434     LOG_FUNCTION_END(NULL);
00435 
00436     return SENSOR_ERROR_NONE;
00437 }
00438 
00439 sensor_error als_set_history_buffer_mode_enable(uint8_t device_base_address)
00440 {
00441     uint8_t mode;
00442     sensor_error ret = SENSOR_ERROR_NONE;
00443     
00444     LOG_FUNCTION_START((void*)&device_base_address);
00445     
00446     mode = i2c_read_byte(SYSTEM_HISTORY_CTRL, device_base_address);
00447     mode = mode | 0x03; // set bits 0 and 1 to set to ALS mode and enable
00448     i2c_write_byte(SYSTEM_HISTORY_CTRL, mode, device_base_address);
00449     
00450     LOG_FUNCTION_END(NULL);
00451 
00452     return ret;
00453 }
00454 
00455 sensor_error als_set_scaler(uint8_t device_base_address, uint8_t scaler)
00456 {
00457     const uint32_t cMask = 0x0f;
00458 
00459     LOG_FUNCTION_START((void*)&device_base_address);
00460 
00461     _als_scaler = (uint32_t)scaler & cMask;
00462     i2c_write_byte(FW_ALS_RESULT_SCALER, (uint8_t)_als_scaler, device_base_address);
00463 
00464     LOG_FUNCTION_END(NULL);
00465 
00466     return SENSOR_ERROR_NONE;
00467 }
00468 
00469 uint32_t als_get_scaler(uint8_t device_base_address)
00470 {
00471     const uint32_t cMask = 0x0f;
00472 
00473     LOG_FUNCTION_START((void*)&device_base_address);
00474 
00475     _als_scaler = (uint32_t)i2c_read_byte(FW_ALS_RESULT_SCALER, device_base_address) & cMask;
00476 
00477     LOG_FUNCTION_END(_als_scaler);
00478 
00479     return _als_scaler;
00480 }
00481 
00482