vlx lib
ranging_driver.cpp@0:bc9f26b5dadf, 2015-02-08 (annotated)
- Committer:
- vijaynvr
- Date:
- Sun Feb 08 14:26:51 2015 +0000
- Revision:
- 0:bc9f26b5dadf
working
Who changed what in which revision?
User | Revision | Line number | New 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 | // Ranging_Driver.c |
vijaynvr | 0:bc9f26b5dadf | 24 | |
vijaynvr | 0:bc9f26b5dadf | 25 | /* |
vijaynvr | 0:bc9f26b5dadf | 26 | ''' |
vijaynvr | 0:bc9f26b5dadf | 27 | Application-level functions used for configuration and operation during ranging. |
vijaynvr | 0:bc9f26b5dadf | 28 | ''' |
vijaynvr | 0:bc9f26b5dadf | 29 | */ |
vijaynvr | 0:bc9f26b5dadf | 30 | |
vijaynvr | 0:bc9f26b5dadf | 31 | |
vijaynvr | 0:bc9f26b5dadf | 32 | // ST libraries |
vijaynvr | 0:bc9f26b5dadf | 33 | #include "ranging_driver.h" |
vijaynvr | 0:bc9f26b5dadf | 34 | #include "utilities.h" |
vijaynvr | 0:bc9f26b5dadf | 35 | #include "platform.h" |
vijaynvr | 0:bc9f26b5dadf | 36 | #include "debug.h" |
vijaynvr | 0:bc9f26b5dadf | 37 | //----------------------------ECE_FACTOR------------------------------------------------- |
vijaynvr | 0:bc9f26b5dadf | 38 | // global variable declarations |
vijaynvr | 0:bc9f26b5dadf | 39 | //----------------------------------------------------------------------------- |
vijaynvr | 0:bc9f26b5dadf | 40 | //double_t ece_factor = ECE_FACTOR; // user set ECE margin (< 1.00 for maximum detection, > 1.00 for minimizing red glow) |
vijaynvr | 0:bc9f26b5dadf | 41 | uint32_t ece_factor_m = ECE_FACTOR_M; |
vijaynvr | 0:bc9f26b5dadf | 42 | uint32_t ece_factor_d = ECE_FACTOR_D; |
vijaynvr | 0:bc9f26b5dadf | 43 | |
vijaynvr | 0:bc9f26b5dadf | 44 | //----------------------------------------------------------------------------- |
vijaynvr | 0:bc9f26b5dadf | 45 | // function definitions |
vijaynvr | 0:bc9f26b5dadf | 46 | //----------------------------------------------------------------------------- |
vijaynvr | 0:bc9f26b5dadf | 47 | |
vijaynvr | 0:bc9f26b5dadf | 48 | sensor_error range_set_dynamic_config(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 49 | { |
vijaynvr | 0:bc9f26b5dadf | 50 | |
vijaynvr | 0:bc9f26b5dadf | 51 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 52 | LOG_FUNCTION_END(NULL); |
vijaynvr | 0:bc9f26b5dadf | 53 | |
vijaynvr | 0:bc9f26b5dadf | 54 | return SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 55 | } |
vijaynvr | 0:bc9f26b5dadf | 56 | |
vijaynvr | 0:bc9f26b5dadf | 57 | sensor_error range_set_systemMode(uint8_t device_base_address, int32_t mode) |
vijaynvr | 0:bc9f26b5dadf | 58 | { |
vijaynvr | 0:bc9f26b5dadf | 59 | uint8_t startRegVal; |
vijaynvr | 0:bc9f26b5dadf | 60 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 61 | |
vijaynvr | 0:bc9f26b5dadf | 62 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&mode); |
vijaynvr | 0:bc9f26b5dadf | 63 | switch (mode) |
vijaynvr | 0:bc9f26b5dadf | 64 | { |
vijaynvr | 0:bc9f26b5dadf | 65 | case RANGE_START_SINGLESHOT: |
vijaynvr | 0:bc9f26b5dadf | 66 | i2c_write_byte(SYSRANGE_START, mode, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 67 | break; |
vijaynvr | 0:bc9f26b5dadf | 68 | case RANGE_START_CONTINUOUS: |
vijaynvr | 0:bc9f26b5dadf | 69 | // ensure mode bit is set! |
vijaynvr | 0:bc9f26b5dadf | 70 | startRegVal = i2c_read_byte(SYSRANGE_START, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 71 | startRegVal |= 0x03; |
vijaynvr | 0:bc9f26b5dadf | 72 | i2c_write_byte(SYSRANGE_START, startRegVal, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 73 | break; |
vijaynvr | 0:bc9f26b5dadf | 74 | case RANGE_STOP: |
vijaynvr | 0:bc9f26b5dadf | 75 | // ensure mode bit is left unaffected! |
vijaynvr | 0:bc9f26b5dadf | 76 | startRegVal = i2c_read_byte(SYSRANGE_START, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 77 | startRegVal |= 0x01; |
vijaynvr | 0:bc9f26b5dadf | 78 | // set the bit, as it is a toggle state, not a 0=Off, 1=ON! |
vijaynvr | 0:bc9f26b5dadf | 79 | i2c_write_byte(SYSRANGE_START, startRegVal, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 80 | break; |
vijaynvr | 0:bc9f26b5dadf | 81 | default: |
vijaynvr | 0:bc9f26b5dadf | 82 | ret = COMMON_INVALID_PARAMS; |
vijaynvr | 0:bc9f26b5dadf | 83 | } |
vijaynvr | 0:bc9f26b5dadf | 84 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 85 | |
vijaynvr | 0:bc9f26b5dadf | 86 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 87 | } |
vijaynvr | 0:bc9f26b5dadf | 88 | |
vijaynvr | 0:bc9f26b5dadf | 89 | uint8_t range_get_systemMode(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 90 | { |
vijaynvr | 0:bc9f26b5dadf | 91 | uint8_t ret = 0; |
vijaynvr | 0:bc9f26b5dadf | 92 | |
vijaynvr | 0:bc9f26b5dadf | 93 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 94 | ret = i2c_read_byte(SYSRANGE_START, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 95 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 96 | |
vijaynvr | 0:bc9f26b5dadf | 97 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 98 | } |
vijaynvr | 0:bc9f26b5dadf | 99 | |
vijaynvr | 0:bc9f26b5dadf | 100 | uint8_t range_get_result(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 101 | { |
vijaynvr | 0:bc9f26b5dadf | 102 | return i2c_read_byte(RESULT_RANGE_VAL, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 103 | } |
vijaynvr | 0:bc9f26b5dadf | 104 | |
vijaynvr | 0:bc9f26b5dadf | 105 | uint32_t range_get_signal_rate(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 106 | { |
vijaynvr | 0:bc9f26b5dadf | 107 | uint32_t rawVal = (uint32_t)i2c_read_word(RESULT_RANGE_SIGNAL_RATE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 108 | |
vijaynvr | 0:bc9f26b5dadf | 109 | return rawVal; |
vijaynvr | 0:bc9f26b5dadf | 110 | } |
vijaynvr | 0:bc9f26b5dadf | 111 | |
vijaynvr | 0:bc9f26b5dadf | 112 | sensor_error range_get_full_result(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 113 | { |
vijaynvr | 0:bc9f26b5dadf | 114 | /* |
vijaynvr | 0:bc9f26b5dadf | 115 | return_list = [] |
vijaynvr | 0:bc9f26b5dadf | 116 | |
vijaynvr | 0:bc9f26b5dadf | 117 | # I2C base address is 8-bit indexing |
vijaynvr | 0:bc9f26b5dadf | 118 | I2C_base_address = Utilities.device['reg_bank_go2']['I2C_SLAVE_DEVICE_ADDRESS'].read() |
vijaynvr | 0:bc9f26b5dadf | 119 | I2C_base_address *= 2 # convert from 7-bit to 8-bit address |
vijaynvr | 0:bc9f26b5dadf | 120 | |
vijaynvr | 0:bc9f26b5dadf | 121 | # make the indexing easy, read from index 0x60, but we only want data from 0x62! |
vijaynvr | 0:bc9f26b5dadf | 122 | # reading from indices 0x60-0x84 |
vijaynvr | 0:bc9f26b5dadf | 123 | # common.Set_System_Group_Parameter_Hold() |
vijaynvr | 0:bc9f26b5dadf | 124 | regs = Utilities.device.comms['read'](I2C_base_address, 0x60, 0x25) |
vijaynvr | 0:bc9f26b5dadf | 125 | conv_threshold = Utilities.device['reg_bank_go2']['USER_CONV_CTRL_RETURN_THRESHOLD_FINE'].read() * 256 |
vijaynvr | 0:bc9f26b5dadf | 126 | # common.Clear_System_Group_Parameter_Hold() |
vijaynvr | 0:bc9f26b5dadf | 127 | |
vijaynvr | 0:bc9f26b5dadf | 128 | result_range_val = regs[2] # address 0x62 |
vijaynvr | 0:bc9f26b5dadf | 129 | result_range_stray = regs[3] |
vijaynvr | 0:bc9f26b5dadf | 130 | result_range_raw = regs[4] |
vijaynvr | 0:bc9f26b5dadf | 131 | # no results at 0x65 |
vijaynvr | 0:bc9f26b5dadf | 132 | result_range_return_rate = (regs[6] << 8) + regs[7] |
vijaynvr | 0:bc9f26b5dadf | 133 | result_range_reference_rate = (regs[8] << 8) + regs[9] |
vijaynvr | 0:bc9f26b5dadf | 134 | # indices 0x6A, 0x6B empty |
vijaynvr | 0:bc9f26b5dadf | 135 | result_range_Return_VCSEL_count = (regs[0x0C] << 24) + (regs[0x0D] << 16) + (regs[0x0E] << 8) + regs[0x0F] |
vijaynvr | 0:bc9f26b5dadf | 136 | result_range_Reference_VCSEL_count = (regs[0x10] << 24) + (regs[0x11] << 16) + (regs[0x12] << 8) + regs[0x13] |
vijaynvr | 0:bc9f26b5dadf | 137 | result_range_Return_AMB_count = (regs[0x14] << 24) + (regs[0x15] << 16) + (regs[0x16] << 8) + regs[0x17] |
vijaynvr | 0:bc9f26b5dadf | 138 | result_range_Reference_AMB_count = (regs[0x18] << 24) + (regs[0x19] << 16) + (regs[0x1A] << 8) + regs[0x1B] |
vijaynvr | 0:bc9f26b5dadf | 139 | result_range_Return_Conv_time = (regs[0x1C] << 24) + (regs[0x1D] << 16) + (regs[0x1E] << 8) + regs[0x1F] |
vijaynvr | 0:bc9f26b5dadf | 140 | result_range_Reference_Conv_time = (regs[0x20] << 24) + (regs[0x21] << 16) + (regs[0x22] << 8) + regs[0x23] |
vijaynvr | 0:bc9f26b5dadf | 141 | |
vijaynvr | 0:bc9f26b5dadf | 142 | return_list.append( result_range_val ) |
vijaynvr | 0:bc9f26b5dadf | 143 | return_list.append( result_range_stray ) |
vijaynvr | 0:bc9f26b5dadf | 144 | return_list.append( result_range_raw ) |
vijaynvr | 0:bc9f26b5dadf | 145 | return_list.append( result_range_return_rate ) |
vijaynvr | 0:bc9f26b5dadf | 146 | return_list.append( result_range_reference_rate ) |
vijaynvr | 0:bc9f26b5dadf | 147 | return_list.append( result_range_Return_VCSEL_count ) |
vijaynvr | 0:bc9f26b5dadf | 148 | return_list.append( result_range_Reference_VCSEL_count ) |
vijaynvr | 0:bc9f26b5dadf | 149 | return_list.append( result_range_Return_AMB_count ) |
vijaynvr | 0:bc9f26b5dadf | 150 | return_list.append( result_range_Reference_AMB_count ) |
vijaynvr | 0:bc9f26b5dadf | 151 | return_list.append( result_range_Return_Conv_time ) |
vijaynvr | 0:bc9f26b5dadf | 152 | return_list.append( result_range_Reference_Conv_time ) |
vijaynvr | 0:bc9f26b5dadf | 153 | return_list.append( conv_threshold ) |
vijaynvr | 0:bc9f26b5dadf | 154 | return return_list |
vijaynvr | 0:bc9f26b5dadf | 155 | |
vijaynvr | 0:bc9f26b5dadf | 156 | */ |
vijaynvr | 0:bc9f26b5dadf | 157 | return SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 158 | |
vijaynvr | 0:bc9f26b5dadf | 159 | } |
vijaynvr | 0:bc9f26b5dadf | 160 | |
vijaynvr | 0:bc9f26b5dadf | 161 | sensor_error range_set_high_threshold(uint8_t device_base_address, uint8_t threshold) |
vijaynvr | 0:bc9f26b5dadf | 162 | { |
vijaynvr | 0:bc9f26b5dadf | 163 | //threshold type is uint8_t. no need to check parameter since from 0-255 |
vijaynvr | 0:bc9f26b5dadf | 164 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold); |
vijaynvr | 0:bc9f26b5dadf | 165 | i2c_write_byte(SYSRANGE_THRESH_HIGH, threshold, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 166 | LOG_FUNCTION_END(NULL); |
vijaynvr | 0:bc9f26b5dadf | 167 | |
vijaynvr | 0:bc9f26b5dadf | 168 | return SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 169 | } |
vijaynvr | 0:bc9f26b5dadf | 170 | |
vijaynvr | 0:bc9f26b5dadf | 171 | uint8_t range_get_high_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 172 | { |
vijaynvr | 0:bc9f26b5dadf | 173 | uint8_t ret = 0; |
vijaynvr | 0:bc9f26b5dadf | 174 | |
vijaynvr | 0:bc9f26b5dadf | 175 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 176 | ret = i2c_read_byte(SYSRANGE_THRESH_HIGH, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 177 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 178 | |
vijaynvr | 0:bc9f26b5dadf | 179 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 180 | } |
vijaynvr | 0:bc9f26b5dadf | 181 | |
vijaynvr | 0:bc9f26b5dadf | 182 | sensor_error range_set_low_threshold(uint8_t device_base_address, uint8_t threshold) |
vijaynvr | 0:bc9f26b5dadf | 183 | { |
vijaynvr | 0:bc9f26b5dadf | 184 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 185 | //threshold type is uint8_t. no need to check parameter since from 0-255 |
vijaynvr | 0:bc9f26b5dadf | 186 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&threshold); |
vijaynvr | 0:bc9f26b5dadf | 187 | i2c_write_byte(SYSRANGE_THRESH_LOW, threshold, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 188 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 189 | |
vijaynvr | 0:bc9f26b5dadf | 190 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 191 | } |
vijaynvr | 0:bc9f26b5dadf | 192 | |
vijaynvr | 0:bc9f26b5dadf | 193 | uint8_t range_get_low_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 194 | { |
vijaynvr | 0:bc9f26b5dadf | 195 | uint8_t ret=0; |
vijaynvr | 0:bc9f26b5dadf | 196 | |
vijaynvr | 0:bc9f26b5dadf | 197 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 198 | ret = i2c_read_byte(SYSRANGE_THRESH_LOW, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 199 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 200 | |
vijaynvr | 0:bc9f26b5dadf | 201 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 202 | } |
vijaynvr | 0:bc9f26b5dadf | 203 | |
vijaynvr | 0:bc9f26b5dadf | 204 | sensor_error range_set_interMeasurement_period(uint8_t device_base_address, uint16_t intermeasurement_period) |
vijaynvr | 0:bc9f26b5dadf | 205 | { |
vijaynvr | 0:bc9f26b5dadf | 206 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 207 | |
vijaynvr | 0:bc9f26b5dadf | 208 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&intermeasurement_period); |
vijaynvr | 0:bc9f26b5dadf | 209 | if (intermeasurement_period > 255*10) |
vijaynvr | 0:bc9f26b5dadf | 210 | { |
vijaynvr | 0:bc9f26b5dadf | 211 | // Clipping value to max of 2.55s |
vijaynvr | 0:bc9f26b5dadf | 212 | intermeasurement_period = 255*10; |
vijaynvr | 0:bc9f26b5dadf | 213 | } |
vijaynvr | 0:bc9f26b5dadf | 214 | i2c_write_byte(SYSRANGE_INTERMEASUREMENT_PERIOD, (uint8_t)(intermeasurement_period/10), device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 215 | |
vijaynvr | 0:bc9f26b5dadf | 216 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 217 | |
vijaynvr | 0:bc9f26b5dadf | 218 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 219 | } |
vijaynvr | 0:bc9f26b5dadf | 220 | |
vijaynvr | 0:bc9f26b5dadf | 221 | uint16_t range_get_interMeasurement_period(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 222 | { |
vijaynvr | 0:bc9f26b5dadf | 223 | uint8_t ret = 0; |
vijaynvr | 0:bc9f26b5dadf | 224 | |
vijaynvr | 0:bc9f26b5dadf | 225 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 226 | ret = i2c_read_byte(SYSRANGE_INTERMEASUREMENT_PERIOD, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 227 | ret *=10; //return in ms |
vijaynvr | 0:bc9f26b5dadf | 228 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 229 | |
vijaynvr | 0:bc9f26b5dadf | 230 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 231 | } |
vijaynvr | 0:bc9f26b5dadf | 232 | |
vijaynvr | 0:bc9f26b5dadf | 233 | sensor_error range_set_max_convergence_time(uint8_t device_base_address, int32_t max_convergence_time) |
vijaynvr | 0:bc9f26b5dadf | 234 | { |
vijaynvr | 0:bc9f26b5dadf | 235 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 236 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&max_convergence_time); |
vijaynvr | 0:bc9f26b5dadf | 237 | i2c_write_byte(SYSRANGE_MAX_CONVERGENCE_TIME, max_convergence_time, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 238 | range_set_early_convergence_estimate_threshold(device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 239 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 240 | |
vijaynvr | 0:bc9f26b5dadf | 241 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 242 | } |
vijaynvr | 0:bc9f26b5dadf | 243 | |
vijaynvr | 0:bc9f26b5dadf | 244 | uint8_t range_get_max_convergence_time(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 245 | { |
vijaynvr | 0:bc9f26b5dadf | 246 | return i2c_read_byte(SYSRANGE_MAX_CONVERGENCE_TIME, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 247 | } |
vijaynvr | 0:bc9f26b5dadf | 248 | |
vijaynvr | 0:bc9f26b5dadf | 249 | sensor_error range_set_crosstalk_compensation_rate(uint8_t device_base_address, int32_t crosstalk_compensation_rate) |
vijaynvr | 0:bc9f26b5dadf | 250 | { |
vijaynvr | 0:bc9f26b5dadf | 251 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 252 | |
vijaynvr | 0:bc9f26b5dadf | 253 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_compensation_rate); |
vijaynvr | 0:bc9f26b5dadf | 254 | i2c_write_word(SYSRANGE_CROSSTALK_COMPENSATION_RATE, crosstalk_compensation_rate, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 255 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 256 | |
vijaynvr | 0:bc9f26b5dadf | 257 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 258 | } |
vijaynvr | 0:bc9f26b5dadf | 259 | |
vijaynvr | 0:bc9f26b5dadf | 260 | int32_t range_get_crosstalk_compensation_rate(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 261 | { |
vijaynvr | 0:bc9f26b5dadf | 262 | uint32_t ret=0; |
vijaynvr | 0:bc9f26b5dadf | 263 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 264 | ret = i2c_read_word(SYSRANGE_CROSSTALK_COMPENSATION_RATE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 265 | LOG_FUNCTION_END(NULL); |
vijaynvr | 0:bc9f26b5dadf | 266 | |
vijaynvr | 0:bc9f26b5dadf | 267 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 268 | } |
vijaynvr | 0:bc9f26b5dadf | 269 | |
vijaynvr | 0:bc9f26b5dadf | 270 | sensor_error range_set_crosstalk_compensation_range(uint8_t device_base_address, int32_t crosstalk_compensation_range) |
vijaynvr | 0:bc9f26b5dadf | 271 | { |
vijaynvr | 0:bc9f26b5dadf | 272 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 273 | |
vijaynvr | 0:bc9f26b5dadf | 274 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_compensation_range); |
vijaynvr | 0:bc9f26b5dadf | 275 | i2c_write_byte(SYSRANGE_CROSSTALK_COMPENSATION_RANGE, crosstalk_compensation_range, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 276 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 277 | |
vijaynvr | 0:bc9f26b5dadf | 278 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 279 | } |
vijaynvr | 0:bc9f26b5dadf | 280 | |
vijaynvr | 0:bc9f26b5dadf | 281 | uint8_t range_get_crosstalk_compensation_range(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 282 | { |
vijaynvr | 0:bc9f26b5dadf | 283 | uint8_t ret = 0; |
vijaynvr | 0:bc9f26b5dadf | 284 | |
vijaynvr | 0:bc9f26b5dadf | 285 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 286 | ret = i2c_read_byte(SYSRANGE_CROSSTALK_COMPENSATION_RANGE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 287 | LOG_FUNCTION_END(NULL); |
vijaynvr | 0:bc9f26b5dadf | 288 | |
vijaynvr | 0:bc9f26b5dadf | 289 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 290 | } |
vijaynvr | 0:bc9f26b5dadf | 291 | |
vijaynvr | 0:bc9f26b5dadf | 292 | sensor_error range_set_crosstalk_valid_height(uint8_t device_base_address, int32_t crosstalk_valid_height) |
vijaynvr | 0:bc9f26b5dadf | 293 | { |
vijaynvr | 0:bc9f26b5dadf | 294 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 295 | |
vijaynvr | 0:bc9f26b5dadf | 296 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&crosstalk_valid_height); |
vijaynvr | 0:bc9f26b5dadf | 297 | i2c_write_byte(SYSRANGE_CROSSTALK_VALID_HEIGHT, crosstalk_valid_height, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 298 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 299 | |
vijaynvr | 0:bc9f26b5dadf | 300 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 301 | } |
vijaynvr | 0:bc9f26b5dadf | 302 | |
vijaynvr | 0:bc9f26b5dadf | 303 | uint8_t range_get_crosstalk_valid_height(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 304 | { |
vijaynvr | 0:bc9f26b5dadf | 305 | uint8_t ret; |
vijaynvr | 0:bc9f26b5dadf | 306 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 307 | ret = i2c_read_byte(SYSRANGE_CROSSTALK_VALID_HEIGHT, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 308 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 309 | |
vijaynvr | 0:bc9f26b5dadf | 310 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 311 | } |
vijaynvr | 0:bc9f26b5dadf | 312 | |
vijaynvr | 0:bc9f26b5dadf | 313 | sensor_error range_set_early_convergence_estimate_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 314 | { |
vijaynvr | 0:bc9f26b5dadf | 315 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 316 | |
vijaynvr | 0:bc9f26b5dadf | 317 | uint32_t cMicroSecPerMilliSec = 1000; |
vijaynvr | 0:bc9f26b5dadf | 318 | uint32_t cEceSampleTime_us = 500; |
vijaynvr | 0:bc9f26b5dadf | 319 | uint32_t maxConv_ms = (uint32_t)range_get_max_convergence_time(device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 320 | uint32_t convergTime_us = maxConv_ms * cMicroSecPerMilliSec - range_get_vernier_ave_total_time(device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 321 | uint32_t fineThresh = range_get_converg_ctrl_rtn_thresh_fine(device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 322 | uint32_t eceThresh = ece_factor_m * cEceSampleTime_us * fineThresh/(convergTime_us * ece_factor_d); |
vijaynvr | 0:bc9f26b5dadf | 323 | LOG_FUNCTION_START((void*)&device_base_address,); |
vijaynvr | 0:bc9f26b5dadf | 324 | |
vijaynvr | 0:bc9f26b5dadf | 325 | i2c_write_word(SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, (uint16_t)eceThresh, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 326 | |
vijaynvr | 0:bc9f26b5dadf | 327 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 328 | |
vijaynvr | 0:bc9f26b5dadf | 329 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 330 | } |
vijaynvr | 0:bc9f26b5dadf | 331 | |
vijaynvr | 0:bc9f26b5dadf | 332 | uint8_t range_get_early_convergence_estimate_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 333 | { |
vijaynvr | 0:bc9f26b5dadf | 334 | uint8_t ret; |
vijaynvr | 0:bc9f26b5dadf | 335 | |
vijaynvr | 0:bc9f26b5dadf | 336 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 337 | |
vijaynvr | 0:bc9f26b5dadf | 338 | ret = i2c_read_word(SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 339 | |
vijaynvr | 0:bc9f26b5dadf | 340 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 341 | |
vijaynvr | 0:bc9f26b5dadf | 342 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 343 | } |
vijaynvr | 0:bc9f26b5dadf | 344 | |
vijaynvr | 0:bc9f26b5dadf | 345 | uint32_t range_get_vernier_ave_total_time(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 346 | { |
vijaynvr | 0:bc9f26b5dadf | 347 | uint32_t cFwOverhead_us = 24; |
vijaynvr | 0:bc9f26b5dadf | 348 | uint32_t cVernVcpSetupTime_us = 70; |
vijaynvr | 0:bc9f26b5dadf | 349 | uint32_t cPLL2_StartupDelay_us = 200; |
vijaynvr | 0:bc9f26b5dadf | 350 | uint8_t cVernMeasMask = 0x07; |
vijaynvr | 0:bc9f26b5dadf | 351 | uint32_t vernSamples; |
vijaynvr | 0:bc9f26b5dadf | 352 | uint32_t vernSamplePeriod; |
vijaynvr | 0:bc9f26b5dadf | 353 | uint32_t singleVernTime_us; |
vijaynvr | 0:bc9f26b5dadf | 354 | uint32_t totalVernAveTime_us; |
vijaynvr | 0:bc9f26b5dadf | 355 | |
vijaynvr | 0:bc9f26b5dadf | 356 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 357 | |
vijaynvr | 0:bc9f26b5dadf | 358 | // Calculate Vernier average time |
vijaynvr | 0:bc9f26b5dadf | 359 | vernSamples = (uint32_t)(i2c_read_byte(VERNIER_MEASUREMENTS, device_base_address) & cVernMeasMask); |
vijaynvr | 0:bc9f26b5dadf | 360 | vernSamplePeriod = (uint32_t)i2c_read_byte(VERNIER_RIPPLE_AVE_SAMPLE_PERIOD, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 361 | singleVernTime_us = cFwOverhead_us + cVernVcpSetupTime_us + (vernSamplePeriod * 10); |
vijaynvr | 0:bc9f26b5dadf | 362 | totalVernAveTime_us = (vernSamples + 1) * singleVernTime_us + cPLL2_StartupDelay_us; |
vijaynvr | 0:bc9f26b5dadf | 363 | |
vijaynvr | 0:bc9f26b5dadf | 364 | LOG_FUNCTION_END(totalVernAveTime_us); |
vijaynvr | 0:bc9f26b5dadf | 365 | |
vijaynvr | 0:bc9f26b5dadf | 366 | return totalVernAveTime_us; |
vijaynvr | 0:bc9f26b5dadf | 367 | } |
vijaynvr | 0:bc9f26b5dadf | 368 | |
vijaynvr | 0:bc9f26b5dadf | 369 | uint32_t range_get_converg_ctrl_rtn_thresh_fine(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 370 | { |
vijaynvr | 0:bc9f26b5dadf | 371 | //uint32_t cBitMask = 0x00FFFFFFF; |
vijaynvr | 0:bc9f26b5dadf | 372 | uint32_t cFineThreshLsb = 256; |
vijaynvr | 0:bc9f26b5dadf | 373 | uint32_t rawValue; |
vijaynvr | 0:bc9f26b5dadf | 374 | uint32_t realValue; |
vijaynvr | 0:bc9f26b5dadf | 375 | |
vijaynvr | 0:bc9f26b5dadf | 376 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 377 | |
vijaynvr | 0:bc9f26b5dadf | 378 | rawValue = i2c_read_uint32(USER_CONV_CTRL_RETURN_THRESHOLD_FINE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 379 | realValue = rawValue * cFineThreshLsb; |
vijaynvr | 0:bc9f26b5dadf | 380 | |
vijaynvr | 0:bc9f26b5dadf | 381 | LOG_FUNCTION_END(realValue); |
vijaynvr | 0:bc9f26b5dadf | 382 | |
vijaynvr | 0:bc9f26b5dadf | 383 | return realValue; |
vijaynvr | 0:bc9f26b5dadf | 384 | } |
vijaynvr | 0:bc9f26b5dadf | 385 | |
vijaynvr | 0:bc9f26b5dadf | 386 | sensor_error range_set_ignore_valid_height(uint8_t device_base_address, int32_t ignore_valid_height) |
vijaynvr | 0:bc9f26b5dadf | 387 | { |
vijaynvr | 0:bc9f26b5dadf | 388 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 389 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&ignore_valid_height); |
vijaynvr | 0:bc9f26b5dadf | 390 | |
vijaynvr | 0:bc9f26b5dadf | 391 | i2c_write_byte(SYSRANGE_RANGE_IGNORE_VALID_HEIGHT, ignore_valid_height, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 392 | |
vijaynvr | 0:bc9f26b5dadf | 393 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 394 | |
vijaynvr | 0:bc9f26b5dadf | 395 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 396 | } |
vijaynvr | 0:bc9f26b5dadf | 397 | |
vijaynvr | 0:bc9f26b5dadf | 398 | uint8_t range_get_ignore_valid_height(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 399 | { |
vijaynvr | 0:bc9f26b5dadf | 400 | uint8_t ret; |
vijaynvr | 0:bc9f26b5dadf | 401 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 402 | |
vijaynvr | 0:bc9f26b5dadf | 403 | ret = i2c_read_byte(SYSRANGE_RANGE_IGNORE_VALID_HEIGHT, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 404 | |
vijaynvr | 0:bc9f26b5dadf | 405 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 406 | |
vijaynvr | 0:bc9f26b5dadf | 407 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 408 | } |
vijaynvr | 0:bc9f26b5dadf | 409 | |
vijaynvr | 0:bc9f26b5dadf | 410 | sensor_error range_set_range_ignore_threshold(uint8_t device_base_address, uint32_t range_ignore_threshold) |
vijaynvr | 0:bc9f26b5dadf | 411 | { |
vijaynvr | 0:bc9f26b5dadf | 412 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 413 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&range_ignore_threshold); |
vijaynvr | 0:bc9f26b5dadf | 414 | i2c_write_word(SYSRANGE_RANGE_IGNORE_THRESHOLD, |
vijaynvr | 0:bc9f26b5dadf | 415 | (uint16_t)range_ignore_threshold, |
vijaynvr | 0:bc9f26b5dadf | 416 | device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 417 | |
vijaynvr | 0:bc9f26b5dadf | 418 | |
vijaynvr | 0:bc9f26b5dadf | 419 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 420 | |
vijaynvr | 0:bc9f26b5dadf | 421 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 422 | } |
vijaynvr | 0:bc9f26b5dadf | 423 | |
vijaynvr | 0:bc9f26b5dadf | 424 | uint32_t range_get_range_ignore_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 425 | { |
vijaynvr | 0:bc9f26b5dadf | 426 | uint16_t ignoreThreshRegVal = i2c_read_word(SYSRANGE_RANGE_IGNORE_THRESHOLD, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 427 | return ignoreThreshRegVal; |
vijaynvr | 0:bc9f26b5dadf | 428 | } |
vijaynvr | 0:bc9f26b5dadf | 429 | |
vijaynvr | 0:bc9f26b5dadf | 430 | sensor_error range_set_emitter_block_threshold(uint8_t device_base_address, int32_t emitter_block_threshold) |
vijaynvr | 0:bc9f26b5dadf | 431 | { |
vijaynvr | 0:bc9f26b5dadf | 432 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 433 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&emitter_block_threshold); |
vijaynvr | 0:bc9f26b5dadf | 434 | i2c_write_word(SYSRANGE_EMITTER_BLOCK_THRESHOLD, emitter_block_threshold, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 435 | |
vijaynvr | 0:bc9f26b5dadf | 436 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 437 | |
vijaynvr | 0:bc9f26b5dadf | 438 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 439 | } |
vijaynvr | 0:bc9f26b5dadf | 440 | |
vijaynvr | 0:bc9f26b5dadf | 441 | uint8_t range_get_emitter_block_threshold(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 442 | { |
vijaynvr | 0:bc9f26b5dadf | 443 | return i2c_read_word(SYSRANGE_EMITTER_BLOCK_THRESHOLD, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 444 | } |
vijaynvr | 0:bc9f26b5dadf | 445 | #ifndef __KERNEL__ |
vijaynvr | 0:bc9f26b5dadf | 446 | sensor_error range_set_snr_thresh(uint8_t device_base_address, float_t snrThresh) |
vijaynvr | 0:bc9f26b5dadf | 447 | { |
vijaynvr | 0:bc9f26b5dadf | 448 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 449 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&snrThresh); |
vijaynvr | 0:bc9f26b5dadf | 450 | |
vijaynvr | 0:bc9f26b5dadf | 451 | const float cRegFractionLsb = 0.0625f; |
vijaynvr | 0:bc9f26b5dadf | 452 | const float cRegWholeLsb = 1.0f; |
vijaynvr | 0:bc9f26b5dadf | 453 | const float cReciprocalMax = 16.0f - cRegFractionLsb; |
vijaynvr | 0:bc9f26b5dadf | 454 | const float cSnrMin = 1.0f/cReciprocalMax; |
vijaynvr | 0:bc9f26b5dadf | 455 | |
vijaynvr | 0:bc9f26b5dadf | 456 | uint8_t snrThresRegVal; |
vijaynvr | 0:bc9f26b5dadf | 457 | |
vijaynvr | 0:bc9f26b5dadf | 458 | // Taking the reciprocal into account the allowable SNR range is 0.0625...16.0. |
vijaynvr | 0:bc9f26b5dadf | 459 | if(snrThresh < cSnrMin) |
vijaynvr | 0:bc9f26b5dadf | 460 | { |
vijaynvr | 0:bc9f26b5dadf | 461 | snrThresh = cSnrMin; |
vijaynvr | 0:bc9f26b5dadf | 462 | } |
vijaynvr | 0:bc9f26b5dadf | 463 | |
vijaynvr | 0:bc9f26b5dadf | 464 | float snrthreshMult = 1.0f/snrThresh; |
vijaynvr | 0:bc9f26b5dadf | 465 | snrThresRegVal = (uint8_t)encodeTo4_4_Format(snrthreshMult); |
vijaynvr | 0:bc9f26b5dadf | 466 | i2c_write_byte(SYSRANGE_MAX_AMBIENT_LEVEL_MULT, (uint8_t)snrThresRegVal, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 467 | |
vijaynvr | 0:bc9f26b5dadf | 468 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 469 | |
vijaynvr | 0:bc9f26b5dadf | 470 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 471 | } |
vijaynvr | 0:bc9f26b5dadf | 472 | |
vijaynvr | 0:bc9f26b5dadf | 473 | |
vijaynvr | 0:bc9f26b5dadf | 474 | float range_get_snr_thresh(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 475 | { |
vijaynvr | 0:bc9f26b5dadf | 476 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 477 | |
vijaynvr | 0:bc9f26b5dadf | 478 | uint8_t snrthreshMult = i2c_read_byte(SYSRANGE_MAX_AMBIENT_LEVEL_MULT, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 479 | float snrThresh = 1.0f/decodeFrom4_4_Format((uint32_t)snrthreshMult); |
vijaynvr | 0:bc9f26b5dadf | 480 | |
vijaynvr | 0:bc9f26b5dadf | 481 | LOG_FUNCTION_END(snrThresh); |
vijaynvr | 0:bc9f26b5dadf | 482 | |
vijaynvr | 0:bc9f26b5dadf | 483 | return snrThresh; |
vijaynvr | 0:bc9f26b5dadf | 484 | } |
vijaynvr | 0:bc9f26b5dadf | 485 | #endif |
vijaynvr | 0:bc9f26b5dadf | 486 | sensor_error range_set_range_check_enables(uint8_t device_base_address, int32_t range_check_enables) |
vijaynvr | 0:bc9f26b5dadf | 487 | { |
vijaynvr | 0:bc9f26b5dadf | 488 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 489 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&range_check_enables); |
vijaynvr | 0:bc9f26b5dadf | 490 | |
vijaynvr | 0:bc9f26b5dadf | 491 | i2c_write_byte(SYSRANGE_RANGE_CHECK_ENABLES, range_check_enables, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 492 | |
vijaynvr | 0:bc9f26b5dadf | 493 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 494 | |
vijaynvr | 0:bc9f26b5dadf | 495 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 496 | } |
vijaynvr | 0:bc9f26b5dadf | 497 | |
vijaynvr | 0:bc9f26b5dadf | 498 | uint8_t range_get_range_check_enables(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 499 | { |
vijaynvr | 0:bc9f26b5dadf | 500 | return i2c_read_byte(SYSRANGE_RANGE_CHECK_ENABLES, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 501 | } |
vijaynvr | 0:bc9f26b5dadf | 502 | |
vijaynvr | 0:bc9f26b5dadf | 503 | sensor_error range_set_vhv_recalibrate(uint8_t device_base_address, int32_t VHV_Recalibrate) |
vijaynvr | 0:bc9f26b5dadf | 504 | { |
vijaynvr | 0:bc9f26b5dadf | 505 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 506 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&VHV_Recalibrate); |
vijaynvr | 0:bc9f26b5dadf | 507 | |
vijaynvr | 0:bc9f26b5dadf | 508 | i2c_write_byte(SYSRANGE_VHV_RECALIBRATE, VHV_Recalibrate, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 509 | |
vijaynvr | 0:bc9f26b5dadf | 510 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 511 | |
vijaynvr | 0:bc9f26b5dadf | 512 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 513 | } |
vijaynvr | 0:bc9f26b5dadf | 514 | |
vijaynvr | 0:bc9f26b5dadf | 515 | uint8_t range_get_vhv_recalibrate(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 516 | { |
vijaynvr | 0:bc9f26b5dadf | 517 | return i2c_read_byte(SYSRANGE_VHV_RECALIBRATE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 518 | } |
vijaynvr | 0:bc9f26b5dadf | 519 | |
vijaynvr | 0:bc9f26b5dadf | 520 | sensor_error range_set_vhv_repeat_rate(uint8_t device_base_address, int32_t VHV_repeat_rate) |
vijaynvr | 0:bc9f26b5dadf | 521 | { |
vijaynvr | 0:bc9f26b5dadf | 522 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 523 | LOG_FUNCTION_START((void*)&device_base_address, (void*)&VHV_repeat_rate); |
vijaynvr | 0:bc9f26b5dadf | 524 | |
vijaynvr | 0:bc9f26b5dadf | 525 | i2c_write_byte(SYSRANGE_VHV_REPEAT_RATE, VHV_repeat_rate, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 526 | |
vijaynvr | 0:bc9f26b5dadf | 527 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 528 | |
vijaynvr | 0:bc9f26b5dadf | 529 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 530 | } |
vijaynvr | 0:bc9f26b5dadf | 531 | |
vijaynvr | 0:bc9f26b5dadf | 532 | uint8_t range_get_vhv_repeat_rate(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 533 | { |
vijaynvr | 0:bc9f26b5dadf | 534 | return i2c_read_byte(SYSRANGE_VHV_REPEAT_RATE, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 535 | } |
vijaynvr | 0:bc9f26b5dadf | 536 | |
vijaynvr | 0:bc9f26b5dadf | 537 | int32_t range_get_result_status(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 538 | { |
vijaynvr | 0:bc9f26b5dadf | 539 | return i2c_read_byte(RESULT_RANGE_STATUS, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 540 | } |
vijaynvr | 0:bc9f26b5dadf | 541 | |
vijaynvr | 0:bc9f26b5dadf | 542 | bool_t range_get_device_ready(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 543 | { |
vijaynvr | 0:bc9f26b5dadf | 544 | if (i2c_read_byte(RESULT_RANGE_STATUS, device_base_address) & RANGE_DEVICE_READY) |
vijaynvr | 0:bc9f26b5dadf | 545 | { |
vijaynvr | 0:bc9f26b5dadf | 546 | return TRUE; |
vijaynvr | 0:bc9f26b5dadf | 547 | } |
vijaynvr | 0:bc9f26b5dadf | 548 | return FALSE; |
vijaynvr | 0:bc9f26b5dadf | 549 | } |
vijaynvr | 0:bc9f26b5dadf | 550 | |
vijaynvr | 0:bc9f26b5dadf | 551 | uint8_t range_get_result_error_codes(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 552 | { |
vijaynvr | 0:bc9f26b5dadf | 553 | return (i2c_read_byte(RESULT_RANGE_STATUS, device_base_address) & RANGE_ERROR_CODE) >> 4; |
vijaynvr | 0:bc9f26b5dadf | 554 | } |
vijaynvr | 0:bc9f26b5dadf | 555 | |
vijaynvr | 0:bc9f26b5dadf | 556 | sensor_error range_set_ece_factor(uint8_t device_base_address, uint32_t ECE_Factor_M, uint32_t ECE_Factor_D) |
vijaynvr | 0:bc9f26b5dadf | 557 | { |
vijaynvr | 0:bc9f26b5dadf | 558 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 559 | |
vijaynvr | 0:bc9f26b5dadf | 560 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&ECE_Factor_M, (void*)&ECE_Factor_D); |
vijaynvr | 0:bc9f26b5dadf | 561 | |
vijaynvr | 0:bc9f26b5dadf | 562 | ece_factor_m = ECE_Factor_M; |
vijaynvr | 0:bc9f26b5dadf | 563 | ece_factor_d = ECE_Factor_D; |
vijaynvr | 0:bc9f26b5dadf | 564 | range_set_early_convergence_estimate_threshold(device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 565 | |
vijaynvr | 0:bc9f26b5dadf | 566 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 567 | |
vijaynvr | 0:bc9f26b5dadf | 568 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 569 | } |
vijaynvr | 0:bc9f26b5dadf | 570 | |
vijaynvr | 0:bc9f26b5dadf | 571 | sensor_error range_get_ece_factor(uint32_t *pece_factor_m, uint32_t *pece_factor_d) |
vijaynvr | 0:bc9f26b5dadf | 572 | { |
vijaynvr | 0:bc9f26b5dadf | 573 | *pece_factor_m = ece_factor_m; |
vijaynvr | 0:bc9f26b5dadf | 574 | *pece_factor_d = ece_factor_d; |
vijaynvr | 0:bc9f26b5dadf | 575 | |
vijaynvr | 0:bc9f26b5dadf | 576 | return SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 577 | } |
vijaynvr | 0:bc9f26b5dadf | 578 | |
vijaynvr | 0:bc9f26b5dadf | 579 | sensor_error range_set_system_interrupt_config_gpio(uint8_t device_base_address, uint8_t Ranging_GPIO_interrupt_config) |
vijaynvr | 0:bc9f26b5dadf | 580 | { |
vijaynvr | 0:bc9f26b5dadf | 581 | uint8_t reg; |
vijaynvr | 0:bc9f26b5dadf | 582 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 583 | |
vijaynvr | 0:bc9f26b5dadf | 584 | LOG_FUNCTION_START((void*)&device_base_address,(void*)&Ranging_GPIO_interrupt_config); |
vijaynvr | 0:bc9f26b5dadf | 585 | switch (Ranging_GPIO_interrupt_config) |
vijaynvr | 0:bc9f26b5dadf | 586 | { |
vijaynvr | 0:bc9f26b5dadf | 587 | case CONFIG_GPIO_INTERRUPT_DISABLED: |
vijaynvr | 0:bc9f26b5dadf | 588 | case CONFIG_GPIO_INTERRUPT_LEVEL_LOW: |
vijaynvr | 0:bc9f26b5dadf | 589 | case CONFIG_GPIO_INTERRUPT_LEVEL_HIGH: |
vijaynvr | 0:bc9f26b5dadf | 590 | case CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW: |
vijaynvr | 0:bc9f26b5dadf | 591 | case CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY: |
vijaynvr | 0:bc9f26b5dadf | 592 | reg = i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 593 | reg &= 0x38; // preserve only the ALS part of reg |
vijaynvr | 0:bc9f26b5dadf | 594 | i2c_write_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, |
vijaynvr | 0:bc9f26b5dadf | 595 | (reg | Ranging_GPIO_interrupt_config), |
vijaynvr | 0:bc9f26b5dadf | 596 | device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 597 | break; |
vijaynvr | 0:bc9f26b5dadf | 598 | default : |
vijaynvr | 0:bc9f26b5dadf | 599 | ret = COMMON_INVALID_PARAMS; |
vijaynvr | 0:bc9f26b5dadf | 600 | break; |
vijaynvr | 0:bc9f26b5dadf | 601 | } |
vijaynvr | 0:bc9f26b5dadf | 602 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 603 | |
vijaynvr | 0:bc9f26b5dadf | 604 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 605 | } |
vijaynvr | 0:bc9f26b5dadf | 606 | |
vijaynvr | 0:bc9f26b5dadf | 607 | uint8_t range_get_system_interrupt_config_gpio(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 608 | { |
vijaynvr | 0:bc9f26b5dadf | 609 | uint8_t ret=0; |
vijaynvr | 0:bc9f26b5dadf | 610 | |
vijaynvr | 0:bc9f26b5dadf | 611 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 612 | // expose only bits [2:0] |
vijaynvr | 0:bc9f26b5dadf | 613 | |
vijaynvr | 0:bc9f26b5dadf | 614 | ret = (i2c_read_byte(SYSTEM_INTERRUPT_CONFIG_GPIO, device_base_address) & 0x07); |
vijaynvr | 0:bc9f26b5dadf | 615 | |
vijaynvr | 0:bc9f26b5dadf | 616 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 617 | |
vijaynvr | 0:bc9f26b5dadf | 618 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 619 | } |
vijaynvr | 0:bc9f26b5dadf | 620 | |
vijaynvr | 0:bc9f26b5dadf | 621 | sensor_error range_set_system_interrupt_clear(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 622 | { |
vijaynvr | 0:bc9f26b5dadf | 623 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 624 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 625 | |
vijaynvr | 0:bc9f26b5dadf | 626 | i2c_write_byte(SYSTEM_INTERRUPT_CLEAR, INTERRUPT_CLEAR_RANGING, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 627 | |
vijaynvr | 0:bc9f26b5dadf | 628 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 629 | |
vijaynvr | 0:bc9f26b5dadf | 630 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 631 | } |
vijaynvr | 0:bc9f26b5dadf | 632 | |
vijaynvr | 0:bc9f26b5dadf | 633 | uint8_t range_get_result_interrupt_status_gpio(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 634 | { |
vijaynvr | 0:bc9f26b5dadf | 635 | uint8_t ret = 0; |
vijaynvr | 0:bc9f26b5dadf | 636 | |
vijaynvr | 0:bc9f26b5dadf | 637 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 638 | |
vijaynvr | 0:bc9f26b5dadf | 639 | ret = (i2c_read_byte(RESULT_INTERRUPT_STATUS_GPIO, device_base_address) & 0x07); |
vijaynvr | 0:bc9f26b5dadf | 640 | |
vijaynvr | 0:bc9f26b5dadf | 641 | LOG_FUNCTION_END(ret); |
vijaynvr | 0:bc9f26b5dadf | 642 | |
vijaynvr | 0:bc9f26b5dadf | 643 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 644 | } |
vijaynvr | 0:bc9f26b5dadf | 645 | |
vijaynvr | 0:bc9f26b5dadf | 646 | sensor_error common_set_history_buffer_range_mode_enable(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 647 | { |
vijaynvr | 0:bc9f26b5dadf | 648 | uint8_t mode; |
vijaynvr | 0:bc9f26b5dadf | 649 | sensor_error ret = SENSOR_ERROR_NONE; |
vijaynvr | 0:bc9f26b5dadf | 650 | |
vijaynvr | 0:bc9f26b5dadf | 651 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 652 | |
vijaynvr | 0:bc9f26b5dadf | 653 | mode = i2c_read_byte(SYSTEM_HISTORY_CTRL, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 654 | mode = mode & 0xFD; // clear bit 1 to set to range mode |
vijaynvr | 0:bc9f26b5dadf | 655 | mode = mode | 0x01; // set bit 0 to enable history buffer |
vijaynvr | 0:bc9f26b5dadf | 656 | i2c_write_byte(SYSTEM_HISTORY_CTRL, mode, device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 657 | |
vijaynvr | 0:bc9f26b5dadf | 658 | LOG_FUNCTION_END(NULL); |
vijaynvr | 0:bc9f26b5dadf | 659 | |
vijaynvr | 0:bc9f26b5dadf | 660 | return ret; |
vijaynvr | 0:bc9f26b5dadf | 661 | } |
vijaynvr | 0:bc9f26b5dadf | 662 | |
vijaynvr | 0:bc9f26b5dadf | 663 | |
vijaynvr | 0:bc9f26b5dadf | 664 | uint32_t range_get_upper_limit(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 665 | { |
vijaynvr | 0:bc9f26b5dadf | 666 | const uint32_t cScalerMax1X = 255; |
vijaynvr | 0:bc9f26b5dadf | 667 | uint32_t upperLim = cScalerMax1X; |
vijaynvr | 0:bc9f26b5dadf | 668 | |
vijaynvr | 0:bc9f26b5dadf | 669 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 670 | |
vijaynvr | 0:bc9f26b5dadf | 671 | LOG_FUNCTION_END(upperLim); |
vijaynvr | 0:bc9f26b5dadf | 672 | |
vijaynvr | 0:bc9f26b5dadf | 673 | return upperLim; |
vijaynvr | 0:bc9f26b5dadf | 674 | } |
vijaynvr | 0:bc9f26b5dadf | 675 | |
vijaynvr | 0:bc9f26b5dadf | 676 | |
vijaynvr | 0:bc9f26b5dadf | 677 | uint32_t range_get_lower_limit(uint8_t device_base_address) |
vijaynvr | 0:bc9f26b5dadf | 678 | { |
vijaynvr | 0:bc9f26b5dadf | 679 | uint32_t lowerLimit = 0; |
vijaynvr | 0:bc9f26b5dadf | 680 | LOG_FUNCTION_START((void*)&device_base_address); |
vijaynvr | 0:bc9f26b5dadf | 681 | |
vijaynvr | 0:bc9f26b5dadf | 682 | LOG_FUNCTION_END(lowerLimit); |
vijaynvr | 0:bc9f26b5dadf | 683 | |
vijaynvr | 0:bc9f26b5dadf | 684 | return lowerLimit; |
vijaynvr | 0:bc9f26b5dadf | 685 | } |
vijaynvr | 0:bc9f26b5dadf | 686 |