Initial release. Mbed library for VL53L1CB
Embed:
(wiki syntax)
Show/hide line numbers
DevI2C.h
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file DevI2C.h 00004 * @author AST / EST 00005 * @version V1.1.0 00006 * @date 21-January-2016 00007 * @brief Header file for a special I2C class DevI2C which provides some 00008 * helper function for on-board communication 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 /* Define to prevent from recursive inclusion --------------------------------*/ 00040 #ifndef __DEV_I2C_H 00041 #define __DEV_I2C_H 00042 00043 /* Includes ------------------------------------------------------------------*/ 00044 #include "mbed.h" 00045 #include "pinmap.h" 00046 00047 /* Classes -------------------------------------------------------------------*/ 00048 /** Helper class DevI2C providing functions for multi-register I2C communication 00049 * common for a series of I2C devices 00050 */ 00051 class DevI2C : public I2C 00052 { 00053 public: 00054 /** Create a DevI2C Master interface, connected to the specified pins 00055 * 00056 * @param sda I2C data line pin 00057 * @param scl I2C clock line pin 00058 */ 00059 DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} 00060 00061 /** 00062 * @brief Writes a buffer towards the I2C peripheral device. 00063 * @param pBuffer pointer to the byte-array data to send 00064 * @param DeviceAddr specifies the peripheral device slave address. 00065 * @param RegisterAddr specifies the internal address register 00066 * where to start writing to (must be correctly masked). 00067 * @param NumByteToWrite number of bytes to be written. 00068 * @retval 0 if ok, 00069 * @retval -1 if an I2C error has occured, or 00070 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) 00071 * @note On some devices if NumByteToWrite is greater 00072 * than one, the RegisterAddr must be masked correctly! 00073 */ 00074 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00075 uint16_t NumByteToWrite) { 00076 int ret; 00077 uint8_t tmp[TEMP_BUF_SIZE]; 00078 00079 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; 00080 00081 /* First, send device address. Then, send data and STOP condition */ 00082 tmp[0] = RegisterAddr; 00083 memcpy(tmp+1, pBuffer, NumByteToWrite); 00084 00085 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); 00086 00087 if(ret) return -1; 00088 return 0; 00089 } 00090 00091 /** 00092 * @brief Reads a buffer from the I2C peripheral device. 00093 * @param pBuffer pointer to the byte-array to read data in to 00094 * @param DeviceAddr specifies the peripheral device slave address. 00095 * @param RegisterAddr specifies the internal address register 00096 * where to start reading from (must be correctly masked). 00097 * @param NumByteToRead number of bytes to be read. 00098 * @retval 0 if ok, 00099 * @retval -1 if an I2C error has occured 00100 * @note On some devices if NumByteToWrite is greater 00101 * than one, the RegisterAddr must be masked correctly! 00102 */ 00103 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00104 uint16_t NumByteToRead) { 00105 int ret; 00106 00107 /* Send device address, with no STOP condition */ 00108 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); 00109 if(!ret) { 00110 /* Read data, with STOP condition */ 00111 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); 00112 } 00113 00114 if(ret) return -1; 00115 return 0; 00116 } 00117 00118 private: 00119 static const unsigned int TEMP_BUF_SIZE = 256; //~~ was 32 00120 }; 00121 00122 #endif /* __DEV_I2C_H */
Generated on Tue Jul 12 2022 20:07:14 by 1.7.2