Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
range_upscaling_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 functions used for configuration and operation during extended 00026 ranging. 00027 ''' 00028 */ 00029 00030 //----------------------------------------------------------------------------- 00031 // module imports 00032 //----------------------------------------------------------------------------- 00033 00034 // ST libraries 00035 00036 #include "range_upscaling_driver.h" 00037 00038 #include "common_driver.h" 00039 #include "ranging_driver.h" 00040 #include "debug.h" 00041 #include "platform.h" 00042 00043 00044 //----------------------------------------------------------------------------- 00045 // constant definitions 00046 //----------------------------------------------------------------------------- 00047 #define IDENTIFICATION_MODEL_ID 0x00 00048 #define IDENTIFICATION_MODULE_REV_MAJOR 0x03 00049 #define IDENTIFICATION_MODULE_REV_MINOR 0x04 00050 00051 #define RESULT_RANGE_VAL 0x62 00052 00053 #define RANGE_SCALER 0x96 00054 00055 //----------------------------------------------------------------------------- 00056 // global variable declarations 00057 //----------------------------------------------------------------------------- 00058 static ExtRangeScaler _rangeScaler = Scale2X; 00059 00060 //----------------------------------------------------------------------------- 00061 // method definitions 00062 //----------------------------------------------------------------------------- 00063 00064 static void er_set_register_tuning(uint8_t device_base_address); 00065 00066 00067 static void er_set_register_tuning(uint8_t device_base_address) 00068 { 00069 // apply REGISTER_TUNING_ER02_100614_CustomerView.tx 00070 00071 // Mandatory : private registers 00072 i2c_write_byte(0x0207, 0x01, device_base_address); 00073 i2c_write_byte(0x0208, 0x01, device_base_address); 00074 i2c_write_byte(0x0096, 0x00, device_base_address); 00075 i2c_write_byte(0x0097, 0x54, device_base_address); 00076 i2c_write_byte(0x00e3, 0x00, device_base_address); 00077 i2c_write_byte(0x00e4, 0x04, device_base_address); 00078 i2c_write_byte(0x00e5, 0x02, device_base_address); 00079 i2c_write_byte(0x00e6, 0x01, device_base_address); 00080 i2c_write_byte(0x00e7, 0x03, device_base_address); 00081 i2c_write_byte(0x00f5, 0x02, device_base_address); 00082 i2c_write_byte(0x00d9, 0x05, device_base_address); 00083 i2c_write_byte(0x00db, 0xce, device_base_address); 00084 i2c_write_byte(0x00dc, 0x03, device_base_address); 00085 i2c_write_byte(0x00dd, 0xf8, device_base_address); 00086 i2c_write_byte(0x009f, 0x00, device_base_address); 00087 i2c_write_byte(0x00a3, 0x28, device_base_address); 00088 i2c_write_byte(0x00b7, 0x00, device_base_address); 00089 i2c_write_byte(0x00bb, 0x28, device_base_address); 00090 i2c_write_byte(0x00b2, 0x09, device_base_address); 00091 i2c_write_byte(0x00ca, 0x09, device_base_address); 00092 i2c_write_byte(0x0198, 0x01, device_base_address); 00093 i2c_write_byte(0x01b0, 0x17, device_base_address); 00094 i2c_write_byte(0x01ad, 0x00, device_base_address); 00095 i2c_write_byte(0x00ff, 0x05, device_base_address); 00096 i2c_write_byte(0x0100, 0x05, device_base_address); 00097 i2c_write_byte(0x0199, 0x05, device_base_address); 00098 i2c_write_byte(0x01a6, 0x1b, device_base_address); 00099 i2c_write_byte(0x01ac, 0x3e, device_base_address); 00100 i2c_write_byte(0x01a7, 0x1f, device_base_address); 00101 i2c_write_byte(0x0030, 0x00, device_base_address); 00102 00103 // Recommended : Public registers - See data sheet for more detail 00104 i2c_write_byte(0x0011, 0x10, device_base_address); // Enables polling for �New Sample ready� when measurement completes 00105 i2c_write_byte(0x010a, 0x30, device_base_address); // Set the averaging sample period (compromise between lower noise and increased execution time) 00106 i2c_write_byte(0x003f, 0x46, device_base_address); // Sets the light and dark gain (upper nibble). Dark gain should not be changed. 00107 i2c_write_byte(0x0031, 0xFF, device_base_address); // sets the # of range measurements after which auto calibration of system is performed 00108 i2c_write_byte(0x0040, 0x63, device_base_address); // Set ALS integration time to 100ms 00109 i2c_write_byte(0x002e, 0x01, device_base_address); // perform a single temperature calibration of the ranging sensor 00110 i2c_write_byte(0x002c, 0xff, device_base_address); // set SNR limit to 0.06 00111 00112 // Optional: Public registers - See data sheet for more detail 00113 i2c_write_byte(0x001b, 0x09, device_base_address); // Set default ranging inter-measurement period to 100ms 00114 i2c_write_byte(0x003e, 0x31, device_base_address); // Set default ALS inter-measurement period to 500ms 00115 i2c_write_byte(0x0014, 0x24, device_base_address); // Configures interrupt on �New sample ready� 00116 } 00117 00118 sensor_error er_set_static_config(uint8_t device_base_address) 00119 { 00120 unsigned char module_id, rev_maj, rev_min; 00121 bool_t module; 00122 int32_t programmedOffset; 00123 00124 LOG_FUNCTION_START((void*)&device_base_address); 00125 00126 module_id = i2c_read_byte(IDENTIFICATION_MODEL_ID, device_base_address); 00127 // for use with NVM-programmed parts 00128 rev_maj = i2c_read_byte(IDENTIFICATION_MODULE_REV_MAJOR, device_base_address); 00129 rev_min = i2c_read_byte(IDENTIFICATION_MODULE_REV_MINOR, device_base_address); 00130 00131 module = FALSE; 00132 if ((module_id == 0xb4) && (rev_maj == 0x01) && (rev_min == 0x02)) 00133 module = TRUE; 00134 00135 if (module) 00136 er_set_register_tuning(device_base_address); 00137 00138 // VHV automatically run on parts that are NVM-programmed, ie customer parts! 00139 00140 if (common_get_system_fresh_out_of_reset(device_base_address)) 00141 { 00142 // must write the scaler at least once to the device to ensure the scaler is in a known state. 00143 er_set_scaler(_rangeScaler, device_base_address); 00144 00145 // for upscaled ranging, recalibrate the part-2-part offset 00146 programmedOffset = er_range_get_part2Part_range_offset(device_base_address); 00147 er_set_part2Part_range_offset(device_base_address, (uint8_t)(programmedOffset/_rangeScaler)); 00148 00149 // *** only if using glass **** 00150 // xtalk_height = ranging.Range_Get_Crosstalk_Valid_Height(device) 00151 // ranging.Range_Set_Crosstalk_Valid_Height(device, int(xtalk_height/_rangeScaler)) 00152 00153 common_clear_system_fresh_out_of_reset(device_base_address); 00154 } 00155 LOG_FUNCTION_END(NULL); 00156 00157 return SENSOR_ERROR_NONE; 00158 } 00159 00160 sensor_error er_set_part2Part_range_offset(uint8_t device_base_address, uint8_t part_to_part_range_offset) 00161 { 00162 LOG_FUNCTION_START((void*)&device_base_address,(void*)&part_to_part_range_offset); 00163 00164 i2c_write_byte(SYSRANGE_PART_TO_PART_RANGE_OFFSET, part_to_part_range_offset, device_base_address); 00165 00166 LOG_FUNCTION_END(NULL); 00167 00168 return SENSOR_ERROR_NONE; 00169 } 00170 00171 uint8_t er_range_get_part2Part_range_offset(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_PART_TO_PART_RANGE_OFFSET, device_base_address); 00177 LOG_FUNCTION_END(ret); 00178 00179 return ret; 00180 } 00181 00182 uint32_t er_get_result(uint8_t device_base_address) 00183 { 00184 uint32_t range = 0; 00185 00186 LOG_FUNCTION_START((void*)&device_base_address); 00187 00188 range = (uint32_t)(i2c_read_byte(RESULT_RANGE_VAL, device_base_address) * _rangeScaler); 00189 00190 LOG_FUNCTION_END(range); 00191 00192 return range; 00193 } 00194 00195 00196 sensor_error er_set_scaler(uint8_t scaler, uint8_t device_base_address) 00197 { 00198 //const uint16_t cMask = 0x01ff; 00199 const uint16_t cScalerVal1X = 253; 00200 const uint16_t cScalerVal2X = 127; 00201 const uint16_t cScalerVal3X = 84; 00202 uint32_t ret=0; 00203 uint16_t scalerVal = cScalerVal2X; 00204 00205 LOG_FUNCTION_START((void*)&device_base_address); 00206 00207 if(scaler == Scale1X) 00208 { 00209 _rangeScaler = Scale1X; 00210 scalerVal = cScalerVal1X; 00211 } 00212 else if(scaler == Scale2X) 00213 { 00214 _rangeScaler = Scale2X; 00215 scalerVal = cScalerVal2X; 00216 } 00217 else 00218 { 00219 _rangeScaler = Scale3X; 00220 scalerVal = cScalerVal3X; 00221 } 00222 00223 i2c_write_word(RANGE_SCALER, scalerVal, device_base_address); 00224 00225 LOG_FUNCTION_END(ret); 00226 00227 return ret; 00228 } 00229 00230 uint8_t er_get_scaler(uint8_t device_base_address) 00231 { 00232 uint8_t ret = _rangeScaler; 00233 00234 //LOG_FUNCTION_START((void*)&device_base_address); 00235 00236 //LOG_FUNCTION_END(ret); 00237 00238 return ret; 00239 } 00240 00241 uint32_t er_get_upper_limit(uint8_t device_base_address) 00242 { 00243 const uint32_t cScalerMax1X = 255; 00244 const uint32_t cScalerMax2X = 510; 00245 const uint32_t cScalerMax3X = 765; 00246 uint32_t upperLim = cScalerMax1X; 00247 00248 LOG_FUNCTION_START((void*)&device_base_address); 00249 00250 if(_rangeScaler == Scale2X) 00251 { 00252 upperLim = cScalerMax2X; 00253 } 00254 else if(_rangeScaler == Scale3X) 00255 { 00256 upperLim = cScalerMax3X; 00257 } 00258 00259 LOG_FUNCTION_END(upperLim); 00260 00261 return upperLim; 00262 } 00263 00264 00265 uint32_t er_get_lower_limit(uint8_t device_base_address) 00266 { 00267 uint32_t lowerLimit = 0; 00268 LOG_FUNCTION_START((void*)&device_base_address); 00269 00270 LOG_FUNCTION_END(lowerLimit); 00271 00272 return lowerLimit; 00273 } 00274 00275
Generated on Tue Jul 12 2022 22:19:50 by
1.7.2