VL53L1X sensor class, for ARM Mbed platform. Based on Ultra-lite, Mass-market C Driver.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
dmathew
Date:
Fri May 17 09:07:55 2019 +0000
Revision:
6:aa13392d16bb
Parent:
5:f16727052990
Changes to class to allow interrupts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JerrySzczurak 5:f16727052990 1 /* Define to prevent from recursive inclusion --------------------------------*/
JerrySzczurak 5:f16727052990 2 #ifndef __DEV_53L1X_I2C_H
JerrySzczurak 5:f16727052990 3 #define __DEV_53L1X_I2C_H
JerrySzczurak 5:f16727052990 4
JerrySzczurak 5:f16727052990 5 /* Includes ------------------------------------------------------------------*/
JerrySzczurak 5:f16727052990 6 #include "mbed.h"
JerrySzczurak 5:f16727052990 7 #include "pinmap.h"
JerrySzczurak 5:f16727052990 8
JerrySzczurak 5:f16727052990 9 //Class replacing DevI2C class as it was not implementing a 16bit address registers
JerrySzczurak 5:f16727052990 10 class vl53L1X_DevI2C : public I2C
JerrySzczurak 5:f16727052990 11 {
JerrySzczurak 5:f16727052990 12 public:
JerrySzczurak 5:f16727052990 13 /** Create a DevI2C Master interface, connected to the specified pins
JerrySzczurak 5:f16727052990 14 *
JerrySzczurak 5:f16727052990 15 * @param sda I2C data line pin
JerrySzczurak 5:f16727052990 16 * @param scl I2C clock line pin
JerrySzczurak 5:f16727052990 17 */
JerrySzczurak 5:f16727052990 18 vl53L1X_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
JerrySzczurak 5:f16727052990 19
JerrySzczurak 5:f16727052990 20 /**
JerrySzczurak 5:f16727052990 21 * @brief Writes a buffer towards the I2C peripheral device.
JerrySzczurak 5:f16727052990 22 * @param pBuffer pointer to the byte-array data to send
JerrySzczurak 5:f16727052990 23 * @param DeviceAddr specifies the peripheral device slave address.
JerrySzczurak 5:f16727052990 24 * @param RegisterAddr specifies the internal address register
JerrySzczurak 5:f16727052990 25 * where to start writing to (must be correctly masked).
JerrySzczurak 5:f16727052990 26 * @param NumByteToWrite number of bytes to be written.
JerrySzczurak 5:f16727052990 27 * @retval 0 if ok,
JerrySzczurak 5:f16727052990 28 * @retval -1 if an I2C error has occured, or
JerrySzczurak 5:f16727052990 29 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
JerrySzczurak 5:f16727052990 30 * @note On some devices if NumByteToWrite is greater
JerrySzczurak 5:f16727052990 31 * than one, the RegisterAddr must be masked correctly!
JerrySzczurak 5:f16727052990 32 */
JerrySzczurak 5:f16727052990 33 int v53l1x_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
JerrySzczurak 5:f16727052990 34 uint16_t NumByteToWrite) {
JerrySzczurak 5:f16727052990 35 int ret;
JerrySzczurak 5:f16727052990 36 uint8_t tmp[TEMP_BUF_SIZE];
JerrySzczurak 5:f16727052990 37
JerrySzczurak 5:f16727052990 38 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
JerrySzczurak 5:f16727052990 39
JerrySzczurak 5:f16727052990 40 /* First, send device address. Then, send data and STOP condition */
JerrySzczurak 5:f16727052990 41 tmp[0] = RegisterAddr >> 8;
JerrySzczurak 5:f16727052990 42 tmp[1] = RegisterAddr & 0x0FF;
JerrySzczurak 5:f16727052990 43 memcpy(tmp+2, pBuffer, NumByteToWrite);
JerrySzczurak 5:f16727052990 44
JerrySzczurak 5:f16727052990 45 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
JerrySzczurak 5:f16727052990 46
JerrySzczurak 5:f16727052990 47 if(ret) return -1;
JerrySzczurak 5:f16727052990 48 return 0;
JerrySzczurak 5:f16727052990 49 }
JerrySzczurak 5:f16727052990 50
JerrySzczurak 5:f16727052990 51 /**
JerrySzczurak 5:f16727052990 52 * @brief Reads a buffer from the I2C peripheral device.
JerrySzczurak 5:f16727052990 53 * @param pBuffer pointer to the byte-array to read data in to
JerrySzczurak 5:f16727052990 54 * @param DeviceAddr specifies the peripheral device slave address.
JerrySzczurak 5:f16727052990 55 * @param RegisterAddr specifies the internal address register
JerrySzczurak 5:f16727052990 56 * where to start reading from (must be correctly masked).
JerrySzczurak 5:f16727052990 57 * @param NumByteToRead number of bytes to be read.
JerrySzczurak 5:f16727052990 58 * @retval 0 if ok,
JerrySzczurak 5:f16727052990 59 * @retval -1 if an I2C error has occured
JerrySzczurak 5:f16727052990 60 * @note On some devices if NumByteToWrite is greater
JerrySzczurak 5:f16727052990 61 * than one, the RegisterAddr must be masked correctly!
JerrySzczurak 5:f16727052990 62 */
JerrySzczurak 5:f16727052990 63 int v53l1x_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
JerrySzczurak 5:f16727052990 64 uint16_t NumByteToRead) {
JerrySzczurak 5:f16727052990 65 int ret;
JerrySzczurak 5:f16727052990 66 uint8_t ExpanderData[2];
JerrySzczurak 5:f16727052990 67 ExpanderData[0] = RegisterAddr >> 8;
JerrySzczurak 5:f16727052990 68 ExpanderData[1] = RegisterAddr & 0x0FF;
JerrySzczurak 5:f16727052990 69 /* Send device address, with no STOP condition */
JerrySzczurak 5:f16727052990 70 ret = write(DeviceAddr, (const char*)ExpanderData, 2, true);
JerrySzczurak 5:f16727052990 71 if(!ret) {
JerrySzczurak 5:f16727052990 72 /* Read data, with STOP condition */
JerrySzczurak 5:f16727052990 73 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
JerrySzczurak 5:f16727052990 74 }
JerrySzczurak 5:f16727052990 75
JerrySzczurak 5:f16727052990 76 if(ret) return -1;
JerrySzczurak 5:f16727052990 77 return 0;
JerrySzczurak 5:f16727052990 78 }
JerrySzczurak 5:f16727052990 79
JerrySzczurak 5:f16727052990 80 private:
JerrySzczurak 5:f16727052990 81 static const unsigned int TEMP_BUF_SIZE = 32;
JerrySzczurak 5:f16727052990 82 };
JerrySzczurak 5:f16727052990 83
JerrySzczurak 5:f16727052990 84 #endif /* __DEV_53L1X_I2C_H */