John Alexander / VL53L3_Lib

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   VL53L3ExpansionBoard

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vl53L3_I2c.h Source File

vl53L3_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 
00007 #include "mbed.h"
00008 
00009 #include "pinmap.h"
00010 
00011 static int mutex =0;
00012 
00013 //Class replacing DevI2C class as it was not implementing a 16bit address registers   
00014 class vl53L3_DevI2C : public I2C
00015 {
00016 public:
00017     /** Create a DevI2C Master interface, connected to the specified pins
00018      *
00019      *  @param sda I2C data line pin
00020      *  @param scl I2C clock line pin
00021      */
00022     vl53L3_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) { printf ("hello\n");}
00023     
00024     
00025         int VL53L3_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00026                   uint16_t NumByteToWrite) {
00027         int ret;
00028         uint8_t tmp[TEMP_BUF_SIZE];
00029 
00030         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00031 
00032         // First, send device address. Then, send data and STOP condition 
00033         tmp[0] = RegisterAddr >> 8;
00034         tmp[1] = RegisterAddr & 0x0FF;
00035         memcpy(tmp+2, pBuffer, NumByteToWrite);
00036 
00037         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00038 
00039         if(ret) return -1;
00040         return 0;
00041     }
00042 
00043     /**
00044      * @brief  Reads a buffer from the I2C peripheral device.
00045      * @param  pBuffer pointer to the byte-array to read data in to
00046      * @param  DeviceAddr specifies the peripheral device slave address.
00047      * @param  RegisterAddr specifies the internal address register
00048      *         where to start reading from (must be correctly masked).
00049      * @param  NumByteToRead number of bytes to be read.
00050      * @retval 0 if ok,
00051      * @retval -1 if an I2C error has occured
00052      * @note   On some devices if NumByteToWrite is greater
00053      *         than one, the RegisterAddr must be masked correctly!
00054      */
00055     int VL53L3_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00056                  uint16_t NumByteToRead) {
00057         int ret;
00058         uint8_t ExpanderData[2];
00059         ExpanderData[0] = RegisterAddr >> 8;
00060         ExpanderData[1] = RegisterAddr & 0x0FF;
00061         /* Send device address, with no STOP condition */
00062 
00063         ret = write(DeviceAddr, (const char*)ExpanderData, 2, true);
00064 
00065         if(!ret) {
00066             /* Read data, with STOP condition  */
00067             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00068         }
00069 
00070         if(ret) return -1;
00071         return 0;
00072     }
00073 
00074 private:
00075     static const unsigned int TEMP_BUF_SIZE = 256;
00076 };
00077 
00078 #endif /* __DEV_53L1X_I2C_H */