Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: capteur_cdf asservissement_robot2_Homologued_real
VL53L0X.h@2:022507164b8e, 2019-04-27 (annotated)
- Committer:
- CharlesBl
- Date:
- Sat Apr 27 18:40:01 2019 +0000
- Revision:
- 2:022507164b8e
- Parent:
- 1:0ea6ac47f1ca
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| CharlesBl | 0:cf752e491870 | 1 | #ifndef VL53L0X_H |
| CharlesBl | 0:cf752e491870 | 2 | #define VL53L0X_H |
| CharlesBl | 0:cf752e491870 | 3 | |
| CharlesBl | 0:cf752e491870 | 4 | #include "mbed.h" |
| CharlesBl | 0:cf752e491870 | 5 | |
| CharlesBl | 0:cf752e491870 | 6 | |
| CharlesBl | 2:022507164b8e | 7 | // Adresse I2C par défaut du capteur |
| CharlesBl | 2:022507164b8e | 8 | #define ADDRESS_DEFAULT 0b0101001 |
| CharlesBl | 2:022507164b8e | 9 | |
| CharlesBl | 2:022507164b8e | 10 | |
| CharlesBl | 0:cf752e491870 | 11 | /** Classe VL53L0X |
| CharlesBl | 0:cf752e491870 | 12 | * Librairie qui interface le capteur ToF VL53L0X. |
| CharlesBl | 0:cf752e491870 | 13 | * Sert à mesurer des distances. |
| CharlesBl | 0:cf752e491870 | 14 | * |
| CharlesBl | 0:cf752e491870 | 15 | * Source: https://os.mbed.com/users/open4416/code/VL53L0X_STM32compatible/ |
| CharlesBl | 0:cf752e491870 | 16 | * Adapté par: Charles Blanchard |
| CharlesBl | 0:cf752e491870 | 17 | * |
| CharlesBl | 0:cf752e491870 | 18 | * Datasheet: https://www.st.com/resource/en/datasheet/vl53l0x.pdf |
| CharlesBl | 0:cf752e491870 | 19 | * Cible: STM32-F401RE |
| CharlesBl | 0:cf752e491870 | 20 | * |
| CharlesBl | 0:cf752e491870 | 21 | * Example: |
| CharlesBl | 0:cf752e491870 | 22 | * @code |
| CharlesBl | 0:cf752e491870 | 23 | * #include "mbed.h" |
| CharlesBl | 0:cf752e491870 | 24 | * #include "VL53L0X.h" |
| CharlesBl | 0:cf752e491870 | 25 | * |
| CharlesBl | 0:cf752e491870 | 26 | * #define SDA_pin D14 |
| CharlesBl | 0:cf752e491870 | 27 | * #define SCL_pin D15 |
| CharlesBl | 0:cf752e491870 | 28 | * |
| CharlesBl | 0:cf752e491870 | 29 | * VL53L0X sensor(SDA_pin,SCL_pin); |
| CharlesBl | 0:cf752e491870 | 30 | * |
| CharlesBl | 0:cf752e491870 | 31 | * Serial pc(SERIAL_TX, SERIAL_RX); //9600 bauds |
| CharlesBl | 0:cf752e491870 | 32 | * |
| CharlesBl | 0:cf752e491870 | 33 | * int main() |
| CharlesBl | 0:cf752e491870 | 34 | * { |
| CharlesBl | 0:cf752e491870 | 35 | * uint32_t distance; |
| CharlesBl | 0:cf752e491870 | 36 | * |
| CharlesBl | 0:cf752e491870 | 37 | * sensor.init(); //init SENSOR |
| CharlesBl | 0:cf752e491870 | 38 | * sensor.setTimeout(500); |
| CharlesBl | 0:cf752e491870 | 39 | * sensor.startContinuous(); |
| CharlesBl | 0:cf752e491870 | 40 | * |
| CharlesBl | 0:cf752e491870 | 41 | * while(1) { |
| CharlesBl | 0:cf752e491870 | 42 | * wait_ms(100); |
| CharlesBl | 0:cf752e491870 | 43 | * distance = sensor.readRangeContinuousMillimeters(); |
| CharlesBl | 0:cf752e491870 | 44 | * pc.printf("%d\r\n", distance); |
| CharlesBl | 0:cf752e491870 | 45 | * } |
| CharlesBl | 0:cf752e491870 | 46 | * } |
| CharlesBl | 0:cf752e491870 | 47 | * @endcode |
| CharlesBl | 0:cf752e491870 | 48 | */ |
| CharlesBl | 0:cf752e491870 | 49 | |
| CharlesBl | 0:cf752e491870 | 50 | class VL53L0X |
| CharlesBl | 0:cf752e491870 | 51 | { |
| CharlesBl | 0:cf752e491870 | 52 | public: |
| CharlesBl | 0:cf752e491870 | 53 | // register addresses from API vl53l0x_device.h (ordered as listed there) |
| CharlesBl | 0:cf752e491870 | 54 | enum regAddr { |
| CharlesBl | 0:cf752e491870 | 55 | SYSRANGE_START = 0x00, |
| CharlesBl | 0:cf752e491870 | 56 | |
| CharlesBl | 0:cf752e491870 | 57 | SYSTEM_THRESH_HIGH = 0x0C, |
| CharlesBl | 0:cf752e491870 | 58 | SYSTEM_THRESH_LOW = 0x0E, |
| CharlesBl | 0:cf752e491870 | 59 | |
| CharlesBl | 0:cf752e491870 | 60 | SYSTEM_SEQUENCE_CONFIG = 0x01, |
| CharlesBl | 0:cf752e491870 | 61 | SYSTEM_RANGE_CONFIG = 0x09, |
| CharlesBl | 0:cf752e491870 | 62 | SYSTEM_INTERMEASUREMENT_PERIOD = 0x04, |
| CharlesBl | 0:cf752e491870 | 63 | |
| CharlesBl | 0:cf752e491870 | 64 | SYSTEM_INTERRUPT_CONFIG_GPIO = 0x0A, |
| CharlesBl | 0:cf752e491870 | 65 | |
| CharlesBl | 0:cf752e491870 | 66 | GPIO_HV_MUX_ACTIVE_HIGH = 0x84, |
| CharlesBl | 0:cf752e491870 | 67 | |
| CharlesBl | 0:cf752e491870 | 68 | SYSTEM_INTERRUPT_CLEAR = 0x0B, |
| CharlesBl | 0:cf752e491870 | 69 | |
| CharlesBl | 0:cf752e491870 | 70 | RESULT_INTERRUPT_STATUS = 0x13, |
| CharlesBl | 0:cf752e491870 | 71 | RESULT_RANGE_STATUS = 0x14, |
| CharlesBl | 0:cf752e491870 | 72 | |
| CharlesBl | 0:cf752e491870 | 73 | RESULT_CORE_AMBIENT_WINDOW_EVENTS_RTN = 0xBC, |
| CharlesBl | 0:cf752e491870 | 74 | RESULT_CORE_RANGING_TOTAL_EVENTS_RTN = 0xC0, |
| CharlesBl | 0:cf752e491870 | 75 | RESULT_CORE_AMBIENT_WINDOW_EVENTS_REF = 0xD0, |
| CharlesBl | 0:cf752e491870 | 76 | RESULT_CORE_RANGING_TOTAL_EVENTS_REF = 0xD4, |
| CharlesBl | 0:cf752e491870 | 77 | RESULT_PEAK_SIGNAL_RATE_REF = 0xB6, |
| CharlesBl | 0:cf752e491870 | 78 | |
| CharlesBl | 0:cf752e491870 | 79 | ALGO_PART_TO_PART_RANGE_OFFSET_MM = 0x28, |
| CharlesBl | 0:cf752e491870 | 80 | |
| CharlesBl | 0:cf752e491870 | 81 | I2C_SLAVE_DEVICE_ADDRESS = 0x8A, |
| CharlesBl | 0:cf752e491870 | 82 | |
| CharlesBl | 0:cf752e491870 | 83 | MSRC_CONFIG_CONTROL = 0x60, |
| CharlesBl | 0:cf752e491870 | 84 | |
| CharlesBl | 0:cf752e491870 | 85 | PRE_RANGE_CONFIG_MIN_SNR = 0x27, |
| CharlesBl | 0:cf752e491870 | 86 | PRE_RANGE_CONFIG_VALID_PHASE_LOW = 0x56, |
| CharlesBl | 0:cf752e491870 | 87 | PRE_RANGE_CONFIG_VALID_PHASE_HIGH = 0x57, |
| CharlesBl | 0:cf752e491870 | 88 | PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT = 0x64, |
| CharlesBl | 0:cf752e491870 | 89 | |
| CharlesBl | 0:cf752e491870 | 90 | FINAL_RANGE_CONFIG_MIN_SNR = 0x67, |
| CharlesBl | 0:cf752e491870 | 91 | FINAL_RANGE_CONFIG_VALID_PHASE_LOW = 0x47, |
| CharlesBl | 0:cf752e491870 | 92 | FINAL_RANGE_CONFIG_VALID_PHASE_HIGH = 0x48, |
| CharlesBl | 0:cf752e491870 | 93 | FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT = 0x44, |
| CharlesBl | 0:cf752e491870 | 94 | |
| CharlesBl | 0:cf752e491870 | 95 | PRE_RANGE_CONFIG_SIGMA_THRESH_HI = 0x61, |
| CharlesBl | 0:cf752e491870 | 96 | PRE_RANGE_CONFIG_SIGMA_THRESH_LO = 0x62, |
| CharlesBl | 0:cf752e491870 | 97 | |
| CharlesBl | 0:cf752e491870 | 98 | PRE_RANGE_CONFIG_VCSEL_PERIOD = 0x50, |
| CharlesBl | 0:cf752e491870 | 99 | PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI = 0x51, |
| CharlesBl | 0:cf752e491870 | 100 | PRE_RANGE_CONFIG_TIMEOUT_MACROP_LO = 0x52, |
| CharlesBl | 0:cf752e491870 | 101 | |
| CharlesBl | 0:cf752e491870 | 102 | SYSTEM_HISTOGRAM_BIN = 0x81, |
| CharlesBl | 0:cf752e491870 | 103 | HISTOGRAM_CONFIG_INITIAL_PHASE_SELECT = 0x33, |
| CharlesBl | 0:cf752e491870 | 104 | HISTOGRAM_CONFIG_READOUT_CTRL = 0x55, |
| CharlesBl | 0:cf752e491870 | 105 | |
| CharlesBl | 0:cf752e491870 | 106 | FINAL_RANGE_CONFIG_VCSEL_PERIOD = 0x70, |
| CharlesBl | 0:cf752e491870 | 107 | FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI = 0x71, |
| CharlesBl | 0:cf752e491870 | 108 | FINAL_RANGE_CONFIG_TIMEOUT_MACROP_LO = 0x72, |
| CharlesBl | 0:cf752e491870 | 109 | CROSSTALK_COMPENSATION_PEAK_RATE_MCPS = 0x20, |
| CharlesBl | 0:cf752e491870 | 110 | |
| CharlesBl | 0:cf752e491870 | 111 | MSRC_CONFIG_TIMEOUT_MACROP = 0x46, |
| CharlesBl | 0:cf752e491870 | 112 | |
| CharlesBl | 0:cf752e491870 | 113 | SOFT_RESET_GO2_SOFT_RESET_N = 0xBF, |
| CharlesBl | 0:cf752e491870 | 114 | IDENTIFICATION_MODEL_ID = 0xC0, |
| CharlesBl | 0:cf752e491870 | 115 | IDENTIFICATION_REVISION_ID = 0xC2, |
| CharlesBl | 0:cf752e491870 | 116 | |
| CharlesBl | 0:cf752e491870 | 117 | OSC_CALIBRATE_VAL = 0xF8, |
| CharlesBl | 0:cf752e491870 | 118 | |
| CharlesBl | 0:cf752e491870 | 119 | GLOBAL_CONFIG_VCSEL_WIDTH = 0x32, |
| CharlesBl | 0:cf752e491870 | 120 | GLOBAL_CONFIG_SPAD_ENABLES_REF_0 = 0xB0, |
| CharlesBl | 0:cf752e491870 | 121 | GLOBAL_CONFIG_SPAD_ENABLES_REF_1 = 0xB1, |
| CharlesBl | 0:cf752e491870 | 122 | GLOBAL_CONFIG_SPAD_ENABLES_REF_2 = 0xB2, |
| CharlesBl | 0:cf752e491870 | 123 | GLOBAL_CONFIG_SPAD_ENABLES_REF_3 = 0xB3, |
| CharlesBl | 0:cf752e491870 | 124 | GLOBAL_CONFIG_SPAD_ENABLES_REF_4 = 0xB4, |
| CharlesBl | 0:cf752e491870 | 125 | GLOBAL_CONFIG_SPAD_ENABLES_REF_5 = 0xB5, |
| CharlesBl | 0:cf752e491870 | 126 | |
| CharlesBl | 0:cf752e491870 | 127 | GLOBAL_CONFIG_REF_EN_START_SELECT = 0xB6, |
| CharlesBl | 0:cf752e491870 | 128 | DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD = 0x4E, |
| CharlesBl | 0:cf752e491870 | 129 | DYNAMIC_SPAD_REF_EN_START_OFFSET = 0x4F, |
| CharlesBl | 0:cf752e491870 | 130 | POWER_MANAGEMENT_GO1_POWER_FORCE = 0x80, |
| CharlesBl | 0:cf752e491870 | 131 | |
| CharlesBl | 0:cf752e491870 | 132 | VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV = 0x89, |
| CharlesBl | 0:cf752e491870 | 133 | |
| CharlesBl | 0:cf752e491870 | 134 | ALGO_PHASECAL_LIM = 0x30, |
| CharlesBl | 0:cf752e491870 | 135 | ALGO_PHASECAL_CONFIG_TIMEOUT = 0x30, |
| CharlesBl | 0:cf752e491870 | 136 | }; |
| CharlesBl | 0:cf752e491870 | 137 | |
| CharlesBl | 0:cf752e491870 | 138 | enum vcselPeriodType { VcselPeriodPreRange, VcselPeriodFinalRange }; |
| CharlesBl | 0:cf752e491870 | 139 | |
| CharlesBl | 0:cf752e491870 | 140 | char last_status; // status of last I2C transmission |
| CharlesBl | 0:cf752e491870 | 141 | |
| CharlesBl | 0:cf752e491870 | 142 | VL53L0X(PinName i2c_sda, PinName i2c_scl); |
| CharlesBl | 0:cf752e491870 | 143 | |
| CharlesBl | 0:cf752e491870 | 144 | void setAddress(char new_addr); |
| CharlesBl | 0:cf752e491870 | 145 | inline char getAddress(void) |
| CharlesBl | 0:cf752e491870 | 146 | { |
| CharlesBl | 0:cf752e491870 | 147 | return address; |
| CharlesBl | 0:cf752e491870 | 148 | } |
| CharlesBl | 0:cf752e491870 | 149 | |
| CharlesBl | 0:cf752e491870 | 150 | bool init(bool io_2v8 = true); |
| CharlesBl | 0:cf752e491870 | 151 | |
| CharlesBl | 0:cf752e491870 | 152 | void writeReg(char reg, char value); |
| CharlesBl | 0:cf752e491870 | 153 | void writeReg16Bit(char reg, short value); |
| CharlesBl | 0:cf752e491870 | 154 | void writeReg32Bit(char reg, long value); |
| CharlesBl | 0:cf752e491870 | 155 | char readReg(char reg); |
| CharlesBl | 0:cf752e491870 | 156 | short readReg16Bit(char reg); |
| CharlesBl | 0:cf752e491870 | 157 | long readReg32Bit(char reg); |
| CharlesBl | 0:cf752e491870 | 158 | |
| CharlesBl | 0:cf752e491870 | 159 | void writeMulti(char reg, char const * src, char count); |
| CharlesBl | 0:cf752e491870 | 160 | void readMulti(char reg, char * dst, char count); |
| CharlesBl | 0:cf752e491870 | 161 | |
| CharlesBl | 0:cf752e491870 | 162 | bool setSignalRateLimit(float limit_Mcps); |
| CharlesBl | 0:cf752e491870 | 163 | float getSignalRateLimit(void); |
| CharlesBl | 0:cf752e491870 | 164 | |
| CharlesBl | 0:cf752e491870 | 165 | bool setMeasurementTimingBudget(long budget_us); |
| CharlesBl | 0:cf752e491870 | 166 | long getMeasurementTimingBudget(void); |
| CharlesBl | 0:cf752e491870 | 167 | |
| CharlesBl | 0:cf752e491870 | 168 | bool setVcselPulsePeriod(vcselPeriodType type, char period_pclks); |
| CharlesBl | 0:cf752e491870 | 169 | char getVcselPulsePeriod(vcselPeriodType type); |
| CharlesBl | 0:cf752e491870 | 170 | |
| CharlesBl | 0:cf752e491870 | 171 | void startContinuous(long period_ms = 0); |
| CharlesBl | 0:cf752e491870 | 172 | void stopContinuous(void); |
| CharlesBl | 0:cf752e491870 | 173 | short readRangeContinuousMillimeters(void); |
| CharlesBl | 0:cf752e491870 | 174 | short readRangeSingleMillimeters(void); |
| CharlesBl | 0:cf752e491870 | 175 | |
| CharlesBl | 0:cf752e491870 | 176 | inline void setTimeout(short timeout) |
| CharlesBl | 0:cf752e491870 | 177 | { |
| CharlesBl | 0:cf752e491870 | 178 | io_timeout = timeout; |
| CharlesBl | 0:cf752e491870 | 179 | } |
| CharlesBl | 0:cf752e491870 | 180 | inline short getTimeout(void) |
| CharlesBl | 0:cf752e491870 | 181 | { |
| CharlesBl | 0:cf752e491870 | 182 | return io_timeout; |
| CharlesBl | 0:cf752e491870 | 183 | } |
| CharlesBl | 0:cf752e491870 | 184 | bool timeoutOccurred(void); |
| CharlesBl | 0:cf752e491870 | 185 | |
| CharlesBl | 0:cf752e491870 | 186 | private: |
| CharlesBl | 0:cf752e491870 | 187 | // TCC: Target CentreCheck |
| CharlesBl | 0:cf752e491870 | 188 | // MSRC: Minimum Signal Rate Check |
| CharlesBl | 0:cf752e491870 | 189 | // DSS: Dynamic Spad Selection |
| CharlesBl | 0:cf752e491870 | 190 | |
| CharlesBl | 0:cf752e491870 | 191 | I2C i2c; |
| CharlesBl | 0:cf752e491870 | 192 | |
| CharlesBl | 0:cf752e491870 | 193 | struct SequenceStepEnables { |
| CharlesBl | 0:cf752e491870 | 194 | bool tcc, msrc, dss, pre_range, final_range; |
| CharlesBl | 0:cf752e491870 | 195 | }; |
| CharlesBl | 0:cf752e491870 | 196 | |
| CharlesBl | 0:cf752e491870 | 197 | struct SequenceStepTimeouts { |
| CharlesBl | 0:cf752e491870 | 198 | short pre_range_vcsel_period_pclks, final_range_vcsel_period_pclks; |
| CharlesBl | 0:cf752e491870 | 199 | |
| CharlesBl | 0:cf752e491870 | 200 | short msrc_dss_tcc_mclks, pre_range_mclks, final_range_mclks; |
| CharlesBl | 0:cf752e491870 | 201 | long msrc_dss_tcc_us, pre_range_us, final_range_us; |
| CharlesBl | 0:cf752e491870 | 202 | }; |
| CharlesBl | 0:cf752e491870 | 203 | |
| CharlesBl | 0:cf752e491870 | 204 | char data_w_2[2]; //buff for write |
| CharlesBl | 0:cf752e491870 | 205 | char data_w_3[3]; //buff for write |
| CharlesBl | 0:cf752e491870 | 206 | char data_w_5[5]; //buff for write |
| CharlesBl | 0:cf752e491870 | 207 | char data_r_1[1]; //buff for read |
| CharlesBl | 0:cf752e491870 | 208 | char data_r_2[2]; //buff for read |
| CharlesBl | 0:cf752e491870 | 209 | char data_r_4[4]; //buff for read |
| CharlesBl | 0:cf752e491870 | 210 | char address; |
| CharlesBl | 0:cf752e491870 | 211 | short io_timeout; |
| CharlesBl | 0:cf752e491870 | 212 | bool did_timeout; |
| CharlesBl | 0:cf752e491870 | 213 | short timeout_start_ms; |
| CharlesBl | 0:cf752e491870 | 214 | |
| CharlesBl | 0:cf752e491870 | 215 | char stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API |
| CharlesBl | 0:cf752e491870 | 216 | long measurement_timing_budget_us; |
| CharlesBl | 0:cf752e491870 | 217 | |
| CharlesBl | 0:cf752e491870 | 218 | bool getSpadInfo(char * count, bool * type_is_aperture); |
| CharlesBl | 0:cf752e491870 | 219 | |
| CharlesBl | 0:cf752e491870 | 220 | void getSequenceStepEnables(SequenceStepEnables * enables); |
| CharlesBl | 0:cf752e491870 | 221 | void getSequenceStepTimeouts(SequenceStepEnables const * enables, SequenceStepTimeouts * timeouts); |
| CharlesBl | 0:cf752e491870 | 222 | |
| CharlesBl | 0:cf752e491870 | 223 | bool performSingleRefCalibration(char vhv_init_byte); |
| CharlesBl | 0:cf752e491870 | 224 | |
| CharlesBl | 0:cf752e491870 | 225 | static short decodeTimeout(short value); |
| CharlesBl | 0:cf752e491870 | 226 | static short encodeTimeout(short timeout_mclks); |
| CharlesBl | 0:cf752e491870 | 227 | static long timeoutMclksToMicroseconds(short timeout_period_mclks, char vcsel_period_pclks); |
| CharlesBl | 0:cf752e491870 | 228 | static long timeoutMicrosecondsToMclks(long timeout_period_us, char vcsel_period_pclks); |
| CharlesBl | 0:cf752e491870 | 229 | }; |
| CharlesBl | 0:cf752e491870 | 230 | |
| CharlesBl | 0:cf752e491870 | 231 | #endif |