Condensed Version of Public VL53L0X
Diff: VL53L0X.cpp
- Revision:
- 10:cd251e0fc2fd
- Parent:
- 8:abea9638127a
- Child:
- 11:c6f95a42d4d7
--- a/VL53L0X.cpp Sun Mar 24 18:38:00 2019 +0000 +++ b/VL53L0X.cpp Sun Mar 24 22:24:16 2019 +0000 @@ -37,10 +37,10 @@ // Some example regex that were used to replace useless macros // \QVL53L0X_SETDEVICESPECIFICPARAMETER(\E([A-Z\d]+)[[:punct:]]([[:space:]]*)([A-Z\d_]+)\Q);\E -// _device->DeviceSpecificParameters.\1 = \3; +// _device->DevSpecParams.\1 = \3; // \QVL53L0X_GETDEVICESPECIFICPARAMETER(\E([A-Z\d]+)\Q);\E -// _device->DeviceSpecificParameters.\1; +// _device->DevSpecParams.\1; // \QVL53L0X_SETPARAMETERFIELD(\E([A-Z\d]+)[[:punct:]]([[:space:]]*)([A-Z\d_]+)\Q);\E // _device->CurrentParameters.\1 = \3; @@ -68,14 +68,14 @@ /* Includes */ #include <stdlib.h> #include "VL53L0X.h" +#include "VL53L0X_tuning.h" #define REF_ARRAY_SPAD_0 0 #define REF_ARRAY_SPAD_5 5 #define REF_ARRAY_SPAD_10 10 uint32_t refArrayQuadrants[4] = {REF_ARRAY_SPAD_10,REF_ARRAY_SPAD_5, - REF_ARRAY_SPAD_0,REF_ARRAY_SPAD_5 }; - + REF_ARRAY_SPAD_0,REF_ARRAY_SPAD_5 }; VL53L0X_Error VL53L0X::VL53L0X_device_read_strobe() { @@ -89,12 +89,8 @@ * use timeout to avoid deadlock*/ if (status == VL53L0X_ERROR_NONE) { loop_nb = 0; - do { - status = VL53L0X_read_byte(0x83,&strobe); - if ((strobe != 0x00) || status != VL53L0X_ERROR_NONE) { - break; - } - + do {status = VL53L0X_read_byte(0x83,&strobe); + if ((strobe != 0x00) || status != VL53L0X_ERROR_NONE) {break;} loop_nb = loop_nb + 1; } while (loop_nb < VL53L0X_DEFAULT_MAX_LOOP); @@ -104,13 +100,11 @@ } status |= VL53L0X_write_byte(0x83,0x01); - return status; } VL53L0X_Error VL53L0X::VL53L0X_get_info_from_device(uint8_t option) -{ - VL53L0X_Error status = VL53L0X_ERROR_NONE; +{ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t byte; uint32_t tmp_dword; uint8_t module_id; @@ -120,7 +114,7 @@ uint32_t part_uid_upper = 0; uint32_t part_uid_lower = 0; uint32_t offset_fixed1104_mm = 0; - int16_t offset_micro_meters = 0; + int16_t offset_um = 0; uint32_t dist_meas_tgt_fixed1104_mm = 400 << 4; uint32_t dist_meas_fixed1104_400_mm = 0; uint32_t signal_rate_meas_fixed1104_400_mm = 0; @@ -130,10 +124,8 @@ FixPoint1616_t signal_rate_meas_fixed400_mm_fix = 0; uint8_t nvm_ref_good_spad_map[VL53L0X_REF_SPAD_BUFFER_SIZE]; int i; - - - read_data_from_device_done = _device->DeviceSpecificParameters.ReadDataFromDeviceDone; - read_data_from_device_done = _device->DeviceSpecificParameters.ReadDataFromDeviceDone; + + read_data_from_device_done = DevSpecParams.ReadDataFromDeviceDone; /* This access is done only once after that a GetDeviceInfo or * datainit is done*/ @@ -142,15 +134,12 @@ status |= VL53L0X_write_byte(0x80,0x01); status |= VL53L0X_write_byte(0xFF,0x01); status |= VL53L0X_write_byte(0x00,0x00); - status |= VL53L0X_write_byte(0xFF,0x06); status |= VL53L0X_read_byte(0x83,&byte); status |= VL53L0X_write_byte(0x83,byte | 4); status |= VL53L0X_write_byte(0xFF,0x07); status |= VL53L0X_write_byte(0x81,0x01); - status |= VL53L0X_polling_delay(); - status |= VL53L0X_write_byte(0x80,0x01); if (((option & 1) == 1) && @@ -206,8 +195,7 @@ status |= VL53L0X_device_read_strobe(); status |= VL53L0X_read_dword(0x90,&tmp_dword); - product_id[4] = (char)(byte + - ((tmp_dword >> 29) & 0x07f)); + product_id[4] = (char)(byte +((tmp_dword >> 29) & 0x07f)); product_id[5] = (char)((tmp_dword >> 22) & 0x07f); product_id[6] = (char)((tmp_dword >> 15) & 0x07f); product_id[7] = (char)((tmp_dword >> 8) & 0x07f); @@ -216,9 +204,7 @@ byte = (uint8_t)((tmp_dword & 0x001) << 6); status |= VL53L0X_write_byte(0x94,0x79); - status |= VL53L0X_device_read_strobe(); - status |= VL53L0X_read_dword(0x90,&tmp_dword); product_id[9] = (char)(byte + @@ -230,9 +216,7 @@ byte = (uint8_t)((tmp_dword & 0x01f) << 2); status |= VL53L0X_write_byte(0x94,0x7A); - status |= VL53L0X_device_read_strobe(); - status |= VL53L0X_read_dword(0x90,&tmp_dword); product_id[13] = (char)(byte + @@ -245,9 +229,8 @@ } - if (((option & 4) == 4) && - ((read_data_from_device_done & 4) == 0)) { - + if (((option & 4) == 4) && ((read_data_from_device_done & 4) == 0)) + { status |= VL53L0X_write_byte(0x94,0x7B); status |= VL53L0X_device_read_strobe(); status |= VL53L0X_read_dword(0x90,&part_uid_upper); @@ -260,8 +243,7 @@ status |= VL53L0X_device_read_strobe(); status |= VL53L0X_read_dword(0x90,&tmp_dword); - signal_rate_meas_fixed1104_400_mm = (tmp_dword & - 0x0000000ff) << 8; + signal_rate_meas_fixed1104_400_mm = (tmp_dword & 0x0000000ff) << 8; status |= VL53L0X_write_byte(0x94,0x74); status |= VL53L0X_device_read_strobe(); @@ -274,15 +256,13 @@ status |= VL53L0X_device_read_strobe(); status |= VL53L0X_read_dword(0x90,&tmp_dword); - dist_meas_fixed1104_400_mm = (tmp_dword & 0x0000000ff) - << 8; + dist_meas_fixed1104_400_mm = (tmp_dword & 0x0000000ff)<< 8; status |= VL53L0X_write_byte(0x94,0x76); status |= VL53L0X_device_read_strobe(); status |= VL53L0X_read_dword(0x90,&tmp_dword); - dist_meas_fixed1104_400_mm |= ((tmp_dword & 0xff000000) - >> 24); + dist_meas_fixed1104_400_mm |= ((tmp_dword & 0xff000000) >> 24); } status |= VL53L0X_write_byte(0x81,0x00); @@ -301,51 +281,51 @@ /* Assign to variable if status is ok */ if (((option & 1) == 1) && ((read_data_from_device_done & 1) == 0)) { - _device->DeviceSpecificParameters.ReferenceSpadCount=reference_spad_count; - _device->DeviceSpecificParameters.ReferenceSpadType =reference_spad_type; + DevSpecParams.ReferenceSpadCount=reference_spad_count; + DevSpecParams.ReferenceSpadType =reference_spad_type; for (i = 0; i < VL53L0X_REF_SPAD_BUFFER_SIZE; i++) { - _device->SpadData.RefGoodSpadMap[i] = + SpadData.RefGoodSpadMap[i] = nvm_ref_good_spad_map[i]; } } if (((option & 2) == 2) && ((read_data_from_device_done & 2) == 0)) { - _device->DeviceSpecificParameters.ModuleId = module_id; - _device->DeviceSpecificParameters.Revision = revision; - product_id_tmp = _device->DeviceSpecificParameters.ProductId; + DevSpecParams.ModuleId = module_id; + DevSpecParams.Revision = revision; + product_id_tmp = DevSpecParams.ProductId; strcpy(product_id_tmp,product_id); } if (((option & 4) == 4) && ((read_data_from_device_done & 4) == 0)) { - _device->DeviceSpecificParameters.PartUIDUpper = part_uid_upper; - _device->DeviceSpecificParameters.PartUIDLower = part_uid_lower; + DevSpecParams.PartUIDUpper = part_uid_upper; + DevSpecParams.PartUIDLower = part_uid_lower; signal_rate_meas_fixed400_mm_fix = VL53L0X_FIXPOINT97TOFIXPOINT1616(signal_rate_meas_fixed1104_400_mm); - _device->DeviceSpecificParameters.SignalRateMeasFixed400mm = signal_rate_meas_fixed400_mm_fix; - _device->DeviceSpecificParameters.SignalRateMeasFixed400mm = signal_rate_meas_fixed400_mm_fix; - - offset_micro_meters = 0; + DevSpecParams.SignalRateMeasFixed400mm = signal_rate_meas_fixed400_mm_fix; + DevSpecParams.SignalRateMeasFixed400mm = signal_rate_meas_fixed400_mm_fix; + + offset_um = 0; if (dist_meas_fixed1104_400_mm != 0) { offset_fixed1104_mm = dist_meas_fixed1104_400_mm - dist_meas_tgt_fixed1104_mm; - offset_micro_meters = (offset_fixed1104_mm * 1000) >> 4; - offset_micro_meters *= -1; + offset_um = (offset_fixed1104_mm * 1000) >> 4; + offset_um *= -1; } - _device->Part2PartOffsetAdjustmentNVMMicroMeter = offset_micro_meters; + Part2PartOffsetAdjustmentNVMMicroMeter = offset_um; } byte = (uint8_t)(read_data_from_device_done | option); - _device->DeviceSpecificParameters.ReadDataFromDeviceDone = byte; + DevSpecParams.ReadDataFromDeviceDone = byte; } return status; } -VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_offset_calibration_data_micro_meter(int32_t *p_offset_calibration_data_micro_meter) +VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_offsetCallDat_um(int32_t *p_offsetCallDat_um) { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint16_t range_offset_register; @@ -362,11 +342,11 @@ /* Apply 12 bit 2's compliment conversion */ if (range_offset_register > c_max_offset) { - *p_offset_calibration_data_micro_meter = + *p_offsetCallDat_um = (int16_t)(range_offset_register - c_offset_range) * 250; } else { - *p_offset_calibration_data_micro_meter = + *p_offsetCallDat_um = (int16_t)range_offset_register * 250; } @@ -375,46 +355,44 @@ return status; } -VL53L0X_Error VL53L0X::VL53L0X_get_offset_calibration_data_micro_meter(int32_t *p_offset_calibration_data_micro_meter) +VL53L0X_Error VL53L0X::VL53L0X_get_offsetCallDat_um(int32_t *p_offsetCallDat_um) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = wrapped_VL53L0X_get_offset_calibration_data_micro_meter(p_offset_calibration_data_micro_meter); + status = wrapped_VL53L0X_get_offsetCallDat_um(p_offsetCallDat_um); return status; } -VL53L0X_Error VL53L0X::wrapped_VL53L0X_set_offset_calibration_data_micro_meter(int32_t offset_calibration_data_micro_meter) +VL53L0X_Error VL53L0X::wrapped_VL53L0X_set_offsetCallDat_um(int32_t offsetCallDat_um) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - int32_t c_max_offset_micro_meter = 511000; - int32_t c_min_offset_micro_meter = -512000; + int32_t c_max_offset_um = 511000; + int32_t c_min_offset_um = -512000; int16_t c_offset_range = 4096; uint32_t encoded_offset_val; - if (offset_calibration_data_micro_meter > c_max_offset_micro_meter) { - offset_calibration_data_micro_meter = c_max_offset_micro_meter; + if (offsetCallDat_um > c_max_offset_um) { + offsetCallDat_um = c_max_offset_um; } else { - if (offset_calibration_data_micro_meter < c_min_offset_micro_meter) { - offset_calibration_data_micro_meter = c_min_offset_micro_meter; + if (offsetCallDat_um < c_min_offset_um) { + offsetCallDat_um = c_min_offset_um; } } /* The offset register is 10.2 format and units are mm - * therefore conversion is applied by a division of - * 250. - */ - if (offset_calibration_data_micro_meter >= 0) { + * therefore conversion is applied by a division of 250. */ + if (offsetCallDat_um >= 0) { encoded_offset_val = - offset_calibration_data_micro_meter / 250; + offsetCallDat_um / 250; } else { encoded_offset_val = c_offset_range + - offset_calibration_data_micro_meter / 250; + offsetCallDat_um / 250; } status = VL53L0X_write_word(VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM, @@ -424,22 +402,19 @@ return status; } -VL53L0X_Error VL53L0X::VL53L0X_set_offset_calibration_data_micro_meter(int32_t offset_calibration_data_micro_meter) -{ - VL53L0X_Error status = VL53L0X_ERROR_NONE; - - - status = wrapped_VL53L0X_set_offset_calibration_data_micro_meter(offset_calibration_data_micro_meter); - - +VL53L0X_Error VL53L0X::VL53L0X_set_offsetCallDat_um(int32_t offsetCallDat_um) +{ VL53L0X_Error status = VL53L0X_ERROR_NONE; + + status = wrapped_VL53L0X_set_offsetCallDat_um(offsetCallDat_um); + return status; } VL53L0X_Error VL53L0X::VL53L0X_apply_offset_adjustment() { VL53L0X_Error status = VL53L0X_ERROR_NONE; - int32_t corrected_offset_micro_meters; - int32_t current_offset_micro_meters; + int32_t corrected_offset_um; + int32_t current_offset_um; /* if we run on this function we can read all the NVM info * used by the API */ @@ -447,23 +422,23 @@ /* Read back current device offset */ if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_get_offset_calibration_data_micro_meter(¤t_offset_micro_meters); + status = VL53L0X_get_offsetCallDat_um(¤t_offset_um); } /* Apply Offset Adjustment derived from 400mm measurements */ if (status == VL53L0X_ERROR_NONE) { /* Store initial device offset */ - _device->Part2PartOffsetNVMMicroMeter = current_offset_micro_meters; - - corrected_offset_micro_meters = current_offset_micro_meters + - (int32_t) _device->Part2PartOffsetAdjustmentNVMMicroMeter; - - status = VL53L0X_set_offset_calibration_data_micro_meter(corrected_offset_micro_meters); + Part2PartOffsetNVMMicroMeter = current_offset_um; + + corrected_offset_um = current_offset_um + + (int32_t) Part2PartOffsetAdjustmentNVMMicroMeter; + + status = VL53L0X_set_offsetCallDat_um(corrected_offset_um); /* store current,adjusted offset */ if (status == VL53L0X_ERROR_NONE) { - _device->CurrentParameters.RangeOffsetMicroMeters = corrected_offset_micro_meters; + CurrentParameters.RangeOffsetMicroMeters = corrected_offset_um; } } @@ -474,16 +449,16 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - *p_device_mode = _device->CurrentParameters.DeviceMode; + *p_device_mode = CurrentParameters.DeviceMode; return status; } -VL53L0X_Error VL53L0X::VL53L0X_get_inter_measurement_period_milli_seconds(uint32_t *p_inter_measurement_period_milli_seconds) +VL53L0X_Error VL53L0X::VL53L0X_get_inter_measurement_period_ms(uint32_t *p_inter_measurement_period_ms) { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint16_t osc_calibrate_val; - uint32_t im_period_milli_seconds; + uint32_t im_period_ms; @@ -492,46 +467,42 @@ if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_read_dword(VL53L0X_REG_SYSTEM_INTERMEASUREMENT_PERIOD, - &im_period_milli_seconds); + &im_period_ms); } if (status == VL53L0X_ERROR_NONE) { if (osc_calibrate_val != 0) { - *p_inter_measurement_period_milli_seconds = - im_period_milli_seconds / osc_calibrate_val; + *p_inter_measurement_period_ms = + im_period_ms / osc_calibrate_val; } - _device->CurrentParameters.InterMeasurementPeriodMilliSeconds= - *p_inter_measurement_period_milli_seconds; + CurrentParameters.InterMeasurementPeriod_ms= + *p_inter_measurement_period_ms; } - return status; } -VL53L0X_Error VL53L0X::VL53L0X_get_x_talk_compensation_rate_mega_cps(FixPoint1616_t *p_xtalk_compensation_rate_mega_cps) +VL53L0X_Error VL53L0X::VL53L0X_get_x_talk_compensation_rate_mega_cps( + FixPoint1616_t *p_xtalk_compensation_rate_mega_cps) { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint16_t value; FixPoint1616_t temp_fix1616; - - status = VL53L0X_read_word(VL53L0X_REG_CROSSTALK_COMPENSATION_PEAK_RATE_MCPS,(uint16_t *)&value); if (status == VL53L0X_ERROR_NONE) { if (value == 0) { /* the Xtalk is disabled return value from memory */ - temp_fix1616 = _device->CurrentParameters.XTalkCompensationRateMegaCps; + temp_fix1616 = CurrentParameters.XTalkCompensationRateMegaCps; *p_xtalk_compensation_rate_mega_cps = temp_fix1616; - _device->CurrentParameters.XTalkCompensationEnable = 0; + CurrentParameters.XTalkCompensationEnable = 0; } else { temp_fix1616 = VL53L0X_FIXPOINT313TOFIXPOINT1616(value); *p_xtalk_compensation_rate_mega_cps = temp_fix1616; - _device->CurrentParameters.XTalkCompensationRateMegaCps = temp_fix1616; - _device->CurrentParameters.XTalkCompensationEnable = 1; + CurrentParameters.XTalkCompensationRateMegaCps = temp_fix1616; + CurrentParameters.XTalkCompensationEnable = 1; } } - - return status; } @@ -543,13 +514,11 @@ uint16_t temp16; FixPoint1616_t temp_fix1616; - - switch (limit_check_id) { case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: /* internal computation: */ - temp_fix1616 = _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE]; + temp_fix1616 = CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE]; enable_zero_value = 0; break; @@ -566,13 +535,13 @@ case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: /* internal computation: */ - temp_fix1616 = _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP]; + temp_fix1616 = CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP]; enable_zero_value = 0; break; case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: /* internal computation: */ - temp_fix1616 = _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD]; + temp_fix1616 = CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD]; enable_zero_value = 0; break; @@ -584,13 +553,11 @@ temp_fix1616 = VL53L0X_FIXPOINT97TOFIXPOINT1616(temp16); } - enable_zero_value = 0; break; default: status = VL53L0X_ERROR_INVALID_PARAMS; - } if (status == VL53L0X_ERROR_NONE) { @@ -599,14 +566,14 @@ if (temp_fix1616 == 0) { /* disabled: return value from memory */ - temp_fix1616 = _device->CurrentParameters.LimitChecksValue[limit_check_id]; + temp_fix1616 = CurrentParameters.LimitChecksValue[limit_check_id]; *p_limit_check_value = temp_fix1616; - _device->CurrentParameters.LimitChecksEnable[limit_check_id] = 0; - _device->CurrentParameters.LimitChecksEnable[limit_check_id] = 0; + CurrentParameters.LimitChecksEnable[limit_check_id] = 0; + CurrentParameters.LimitChecksEnable[limit_check_id] = 0; } else { *p_limit_check_value = temp_fix1616; - _device->CurrentParameters.LimitChecksValue[limit_check_id] = temp_fix1616; - _device->CurrentParameters.LimitChecksEnable[limit_check_id] = 1; + CurrentParameters.LimitChecksValue[limit_check_id] = temp_fix1616; + CurrentParameters.LimitChecksEnable[limit_check_id] = 1; } } else { *p_limit_check_value = temp_fix1616; @@ -624,42 +591,31 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t temp8; - - if (limit_check_id >= VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS) { status = VL53L0X_ERROR_INVALID_PARAMS; *p_limit_check_enable = 0; } else { - temp8 = _device->CurrentParameters.LimitChecksEnable[limit_check_id]; + temp8 = CurrentParameters.LimitChecksEnable[limit_check_id]; *p_limit_check_enable = temp8; } - - + return status; } VL53L0X_Error VL53L0X::VL53L0X_get_wrap_around_check_enable(uint8_t *p_wrap_around_check_enable) -{ - VL53L0X_Error status = VL53L0X_ERROR_NONE; +{ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t data; - - status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,&data); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = data; - if (data & (0x01 << 7)) { - *p_wrap_around_check_enable = 0x01; - } else { - *p_wrap_around_check_enable = 0x00; - } + SequenceConfig = data; + if (data & (0x01 << 7)) { *p_wrap_around_check_enable = 0x01; } + else { *p_wrap_around_check_enable = 0x00; } } if (status == VL53L0X_ERROR_NONE) { - _device->CurrentParameters.WrapAroundCheckEnable= - *p_wrap_around_check_enable; - } - - + CurrentParameters.WrapAroundCheckEnable= + *p_wrap_around_check_enable; } + return status; } @@ -669,7 +625,6 @@ VL53L0X_Error Status = VL53L0X_ERROR_NONE; *p_sequence_step_enabled = 0; - switch (sequence_step_id) { case VL53L0X_SEQUENCESTEP_TCC: *p_sequence_step_enabled = (sequence_config & 0x10) >> 4; @@ -689,8 +644,7 @@ default: Status = VL53L0X_ERROR_INVALID_PARAMS; } - - + return Status; } @@ -698,33 +652,25 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t sequence_config = 0; - status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, &sequence_config); if (status == VL53L0X_ERROR_NONE) { status = sequence_step_enabled(VL53L0X_SEQUENCESTEP_TCC,sequence_config, - &p_scheduler_sequence_steps->TccOn); - } + &p_scheduler_sequence_steps->TccOn);} if (status == VL53L0X_ERROR_NONE) { status = sequence_step_enabled(VL53L0X_SEQUENCESTEP_DSS,sequence_config, - &p_scheduler_sequence_steps->DssOn); - } + &p_scheduler_sequence_steps->DssOn);} if (status == VL53L0X_ERROR_NONE) { status = sequence_step_enabled(VL53L0X_SEQUENCESTEP_MSRC,sequence_config, - &p_scheduler_sequence_steps->MsrcOn); - } + &p_scheduler_sequence_steps->MsrcOn);} if (status == VL53L0X_ERROR_NONE) { status = sequence_step_enabled(VL53L0X_SEQUENCESTEP_PRE_RANGE,sequence_config, - &p_scheduler_sequence_steps->PreRangeOn); - } + &p_scheduler_sequence_steps->PreRangeOn);} if (status == VL53L0X_ERROR_NONE) { status = sequence_step_enabled(VL53L0X_SEQUENCESTEP_FINAL_RANGE,sequence_config, - &p_scheduler_sequence_steps->FinalRangeOn); - } - - + &p_scheduler_sequence_steps->FinalRangeOn);} return status; } @@ -736,9 +682,7 @@ */ uint8_t vcsel_period_pclks = 0; - vcsel_period_pclks = (vcsel_period_reg + 1) << 1; - return vcsel_period_pclks; } @@ -748,11 +692,8 @@ * Converts the encoded VCSEL period register value into the real period * in PLL clocks */ - uint8_t vcsel_period_reg = 0; - vcsel_period_reg = (vcsel_period_pclks >> 1) - 1; - return vcsel_period_reg; } @@ -765,10 +706,10 @@ uint8_t max_pre_vcsel_period_pclk = 18; uint8_t min_final_vcsel_period_pclk = 8; uint8_t max_final_vcsel_period_pclk = 14; - uint32_t measurement_timing_budget_micro_seconds; - uint32_t final_range_timeout_micro_seconds; - uint32_t pre_range_timeout_micro_seconds; - uint32_t msrc_timeout_micro_seconds; + uint32_t measurement_timing_budget_us; + uint32_t final_range_timeout_us; + uint32_t pre_range_timeout_us; + uint32_t msrc_timeout_us; uint8_t phase_cal_int = 0; /* Check if valid clock period requested */ @@ -788,11 +729,7 @@ } /* Apply specific settings for the requested clock period */ - - if (status != VL53L0X_ERROR_NONE) { - return status; - } - + if (status != VL53L0X_ERROR_NONE) { return status; } if (vcsel_period_type == VL53L0X_VCSEL_PERIOD_PRE_RANGE) { @@ -849,11 +786,9 @@ status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT,0x09); status |= VL53L0X_write_byte(0xff,0x01); - status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM, - 0x20); + status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM,0x20); status |= VL53L0X_write_byte(0xff,0x00); } else if (vcsel_pulse_period_pclk == 12) { - status = VL53L0X_write_byte(VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x38); status = VL53L0X_write_byte(VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW, @@ -861,10 +796,8 @@ status |= VL53L0X_write_byte(VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH,0x03); status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT,0x08); - status |= VL53L0X_write_byte(0xff,0x01); - status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM, - 0x20); + status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM,0x20); status |= VL53L0X_write_byte(0xff,0x00); } else if (vcsel_pulse_period_pclk == 14) { @@ -875,10 +808,8 @@ status |= VL53L0X_write_byte(VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH,0x03); status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT,0x07); - status |= VL53L0X_write_byte(0xff,0x01); - status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM, - 0x20); + status |= VL53L0X_write_byte(VL53L0X_REG_ALGO_PHASECAL_LIM, 0x20); status |= VL53L0X_write_byte(0xff,0x00); } } @@ -902,11 +833,11 @@ switch (vcsel_period_type) { case VL53L0X_VCSEL_PERIOD_PRE_RANGE: status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_PRE_RANGE, - &pre_range_timeout_micro_seconds); + &pre_range_timeout_us); if (status == VL53L0X_ERROR_NONE) status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_MSRC, - &msrc_timeout_micro_seconds); + &msrc_timeout_us); if (status == VL53L0X_ERROR_NONE) status = VL53L0X_write_byte(VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD, @@ -915,29 +846,28 @@ if (status == VL53L0X_ERROR_NONE) status = set_sequence_step_timeout(VL53L0X_SEQUENCESTEP_PRE_RANGE, - pre_range_timeout_micro_seconds); + pre_range_timeout_us); if (status == VL53L0X_ERROR_NONE) status = set_sequence_step_timeout(VL53L0X_SEQUENCESTEP_MSRC, - msrc_timeout_micro_seconds); - - _device->DeviceSpecificParameters.PreRangeVcselPulsePeriod = vcsel_pulse_period_pclk; + msrc_timeout_us); + + DevSpecParams.PreRangeVcselPulsePeriod = vcsel_pulse_period_pclk; break; case VL53L0X_VCSEL_PERIOD_FINAL_RANGE: status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_FINAL_RANGE, - &final_range_timeout_micro_seconds); + &final_range_timeout_us); if (status == VL53L0X_ERROR_NONE) status = VL53L0X_write_byte(VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD, vcsel_period_reg); - if (status == VL53L0X_ERROR_NONE) status = set_sequence_step_timeout(VL53L0X_SEQUENCESTEP_FINAL_RANGE, - final_range_timeout_micro_seconds); - - _device->DeviceSpecificParameters.FinalRangeVcselPulsePeriod = vcsel_pulse_period_pclk; + final_range_timeout_us); + + DevSpecParams.FinalRangeVcselPulsePeriod = vcsel_pulse_period_pclk; break; default: status = VL53L0X_ERROR_INVALID_PARAMS; @@ -946,9 +876,9 @@ /* Finally,the timing budget must be re-applied */ if (status == VL53L0X_ERROR_NONE) { - measurement_timing_budget_micro_seconds = _device->CurrentParameters.MeasurementTimingBudgetMicroSeconds; - - status = VL53L0X_set_measurement_timing_budget_micro_seconds(measurement_timing_budget_micro_seconds); + measurement_timing_budget_us = CurrentParameters.MeasurementTimingBudget_us; + + status = VL53L0X_set_measurement_timing_budget_us(measurement_timing_budget_us); } /* Perform the phase calibration. This is needed after changing on @@ -960,19 +890,18 @@ return status; } -VL53L0X_Error VL53L0X::VL53L0X_set_vcsel_pulse_period(VL53L0X_VcselPeriod vcsel_period_type,uint8_t vcsel_pulse_period) -{ - VL53L0X_Error status = VL53L0X_ERROR_NONE; +VL53L0X_Error VL53L0X::VL53L0X_set_vcsel_pulse_period(VL53L0X_VcselPeriod vcsel_period_type, + uint8_t vcsel_pulse_period) +{ VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = wrapped_VL53L0X_set_vcsel_pulse_period(vcsel_period_type, vcsel_pulse_period); - return status; } -VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_vcsel_pulse_period(VL53L0X_VcselPeriod vcsel_period_type,uint8_t *p_vcsel_pulse_period_pclk) +VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_vcsel_pulse_period( + VL53L0X_VcselPeriod vcsel_period_type, uint8_t *p_vcsel_pulse_period_pclk) { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t vcsel_period_reg; @@ -991,8 +920,7 @@ } if (status == VL53L0X_ERROR_NONE) - *p_vcsel_pulse_period_pclk = - VL53L0X_decode_vcsel_period(vcsel_period_reg); + *p_vcsel_pulse_period_pclk = VL53L0X_decode_vcsel_period(vcsel_period_reg); return status; } @@ -1001,11 +929,9 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = wrapped_VL53L0X_get_vcsel_pulse_period(vcsel_period_type, p_vcsel_pulse_period_pclk); - return status; } @@ -1019,7 +945,6 @@ timeout_macro_clks = ((uint32_t)(encoded_timeout & 0x00FF) << (uint32_t)((encoded_timeout & 0xFF00) >> 8)) + 1; - return timeout_macro_clks; } @@ -1029,13 +954,9 @@ uint32_t macro_period_vclks; uint32_t macro_period_ps; - - /* The above calculation will produce rounding errors, - therefore set fixed value - */ + therefore set fixed value */ pll_period_ps = 1655; - macro_period_vclks = 2304; macro_period_ps = (uint32_t)(macro_period_vclks * vcsel_period_pclks * pll_period_ps); @@ -1065,7 +986,7 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t current_vcsel_pulse_period_p_clk; uint8_t encoded_time_out_byte = 0; - uint32_t timeout_micro_seconds = 0; + uint32_t timeout_us = 0; uint16_t pre_range_encoded_time_out = 0; uint16_t msrc_time_out_m_clks; uint16_t pre_range_time_out_m_clks; @@ -1085,7 +1006,7 @@ } msrc_time_out_m_clks = VL53L0X_decode_timeout(encoded_time_out_byte); - timeout_micro_seconds = VL53L0X_calc_timeout_us(msrc_time_out_m_clks, + timeout_us = VL53L0X_calc_timeout_us(msrc_time_out_m_clks, current_vcsel_pulse_period_p_clk); } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_PRE_RANGE) { /* Retrieve PRE-RANGE VCSEL Period */ @@ -1106,7 +1027,7 @@ pre_range_time_out_m_clks = VL53L0X_decode_timeout(pre_range_encoded_time_out); - timeout_micro_seconds = VL53L0X_calc_timeout_us(pre_range_time_out_m_clks, + timeout_us = VL53L0X_calc_timeout_us(pre_range_time_out_m_clks, current_vcsel_pulse_period_p_clk); } } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_FINAL_RANGE) { @@ -1142,66 +1063,59 @@ } final_range_time_out_m_clks -= pre_range_time_out_m_clks; - timeout_micro_seconds = VL53L0X_calc_timeout_us(final_range_time_out_m_clks, + timeout_us = VL53L0X_calc_timeout_us(final_range_time_out_m_clks, current_vcsel_pulse_period_p_clk); } - *p_time_out_micro_secs = timeout_micro_seconds; + *p_time_out_micro_secs = timeout_us; return status; } -VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_measurement_timing_budget_micro_seconds(uint32_t *p_measurement_timing_budget_micro_seconds) +VL53L0X_Error VL53L0X::wrapped_VL53L0X_get_measurement_timing_budget_us(uint32_t *p_measurement_timing_budget_us) { VL53L0X_Error status = VL53L0X_ERROR_NONE; VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; - uint32_t final_range_timeout_micro_seconds; - uint32_t msrc_dcc_tcc_timeout_micro_seconds = 2000; - uint32_t start_overhead_micro_seconds = 1910; - uint32_t end_overhead_micro_seconds = 960; - uint32_t msrc_overhead_micro_seconds = 660; - uint32_t tcc_overhead_micro_seconds = 590; - uint32_t dss_overhead_micro_seconds = 690; - uint32_t pre_range_overhead_micro_seconds = 660; - uint32_t final_range_overhead_micro_seconds = 550; - uint32_t pre_range_timeout_micro_seconds = 0; - - + uint32_t final_range_timeout_us; + uint32_t msrc_dcc_tcc_timeout_us = 2000; + uint32_t start_overhead_us = 1910; + uint32_t end_overhead_us = 960; + uint32_t msrc_overhead_us = 660; + uint32_t tcc_overhead_us = 590; + uint32_t dss_overhead_us = 690; + uint32_t pre_range_overhead_us = 660; + uint32_t final_range_overhead_us = 550; + uint32_t pre_range_timeout_us = 0; /* Start and end overhead times always present */ - *p_measurement_timing_budget_micro_seconds - = start_overhead_micro_seconds + end_overhead_micro_seconds; + *p_measurement_timing_budget_us + = start_overhead_us + end_overhead_us; status = VL53L0X_get_sequence_step_enables(&scheduler_sequence_steps); - if (status != VL53L0X_ERROR_NONE) { - - return status; - } - - - if (scheduler_sequence_steps.TccOn || - scheduler_sequence_steps.MsrcOn || - scheduler_sequence_steps.DssOn) { + if (status != VL53L0X_ERROR_NONE) { return status; } + + if (scheduler_sequence_steps.TccOn || scheduler_sequence_steps.MsrcOn || + scheduler_sequence_steps.DssOn) { status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_MSRC, - &msrc_dcc_tcc_timeout_micro_seconds); + &msrc_dcc_tcc_timeout_us); if (status == VL53L0X_ERROR_NONE) { if (scheduler_sequence_steps.TccOn) { - *p_measurement_timing_budget_micro_seconds += - msrc_dcc_tcc_timeout_micro_seconds + - tcc_overhead_micro_seconds; + *p_measurement_timing_budget_us += + msrc_dcc_tcc_timeout_us + + tcc_overhead_us; } if (scheduler_sequence_steps.DssOn) { - *p_measurement_timing_budget_micro_seconds += - 2 * (msrc_dcc_tcc_timeout_micro_seconds + - dss_overhead_micro_seconds); + *p_measurement_timing_budget_us += + 2 * (msrc_dcc_tcc_timeout_us + + dss_overhead_us); } else if (scheduler_sequence_steps.MsrcOn) { - *p_measurement_timing_budget_micro_seconds += - msrc_dcc_tcc_timeout_micro_seconds + - msrc_overhead_micro_seconds; + *p_measurement_timing_budget_us += + msrc_dcc_tcc_timeout_us + + msrc_overhead_us; } } } @@ -1209,67 +1123,62 @@ if (status == VL53L0X_ERROR_NONE) { if (scheduler_sequence_steps.PreRangeOn) { status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_PRE_RANGE, - &pre_range_timeout_micro_seconds); - *p_measurement_timing_budget_micro_seconds += - pre_range_timeout_micro_seconds + - pre_range_overhead_micro_seconds; + &pre_range_timeout_us); + *p_measurement_timing_budget_us += + pre_range_timeout_us + + pre_range_overhead_us; } } if (status == VL53L0X_ERROR_NONE) { if (scheduler_sequence_steps.FinalRangeOn) { status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_FINAL_RANGE, - &final_range_timeout_micro_seconds); - *p_measurement_timing_budget_micro_seconds += - (final_range_timeout_micro_seconds + - final_range_overhead_micro_seconds); + &final_range_timeout_us); + *p_measurement_timing_budget_us += + (final_range_timeout_us + + final_range_overhead_us); } } if (status == VL53L0X_ERROR_NONE) { - _device->CurrentParameters.MeasurementTimingBudgetMicroSeconds = - *p_measurement_timing_budget_micro_seconds; + CurrentParameters.MeasurementTimingBudget_us = + *p_measurement_timing_budget_us; } return status; } -VL53L0X_Error VL53L0X::VL53L0X_get_measurement_timing_budget_micro_seconds(uint32_t *p_measurement_timing_budget_micro_seconds) +VL53L0X_Error VL53L0X::VL53L0X_get_measurement_timing_budget_us( + uint32_t *p_measurement_timing_budget_us) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - - - status = wrapped_VL53L0X_get_measurement_timing_budget_micro_seconds(p_measurement_timing_budget_micro_seconds); - + + status = wrapped_VL53L0X_get_measurement_timing_budget_us( + p_measurement_timing_budget_us); return status; } -VL53L0X_Error VL53L0X::VL53L0X_get_device_parameters(VL53L0X_DeviceParameters_t *p_device_parameters) +VL53L0X_Error VL53L0X::VL53L0X_get_device_parameters( + VL53L0X_DeviceParameters_t *p_device_parameters) { VL53L0X_Error status = VL53L0X_ERROR_NONE; int i; - - status = VL53L0X_get_device_mode(&(p_device_parameters->DeviceMode)); if (status == VL53L0X_ERROR_NONE) - status = VL53L0X_get_inter_measurement_period_milli_seconds(&(p_device_parameters->InterMeasurementPeriodMilliSeconds)); - + status = VL53L0X_get_inter_measurement_period_ms(&(p_device_parameters->InterMeasurementPeriod_ms)); if (status == VL53L0X_ERROR_NONE) { - p_device_parameters->XTalkCompensationEnable = 0; - } + p_device_parameters->XTalkCompensationEnable = 0; } if (status == VL53L0X_ERROR_NONE) status = VL53L0X_get_x_talk_compensation_rate_mega_cps(&(p_device_parameters->XTalkCompensationRateMegaCps)); - if (status == VL53L0X_ERROR_NONE) - status = VL53L0X_get_offset_calibration_data_micro_meter(&(p_device_parameters->RangeOffsetMicroMeters)); - + status = VL53L0X_get_offsetCallDat_um(&(p_device_parameters->RangeOffsetMicroMeters)); if (status == VL53L0X_ERROR_NONE) { for (i = 0; i < VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS; i++) { @@ -1278,17 +1187,14 @@ * flags */ if (status == VL53L0X_ERROR_NONE) { - status |= VL53L0X_get_limit_check_value(i, - &(p_device_parameters->LimitChecksValue[i])); + status |= VL53L0X_get_limit_check_value(i,&(p_device_parameters->LimitChecksValue[i])); } else { break; } if (status == VL53L0X_ERROR_NONE) { status |= VL53L0X_get_limit_check_enable(i, - &(p_device_parameters->LimitChecksEnable[i])); - } else { - break; - } + &(p_device_parameters->LimitChecksEnable[i])); } + else { break; } } } @@ -1298,10 +1204,10 @@ /* Need to be done at the end as it uses VCSELPulsePeriod */ if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_get_measurement_timing_budget_micro_seconds(&(p_device_parameters->MeasurementTimingBudgetMicroSeconds)); + status = VL53L0X_get_measurement_timing_budget_us( + &(p_device_parameters->MeasurementTimingBudget_us)); } - return status; } @@ -1309,62 +1215,50 @@ FixPoint1616_t limit_check_value) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - uint8_t temp8; - - - - temp8 = _device->CurrentParameters.LimitChecksEnable[limit_check_id]; + uint8_t temp8; + + temp8 = CurrentParameters.LimitChecksEnable[limit_check_id]; if (temp8 == 0) { /* disabled write only internal value */ - _device->CurrentParameters.LimitChecksValue[limit_check_id] = limit_check_value; + CurrentParameters.LimitChecksValue[limit_check_id] = limit_check_value; } else { switch (limit_check_id) { case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: /* internal computation: */ - _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = limit_check_value; + CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = limit_check_value; break; case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE: - status = VL53L0X_write_word(VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, VL53L0X_FIXPOINT1616TOFIXPOINT97(limit_check_value)); - break; case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: - /* internal computation: */ - _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = limit_check_value; - + CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = limit_check_value; break; case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: - /* internal computation: */ - _device->CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = limit_check_value; - + CurrentParameters.LimitChecksValue[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = limit_check_value; break; case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC: case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: - status = VL53L0X_write_word(VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT, VL53L0X_FIXPOINT1616TOFIXPOINT97(limit_check_value)); - break; default: status = VL53L0X_ERROR_INVALID_PARAMS; - } if (status == VL53L0X_ERROR_NONE) { - _device->CurrentParameters.LimitChecksValue[limit_check_id] = limit_check_value; + CurrentParameters.LimitChecksValue[limit_check_id] = limit_check_value; } } - return status; } @@ -1372,71 +1266,62 @@ VL53L0X_Error VL53L0X::VL53L0X_data_init() { VL53L0X_Error status = VL53L0X_ERROR_NONE; - VL53L0X_DeviceParameters_t CurrentParameters; + VL53L0X_DeviceParameters_t Updated_Curr_Params; int i; - uint8_t StopVariable; - - /* by default the I2C is running at 1V8 if you want to change it you * need to include this define at compilation level. */ #ifdef USE_I2C_2V8 Status = VL53L0X_UpdateByte(VL53L0X_REG_VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV, - 0xFE, - 0x01); + 0xFE, 0x01); #endif /* Set I2C standard mode */ - if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_write_byte(0x88,0x00); - } - - _device->DeviceSpecificParameters.ReadDataFromDeviceDone = 0; - _device->DeviceSpecificParameters.ReadDataFromDeviceDone = 0; + if (status == VL53L0X_ERROR_NONE) {status = VL53L0X_write_byte(0x88,0x00);} + DevSpecParams.ReadDataFromDeviceDone = 0; + DevSpecParams.ReadDataFromDeviceDone = 0; #ifdef USE_IQC_STATION if (Status == VL53L0X_ERROR_NONE) { - Status = VL53L0X_apply_offset_adjustment(); - } + Status = VL53L0X_apply_offset_adjustment(); } #endif /* Default value is 1000 for Linearity Corrective Gain */ - _device->LinearityCorrectiveGain = 1000; + LinearityCorrectiveGain = 1000; /* Dmax default Parameter */ - _device->DmaxCalRangeMilliMeter = 400; - _device->DmaxCalSignalRateRtnMegaCps = (FixPoint1616_t)((0x00016B85)); /* 1.42 No Cover Glass*/ + DmaxCalRangeMilliMeter = 400; + DmaxCalSignalRateRtnMegaCps = (FixPoint1616_t)((0x00016B85)); /* 1.42 No Cover Glass*/ /* Set Default static parameters *set first temporary values 9.44MHz * 65536 = 618660 */ - _device->DeviceSpecificParameters.OscFrequencyMHz = 618660; + DevSpecParams.OscFrequencyMHz = 618660; /* Set Default XTalkCompensationRateMegaCps to 0 */ - _device->CurrentParameters.XTalkCompensationRateMegaCps = 0; + CurrentParameters.XTalkCompensationRateMegaCps = 0; /* Get default parameters */ - status = VL53L0X_get_device_parameters(&CurrentParameters); + status = VL53L0X_get_device_parameters(&Updated_Curr_Params); if (status == VL53L0X_ERROR_NONE) { /* initialize PAL values */ - CurrentParameters.DeviceMode = VL53L0X_DEVICEMODE_SINGLE_RANGING; - CurrentParameters.HistogramMode = VL53L0X_HISTOGRAMMODE_DISABLED; - _device->CurrentParameters = CurrentParameters; - } + Updated_Curr_Params.DeviceMode = VL53L0X_DEVICEMODE_SINGLE_RANGING; + Updated_Curr_Params.HistogramMode = VL53L0X_HISTOGRAMMODE_DISABLED; + CurrentParameters = Updated_Curr_Params; + } /* Sigma estimator variable */ - _device->SigmaEstRefArray = 100; - _device->SigmaEstEffPulseWidth = 900; - _device->SigmaEstEffAmbWidth = 500; - _device->targetRefRate = 0x0A00; /* 20 MCPS in 9:7 format */ + SigmaEstRefArray = 100; + SigmaEstEffPulseWidth = 900; + SigmaEstEffAmbWidth = 500; + targetRefRate = 0x0A00; /* 20 MCPS in 9:7 format */ /* Use internal default settings */ - _device->UseInternalTuningSettings = 1; + UseInternalTuningSettings = 1; status |= VL53L0X_write_byte(0x80,0x01); status |= VL53L0X_write_byte(0xFF,0x01); status |= VL53L0X_write_byte(0x00,0x00); - status |= VL53L0X_read_byte(0x91,&StopVariable); - _device->StopVariable = StopVariable; + status |= VL53L0X_read_byte (0x91,&StopVariable); status |= VL53L0X_write_byte(0x00,0x01); status |= VL53L0X_write_byte(0xFF,0x00); status |= VL53L0X_write_byte(0x80,0x00); @@ -1486,22 +1371,15 @@ } if (status == VL53L0X_ERROR_NONE) { - - _device->SequenceConfig = 0xFF; - status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, - 0xFF); - + SequenceConfig = 0xFF; + status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0xFF); /* Set PAL state to tell that we are waiting for call to * VL53L0X_StaticInit */ - _device->PalState = VL53L0X_STATE_WAIT_STATICINIT; + PalState = VL53L0X_STATE_WAIT_STATICINIT; } - if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.RefSpadsInitialised = 0; - } - - - + if (status == VL53L0X_ERROR_NONE) { DevSpecParams.RefSpadsInitialised = 0; } + return status; } @@ -1512,23 +1390,20 @@ uint8_t module_id_int; char *product_id_tmp; - - status = VL53L0X_get_info_from_device(2); if (status == VL53L0X_ERROR_NONE) { - module_id_int = _device->DeviceSpecificParameters.ModuleId; + module_id_int = DevSpecParams.ModuleId; if (module_id_int == 0) { *revision = 0; strcpy(p_VL53L0X_device_info->ProductId,""); } else { - *revision = _device->DeviceSpecificParameters.Revision; - product_id_tmp = _device->DeviceSpecificParameters.ProductId; + *revision = DevSpecParams.Revision; + product_id_tmp = DevSpecParams.ProductId; strcpy(p_VL53L0X_device_info->ProductId,product_id_tmp); } } - return status; } @@ -1578,17 +1453,13 @@ VL53L0X_Error VL53L0X::VL53L0X_get_interrupt_mask_status(uint32_t *p_interrupt_mask_status) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - uint8_t byte; - - - status = VL53L0X_read_byte(VL53L0X_REG_RESULT_INTERRUPT_STATUS,&byte); - *p_interrupt_mask_status = byte & 0x07; - - if (byte & 0x18) { - status = VL53L0X_ERROR_RANGE_ERROR; - } - - + uint8_t intStat; + + status = VL53L0X_read_byte(VL53L0X_REG_RESULT_INTERRUPT_STATUS,&intStat); + *p_interrupt_mask_status = intStat & 0x07; + + if (intStat & 0x18) { status = VL53L0X_ERROR_RANGE_ERROR; } + return status; } @@ -1598,9 +1469,8 @@ uint8_t sys_range_status_register; uint8_t interrupt_config; uint32_t interrupt_mask; - - - interrupt_config = _device->DeviceSpecificParameters.Pin0GpioFunctionality; + + interrupt_config = DevSpecParams.Pin0GpioFunctionality; if (interrupt_config == VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY) { @@ -1622,14 +1492,12 @@ } } } - return status; } VL53L0X_Error VL53L0X::VL53L0X_polling_delay() { VL53L0X_Error status = VL53L0X_ERROR_NONE; - // do nothing VL53L0X_OsDelay(); return status; } @@ -1663,7 +1531,6 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t loop_count; uint8_t byte; - /* clear bit 0 range interrupt,bit 1 error interrupt */ loop_count = 0; @@ -1676,11 +1543,9 @@ && (loop_count < 3) && (status == VL53L0X_ERROR_NONE)); - if (loop_count >= 3) { status = VL53L0X_ERROR_INTERRUPT_NOT_CLEARED; } - return status; } @@ -1760,7 +1625,7 @@ /* store the value of the sequence config, * this will be reset before the end of the function */ - if (restore_config) {sequence_config = _device->SequenceConfig;} + if (restore_config) {sequence_config = SequenceConfig;} /* Run VHV */ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,0x01); @@ -1779,15 +1644,13 @@ *p_vhv_settings = 0; } - if ((status == VL53L0X_ERROR_NONE) && restore_config) { /* restore the previous Sequence Config */ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, sequence_config); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = sequence_config; + SequenceConfig = sequence_config; } - } return status; @@ -1805,10 +1668,7 @@ /* store the value of the sequence config, * this will be reset before the end of the function */ - - if (restore_config) { - sequence_config = _device-> SequenceConfig; - } + if (restore_config) { sequence_config = SequenceConfig; } /* Run PhaseCal */ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,0x02); @@ -1827,30 +1687,27 @@ *p_phase_cal = 0; } - if ((status == VL53L0X_ERROR_NONE) && restore_config) { /* restore the previous Sequence Config */ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, sequence_config); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = sequence_config; + SequenceConfig = sequence_config; } - } return status; } -VL53L0X_Error VL53L0X::VL53L0X_perform_ref_calibration(uint8_t *p_vhv_settings,uint8_t *p_phase_cal,uint8_t get_data_enable) +VL53L0X_Error VL53L0X::VL53L0X_perform_ref_calibration(uint8_t *p_vhv_settings, + uint8_t *p_phase_cal, uint8_t get_data_enable) { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t sequence_config = 0; /* store the value of the sequence config, - * this will be reset before the end of the function - */ - - sequence_config = _device-> SequenceConfig; + * this will be reset before the end of the function */ + sequence_config = SequenceConfig; /* In the following function we don't save the config to optimize * writes on device. Config is saved and restored only once. */ @@ -1860,24 +1717,19 @@ status = VL53L0X_perform_phase_calibration(p_phase_cal,get_data_enable,0); } - if (status == VL53L0X_ERROR_NONE) { /* restore the previous Sequence Config */ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, sequence_config); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = sequence_config; - } - + SequenceConfig = sequence_config; } } - return status; } void VL53L0X::get_next_good_spad(uint8_t good_spad_array[],uint32_t size, uint32_t curr,int32_t *p_next) -{ - uint32_t start_index; +{ uint32_t start_index; uint32_t fine_offset; uint32_t c_spads_per_byte = 8; uint32_t coarse_index; @@ -1885,8 +1737,7 @@ uint8_t data_byte; uint8_t success = 0; - /* - * Starting with the current good spad,loop through the array to find + /* Starting with the current good spad,loop through the array to find * the next. i.e. the next bit set in the sequence. * * The coarse index is the byte index of the array and the fine index is @@ -1934,7 +1785,6 @@ if (refArrayQuadrants[quadrant] == REF_ARRAY_SPAD_0) { is_aperture = 0; } - return is_aperture; } @@ -1961,7 +1811,6 @@ { VL53L0X_Error status = VL53L0X_write_multi(VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, p_ref_spad_array,6); - return status; } @@ -1970,11 +1819,6 @@ VL53L0X_Error status = VL53L0X_read_multi(VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, p_ref_spad_array, 6); -// VL53L0X_Error status = VL53L0X_ERROR_NONE; -// uint8_t count=0; - -// for (count = 0; count < 6; count++) -// status = VL53L0X_RdByte((VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0 + count),&refSpadArray[count]); return status; } @@ -2029,13 +1873,10 @@ *p_last_spad = current_spad; if (status == VL53L0X_ERROR_NONE) { - status = set_ref_spad_map(spad_array); - } - + status = set_ref_spad_map(spad_array); } if (status == VL53L0X_ERROR_NONE) { status = get_ref_spad_map(check_spad_array); - i = 0; /* Compare spad maps. If not equal report error. */ @@ -2061,13 +1902,12 @@ case VL53L0X_DEVICEMODE_GPIO_DRIVE: case VL53L0X_DEVICEMODE_GPIO_OSC: /* Supported modes */ - _device->CurrentParameters.DeviceMode = device_mode; + CurrentParameters.DeviceMode = device_mode; break; default: /* Unsupported mode */ status = VL53L0X_ERROR_MODE_NOT_SUPPORTED; } - return status; } @@ -2087,10 +1927,8 @@ if (status == VL53L0X_ERROR_NONE) { /* Need to divide by 2 because the FW will apply a x2 */ threshold16 = (uint16_t)((threshold_high >> 17) & 0x00fff); - status = VL53L0X_write_word(VL53L0X_REG_SYSTEM_THRESH_HIGH, - threshold16); + status = VL53L0X_write_word(VL53L0X_REG_SYSTEM_THRESH_HIGH,threshold16); } - return status; } @@ -2100,10 +1938,8 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint16_t threshold16; - /* no dependency on DeviceMode for FlightSense */ - status = VL53L0X_read_word(VL53L0X_REG_SYSTEM_THRESH_LOW,&threshold16); /* Need to multiply by 2 because the FW will apply a x2 */ *p_threshold_low = (FixPoint1616_t)((0x00fff & threshold16) << 17); @@ -2115,7 +1951,6 @@ *p_threshold_high = (FixPoint1616_t)((0x00fff & threshold16) << 17); } - return status; } @@ -2133,8 +1968,6 @@ uint8_t local_buffer[4]; /* max */ uint16_t temp16; - - index = 0; while ((*(p_tuning_setting_buffer + index) != 0) && @@ -2152,7 +1985,7 @@ lsb = *(p_tuning_setting_buffer + index); index++; temp16 = VL53L0X_MAKEUINT16(lsb,msb); - _device->SigmaEstRefArray = temp16; + SigmaEstRefArray = temp16; break; case 1: /* uint16_t SigmaEstEffPulseWidth -> 2 bytes */ msb = *(p_tuning_setting_buffer + index); @@ -2160,7 +1993,7 @@ lsb = *(p_tuning_setting_buffer + index); index++; temp16 = VL53L0X_MAKEUINT16(lsb,msb); - _device->SigmaEstEffPulseWidth = temp16; + SigmaEstEffPulseWidth = temp16; break; case 2: /* uint16_t SigmaEstEffAmbWidth -> 2 bytes */ msb = *(p_tuning_setting_buffer + index); @@ -2168,7 +2001,7 @@ lsb = *(p_tuning_setting_buffer + index); index++; temp16 = VL53L0X_MAKEUINT16(lsb,msb); - _device->SigmaEstEffAmbWidth = temp16; + SigmaEstEffAmbWidth = temp16; break; case 3: /* uint16_t targetRefRate -> 2 bytes */ msb = *(p_tuning_setting_buffer + index); @@ -2176,12 +2009,11 @@ lsb = *(p_tuning_setting_buffer + index); index++; temp16 = VL53L0X_MAKEUINT16(lsb,msb); - _device->targetRefRate = temp16; + targetRefRate = temp16; break; default: /* invalid parameter */ status = VL53L0X_ERROR_INVALID_PARAMS; } - } else if (number_of_writes <= 4) { address = *(p_tuning_setting_buffer + index); index++; @@ -2199,7 +2031,6 @@ status = VL53L0X_ERROR_INVALID_PARAMS; } } - return status; } @@ -2211,7 +2042,7 @@ FixPoint1616_t threshold_high; VL53L0X_Error status = VL53L0X_ERROR_NONE; - interrupt_config = _device->DeviceSpecificParameters.Pin0GpioFunctionality; + interrupt_config = DevSpecParams.Pin0GpioFunctionality; if ((interrupt_config == VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW) || @@ -2235,14 +2066,10 @@ status |= VL53L0X_write_byte(0xFF,0x00); status |= VL53L0X_write_byte(0x80,0x00); } - } - - } return status; - } VL53L0X_Error VL53L0X::VL53L0X_start_measurement() @@ -2252,7 +2079,6 @@ uint8_t byte; uint8_t start_stop_byte = VL53L0X_REG_SYSRANGE_MODE_START_STOP; uint32_t loop_nb; - /* Get Current DeviceMode */ VL53L0X_get_device_mode(&device_mode); @@ -2260,7 +2086,7 @@ status = VL53L0X_write_byte(0x80,0x01); status = VL53L0X_write_byte(0xFF,0x01); status = VL53L0X_write_byte(0x00,0x00); - status = VL53L0X_write_byte(0x91,_device-> StopVariable); + status = VL53L0X_write_byte(0x91,StopVariable); status = VL53L0X_write_byte(0x00,0x01); status = VL53L0X_write_byte(0xFF,0x00); status = VL53L0X_write_byte(0x80,0x00); @@ -2290,7 +2116,6 @@ break; case VL53L0X_DEVICEMODE_CONTINUOUS_RANGING: /* Back-to-back mode */ - /* Check if need to apply interrupt settings */ if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_check_and_load_interrupt_settings(1); @@ -2300,7 +2125,7 @@ VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK); if (status == VL53L0X_ERROR_NONE) { /* Set PAL State to Running */ - _device->PalState = VL53L0X_STATE_RUNNING; + PalState = VL53L0X_STATE_RUNNING; } break; case VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING: @@ -2315,15 +2140,13 @@ if (status == VL53L0X_ERROR_NONE) { /* Set PAL State to Running */ - _device->PalState = VL53L0X_STATE_RUNNING; + PalState = VL53L0X_STATE_RUNNING; } break; default: /* Selected mode not supported */ status = VL53L0X_ERROR_MODE_NOT_SUPPORTED; } - - return status; } @@ -2334,8 +2157,6 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; VL53L0X_DeviceModes device_mode; - - /* Get Current DeviceMode */ status = VL53L0X_get_device_mode(&device_mode); @@ -2346,19 +2167,15 @@ status = VL53L0X_start_measurement(); } - if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_measurement_poll_for_completion(); } - /* Change PAL State in case of single ranging or single histogram */ if (status == VL53L0X_ERROR_NONE && device_mode == VL53L0X_DEVICEMODE_SINGLE_RANGING) { - _device->PalState = VL53L0X_STATE_IDLE; + PalState = VL53L0X_STATE_IDLE; } - - return status; } @@ -2368,11 +2185,9 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t temp8; - - temp8 = _device->CurrentParameters.XTalkCompensationEnable; + temp8 = CurrentParameters.XTalkCompensationEnable; *p_x_talk_compensation_enable = temp8; - return status; } @@ -2392,7 +2207,7 @@ if (xtalk_comp_enable) { - xtalk_per_spad_mega_cps = _device->CurrentParameters.XTalkCompensationRateMegaCps; + xtalk_per_spad_mega_cps = CurrentParameters.XTalkCompensationRateMegaCps; /* FixPoint1616 * FixPoint 8:8 = FixPoint0824 */ total_xtalk_mega_cps = @@ -2413,9 +2228,7 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; FixPoint1616_t total_xtalk_mega_cps; - - *p_total_signal_rate_mcps = p_ranging_measurement_data->SignalRateRtnMegaCps; @@ -2448,9 +2261,7 @@ uint32_t VL53L0X::VL53L0X_isqrt(uint32_t num) { - /* - * Implements an integer square root - * + /* Implements an integer square root * From: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots */ @@ -2460,10 +2271,7 @@ * 1 << 14 for 16-bits,1 << 30 for 32 bits */ /* "bit" starts at the highest power of four <= the argument. */ - while (bit > num) { - bit >>= 2; - } - + while (bit > num) { bit >>= 2; } while (bit != 0) { if (num >= res + bit) { @@ -2472,7 +2280,6 @@ } else { res >>= 1; } - bit >>= 2; } @@ -2511,13 +2318,9 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; - - - dmax_cal_range_mm = - _device-> DmaxCalRangeMilliMeter; - - dmax_cal_signal_rate_rtn_mcps = - _device-> DmaxCalSignalRateRtnMegaCps; + dmax_cal_range_mm = DmaxCalRangeMilliMeter; + + dmax_cal_signal_rate_rtn_mcps = DmaxCalSignalRateRtnMegaCps; /* uint32 * FixPoint1616 = FixPoint1616 */ signal_at0_mm = dmax_cal_range_mm * dmax_cal_signal_rate_rtn_mcps; @@ -2572,7 +2375,6 @@ if (sigma_est_p2_tmp > 0xffff) { min_signal_needed_p3 = 0xfff00000; } else { - /* DMAX uses a different ambient width from sigma,so apply * correction. * Perform division before multiplication to prevent overflow. @@ -2585,7 +2387,6 @@ min_signal_needed_p3 = (sigma_estimate_p2 + 0x8000) >> 16; min_signal_needed_p3 *= min_signal_needed_p3; - } /* FixPoint1814 / uint32 = FixPoint1814 */ @@ -2666,8 +2467,6 @@ *pd_max_mm = dmax_ambient; } - - return status; } @@ -2726,30 +2525,23 @@ uint8_t pre_range_vcsel_pclks; /*! \addtogroup calc_sigma_estimate * @{ - * * Estimates the range sigma */ - - - x_talk_comp_rate_mcps = _device->CurrentParameters.XTalkCompensationRateMegaCps; - - /* - * We work in kcps rather than mcps as this helps keep within the + x_talk_comp_rate_mcps = CurrentParameters.XTalkCompensationRateMegaCps; + + /* We work in kcps rather than mcps as this helps keep within the * confines of the 32 Fix1616 type. */ - ambient_rate_kcps = (p_ranging_measurement_data->AmbientRateRtnMegaCps * 1000) >> 16; corrected_signal_rate_mcps = p_ranging_measurement_data->SignalRateRtnMegaCps; - status = VL53L0X_get_total_signal_rate(p_ranging_measurement_data,&total_signal_rate_mcps); status = VL53L0X_get_total_xtalk_rate(p_ranging_measurement_data,&x_talk_comp_rate_mcps); - /* Signal rate measurement provided by device is the * peak signal rate,not average. */ @@ -2763,27 +2555,22 @@ } if (status == VL53L0X_ERROR_NONE) { - /* Calculate final range macro periods */ - final_range_timeout_micro_secs = _device->DeviceSpecificParameters.FinalRangeTimeoutMicroSecs; - - final_range_vcsel_pclks = _device->DeviceSpecificParameters.FinalRangeVcselPulsePeriod; + final_range_timeout_micro_secs = DevSpecParams.FinalRangeTimeoutMicroSecs; + + final_range_vcsel_pclks = DevSpecParams.FinalRangeVcselPulsePeriod; final_range_macro_pclks = VL53L0X_calc_timeout_mclks(final_range_timeout_micro_secs,final_range_vcsel_pclks); /* Calculate pre-range macro periods */ - pre_range_timeout_micro_secs = _device->DeviceSpecificParameters.PreRangeTimeoutMicroSecs; - - pre_range_vcsel_pclks = _device->DeviceSpecificParameters.PreRangeVcselPulsePeriod; - + pre_range_timeout_micro_secs = DevSpecParams.PreRangeTimeoutMicroSecs; + pre_range_vcsel_pclks = DevSpecParams.PreRangeVcselPulsePeriod; pre_range_macro_pclks = VL53L0X_calc_timeout_mclks(pre_range_timeout_micro_secs,pre_range_vcsel_pclks); - vcsel_width = 3; if (final_range_vcsel_pclks == 8) { vcsel_width = 2; } - peak_vcsel_duration_us = vcsel_width * 2048 * (pre_range_macro_pclks + final_range_macro_pclks); peak_vcsel_duration_us = (peak_vcsel_duration_us + 500) / 1000; @@ -2804,14 +2591,11 @@ total_signal_rate_mcps <<= 8; } - if (status != VL53L0X_ERROR_NONE) { - - return status; - } + if (status != VL53L0X_ERROR_NONE) { return status; } if (peak_signal_rate_kcps == 0) { *p_sigma_estimate = c_sigma_est_max; - _device->SigmaEstimate = c_sigma_est_max; + SigmaEstimate = c_sigma_est_max; *p_dmax_mm = 0; } else { if (vcsel_total_events_rtn < 1) { @@ -2828,7 +2612,6 @@ sigma_estimate_p2 = c_amb_to_signal_ratio_max; } sigma_estimate_p2 *= c_ambient_effective_width_centi_ns; - sigma_estimate_p3 = 2 * VL53L0X_isqrt(vcsel_total_events_rtn * 12); /* uint32 * FixPoint1616 = FixPoint1616 */ @@ -2866,8 +2649,7 @@ /* FixPoint1616/uint32 = FixPoint1616 */ pw_mult = delta_t_ps / c_vcsel_pulse_width_ps; /* smaller than 1.0f */ - /* - * FixPoint1616 * FixPoint1616 = FixPoint3232,however both + /* FixPoint1616 * FixPoint1616 = FixPoint3232,however both * values are small enough such that32 bits will not be * exceeded. */ @@ -2879,8 +2661,7 @@ /* FixPoint1616 + FixPoint1616 = FixPoint1616 */ pw_mult += (1 << 16); - /* - * At this point the value will be 1.xx,therefore if we square + /* At this point the value will be 1.xx,therefore if we square * the value this will exceed 32 bits. To address this perform * a single shift to the right before the multiplication. */ @@ -2972,7 +2753,7 @@ } *p_sigma_estimate = (uint32_t)(sigma_estimate); - _device->SigmaEstimate = *p_sigma_estimate; + SigmaEstimate = *p_sigma_estimate; status = VL53L0X_calc_dmax(total_signal_rate_mcps, corrected_signal_rate_mcps, pw_mult, @@ -2982,7 +2763,6 @@ p_dmax_mm); } - return status; } @@ -3012,9 +2792,6 @@ uint32_t dmax_mm = 0; FixPoint1616_t last_signal_ref_mcps; - - - /* * VL53L0X has a good ranging when the value of the * DeviceRangeStatus = 11. This function will replace the value 0 with @@ -3034,9 +2811,7 @@ device_range_status_internal == 15 ) { none_flag = 1; - } else { - none_flag = 0; - } + } else { none_flag = 0; } /* * Check if Sigma limit is enabled,if yes then do comparison with limit @@ -3100,7 +2875,7 @@ } last_signal_ref_mcps = VL53L0X_FIXPOINT97TOFIXPOINT1616(tmp_word); - _device->LastSignalRefMcps = last_signal_ref_mcps; + LastSignalRefMcps = last_signal_ref_mcps; if ((signal_ref_clip_value > 0) && (last_signal_ref_mcps > signal_ref_clip_value)) { @@ -3181,7 +2956,7 @@ } else { temp8 = 0; } - _device->CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = temp8; + CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = temp8; if ((device_range_status_internal == 4) || (signal_rate_final_range_limit_check_enable == 0)) { @@ -3189,7 +2964,7 @@ } else { temp8 = 0; } - _device->CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE] = temp8; + CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE] = temp8; if ((signal_ref_clip_limit_check_enable == 0) || (signal_ref_clipflag == 1)) { @@ -3198,7 +2973,7 @@ temp8 = 0; } - _device->CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = temp8; + CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = temp8; if ((range_ignore_threshold_limit_check_enable == 0) || (range_ignore_thresholdflag == 1)) { @@ -3206,13 +2981,10 @@ } else { temp8 = 0; } - - _device->CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = temp8; + CurrentParameters.LimitChecksStatus[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = temp8; } - return status; - } VL53L0X_Error VL53L0X::VL53L0X_get_ranging_measurement_data(VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data) @@ -3232,8 +3004,6 @@ uint8_t localBuffer[12]; VL53L0X_RangingMeasurementData_t last_range_data_buffer; - - /* * use multi read even if some registers are not useful,result will * be more efficient @@ -3271,21 +3041,18 @@ device_range_status = localBuffer[0]; /* Get Linearity Corrective Gain */ - linearity_corrective_gain = _device-> - LinearityCorrectiveGain; + linearity_corrective_gain = LinearityCorrectiveGain; /* Get ranging configuration */ - range_fractional_enable = _device-> - RangeFractionalEnable; + range_fractional_enable = RangeFractionalEnable; if (linearity_corrective_gain != 1000) { - tmpuint16 = (uint16_t)((linearity_corrective_gain * tmpuint16 + 500) / 1000); /* Implement Xtalk */ - x_talk_compensation_rate_mega_cps = _device->CurrentParameters.XTalkCompensationRateMegaCps; - x_talk_compensation_enable = _device->CurrentParameters.XTalkCompensationEnable; + x_talk_compensation_rate_mega_cps = CurrentParameters.XTalkCompensationRateMegaCps; + x_talk_compensation_enable = CurrentParameters.XTalkCompensationEnable; if (x_talk_compensation_enable) { @@ -3309,7 +3076,6 @@ tmpuint16 = xtalk_range_milli_meter; } - } if (range_fractional_enable) { @@ -3331,17 +3097,14 @@ status |= VL53L0X_get_pal_range_status(device_range_status, signal_rate,effective_spad_rtn_count, p_ranging_measurement_data,&pal_range_status); - if (status == VL53L0X_ERROR_NONE) { p_ranging_measurement_data->RangeStatus = pal_range_status; } - } if (status == VL53L0X_ERROR_NONE) { /* Copy last read data into Dev buffer */ - last_range_data_buffer = _device-> LastRangeMeasure; - + last_range_data_buffer = LastRangeMeasure; last_range_data_buffer.RangeMilliMeter = p_ranging_measurement_data->RangeMilliMeter; last_range_data_buffer.RangeFractionalPart = @@ -3358,11 +3121,8 @@ p_ranging_measurement_data->EffectiveSpadRtnCount; last_range_data_buffer.RangeStatus = p_ranging_measurement_data->RangeStatus; - - _device->LastRangeMeasure = last_range_data_buffer; + LastRangeMeasure = last_range_data_buffer; } - - return status; } @@ -3370,8 +3130,6 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - - /* This function will do a complete single ranging * Here we fix the mode! */ status = VL53L0X_set_device_mode(VL53L0X_DEVICEMODE_SINGLE_RANGING); @@ -3388,7 +3146,6 @@ status = VL53L0X_clear_interrupt_mask(0); } - return status; } @@ -3400,14 +3157,10 @@ uint8_t sequence_config = 0; /* store the value of the sequence config, - * this will be reset before the end of the function - */ - - sequence_config = _device-> SequenceConfig; - - /* - * This function performs a reference signal rate measurement. - */ + * this will be reset before the end of the function*/ + sequence_config = SequenceConfig; + + /* This function performs a reference signal rate measurement.*/ if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,0xC0); } @@ -3434,7 +3187,7 @@ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, sequence_config); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = sequence_config; + SequenceConfig = sequence_config; } } @@ -3486,7 +3239,7 @@ * of the non-aperture quadrant and runs in to the adjacent aperture * quadrant. */ - target_ref_rate = _device-> targetRefRate; + target_ref_rate = targetRefRate; /* * Initialize Spad arrays. @@ -3497,10 +3250,9 @@ * represent spads. */ for (index = 0; index < spad_array_size; index++) { - _device->SpadData.RefSpadEnables[index] = 0; + SpadData.RefSpadEnables[index] = 0; } - status = VL53L0X_write_byte(0xFF,0x01); if (status == VL53L0X_ERROR_NONE) { @@ -3526,8 +3278,7 @@ /* Perform ref calibration */ if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_perform_ref_calibration(&vhv_settings, - &phase_cal,0); + status = VL53L0X_perform_ref_calibration(&vhv_settings, &phase_cal, 0); } if (status == VL53L0X_ERROR_NONE) { @@ -3536,8 +3287,8 @@ last_spad_index = current_spad_index; need_apt_spads = 0; status = enable_ref_spads(need_apt_spads, - _device->SpadData.RefGoodSpadMap, - _device->SpadData.RefSpadEnables, + SpadData.RefGoodSpadMap, + SpadData.RefSpadEnables, spad_array_size, start_select, current_spad_index, @@ -3555,7 +3306,7 @@ * switch to APERTURE SPADs */ for (index = 0; index < spad_array_size; index++) { - _device->SpadData.RefSpadEnables[index] = 0; + SpadData.RefSpadEnables[index] = 0; } @@ -3568,8 +3319,8 @@ need_apt_spads = 1; status = enable_ref_spads(need_apt_spads, - _device->SpadData.RefGoodSpadMap, - _device->SpadData.RefSpadEnables, + SpadData.RefGoodSpadMap, + SpadData.RefSpadEnables, spad_array_size, start_select, current_spad_index, @@ -3592,9 +3343,7 @@ ref_spad_count_int = minimum_spad_count; } } - } else { - need_apt_spads = 0; - } + } else { need_apt_spads = 0;} } if ((status == VL53L0X_ERROR_NONE) && @@ -3607,14 +3356,14 @@ is_aperture_spads_int = need_apt_spads; ref_spad_count_int = minimum_spad_count; - memcpy(last_spad_array,_device->SpadData.RefSpadEnables, + memcpy(last_spad_array,SpadData.RefSpadEnables, spad_array_size); last_signal_rate_diff = abs(peak_signal_rate_ref - target_ref_rate); complete = 0; while (!complete) { - get_next_good_spad(_device->SpadData.RefGoodSpadMap, + get_next_good_spad(SpadData.RefGoodSpadMap, spad_array_size,current_spad_index, &next_good_spad); @@ -3638,25 +3387,21 @@ (ref_spad_count_int)++; current_spad_index = next_good_spad; - status = enable_spad_bit(_device->SpadData.RefSpadEnables, + status = enable_spad_bit(SpadData.RefSpadEnables, spad_array_size,current_spad_index); if (status == VL53L0X_ERROR_NONE) { current_spad_index++; /* Proceed to apply the additional spad and * perform measurement. */ - status = set_ref_spad_map(_device->SpadData.RefSpadEnables); + status = set_ref_spad_map(SpadData.RefSpadEnables); } - if (status != VL53L0X_ERROR_NONE) { - break; - } + if (status != VL53L0X_ERROR_NONE) { break; } status = perform_ref_signal_measurement(&peak_signal_rate_ref); - if (status != VL53L0X_ERROR_NONE) { - break; - } + if (status != VL53L0X_ERROR_NONE) { break; } signal_rate_diff = abs(peak_signal_rate_ref - target_ref_rate); @@ -3669,7 +3414,7 @@ /* Previous spad map produced a closer * measurement,so choose this. */ status = set_ref_spad_map(last_spad_array); - memcpy(_device->SpadData.RefSpadEnables, + memcpy(SpadData.RefSpadEnables, last_spad_array,spad_array_size); (ref_spad_count_int)--; @@ -3679,7 +3424,7 @@ /* Continue to add spads */ last_signal_rate_diff = signal_rate_diff; memcpy(last_spad_array, - _device->SpadData.RefSpadEnables, + SpadData.RefSpadEnables, spad_array_size); } @@ -3689,10 +3434,10 @@ if (status == VL53L0X_ERROR_NONE) { *ref_spad_count = ref_spad_count_int; *is_aperture_spads = is_aperture_spads_int; - - _device->DeviceSpecificParameters.RefSpadsInitialised = 1; - _device->DeviceSpecificParameters.ReferenceSpadCount = (uint8_t)(*ref_spad_count); - _device->DeviceSpecificParameters.ReferenceSpadType = *is_aperture_spads; + + DevSpecParams.RefSpadsInitialised = 1; + DevSpecParams.ReferenceSpadCount = (uint8_t)(*ref_spad_count); + DevSpecParams.ReferenceSpadType = *is_aperture_spads; } return status; @@ -3714,7 +3459,6 @@ * non-aperture,as requested. * The good spad map will be applied. */ - status = VL53L0X_write_byte(0xFF,0x01); if (status == VL53L0X_ERROR_NONE) { @@ -3725,9 +3469,7 @@ status = VL53L0X_write_byte(VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD,0x2C); } - if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_write_byte(0xFF,0x00); - } + if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_write_byte(0xFF,0x00); } if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_write_byte(VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, @@ -3735,7 +3477,7 @@ } for (index = 0; index < spad_array_size; index++) { - _device->SpadData.RefSpadEnables[index] = 0; + SpadData.RefSpadEnables[index] = 0; } if (is_aperture_spads) { @@ -3746,8 +3488,8 @@ } } status = enable_ref_spads(is_aperture_spads, - _device->SpadData.RefGoodSpadMap, - _device->SpadData.RefSpadEnables, + SpadData.RefGoodSpadMap, + SpadData.RefSpadEnables, spad_array_size, start_select, current_spad_index, @@ -3755,48 +3497,21 @@ &last_spad_index); if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.RefSpadsInitialised = 1; - _device->DeviceSpecificParameters.ReferenceSpadCount = (uint8_t)(count); - _device->DeviceSpecificParameters.ReferenceSpadType = is_aperture_spads; + DevSpecParams.RefSpadsInitialised = 1; + DevSpecParams.ReferenceSpadCount = (uint8_t)(count); + DevSpecParams.ReferenceSpadType = is_aperture_spads; } return status; } -VL53L0X_Error VL53L0X::VL53L0X_wait_device_booted() -{ - VL53L0X_Error status = VL53L0X_ERROR_NOT_IMPLEMENTED; - - - /* not implemented on VL53L0X */ - - - return status; -} - -VL53L0X_Error VL53L0X::VL53L0X_perform_ref_calibration(uint8_t *p_vhv_settings, - uint8_t *p_phase_cal) -{ - VL53L0X_Error status = VL53L0X_ERROR_NONE; - - - status = VL53L0X_perform_ref_calibration(p_vhv_settings, - p_phase_cal,1); - - - return status; -} - VL53L0X_Error VL53L0X::VL53L0X_perform_ref_spad_management(uint32_t *ref_spad_count,uint8_t *is_aperture_spads) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = wrapped_VL53L0X_perform_ref_spad_management(ref_spad_count, is_aperture_spads); - - return status; } @@ -3805,11 +3520,8 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = VL53L0X_write_byte(VL53L0X_REG_I2C_SLAVE_DEVICE_ADDRESS, device_address / 2); - - return status; } @@ -3820,8 +3532,6 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t data; - - if (pin != 0) { status = VL53L0X_ERROR_GPIO_NOT_EXISTING; } else if (device_mode == VL53L0X_DEVICEMODE_GPIO_DRIVE) { @@ -3896,7 +3606,7 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.Pin0GpioFunctionality = functionality; + DevSpecParams.Pin0GpioFunctionality = functionality; } if (status == VL53L0X_ERROR_NONE) { @@ -3912,14 +3622,10 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_RANGE_CONFIG,p_enabled); - if (status == VL53L0X_ERROR_NONE) { - *p_enabled = (*p_enabled & 1); - } - - + if (status == VL53L0X_ERROR_NONE) {*p_enabled = (*p_enabled & 1);} + return status; } @@ -3941,8 +3647,7 @@ ms_byte++; } - encoded_timeout = (ms_byte << 8) - + (uint16_t)(ls_byte & 0x000000FF); + encoded_timeout = (ms_byte << 8) + (uint16_t)(ls_byte & 0x000000FF); } return encoded_timeout; @@ -3980,7 +3685,7 @@ (uint8_t)msrc_range_time_out_m_clks - 1; } - _device->DeviceSpecificParameters.LastEncodedTimeout = msrc_encoded_time_out; + DevSpecParams.LastEncodedTimeout = msrc_encoded_time_out; } if (status == VL53L0X_ERROR_NONE) { @@ -3999,7 +3704,7 @@ (uint8_t)current_vcsel_pulse_period_p_clk); pre_range_encoded_time_out = VL53L0X_encode_timeout(pre_range_time_out_m_clks); - _device->DeviceSpecificParameters.LastEncodedTimeout = pre_range_encoded_time_out; + DevSpecParams.LastEncodedTimeout = pre_range_encoded_time_out; } if (status == VL53L0X_ERROR_NONE) { @@ -4008,7 +3713,7 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.PreRangeTimeoutMicroSecs=timeout_micro_secs; + DevSpecParams.PreRangeTimeoutMicroSecs=timeout_micro_secs; } } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_FINAL_RANGE) { @@ -4059,45 +3764,43 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.FinalRangeTimeoutMicroSecs = timeout_micro_secs; + DevSpecParams.FinalRangeTimeoutMicroSecs = timeout_micro_secs; } } } else { status = VL53L0X_ERROR_INVALID_PARAMS; } - } return status; } -VL53L0X_Error VL53L0X::wrapped_VL53L0X_set_measurement_timing_budget_micro_seconds(uint32_t measurement_timing_budget_micro_seconds) +VL53L0X_Error VL53L0X::wrapped_VL53L0X_set_measurement_timing_budget_us + (uint32_t measurement_timing_budget_us) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - uint32_t final_range_timing_budget_micro_seconds; + uint32_t final_range_timing_budget_us; VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; - uint32_t msrc_dcc_tcc_timeout_micro_seconds = 2000; - uint32_t start_overhead_micro_seconds = 1910; - uint32_t end_overhead_micro_seconds = 960; - uint32_t msrc_overhead_micro_seconds = 660; - uint32_t tcc_overhead_micro_seconds = 590; - uint32_t dss_overhead_micro_seconds = 690; - uint32_t pre_range_overhead_micro_seconds = 660; - uint32_t final_range_overhead_micro_seconds = 550; - uint32_t pre_range_timeout_micro_seconds = 0; - uint32_t c_min_timing_budget_micro_seconds = 20000; + uint32_t msrc_dcc_tcc_timeout_us = 2000; + uint32_t start_overhead_us = 1910; + uint32_t end_overhead_us = 960; + uint32_t msrc_overhead_us = 660; + uint32_t tcc_overhead_us = 590; + uint32_t dss_overhead_us = 690; + uint32_t pre_range_overhead_us = 660; + uint32_t final_range_overhead_us = 550; + uint32_t pre_range_timeout_us = 0; + uint32_t c_min_timing_budget_us = 20000; uint32_t sub_timeout = 0; - - - if (measurement_timing_budget_micro_seconds - < c_min_timing_budget_micro_seconds) { + if (measurement_timing_budget_us + < c_min_timing_budget_us) { status = VL53L0X_ERROR_INVALID_PARAMS; return status; } - final_range_timing_budget_micro_seconds = - measurement_timing_budget_micro_seconds - - (start_overhead_micro_seconds + end_overhead_micro_seconds); + final_range_timing_budget_us = + measurement_timing_budget_us - + (start_overhead_us + end_overhead_us); status = VL53L0X_get_sequence_step_enables(&scheduler_sequence_steps); @@ -4108,24 +3811,22 @@ /* TCC,MSRC and DSS all share the same timeout */ status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_MSRC, - &msrc_dcc_tcc_timeout_micro_seconds); + &msrc_dcc_tcc_timeout_us); /* Subtract the TCC,MSRC and DSS timeouts if they are * enabled. */ - if (status != VL53L0X_ERROR_NONE) { - return status; - } + if (status != VL53L0X_ERROR_NONE) { return status; } /* TCC */ if (scheduler_sequence_steps.TccOn) { - sub_timeout = msrc_dcc_tcc_timeout_micro_seconds - + tcc_overhead_micro_seconds; + sub_timeout = msrc_dcc_tcc_timeout_us + + tcc_overhead_us; if (sub_timeout < - final_range_timing_budget_micro_seconds) { - final_range_timing_budget_micro_seconds -= + final_range_timing_budget_us) { + final_range_timing_budget_us -= sub_timeout; } else { /* Requested timeout too big. */ @@ -4133,19 +3834,16 @@ } } - if (status != VL53L0X_ERROR_NONE) { - - return status; - } + if (status != VL53L0X_ERROR_NONE) { return status; } /* DSS */ if (scheduler_sequence_steps.DssOn) { - sub_timeout = 2 * (msrc_dcc_tcc_timeout_micro_seconds + - dss_overhead_micro_seconds); - - if (sub_timeout < final_range_timing_budget_micro_seconds) { - final_range_timing_budget_micro_seconds + sub_timeout = 2 * (msrc_dcc_tcc_timeout_us + + dss_overhead_us); + + if (sub_timeout < final_range_timing_budget_us) { + final_range_timing_budget_us -= sub_timeout; } else { /* Requested timeout too big. */ @@ -4153,11 +3851,11 @@ } } else if (scheduler_sequence_steps.MsrcOn) { /* MSRC */ - sub_timeout = msrc_dcc_tcc_timeout_micro_seconds + - msrc_overhead_micro_seconds; - - if (sub_timeout < final_range_timing_budget_micro_seconds) { - final_range_timing_budget_micro_seconds + sub_timeout = msrc_dcc_tcc_timeout_us + + msrc_overhead_us; + + if (sub_timeout < final_range_timing_budget_us) { + final_range_timing_budget_us -= sub_timeout; } else { /* Requested timeout too big. */ @@ -4167,23 +3865,20 @@ } - if (status != VL53L0X_ERROR_NONE) { - - return status; - } + if (status != VL53L0X_ERROR_NONE) { return status; } if (scheduler_sequence_steps.PreRangeOn) { /* Subtract the Pre-range timeout if enabled. */ status = get_sequence_step_timeout(VL53L0X_SEQUENCESTEP_PRE_RANGE, - &pre_range_timeout_micro_seconds); - - sub_timeout = pre_range_timeout_micro_seconds + - pre_range_overhead_micro_seconds; - - if (sub_timeout < final_range_timing_budget_micro_seconds) { - final_range_timing_budget_micro_seconds -= sub_timeout; + &pre_range_timeout_us); + + sub_timeout = pre_range_timeout_us + + pre_range_overhead_us; + + if (sub_timeout < final_range_timing_budget_us) { + final_range_timing_budget_us -= sub_timeout; } else { /* Requested timeout too big. */ status = VL53L0X_ERROR_INVALID_PARAMS; @@ -4194,8 +3889,8 @@ if (status == VL53L0X_ERROR_NONE && scheduler_sequence_steps.FinalRangeOn) { - final_range_timing_budget_micro_seconds -= - final_range_overhead_micro_seconds; + final_range_timing_budget_us -= + final_range_overhead_us; /* Final Range Timeout * Note that the final range timeout is determined by the timing @@ -4205,22 +3900,19 @@ * the final range. */ status = set_sequence_step_timeout(VL53L0X_SEQUENCESTEP_FINAL_RANGE, - final_range_timing_budget_micro_seconds); - - _device->CurrentParameters.MeasurementTimingBudgetMicroSeconds = measurement_timing_budget_micro_seconds; + final_range_timing_budget_us); + + CurrentParameters.MeasurementTimingBudget_us = measurement_timing_budget_us; } - - return status; } -VL53L0X_Error VL53L0X::VL53L0X_set_measurement_timing_budget_micro_seconds(uint32_t measurement_timing_budget_micro_seconds) +VL53L0X_Error VL53L0X::VL53L0X_set_measurement_timing_budget_us(uint32_t measurement_timing_budget_us) { VL53L0X_Error status = VL53L0X_ERROR_NONE; - - status = wrapped_VL53L0X_set_measurement_timing_budget_micro_seconds(measurement_timing_budget_micro_seconds); + status = wrapped_VL53L0X_set_measurement_timing_budget_us(measurement_timing_budget_us); return status; } @@ -4230,11 +3922,9 @@ VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t sequence_config = 0; uint8_t sequence_config_new = 0; - uint32_t measurement_timing_budget_micro_seconds; - - - status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, - &sequence_config); + uint32_t measurement_timing_budget_us; + + status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, &sequence_config); sequence_config_new = sequence_config; @@ -4293,20 +3983,16 @@ status = VL53L0X_write_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,sequence_config_new); } if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = sequence_config_new; + SequenceConfig = sequence_config_new; } - /* Recalculate timing budget */ if (status == VL53L0X_ERROR_NONE) { - measurement_timing_budget_micro_seconds = _device->CurrentParameters.MeasurementTimingBudgetMicroSeconds; - - VL53L0X_set_measurement_timing_budget_micro_seconds(measurement_timing_budget_micro_seconds); + measurement_timing_budget_us = CurrentParameters.MeasurementTimingBudget_us; + VL53L0X_set_measurement_timing_budget_us(measurement_timing_budget_us); } } - - return status; } @@ -4319,8 +4005,6 @@ uint8_t limit_check_disable = 0; uint8_t temp8; - - if (limit_check_id >= VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS) { status = VL53L0X_ERROR_INVALID_PARAMS; } else { @@ -4330,7 +4014,7 @@ limit_check_disable = 1; } else { - temp_fix1616 = _device->CurrentParameters.LimitChecksValue[limit_check_id]; + temp_fix1616 = CurrentParameters.LimitChecksValue[limit_check_id]; limit_check_disable = 0; /* this to be sure to have either 0 or 1 */ limit_check_enable_int = 1; @@ -4340,7 +4024,7 @@ case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: /* internal computation: */ - _device->CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = limit_check_enable_int; + CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE] = limit_check_enable_int; break; @@ -4352,59 +4036,47 @@ break; case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: - /* internal computation: */ - _device->CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = limit_check_enable_int; - + CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP] = limit_check_enable_int; break; case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: - /* internal computation: */ - _device->CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = limit_check_enable_int; - + CurrentParameters.LimitChecksEnable[VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD] = limit_check_enable_int; break; case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC: - temp8 = (uint8_t)(limit_check_disable << 1); status = VL53L0X_update_byte(VL53L0X_REG_MSRC_CONFIG_CONTROL, 0xFE,temp8); - break; case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: - temp8 = (uint8_t)(limit_check_disable << 4); status = VL53L0X_update_byte(VL53L0X_REG_MSRC_CONFIG_CONTROL, 0xEF,temp8); - break; - default: status = VL53L0X_ERROR_INVALID_PARAMS; - } - } if (status == VL53L0X_ERROR_NONE) { if (limit_check_enable == 0) { - _device->CurrentParameters.LimitChecksEnable[limit_check_id] = 0; + CurrentParameters.LimitChecksEnable[limit_check_id] = 0; } else { - _device->CurrentParameters.LimitChecksEnable[limit_check_id] = 1; + CurrentParameters.LimitChecksEnable[limit_check_id] = 1; } } - return status; } VL53L0X_Error VL53L0X::VL53L0X_static_init() { VL53L0X_Error status = VL53L0X_ERROR_NONE; - VL53L0X_DeviceParameters_t current_parameters = {0}; + VL53L0X_DeviceParameters_t new_curr_parameters = {0}; uint8_t *p_tuning_setting_buffer; uint16_t tempword = 0; uint8_t tempbyte = 0; @@ -4416,13 +4088,11 @@ uint8_t vcsel_pulse_period_pclk; uint32_t seq_timeout_micro_secs; - - status = VL53L0X_get_info_from_device(1); /* set the ref spad from NVM */ - count = (uint32_t)_device->DeviceSpecificParameters.ReferenceSpadCount; - aperture_spads = _device->DeviceSpecificParameters.ReferenceSpadType; + count = (uint32_t)DevSpecParams.ReferenceSpadCount; + aperture_spads = DevSpecParams.ReferenceSpadType; /* NVM value invalid */ if ((aperture_spads > 1) || @@ -4439,10 +4109,10 @@ p_tuning_setting_buffer = DefaultTuningSettings; if (status == VL53L0X_ERROR_NONE) { - use_internal_tuning_settings = _device->UseInternalTuningSettings; + use_internal_tuning_settings = UseInternalTuningSettings; if (use_internal_tuning_settings == 0) { - p_tuning_setting_buffer = _device->pTuningSettingsPointer; + p_tuning_setting_buffer = pTuningSettingsPointer; } else { p_tuning_setting_buffer = DefaultTuningSettings; } @@ -4456,9 +4126,9 @@ /* Set interrupt config to new sample ready */ if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_set_gpio_config(0,0, - VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, - VL53L0X_INTERRUPTPOLARITY_LOW); + status = VL53L0X_set_gpio_config(0,0, + VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, + VL53L0X_INTERRUPTPOLARITY_LOW); } if (status == VL53L0X_ERROR_NONE) { @@ -4468,35 +4138,29 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.OscFrequencyMHz= + DevSpecParams.OscFrequencyMHz= VL53L0X_FIXPOINT412TOFIXPOINT1616(tempword); } /* After static init,some device parameters may be changed, * so update them */ if (status == VL53L0X_ERROR_NONE) { - status = VL53L0X_get_device_parameters(¤t_parameters); - } - + status = VL53L0X_get_device_parameters(&new_curr_parameters); } if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_get_fraction_enable(&tempbyte); if (status == VL53L0X_ERROR_NONE) { - _device->RangeFractionalEnable = tempbyte; - } - + RangeFractionalEnable = tempbyte; } } - if (status == VL53L0X_ERROR_NONE) { - _device->CurrentParameters = current_parameters; - } + if (status == VL53L0X_ERROR_NONE) { CurrentParameters = new_curr_parameters; } /* read the sequence config and save it */ if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_read_byte(VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,&tempbyte); if (status == VL53L0X_ERROR_NONE) { - _device->SequenceConfig = tempbyte; + SequenceConfig = tempbyte; } } @@ -4510,9 +4174,7 @@ } /* Set PAL State to standby */ - if (status == VL53L0X_ERROR_NONE) { - _device->PalState = VL53L0X_STATE_IDLE; - } + if (status == VL53L0X_ERROR_NONE) { PalState = VL53L0X_STATE_IDLE; } /* Store pre-range vcsel period */ if (status == VL53L0X_ERROR_NONE) { @@ -4522,7 +4184,7 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.PreRangeVcselPulsePeriod = vcsel_pulse_period_pclk; + DevSpecParams.PreRangeVcselPulsePeriod = vcsel_pulse_period_pclk; } /* Store final-range vcsel period */ @@ -4533,7 +4195,7 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.FinalRangeVcselPulsePeriod = vcsel_pulse_period_pclk; + DevSpecParams.FinalRangeVcselPulsePeriod = vcsel_pulse_period_pclk; } /* Store pre-range timeout */ @@ -4544,7 +4206,7 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.PreRangeTimeoutMicroSecs = seq_timeout_micro_secs; + DevSpecParams.PreRangeTimeoutMicroSecs = seq_timeout_micro_secs; } /* Store final-range timeout */ @@ -4555,9 +4217,8 @@ } if (status == VL53L0X_ERROR_NONE) { - _device->DeviceSpecificParameters.FinalRangeTimeoutMicroSecs = seq_timeout_micro_secs; + DevSpecParams.FinalRangeTimeoutMicroSecs = seq_timeout_micro_secs; } - return status; } @@ -4567,7 +4228,6 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; - status = VL53L0X_write_byte(VL53L0X_REG_SYSRANGE_START, VL53L0X_REG_SYSRANGE_MODE_SINGLESHOT); @@ -4579,14 +4239,13 @@ if (status == VL53L0X_ERROR_NONE) { /* Set PAL State to Idle */ - _device->PalState = VL53L0X_STATE_IDLE; + PalState = VL53L0X_STATE_IDLE; } /* Check if need to apply interrupt settings */ if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_check_and_load_interrupt_settings(0); } - return status; } @@ -4595,7 +4254,6 @@ { VL53L0X_Error status = VL53L0X_ERROR_NONE; uint8_t byte = 0; - status = VL53L0X_write_byte(0xFF,0x01); @@ -4613,12 +4271,11 @@ status = VL53L0X_write_byte(0x80,0x01); status = VL53L0X_write_byte(0xFF,0x01); status = VL53L0X_write_byte(0x00,0x00); - status = VL53L0X_write_byte(0x91,_device->StopVariable); + status = VL53L0X_write_byte(0x91,StopVariable); status = VL53L0X_write_byte(0x00,0x01); status = VL53L0X_write_byte(0xFF,0x00); status = VL53L0X_write_byte(0x80,0x00); } - return status; } @@ -4628,7 +4285,6 @@ VL53L0X_Error VL53L0X::VL53L0X_write_multi(uint8_t index,uint8_t *p_data,uint32_t count) { int status; - status = VL53L0X_i2c_write(index,p_data,(uint16_t)count); return status; } @@ -4650,7 +4306,6 @@ VL53L0X_Error VL53L0X::VL53L0X_write_byte(uint8_t index,uint8_t data) { int status; - status = VL53L0X_i2c_write(index,&data,1); return status; } @@ -4683,13 +4338,9 @@ VL53L0X_Error VL53L0X::VL53L0X_read_byte(uint8_t index,uint8_t *p_data) { int status; - status = VL53L0X_i2c_read(index,p_data,1); - if (status) { - return -1; - } - + if (status) { return -1; } return 0; } @@ -4699,9 +4350,7 @@ uint8_t buffer[2] = {0,0}; status = VL53L0X_i2c_read(index,buffer,2); - if (!status) { - *p_data = (buffer[0] << 8) + buffer[1]; - } + if (!status) { *p_data = (buffer[0] << 8) + buffer[1]; } return status; } @@ -4737,12 +4386,9 @@ uint16_t NumByteToWrite) { int ret; - - ret = _dev_i2c->i2c_write(p_data,_device->I2cDevAddr,RegisterAddr,NumByteToWrite); - - if (ret) { - return -1; - } + ret = _dev_i2c->i2c_write(p_data,I2cDevAddr,RegisterAddr,NumByteToWrite); + + if (ret) { return -1; } return 0; } @@ -4750,12 +4396,9 @@ uint16_t NumByteToRead) { int ret; - - ret = _dev_i2c->i2c_read(p_data,_device->I2cDevAddr,RegisterAddr,NumByteToRead); - - if (ret) { - return -1; - } + ret = _dev_i2c->i2c_read(p_data,I2cDevAddr,RegisterAddr,NumByteToRead); + + if (ret) { return -1; } return 0; } @@ -4765,9 +4408,7 @@ uint16_t rl_id = 0; status = VL53L0X_read_word(VL53L0X_REG_IDENTIFICATION_MODEL_ID,&rl_id); - if (rl_id == 0xEEAA) { - return status; - } + if (rl_id == 0xEEAA) { return status; } return -1; } @@ -4822,7 +4463,6 @@ if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) { status = VL53L0X_ERROR_TIME_OUT; } - } return status; @@ -4830,18 +4470,14 @@ int VL53L0X::init_sensor(uint8_t new_addr) -{ - int status; +{ int status; VL53L0X_off(); VL53L0X_on(); -// status=VL53L0X_WaitDeviceBooted(ice); -// if(status) -// printf("WaitDeviceBooted fail\n\r"); status = is_present(); if (!status) { - status = init(&_my_device); + status = VL53L0X_data_init(); if (status != VL53L0X_ERROR_NONE) { printf("Failed to init VL53L0X sensor!\n\r"); return status; @@ -4849,7 +4485,6 @@ // deduce silicon version status = VL53L0X_get_device_info(&_device_info); - status = prepare(); if (status != VL53L0X_ERROR_NONE) { printf("Failed to prepare VL53L0X!\n\r"); @@ -4879,7 +4514,6 @@ status = VL53L0X_set_gpio_config(0,VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, VL53L0X_INTERRUPTPOLARITY_HIGH); - if (!status) { attach_interrupt_measure_detection_irq(fptr); enable_interrupt_measure_detection_irq(); @@ -4898,7 +4532,6 @@ int VL53L0X::start_measurement(OperatingMode operating_mode,void (*fptr)(void)) { int Status = VL53L0X_ERROR_NONE; - uint8_t VhvSettings; uint8_t PhaseCal; // *** from mass market cube expansion v1.1,ranging with satellites. @@ -4987,32 +4620,31 @@ if (Status == VL53L0X_ERROR_NONE) { Status = VL53L0X_set_limit_check_value( - VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,signalLimit); + VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,signalLimit); } if (Status == VL53L0X_ERROR_NONE) { Status = VL53L0X_set_limit_check_value( - VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,sigmaLimit); + VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,sigmaLimit); } if (Status == VL53L0X_ERROR_NONE) { - Status = VL53L0X_set_measurement_timing_budget_micro_seconds(timingBudget); + Status = VL53L0X_set_measurement_timing_budget_us(timingBudget); } if (Status == VL53L0X_ERROR_NONE) { Status = VL53L0X_set_vcsel_pulse_period( - VL53L0X_VCSEL_PERIOD_PRE_RANGE,preRangeVcselPeriod); + VL53L0X_VCSEL_PERIOD_PRE_RANGE,preRangeVcselPeriod); } if (Status == VL53L0X_ERROR_NONE) { Status = VL53L0X_set_vcsel_pulse_period( - VL53L0X_VCSEL_PERIOD_FINAL_RANGE,finalRangeVcselPeriod); + VL53L0X_VCSEL_PERIOD_FINAL_RANGE,finalRangeVcselPeriod); } if (Status == VL53L0X_ERROR_NONE) { - Status = VL53L0X_perform_ref_calibration(&VhvSettings,&PhaseCal); + Status = VL53L0X_perform_ref_calibration(&VhvSettings,&PhaseCal,1); } - } if (operating_mode == range_continuous_polling) { @@ -5036,17 +4668,14 @@ int Status = VL53L0X_ERROR_NONE; if (operating_mode == range_single_shot_polling) { - Status = VL53L0X_perform_single_ranging_measurement(p_data); - } + Status = VL53L0X_perform_single_ranging_measurement(p_data); } if (operating_mode == range_continuous_polling) { if (Status == VL53L0X_ERROR_NONE) { - Status = VL53L0X_measurement_poll_for_completion(); - } + Status = VL53L0X_measurement_poll_for_completion(); } if (Status == VL53L0X_ERROR_NONE) { Status = VL53L0X_get_ranging_measurement_data(p_data); - // Clear the interrupt VL53L0X_clear_interrupt_mask(VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); VL53L0X_polling_delay(); @@ -5055,7 +4684,8 @@ if (operating_mode == range_continuous_interrupt) { Status = VL53L0X_get_ranging_measurement_data(p_data); - VL53L0X_clear_interrupt_mask(VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR | VL53L0X_REG_RESULT_INTERRUPT_STATUS); + VL53L0X_clear_interrupt_mask(VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR | + VL53L0X_REG_RESULT_INTERRUPT_STATUS); } return Status; @@ -5066,12 +4696,11 @@ { int status = VL53L0X_ERROR_NONE; - // don't need to stop for a singleshot range! - if (operating_mode == range_single_shot_polling) { - } - - if (operating_mode == range_continuous_interrupt || operating_mode == range_continuous_polling) { + if (operating_mode == range_single_shot_polling) { } + + if (operating_mode == range_continuous_interrupt || + operating_mode == range_continuous_polling) { // continuous mode if (status == VL53L0X_ERROR_NONE) { //printf("Call of VL53L0X_StopMeasurement\n"); @@ -5083,16 +4712,17 @@ status = wait_stop_completed(); } - if (status == VL53L0X_ERROR_NONE) + if (status == VL53L0X_ERROR_NONE) { status = VL53L0X_clear_interrupt_mask( - VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); + VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); } } return status; } -int VL53L0X::handle_irq(OperatingMode operating_mode,VL53L0X_RangingMeasurementData_t *data) +int VL53L0X::handle_irq(OperatingMode operating_mode, + VL53L0X_RangingMeasurementData_t *data) { int status; status = get_measurement(operating_mode,data);