ST / VL53L1X_mbed

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_53L1A1_mbed X_NUCLEO_53L1A1_mbed VL53L1X_Ranging_With_Standalone_Satellite_MbedOS X_NUCLEO_53L1A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VL53L1X_I2C.h Source File

VL53L1X_I2C.h

00001 /* Define to prevent from recursive inclusion --------------------------------*/
00002 #ifndef __DEV_53L1X_I2C_H
00003 #define __DEV_53L1X_I2C_H
00004 
00005 /* Includes ------------------------------------------------------------------*/
00006 #include "mbed.h"
00007 #include "pinmap.h"
00008 
00009 //Class replacing DevI2C class as it was not implementing a 16bit address registers
00010 class VL53L1X_DevI2C : public I2C
00011 {
00012 public:
00013     /** Create a DevI2C Master interface, connected to the specified pins
00014      *
00015      *  @param sda I2C data line pin
00016      *  @param scl I2C clock line pin
00017      */
00018     VL53L1X_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
00019 
00020     /**
00021      * @brief  Writes a buffer towards the I2C peripheral device.
00022      * @param  pBuffer pointer to the byte-array data to send
00023      * @param  DeviceAddr specifies the peripheral device slave address.
00024      * @param  RegisterAddr specifies the internal address register
00025      *         where to start writing to (must be correctly masked).
00026      * @param  NumByteToWrite number of bytes to be written.
00027      * @retval 0 if ok,
00028      * @retval -1 if an I2C error has occured, or
00029      * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
00030      * @note   On some devices if NumByteToWrite is greater
00031      *         than one, the RegisterAddr must be masked correctly!
00032      */
00033     int v53l1x_i2c_write(uint8_t *pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00034                          uint16_t NumByteToWrite)
00035     {
00036         int ret;
00037         uint8_t tmp[TEMP_BUF_SIZE];
00038 
00039         if (NumByteToWrite >= TEMP_BUF_SIZE)
00040             return -2;
00041 
00042         /* First, send device address. Then, send data and STOP condition */
00043         tmp[0] = RegisterAddr >> 8;
00044         tmp[1] = RegisterAddr & 0x0FF;
00045         memcpy(tmp + 2, pBuffer, NumByteToWrite);
00046 
00047         ret = write(DeviceAddr, (const char *)tmp, NumByteToWrite + 2, false);
00048 
00049         if (ret)
00050             return -1;
00051         return 0;
00052     }
00053 
00054     /**
00055      * @brief  Reads a buffer from the I2C peripheral device.
00056      * @param  pBuffer pointer to the byte-array to read data in to
00057      * @param  DeviceAddr specifies the peripheral device slave address.
00058      * @param  RegisterAddr specifies the internal address register
00059      *         where to start reading from (must be correctly masked).
00060      * @param  NumByteToRead number of bytes to be read.
00061      * @retval 0 if ok,
00062      * @retval -1 if an I2C error has occured
00063      * @note   On some devices if NumByteToWrite is greater
00064      *         than one, the RegisterAddr must be masked correctly!
00065      */
00066     int v53l1x_i2c_read(uint8_t *pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00067                         uint16_t NumByteToRead)
00068     {
00069         int ret;
00070         uint8_t ExpanderData[2];
00071         ExpanderData[0] = RegisterAddr >> 8;
00072         ExpanderData[1] = RegisterAddr & 0x0FF;
00073         /* Send device address, with no STOP condition */
00074         ret = write(DeviceAddr, (const char *)ExpanderData, 2, true);
00075         if (!ret) {
00076             /* Read data, with STOP condition  */
00077             ret = read(DeviceAddr, (char *)pBuffer, NumByteToRead, false);
00078         }
00079 
00080         if (ret)
00081             return -1;
00082         return 0;
00083     }
00084 
00085 private:
00086     static const unsigned int TEMP_BUF_SIZE = 32;
00087 };
00088 
00089 #endif /* __DEV_53L1X_I2C_H */