The VL53L1CB proximity sensor, based on ST’s FlightSense™, Time-of-Flight technology.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2
Based on VL53L1 library, this is a library for the VL53L1CB ToF chip.
X_NUCLEO_COMMON/DevI2C/ToF_I2C.h@0:3ac96e360672, 2020-11-06 (annotated)
- Committer:
- charlesmn
- Date:
- Fri Nov 06 10:06:37 2020 +0000
- Revision:
- 0:3ac96e360672
Library for ST Vl53L1A1 time of flight sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:3ac96e360672 | 1 | /* Define to prevent from recursive inclusion --------------------------------*/ |
charlesmn | 0:3ac96e360672 | 2 | #ifndef __DEV_ToF_I2C_H |
charlesmn | 0:3ac96e360672 | 3 | #define __DEV_ToF_I2C_H |
charlesmn | 0:3ac96e360672 | 4 | |
charlesmn | 0:3ac96e360672 | 5 | /* Includes ------------------------------------------------------------------*/ |
charlesmn | 0:3ac96e360672 | 6 | |
charlesmn | 0:3ac96e360672 | 7 | #include "mbed.h" |
charlesmn | 0:3ac96e360672 | 8 | |
charlesmn | 0:3ac96e360672 | 9 | #include "pinmap.h" |
charlesmn | 0:3ac96e360672 | 10 | |
charlesmn | 0:3ac96e360672 | 11 | static int mutex =0; |
charlesmn | 0:3ac96e360672 | 12 | |
charlesmn | 0:3ac96e360672 | 13 | //Class replacing DevI2C class as it was not implementing a 16bit address registers |
charlesmn | 0:3ac96e360672 | 14 | class ToF_DevI2C : public I2C |
charlesmn | 0:3ac96e360672 | 15 | { |
charlesmn | 0:3ac96e360672 | 16 | public: |
charlesmn | 0:3ac96e360672 | 17 | /** Create a DevI2C Master interface, connected to the specified pins |
charlesmn | 0:3ac96e360672 | 18 | * |
charlesmn | 0:3ac96e360672 | 19 | * @param sda I2C data line pin |
charlesmn | 0:3ac96e360672 | 20 | * @param scl I2C clock line pin |
charlesmn | 0:3ac96e360672 | 21 | */ |
charlesmn | 0:3ac96e360672 | 22 | ToF_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) { printf ("hello\n");} |
charlesmn | 0:3ac96e360672 | 23 | |
charlesmn | 0:3ac96e360672 | 24 | /** |
charlesmn | 0:3ac96e360672 | 25 | * @brief Writes a buffer towards the I2C peripheral device. |
charlesmn | 0:3ac96e360672 | 26 | * @param pBuffer pointer to the byte-array data to send |
charlesmn | 0:3ac96e360672 | 27 | * @param DeviceAddr specifies the peripheral device slave address. |
charlesmn | 0:3ac96e360672 | 28 | * @param RegisterAddr specifies the internal address register |
charlesmn | 0:3ac96e360672 | 29 | * where to start writing to (must be correctly masked). |
charlesmn | 0:3ac96e360672 | 30 | * @param NumByteToWrite number of bytes to be written. |
charlesmn | 0:3ac96e360672 | 31 | * @retval 0 if ok, |
charlesmn | 0:3ac96e360672 | 32 | * @retval -1 if an I2C error has occured, or |
charlesmn | 0:3ac96e360672 | 33 | * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) |
charlesmn | 0:3ac96e360672 | 34 | * @note On some devices if NumByteToWrite is greater |
charlesmn | 0:3ac96e360672 | 35 | * than one, the RegisterAddr must be masked correctly! |
charlesmn | 0:3ac96e360672 | 36 | */ |
charlesmn | 0:3ac96e360672 | 37 | /* |
charlesmn | 0:3ac96e360672 | 38 | int v53l1x_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
charlesmn | 0:3ac96e360672 | 39 | uint16_t NumByteToWrite) { |
charlesmn | 0:3ac96e360672 | 40 | int ret; |
charlesmn | 0:3ac96e360672 | 41 | uint8_t tmp[TEMP_BUF_SIZE]; |
charlesmn | 0:3ac96e360672 | 42 | |
charlesmn | 0:3ac96e360672 | 43 | if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; |
charlesmn | 0:3ac96e360672 | 44 | |
charlesmn | 0:3ac96e360672 | 45 | // First, send device address. Then, send data and STOP condition |
charlesmn | 0:3ac96e360672 | 46 | tmp[0] = RegisterAddr >> 8; |
charlesmn | 0:3ac96e360672 | 47 | tmp[1] = RegisterAddr & 0x0FF; |
charlesmn | 0:3ac96e360672 | 48 | memcpy(tmp+2, pBuffer, NumByteToWrite); |
charlesmn | 0:3ac96e360672 | 49 | while ( mutex ) |
charlesmn | 0:3ac96e360672 | 50 | { |
charlesmn | 0:3ac96e360672 | 51 | wait_ms(1); |
charlesmn | 0:3ac96e360672 | 52 | } |
charlesmn | 0:3ac96e360672 | 53 | mutex = 1; |
charlesmn | 0:3ac96e360672 | 54 | |
charlesmn | 0:3ac96e360672 | 55 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false); |
charlesmn | 0:3ac96e360672 | 56 | mutex = 0; |
charlesmn | 0:3ac96e360672 | 57 | if(ret) |
charlesmn | 0:3ac96e360672 | 58 | { |
charlesmn | 0:3ac96e360672 | 59 | wait_ms(1); |
charlesmn | 0:3ac96e360672 | 60 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false); |
charlesmn | 0:3ac96e360672 | 61 | if(ret) |
charlesmn | 0:3ac96e360672 | 62 | { |
charlesmn | 0:3ac96e360672 | 63 | printf("***************** v53l1x_i2c_write twice times %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToWrite,ret); |
charlesmn | 0:3ac96e360672 | 64 | } |
charlesmn | 0:3ac96e360672 | 65 | // printf("v53l1x_i2c_write %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToWrite,ret); |
charlesmn | 0:3ac96e360672 | 66 | return -1; |
charlesmn | 0:3ac96e360672 | 67 | } |
charlesmn | 0:3ac96e360672 | 68 | return 0; |
charlesmn | 0:3ac96e360672 | 69 | } |
charlesmn | 0:3ac96e360672 | 70 | */ |
charlesmn | 0:3ac96e360672 | 71 | |
charlesmn | 0:3ac96e360672 | 72 | /** |
charlesmn | 0:3ac96e360672 | 73 | * @brief Reads a buffer from the I2C peripheral device. |
charlesmn | 0:3ac96e360672 | 74 | * @param pBuffer pointer to the byte-array to read data in to |
charlesmn | 0:3ac96e360672 | 75 | * @param DeviceAddr specifies the peripheral device slave address. |
charlesmn | 0:3ac96e360672 | 76 | * @param RegisterAddr specifies the internal address register |
charlesmn | 0:3ac96e360672 | 77 | * where to start reading from (must be correctly masked). |
charlesmn | 0:3ac96e360672 | 78 | * @param NumByteToRead number of bytes to be read. |
charlesmn | 0:3ac96e360672 | 79 | * @retval 0 if ok, |
charlesmn | 0:3ac96e360672 | 80 | * @retval -1 if an I2C error has occured |
charlesmn | 0:3ac96e360672 | 81 | * @note On some devices if NumByteToWrite is greater |
charlesmn | 0:3ac96e360672 | 82 | * than one, the RegisterAddr must be masked correctly! |
charlesmn | 0:3ac96e360672 | 83 | */ |
charlesmn | 0:3ac96e360672 | 84 | /* |
charlesmn | 0:3ac96e360672 | 85 | int v53l1x_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
charlesmn | 0:3ac96e360672 | 86 | uint16_t NumByteToRead) { |
charlesmn | 0:3ac96e360672 | 87 | int ret; |
charlesmn | 0:3ac96e360672 | 88 | |
charlesmn | 0:3ac96e360672 | 89 | |
charlesmn | 0:3ac96e360672 | 90 | // Read data, with STOP condition |
charlesmn | 0:3ac96e360672 | 91 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); |
charlesmn | 0:3ac96e360672 | 92 | |
charlesmn | 0:3ac96e360672 | 93 | // printf("class v53l1x_i2c_read read %d %d %d\n",pBuffer[0],pBuffer[1],pBuffer[2]); |
charlesmn | 0:3ac96e360672 | 94 | |
charlesmn | 0:3ac96e360672 | 95 | |
charlesmn | 0:3ac96e360672 | 96 | if(ret) |
charlesmn | 0:3ac96e360672 | 97 | { |
charlesmn | 0:3ac96e360672 | 98 | printf("********************* class v53l1x_i2c_read %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToRead,ret); |
charlesmn | 0:3ac96e360672 | 99 | return -1; |
charlesmn | 0:3ac96e360672 | 100 | } |
charlesmn | 0:3ac96e360672 | 101 | return 0; |
charlesmn | 0:3ac96e360672 | 102 | } |
charlesmn | 0:3ac96e360672 | 103 | */ |
charlesmn | 0:3ac96e360672 | 104 | |
charlesmn | 0:3ac96e360672 | 105 | int ToF_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
charlesmn | 0:3ac96e360672 | 106 | uint16_t NumByteToWrite) { |
charlesmn | 0:3ac96e360672 | 107 | int ret; |
charlesmn | 0:3ac96e360672 | 108 | uint8_t tmp[TEMP_BUF_SIZE]; |
charlesmn | 0:3ac96e360672 | 109 | |
charlesmn | 0:3ac96e360672 | 110 | if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; |
charlesmn | 0:3ac96e360672 | 111 | |
charlesmn | 0:3ac96e360672 | 112 | // First, send device address. Then, send data and STOP condition |
charlesmn | 0:3ac96e360672 | 113 | tmp[0] = RegisterAddr >> 8; |
charlesmn | 0:3ac96e360672 | 114 | tmp[1] = RegisterAddr & 0x0FF; |
charlesmn | 0:3ac96e360672 | 115 | memcpy(tmp+2, pBuffer, NumByteToWrite); |
charlesmn | 0:3ac96e360672 | 116 | |
charlesmn | 0:3ac96e360672 | 117 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false); |
charlesmn | 0:3ac96e360672 | 118 | |
charlesmn | 0:3ac96e360672 | 119 | if(ret) return -1; |
charlesmn | 0:3ac96e360672 | 120 | return 0; |
charlesmn | 0:3ac96e360672 | 121 | } |
charlesmn | 0:3ac96e360672 | 122 | |
charlesmn | 0:3ac96e360672 | 123 | /** |
charlesmn | 0:3ac96e360672 | 124 | * @brief Reads a buffer from the I2C peripheral device. |
charlesmn | 0:3ac96e360672 | 125 | * @param pBuffer pointer to the byte-array to read data in to |
charlesmn | 0:3ac96e360672 | 126 | * @param DeviceAddr specifies the peripheral device slave address. |
charlesmn | 0:3ac96e360672 | 127 | * @param RegisterAddr specifies the internal address register |
charlesmn | 0:3ac96e360672 | 128 | * where to start reading from (must be correctly masked). |
charlesmn | 0:3ac96e360672 | 129 | * @param NumByteToRead number of bytes to be read. |
charlesmn | 0:3ac96e360672 | 130 | * @retval 0 if ok, |
charlesmn | 0:3ac96e360672 | 131 | * @retval -1 if an I2C error has occured |
charlesmn | 0:3ac96e360672 | 132 | * @note On some devices if NumByteToWrite is greater |
charlesmn | 0:3ac96e360672 | 133 | * than one, the RegisterAddr must be masked correctly! |
charlesmn | 0:3ac96e360672 | 134 | */ |
charlesmn | 0:3ac96e360672 | 135 | int ToF_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
charlesmn | 0:3ac96e360672 | 136 | uint16_t NumByteToRead) { |
charlesmn | 0:3ac96e360672 | 137 | int ret; |
charlesmn | 0:3ac96e360672 | 138 | uint8_t ExpanderData[2]; |
charlesmn | 0:3ac96e360672 | 139 | ExpanderData[0] = RegisterAddr >> 8; |
charlesmn | 0:3ac96e360672 | 140 | ExpanderData[1] = RegisterAddr & 0x0FF; |
charlesmn | 0:3ac96e360672 | 141 | /* Send device address, with no STOP condition */ |
charlesmn | 0:3ac96e360672 | 142 | ret = write(DeviceAddr, (const char*)ExpanderData, 2, true); |
charlesmn | 0:3ac96e360672 | 143 | if(!ret) { |
charlesmn | 0:3ac96e360672 | 144 | /* Read data, with STOP condition */ |
charlesmn | 0:3ac96e360672 | 145 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); |
charlesmn | 0:3ac96e360672 | 146 | } |
charlesmn | 0:3ac96e360672 | 147 | |
charlesmn | 0:3ac96e360672 | 148 | if(ret) return -1; |
charlesmn | 0:3ac96e360672 | 149 | return 0; |
charlesmn | 0:3ac96e360672 | 150 | } |
charlesmn | 0:3ac96e360672 | 151 | |
charlesmn | 0:3ac96e360672 | 152 | private: |
charlesmn | 0:3ac96e360672 | 153 | static const unsigned int TEMP_BUF_SIZE = 256; |
charlesmn | 0:3ac96e360672 | 154 | }; |
charlesmn | 0:3ac96e360672 | 155 | |
charlesmn | 0:3ac96e360672 | 156 | #endif /* __DEV_53L1X_I2C_H */ |