John Alexander / VL53L1X_mbed

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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         int ret;
00036         uint8_t tmp[TEMP_BUF_SIZE];
00037 
00038         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00039 
00040         /* First, send device address. Then, send data and STOP condition */
00041         tmp[0] = RegisterAddr >> 8;
00042         tmp[1] = RegisterAddr & 0x0FF;
00043         memcpy(tmp+2, pBuffer, NumByteToWrite);
00044 
00045         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00046 
00047         if(ret) return -1;
00048         return 0;
00049     }
00050 
00051     /**
00052      * @brief  Reads a buffer from the I2C peripheral device.
00053      * @param  pBuffer pointer to the byte-array to read data in to
00054      * @param  DeviceAddr specifies the peripheral device slave address.
00055      * @param  RegisterAddr specifies the internal address register
00056      *         where to start reading from (must be correctly masked).
00057      * @param  NumByteToRead number of bytes to be read.
00058      * @retval 0 if ok,
00059      * @retval -1 if an I2C error has occured
00060      * @note   On some devices if NumByteToWrite is greater
00061      *         than one, the RegisterAddr must be masked correctly!
00062      */
00063     int v53l1x_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00064                  uint16_t NumByteToRead) {
00065         int ret;
00066         uint8_t ExpanderData[2];
00067         ExpanderData[0] = RegisterAddr >> 8;
00068         ExpanderData[1] = RegisterAddr & 0x0FF;
00069         /* Send device address, with no STOP condition */
00070         ret = write(DeviceAddr, (const char*)ExpanderData, 2, true);
00071         if(!ret) {
00072             /* Read data, with STOP condition  */
00073             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00074         }
00075 
00076         if(ret) return -1;
00077         return 0;
00078     }
00079 
00080 private:
00081     static const unsigned int TEMP_BUF_SIZE = 32;
00082 };
00083 
00084 #endif /* __DEV_53L1X_I2C_H */