ST Expansion SW Team / VL53L3CX_mbed

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   VL53L3CX_NoShield_1Sensor_poll_Mb06x VL53L3_NoShield_1Sensor_polling_Mb63 X_NUCLEO_53L3A2 53L3A2_Ranging

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ToF_I2C.h Source File

ToF_I2C.h

00001 /* Define to prevent from recursive inclusion --------------------------------*/
00002 #ifndef __DEV_ToF_I2C_H
00003 #define __DEV_ToF_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 ToF_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     ToF_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) { printf ("ToF_DevI2C\n");}
00023     
00024     /**
00025      * @brief  Writes a buffer towards the I2C peripheral device.
00026      * @param  pBuffer pointer to the byte-array data to send
00027      * @param  DeviceAddr specifies the peripheral device slave address.
00028      * @param  RegisterAddr specifies the internal address register
00029      *         where to start writing to (must be correctly masked).
00030      * @param  NumByteToWrite number of bytes to be written.
00031      * @retval 0 if ok,
00032      * @retval -1 if an I2C error has occured, or
00033      * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
00034      * @note   On some devices if NumByteToWrite is greater
00035      *         than one, the RegisterAddr must be masked correctly!
00036      */
00037     
00038         int ToF_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00039                   uint16_t NumByteToWrite) {
00040         int ret;
00041         uint8_t tmp[TEMP_BUF_SIZE];
00042 
00043         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00044 
00045         // First, send device address. Then, send data and STOP condition 
00046         tmp[0] = RegisterAddr >> 8;
00047         tmp[1] = RegisterAddr & 0x0FF;
00048         memcpy(tmp+2, pBuffer, NumByteToWrite);
00049 
00050         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00051 
00052         if(ret) return -1;
00053         return 0;
00054     }
00055 
00056     /**
00057      * @brief  Reads a buffer from the I2C peripheral device.
00058      * @param  pBuffer pointer to the byte-array to read data in to
00059      * @param  DeviceAddr specifies the peripheral device slave address.
00060      * @param  RegisterAddr specifies the internal address register
00061      *         where to start reading from (must be correctly masked).
00062      * @param  NumByteToRead number of bytes to be read.
00063      * @retval 0 if ok,
00064      * @retval -1 if an I2C error has occured
00065      * @note   On some devices if NumByteToWrite is greater
00066      *         than one, the RegisterAddr must be masked correctly!
00067      */
00068     int ToF_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00069                  uint16_t NumByteToRead) {
00070         int ret;
00071         uint8_t ExpanderData[2];
00072         ExpanderData[0] = RegisterAddr >> 8;
00073         ExpanderData[1] = RegisterAddr & 0x0FF;
00074         /* Send device address, with no STOP condition */
00075         ret = write(DeviceAddr, (const char*)ExpanderData, 2, true);
00076         if(!ret) {
00077             /* Read data, with STOP condition  */
00078             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00079         }
00080         else
00081         {
00082             printf("ToF_i2c_read write failed %d %d %d %d\n",ret,DeviceAddr,RegisterAddr,NumByteToRead);
00083         }
00084 
00085         if(ret) return -1;
00086         return 0;
00087     }
00088     
00089     
00090     int ToF_i2c_write_direct(uint8_t* pBuffer, uint16_t DeviceAddr,
00091                   uint16_t NumByteToWrite) {
00092         int ret;
00093         uint8_t tmp[TEMP_BUF_SIZE];
00094 
00095         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00096 
00097         // First, send device address. Then, send data and STOP condition 
00098 
00099         memcpy(tmp, pBuffer, NumByteToWrite);
00100 
00101         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite, false);
00102 //printf("ToF_i2c_write_direct %d %d %d %d %d %d\n",DeviceAddr,tmp[0],(uint8_t)tmp[1],(uint8_t)tmp[2],NumByteToWrite,ret);
00103         if(ret) return -1;
00104         return 0;
00105     }
00106 
00107 private:
00108     static const unsigned int TEMP_BUF_SIZE = 256;
00109 };
00110 
00111 #endif /* __DEV_53L1X_I2C_H */