ST Expansion SW Team / VL53L1

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_53L1CB

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 ("hello\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 v53l1x_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         while ( mutex )
00050         {
00051             wait_ms(1);
00052         }
00053         mutex = 1;
00054 
00055         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00056         mutex = 0;
00057         if(ret)
00058         {
00059             wait_ms(1);
00060             ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00061             if(ret)
00062             {
00063                             printf("***************** v53l1x_i2c_write twice times %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToWrite,ret);
00064             }
00065  //           printf("v53l1x_i2c_write %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToWrite,ret);
00066         return -1;
00067         }
00068         return 0;
00069     }
00070     */
00071 
00072     /**
00073      * @brief  Reads a buffer from the I2C peripheral device.
00074      * @param  pBuffer pointer to the byte-array to read data in to
00075      * @param  DeviceAddr specifies the peripheral device slave address.
00076      * @param  RegisterAddr specifies the internal address register
00077      *         where to start reading from (must be correctly masked).
00078      * @param  NumByteToRead number of bytes to be read.
00079      * @retval 0 if ok,
00080      * @retval -1 if an I2C error has occured
00081      * @note   On some devices if NumByteToWrite is greater
00082      *         than one, the RegisterAddr must be masked correctly!
00083      */
00084      /*
00085     int v53l1x_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00086                  uint16_t NumByteToRead) {
00087         int ret;
00088 
00089 
00090             // Read data, with STOP condition  
00091             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00092 
00093        //     printf("class v53l1x_i2c_read read %d %d %d\n",pBuffer[0],pBuffer[1],pBuffer[2]);
00094 
00095 
00096         if(ret)
00097         {
00098            printf("********************* class v53l1x_i2c_read %d %d %d %d\n",DeviceAddr,RegisterAddr,NumByteToRead,ret);
00099            return -1;
00100         }
00101         return 0;
00102     }
00103     */
00104     
00105         int ToF_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00106                   uint16_t NumByteToWrite) {
00107         int ret;
00108         uint8_t tmp[TEMP_BUF_SIZE];
00109 
00110         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00111 
00112         // First, send device address. Then, send data and STOP condition 
00113         tmp[0] = RegisterAddr >> 8;
00114         tmp[1] = RegisterAddr & 0x0FF;
00115         memcpy(tmp+2, pBuffer, NumByteToWrite);
00116 
00117         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
00118 
00119         if(ret) return -1;
00120         return 0;
00121     }
00122 
00123     /**
00124      * @brief  Reads a buffer from the I2C peripheral device.
00125      * @param  pBuffer pointer to the byte-array to read data in to
00126      * @param  DeviceAddr specifies the peripheral device slave address.
00127      * @param  RegisterAddr specifies the internal address register
00128      *         where to start reading from (must be correctly masked).
00129      * @param  NumByteToRead number of bytes to be read.
00130      * @retval 0 if ok,
00131      * @retval -1 if an I2C error has occured
00132      * @note   On some devices if NumByteToWrite is greater
00133      *         than one, the RegisterAddr must be masked correctly!
00134      */
00135     int ToF_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
00136                  uint16_t NumByteToRead) {
00137         int ret;
00138         uint8_t ExpanderData[2];
00139         ExpanderData[0] = RegisterAddr >> 8;
00140         ExpanderData[1] = RegisterAddr & 0x0FF;
00141         /* Send device address, with no STOP condition */
00142         ret = write(DeviceAddr, (const char*)ExpanderData, 2, true);
00143         if(!ret) {
00144             /* Read data, with STOP condition  */
00145             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00146         }
00147 
00148         if(ret) return -1;
00149         return 0;
00150     }
00151 
00152 private:
00153     static const unsigned int TEMP_BUF_SIZE = 256;
00154 };
00155 
00156 #endif /* __DEV_53L1X_I2C_H */