Vijayaraghavan Narayanan / VLX6180X_API
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ranging_driver.cpp Source File

ranging_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 // Ranging_Driver.c
00024 
00025 /*
00026 '''
00027 Application-level functions used for configuration and operation during ranging.
00028 '''
00029 */
00030 
00031 
00032 // ST libraries
00033 #include "ranging_driver.h"
00034 #include "utilities.h"
00035 #include "platform.h"
00036 #include "debug.h"
00037 //----------------------------ECE_FACTOR-------------------------------------------------
00038 // global variable declarations
00039 //-----------------------------------------------------------------------------
00040 //double_t ece_factor = ECE_FACTOR; // user set ECE margin (< 1.00 for maximum detection, > 1.00 for minimizing red glow)
00041 uint32_t ece_factor_m = ECE_FACTOR_M;
00042 uint32_t ece_factor_d = ECE_FACTOR_D;
00043 
00044 //-----------------------------------------------------------------------------
00045 // function definitions
00046 //-----------------------------------------------------------------------------
00047 
00048 sensor_error range_set_dynamic_config(uint8_t device_base_address)
00049 {
00050 
00051     LOG_FUNCTION_START((void*)&device_base_address);
00052     LOG_FUNCTION_END(NULL);
00053 
00054     return SENSOR_ERROR_NONE;
00055 }
00056 
00057 sensor_error range_set_systemMode(uint8_t device_base_address, int32_t mode)
00058 {
00059     uint8_t startRegVal;
00060     sensor_error ret = SENSOR_ERROR_NONE;
00061 
00062     LOG_FUNCTION_START((void*)&device_base_address,(void*)&mode);
00063     switch (mode)
00064     {
00065         case RANGE_START_SINGLESHOT:
00066             i2c_write_byte(SYSRANGE_START, mode, device_base_address);
00067             break;
00068         case RANGE_START_CONTINUOUS:
00069             // ensure mode bit is set!
00070             startRegVal = i2c_read_byte(SYSRANGE_START, device_base_address);
00071             startRegVal |= 0x03;
00072             i2c_write_byte(SYSRANGE_START, startRegVal, device_base_address);
00073             break;
00074         case RANGE_STOP:
00075             // ensure mode bit is left unaffected!
00076             startRegVal = i2c_read_byte(SYSRANGE_START, device_base_address);
00077             startRegVal |= 0x01;
00078             // set the bit, as it is a toggle state, not a 0=Off, 1=ON!
00079             i2c_write_byte(SYSRANGE_START, startRegVal, device_base_address);
00080             break;
00081         default:
00082             ret = COMMON_INVALID_PARAMS;
00083     }
00084     LOG_FUNCTION_END(ret);
00085 
00086     return ret;
00087 }
00088 
00089 uint8_t range_get_systemMode(uint8_t device_base_address)
00090 {
00091     uint8_t ret = 0;
00092 
00093     LOG_FUNCTION_START((void*)&device_base_address);
00094     ret = i2c_read_byte(SYSRANGE_START, device_base_address);
00095     LOG_FUNCTION_END(ret);
00096 
00097     return ret;
00098 }
00099 
00100 uint8_t range_get_result(uint8_t device_base_address)
00101 {
00102     return i2c_read_byte(RESULT_RANGE_VAL, device_base_address);
00103 }
00104 
00105 uint32_t range_get_signal_rate(uint8_t device_base_address)
00106 {
00107     uint32_t rawVal = (uint32_t)i2c_read_word(RESULT_RANGE_SIGNAL_RATE, device_base_address);
00108 
00109     return rawVal;
00110 }
00111 
00112 sensor_error range_get_full_result(uint8_t device_base_address)
00113 {
00114 /*
00115     return_list = []
00116 
00117 # I2C base address is 8-bit indexing
00118     I2C_base_address = Utilities.device['reg_bank_go2']['I2C_SLAVE_DEVICE_ADDRESS'].read()
00119     I2C_base_address *= 2 # convert from 7-bit to 8-bit address
00120 
00121 # make the indexing easy, read from index 0x60, but we only want data from 0x62!
00122 # reading from indices 0x60-0x84
00123 #    common.Set_System_Group_Parameter_Hold()
00124     regs = Utilities.device.comms['read'](I2C_base_address, 0x60, 0x25)
00125     conv_threshold = Utilities.device['reg_bank_go2']['USER_CONV_CTRL_RETURN_THRESHOLD_FINE'].read() * 256
00126 #    common.Clear_System_Group_Parameter_Hold()
00127 
00128     result_range_val = regs[2] # address 0x62
00129     result_range_stray = regs[3]
00130     result_range_raw = regs[4]
00131 # no results at 0x65
00132     result_range_return_rate = (regs[6] << 8) + regs[7]
00133     result_range_reference_rate = (regs[8] << 8) + regs[9]
00134 # indices 0x6A, 0x6B empty
00135     result_range_Return_VCSEL_count = (regs[0x0C] << 24) + (regs[0x0D] << 16) + (regs[0x0E] << 8) + regs[0x0F]
00136     result_range_Reference_VCSEL_count = (regs[0x10] << 24) + (regs[0x11] << 16) + (regs[0x12] << 8) + regs[0x13]
00137     result_range_Return_AMB_count = (regs[0x14] << 24) + (regs[0x15] << 16) + (regs[0x16] << 8) + regs[0x17]
00138     result_range_Reference_AMB_count = (regs[0x18] << 24) + (regs[0x19] << 16) + (regs[0x1A] << 8) + regs[0x1B]
00139     result_range_Return_Conv_time = (regs[0x1C] << 24) + (regs[0x1D] << 16) + (regs[0x1E] << 8) + regs[0x1F]
00140     result_range_Reference_Conv_time = (regs[0x20] << 24) + (regs[0x21] << 16) + (regs[0x22] << 8) + regs[0x23]
00141 
00142     return_list.append( result_range_val )
00143     return_list.append( result_range_stray )
00144     return_list.append( result_range_raw )
00145     return_list.append( result_range_return_rate )
00146     return_list.append( result_range_reference_rate )
00147     return_list.append( result_range_Return_VCSEL_count )
00148     return_list.append( result_range_Reference_VCSEL_count )
00149     return_list.append( result_range_Return_AMB_count )
00150     return_list.append( result_range_Reference_AMB_count )
00151     return_list.append( result_range_Return_Conv_time )
00152     return_list.append( result_range_Reference_Conv_time )
00153     return_list.append( conv_threshold )
00154     return return_list
00155 
00156 */
00157     return SENSOR_ERROR_NONE;
00158 
00159 }
00160 
00161 sensor_error range_set_high_threshold(uint8_t device_base_address, uint8_t threshold)
00162 {
00163     //threshold type is uint8_t. no need to check parameter since from 0-255
00164     LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
00165     i2c_write_byte(SYSRANGE_THRESH_HIGH, threshold, device_base_address);
00166     LOG_FUNCTION_END(NULL);
00167 
00168     return SENSOR_ERROR_NONE;
00169 }
00170 
00171 uint8_t range_get_high_threshold(uint8_t device_base_address)
00172 {
00173     uint8_t ret = 0;
00174 
00175     LOG_FUNCTION_START((void*)&device_base_address);
00176     ret = i2c_read_byte(SYSRANGE_THRESH_HIGH, device_base_address);
00177     LOG_FUNCTION_END(ret);
00178 
00179     return ret;
00180 }
00181 
00182 sensor_error range_set_low_threshold(uint8_t device_base_address, uint8_t threshold)
00183 {
00184     sensor_error ret = SENSOR_ERROR_NONE;
00185     //threshold type is uint8_t. no need to check parameter since from 0-255
00186     LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold);
00187     i2c_write_byte(SYSRANGE_THRESH_LOW, threshold, device_base_address);
00188     LOG_FUNCTION_END(ret);
00189 
00190     return ret;
00191 }
00192 
00193 uint8_t range_get_low_threshold(uint8_t device_base_address)
00194 {
00195     uint8_t ret=0;
00196 
00197     LOG_FUNCTION_START((void*)&device_base_address);
00198     ret = i2c_read_byte(SYSRANGE_THRESH_LOW, device_base_address);
00199     LOG_FUNCTION_END(ret);
00200 
00201     return ret;
00202 }
00203 
00204 sensor_error range_set_interMeasurement_period(uint8_t device_base_address, uint16_t intermeasurement_period)
00205 {
00206     sensor_error ret = SENSOR_ERROR_NONE;
00207 
00208     LOG_FUNCTION_START((void*)&device_base_address,(void*)&intermeasurement_period);
00209     if (intermeasurement_period > 255*10)
00210     {
00211         // Clipping value to max of 2.55s
00212         intermeasurement_period = 255*10;
00213     }
00214     i2c_write_byte(SYSRANGE_INTERMEASUREMENT_PERIOD, (uint8_t)(intermeasurement_period/10), device_base_address);
00215 
00216     LOG_FUNCTION_END(ret);
00217 
00218     return ret;
00219 }
00220 
00221 uint16_t range_get_interMeasurement_period(uint8_t device_base_address)
00222 {
00223     uint8_t ret = 0;
00224 
00225     LOG_FUNCTION_START((void*)&device_base_address);
00226     ret = i2c_read_byte(SYSRANGE_INTERMEASUREMENT_PERIOD, device_base_address);
00227     ret *=10; //return in ms
00228     LOG_FUNCTION_END(ret);
00229 
00230     return ret;
00231 }
00232 
00233 sensor_error range_set_max_convergence_time(uint8_t device_base_address, int32_t max_convergence_time)
00234 {
00235     sensor_error ret = SENSOR_ERROR_NONE;
00236     LOG_FUNCTION_START((void*)&device_base_address,(void*)&max_convergence_time);
00237     i2c_write_byte(SYSRANGE_MAX_CONVERGENCE_TIME, max_convergence_time, device_base_address);
00238     range_set_early_convergence_estimate_threshold(device_base_address);
00239     LOG_FUNCTION_END(ret);
00240 
00241     return ret;
00242 }
00243 
00244 uint8_t range_get_max_convergence_time(uint8_t device_base_address)
00245 {
00246     return i2c_read_byte(SYSRANGE_MAX_CONVERGENCE_TIME, device_base_address);
00247 }
00248 
00249 sensor_error  range_set_crosstalk_compensation_rate(uint8_t device_base_address, int32_t crosstalk_compensation_rate)
00250 {
00251     sensor_error ret = SENSOR_ERROR_NONE;
00252 
00253     LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_compensation_rate);
00254     i2c_write_word(SYSRANGE_CROSSTALK_COMPENSATION_RATE, crosstalk_compensation_rate, device_base_address);
00255     LOG_FUNCTION_END(ret);
00256 
00257     return ret;
00258 }
00259 
00260 int32_t range_get_crosstalk_compensation_rate(uint8_t device_base_address)
00261 {
00262     uint32_t ret=0;
00263     LOG_FUNCTION_START((void*)&device_base_address);
00264     ret =  i2c_read_word(SYSRANGE_CROSSTALK_COMPENSATION_RATE, device_base_address);
00265     LOG_FUNCTION_END(NULL);
00266 
00267     return ret;
00268 }
00269 
00270 sensor_error range_set_crosstalk_compensation_range(uint8_t device_base_address, int32_t crosstalk_compensation_range)
00271 {
00272     sensor_error ret = SENSOR_ERROR_NONE;
00273 
00274     LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_compensation_range);
00275     i2c_write_byte(SYSRANGE_CROSSTALK_COMPENSATION_RANGE, crosstalk_compensation_range, device_base_address);
00276     LOG_FUNCTION_END(ret);
00277 
00278     return ret;
00279 }
00280 
00281 uint8_t range_get_crosstalk_compensation_range(uint8_t device_base_address)
00282 {
00283     uint8_t ret = 0;
00284 
00285     LOG_FUNCTION_START((void*)&device_base_address);
00286     ret = i2c_read_byte(SYSRANGE_CROSSTALK_COMPENSATION_RANGE, device_base_address);
00287     LOG_FUNCTION_END(NULL);
00288 
00289     return ret;
00290 }
00291 
00292 sensor_error range_set_crosstalk_valid_height(uint8_t device_base_address, int32_t crosstalk_valid_height)
00293 {
00294     sensor_error ret = SENSOR_ERROR_NONE;
00295 
00296     LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_valid_height);
00297     i2c_write_byte(SYSRANGE_CROSSTALK_VALID_HEIGHT, crosstalk_valid_height, device_base_address);
00298     LOG_FUNCTION_END(ret);
00299 
00300     return ret;
00301 }
00302 
00303 uint8_t range_get_crosstalk_valid_height(uint8_t device_base_address)
00304 {
00305     uint8_t ret;
00306     LOG_FUNCTION_START((void*)&device_base_address);
00307     ret = i2c_read_byte(SYSRANGE_CROSSTALK_VALID_HEIGHT, device_base_address);
00308     LOG_FUNCTION_END(ret);
00309 
00310     return ret;
00311 }
00312 
00313 sensor_error  range_set_early_convergence_estimate_threshold(uint8_t device_base_address)
00314 {
00315     sensor_error ret = SENSOR_ERROR_NONE;
00316   
00317     uint32_t cMicroSecPerMilliSec  = 1000;
00318     uint32_t cEceSampleTime_us     = 500;
00319     uint32_t maxConv_ms            = (uint32_t)range_get_max_convergence_time(device_base_address);
00320     uint32_t convergTime_us        = maxConv_ms * cMicroSecPerMilliSec - range_get_vernier_ave_total_time(device_base_address);
00321     uint32_t fineThresh            = range_get_converg_ctrl_rtn_thresh_fine(device_base_address);
00322     uint32_t eceThresh             = ece_factor_m * cEceSampleTime_us * fineThresh/(convergTime_us * ece_factor_d);
00323     LOG_FUNCTION_START((void*)&device_base_address,);
00324 
00325     i2c_write_word(SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, (uint16_t)eceThresh, device_base_address);
00326 
00327     LOG_FUNCTION_END(ret);
00328 
00329     return ret;
00330 }
00331 
00332 uint8_t range_get_early_convergence_estimate_threshold(uint8_t device_base_address)
00333 {
00334     uint8_t ret;
00335 
00336     LOG_FUNCTION_START((void*)&device_base_address);
00337 
00338     ret = i2c_read_word(SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, device_base_address);
00339 
00340     LOG_FUNCTION_END(ret);
00341 
00342     return ret;
00343 }
00344 
00345 uint32_t range_get_vernier_ave_total_time(uint8_t device_base_address)
00346 {
00347     uint32_t cFwOverhead_us        = 24;
00348     uint32_t cVernVcpSetupTime_us  = 70;
00349     uint32_t cPLL2_StartupDelay_us = 200;
00350     uint8_t cVernMeasMask          = 0x07;
00351     uint32_t vernSamples;
00352     uint32_t vernSamplePeriod;
00353     uint32_t singleVernTime_us;
00354     uint32_t totalVernAveTime_us;
00355 
00356     LOG_FUNCTION_START((void*)&device_base_address);
00357 
00358     // Calculate Vernier average time
00359     vernSamples         = (uint32_t)(i2c_read_byte(VERNIER_MEASUREMENTS, device_base_address) & cVernMeasMask);
00360     vernSamplePeriod    = (uint32_t)i2c_read_byte(VERNIER_RIPPLE_AVE_SAMPLE_PERIOD, device_base_address);
00361     singleVernTime_us   = cFwOverhead_us + cVernVcpSetupTime_us + (vernSamplePeriod * 10);
00362     totalVernAveTime_us = (vernSamples + 1) * singleVernTime_us + cPLL2_StartupDelay_us;
00363 
00364     LOG_FUNCTION_END(totalVernAveTime_us);
00365 
00366     return totalVernAveTime_us;
00367 }
00368 
00369 uint32_t range_get_converg_ctrl_rtn_thresh_fine(uint8_t device_base_address)
00370 {
00371     //uint32_t cBitMask = 0x00FFFFFFF;
00372     uint32_t cFineThreshLsb = 256;
00373     uint32_t rawValue;
00374     uint32_t realValue;
00375 
00376     LOG_FUNCTION_START((void*)&device_base_address);
00377 
00378     rawValue = i2c_read_uint32(USER_CONV_CTRL_RETURN_THRESHOLD_FINE, device_base_address);
00379     realValue = rawValue * cFineThreshLsb;
00380 
00381     LOG_FUNCTION_END(realValue);
00382 
00383     return realValue;
00384 }
00385 
00386 sensor_error range_set_ignore_valid_height(uint8_t device_base_address, int32_t ignore_valid_height)
00387 {
00388     sensor_error ret = SENSOR_ERROR_NONE;
00389     LOG_FUNCTION_START((void*)&device_base_address, (void*)&ignore_valid_height);
00390 
00391     i2c_write_byte(SYSRANGE_RANGE_IGNORE_VALID_HEIGHT, ignore_valid_height, device_base_address);
00392 
00393     LOG_FUNCTION_END(ret);
00394 
00395     return ret;
00396 }
00397 
00398 uint8_t range_get_ignore_valid_height(uint8_t device_base_address)
00399 {
00400     uint8_t ret; 
00401     LOG_FUNCTION_START((void*)&device_base_address);
00402 
00403     ret = i2c_read_byte(SYSRANGE_RANGE_IGNORE_VALID_HEIGHT, device_base_address);
00404 
00405     LOG_FUNCTION_END(ret);
00406 
00407     return ret;
00408 }
00409 
00410 sensor_error range_set_range_ignore_threshold(uint8_t device_base_address, uint32_t range_ignore_threshold)
00411 {
00412     sensor_error ret = SENSOR_ERROR_NONE;
00413     LOG_FUNCTION_START((void*)&device_base_address, (void*)&range_ignore_threshold);
00414     i2c_write_word(SYSRANGE_RANGE_IGNORE_THRESHOLD,
00415                    (uint16_t)range_ignore_threshold,
00416                    device_base_address);
00417 
00418 
00419     LOG_FUNCTION_END(ret);
00420 
00421     return ret;
00422 }
00423 
00424 uint32_t range_get_range_ignore_threshold(uint8_t device_base_address)
00425 {
00426     uint16_t ignoreThreshRegVal = i2c_read_word(SYSRANGE_RANGE_IGNORE_THRESHOLD, device_base_address);
00427     return ignoreThreshRegVal;
00428 }
00429 
00430 sensor_error range_set_emitter_block_threshold(uint8_t device_base_address, int32_t emitter_block_threshold)
00431 {
00432     sensor_error ret = SENSOR_ERROR_NONE;
00433     LOG_FUNCTION_START((void*)&device_base_address, (void*)&emitter_block_threshold);
00434     i2c_write_word(SYSRANGE_EMITTER_BLOCK_THRESHOLD, emitter_block_threshold, device_base_address);
00435 
00436     LOG_FUNCTION_END(ret);
00437 
00438     return ret;
00439 }
00440 
00441 uint8_t range_get_emitter_block_threshold(uint8_t device_base_address)
00442 {
00443     return i2c_read_word(SYSRANGE_EMITTER_BLOCK_THRESHOLD, device_base_address);
00444 }
00445 #ifndef __KERNEL__
00446 sensor_error range_set_snr_thresh(uint8_t device_base_address, float_t snrThresh)
00447 {
00448     sensor_error ret = SENSOR_ERROR_NONE;
00449     LOG_FUNCTION_START((void*)&device_base_address, (void*)&snrThresh);
00450 
00451     const float cRegFractionLsb = 0.0625f;
00452     const float cRegWholeLsb    = 1.0f;
00453     const float cReciprocalMax  = 16.0f - cRegFractionLsb;
00454     const float cSnrMin = 1.0f/cReciprocalMax;
00455 
00456     uint8_t snrThresRegVal;
00457 
00458     // Taking the reciprocal into account the allowable SNR range is 0.0625...16.0.
00459     if(snrThresh < cSnrMin)
00460     {
00461         snrThresh = cSnrMin;
00462     }
00463 
00464     float snrthreshMult = 1.0f/snrThresh;
00465     snrThresRegVal = (uint8_t)encodeTo4_4_Format(snrthreshMult);
00466     i2c_write_byte(SYSRANGE_MAX_AMBIENT_LEVEL_MULT, (uint8_t)snrThresRegVal, device_base_address);
00467 
00468     LOG_FUNCTION_END(ret);
00469         
00470       return ret;
00471 }
00472 
00473 
00474 float range_get_snr_thresh(uint8_t device_base_address)
00475 {
00476     LOG_FUNCTION_START((void*)&device_base_address);
00477 
00478     uint8_t snrthreshMult = i2c_read_byte(SYSRANGE_MAX_AMBIENT_LEVEL_MULT, device_base_address);
00479     float snrThresh = 1.0f/decodeFrom4_4_Format((uint32_t)snrthreshMult);
00480 
00481     LOG_FUNCTION_END(snrThresh);
00482 
00483     return snrThresh;
00484 }
00485 #endif
00486 sensor_error range_set_range_check_enables(uint8_t device_base_address, int32_t range_check_enables)
00487 {
00488     sensor_error ret = SENSOR_ERROR_NONE;
00489     LOG_FUNCTION_START((void*)&device_base_address, (void*)&range_check_enables);
00490 
00491     i2c_write_byte(SYSRANGE_RANGE_CHECK_ENABLES, range_check_enables, device_base_address);
00492 
00493     LOG_FUNCTION_END(ret);
00494 
00495     return ret;
00496 }
00497 
00498 uint8_t range_get_range_check_enables(uint8_t device_base_address)
00499 {
00500     return i2c_read_byte(SYSRANGE_RANGE_CHECK_ENABLES, device_base_address);
00501 }
00502 
00503 sensor_error range_set_vhv_recalibrate(uint8_t device_base_address, int32_t VHV_Recalibrate)
00504 {
00505     sensor_error ret = SENSOR_ERROR_NONE;
00506     LOG_FUNCTION_START((void*)&device_base_address, (void*)&VHV_Recalibrate);
00507 
00508     i2c_write_byte(SYSRANGE_VHV_RECALIBRATE, VHV_Recalibrate, device_base_address);
00509 
00510     LOG_FUNCTION_END(ret);
00511 
00512     return ret;
00513 }
00514 
00515 uint8_t range_get_vhv_recalibrate(uint8_t device_base_address)
00516 {
00517     return i2c_read_byte(SYSRANGE_VHV_RECALIBRATE, device_base_address);
00518 }
00519 
00520 sensor_error range_set_vhv_repeat_rate(uint8_t device_base_address, int32_t VHV_repeat_rate)
00521 {
00522     sensor_error ret = SENSOR_ERROR_NONE;
00523     LOG_FUNCTION_START((void*)&device_base_address, (void*)&VHV_repeat_rate);
00524 
00525     i2c_write_byte(SYSRANGE_VHV_REPEAT_RATE, VHV_repeat_rate, device_base_address);
00526 
00527     LOG_FUNCTION_END(ret);
00528 
00529     return ret;
00530 }
00531 
00532 uint8_t range_get_vhv_repeat_rate(uint8_t device_base_address)
00533 {
00534     return i2c_read_byte(SYSRANGE_VHV_REPEAT_RATE, device_base_address);
00535 }
00536 
00537 int32_t range_get_result_status(uint8_t device_base_address)
00538 {
00539     return i2c_read_byte(RESULT_RANGE_STATUS, device_base_address);
00540 }
00541 
00542 bool_t range_get_device_ready(uint8_t device_base_address)
00543 {
00544     if (i2c_read_byte(RESULT_RANGE_STATUS, device_base_address) & RANGE_DEVICE_READY)
00545     {
00546         return TRUE;
00547     }
00548     return FALSE;
00549 }
00550 
00551 uint8_t range_get_result_error_codes(uint8_t device_base_address)
00552 {
00553     return (i2c_read_byte(RESULT_RANGE_STATUS, device_base_address) & RANGE_ERROR_CODE) >> 4;
00554 }
00555 
00556 sensor_error range_set_ece_factor(uint8_t device_base_address, uint32_t ECE_Factor_M, uint32_t ECE_Factor_D)
00557 {
00558     sensor_error ret = SENSOR_ERROR_NONE;
00559 
00560     LOG_FUNCTION_START((void*)&device_base_address,(void*)&ECE_Factor_M, (void*)&ECE_Factor_D);
00561 
00562     ece_factor_m = ECE_Factor_M;
00563     ece_factor_d = ECE_Factor_D;
00564     range_set_early_convergence_estimate_threshold(device_base_address);
00565 
00566     LOG_FUNCTION_END(ret);
00567 
00568     return ret;
00569 }
00570 
00571 sensor_error range_get_ece_factor(uint32_t *pece_factor_m, uint32_t *pece_factor_d)
00572 {
00573     *pece_factor_m = ece_factor_m;
00574     *pece_factor_d = ece_factor_d;
00575 
00576     return SENSOR_ERROR_NONE;
00577 }
00578 
00579 sensor_error range_set_system_interrupt_config_gpio(uint8_t device_base_address, uint8_t Ranging_GPIO_interrupt_config)
00580 {
00581     uint8_t reg;
00582     sensor_error ret = SENSOR_ERROR_NONE;
00583 
00584     LOG_FUNCTION_START((void*)&device_base_address,(void*)&Ranging_GPIO_interrupt_config);
00585     switch (Ranging_GPIO_interrupt_config)
00586     {
00587         case CONFIG_GPIO_INTERRUPT_DISABLED:
00588         case CONFIG_GPIO_INTERRUPT_LEVEL_LOW:
00589         case CONFIG_GPIO_INTERRUPT_LEVEL_HIGH:
00590         case CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW:
00591         case CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY:
00592             reg = i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address);
00593             reg &= 0x38; // preserve only the ALS part of reg
00594             i2c_write_byte(SYSTEM_INTERRUPT_CONFIG_GPIO,
00595                                    (reg | Ranging_GPIO_interrupt_config),
00596                                    device_base_address);
00597             break;
00598         default :
00599             ret = COMMON_INVALID_PARAMS;
00600             break;
00601     }
00602     LOG_FUNCTION_END(ret);
00603 
00604     return ret;
00605 }
00606 
00607 uint8_t range_get_system_interrupt_config_gpio(uint8_t device_base_address)
00608 {
00609     uint8_t ret=0;
00610 
00611     LOG_FUNCTION_START((void*)&device_base_address);
00612     // expose only bits [2:0]
00613 
00614     ret = (i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address) & 0x07);
00615 
00616     LOG_FUNCTION_END(ret);
00617 
00618     return ret;
00619 }
00620 
00621 sensor_error range_set_system_interrupt_clear(uint8_t device_base_address)
00622 {
00623     sensor_error ret = SENSOR_ERROR_NONE;
00624     LOG_FUNCTION_START((void*)&device_base_address);
00625 
00626     i2c_write_byte(SYSTEM_INTERRUPT_CLEAR, INTERRUPT_CLEAR_RANGING, device_base_address);
00627 
00628     LOG_FUNCTION_END(ret);
00629 
00630     return ret;
00631 }
00632 
00633 uint8_t range_get_result_interrupt_status_gpio(uint8_t device_base_address)
00634 {
00635     uint8_t ret = 0;
00636 
00637     LOG_FUNCTION_START((void*)&device_base_address);
00638 
00639     ret = (i2c_read_byte(RESULT_INTERRUPT_STATUS_GPIO, device_base_address) & 0x07);
00640 
00641     LOG_FUNCTION_END(ret);
00642 
00643     return ret;
00644 }
00645 
00646 sensor_error common_set_history_buffer_range_mode_enable(uint8_t device_base_address)
00647 {
00648     uint8_t mode;
00649     sensor_error ret = SENSOR_ERROR_NONE;
00650     
00651     LOG_FUNCTION_START((void*)&device_base_address);
00652     
00653     mode = i2c_read_byte(SYSTEM_HISTORY_CTRL, device_base_address);
00654     mode = mode & 0xFD; // clear bit 1 to set to range mode
00655     mode = mode | 0x01; // set bit 0 to enable history buffer
00656     i2c_write_byte(SYSTEM_HISTORY_CTRL, mode, device_base_address);
00657     
00658     LOG_FUNCTION_END(NULL);
00659 
00660     return ret;
00661 }
00662 
00663 
00664 uint32_t range_get_upper_limit(uint8_t device_base_address)
00665 {
00666     const uint32_t cScalerMax1X = 255;
00667     uint32_t upperLim = cScalerMax1X;
00668 
00669     LOG_FUNCTION_START((void*)&device_base_address);
00670 
00671     LOG_FUNCTION_END(upperLim);
00672 
00673     return upperLim;
00674 }
00675 
00676 
00677 uint32_t range_get_lower_limit(uint8_t device_base_address)
00678 {
00679     uint32_t lowerLimit = 0;
00680     LOG_FUNCTION_START((void*)&device_base_address);
00681 
00682     LOG_FUNCTION_END(lowerLimit);
00683 
00684     return lowerLimit;
00685 }
00686