Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of X_NUCLEO_COMMON by
DevI2C.h
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 /** Create a DevI2C Master interface, connected to the specified pins and set their pin modes 00062 * 00063 * @param sda I2C data line pin 00064 * @param sda I2C data pin mode 00065 * @param scl I2C clock line pin 00066 * @param scl I2C clock pin mode 00067 * 00068 * @note this is a workaround to provide a constructor which currently 00069 * is somehow missing in the I2C base class and it's underlying 00070 * implementations. In some circumstances (e.g. while debugging) 00071 * where long latencies between the initialization of the i2c 00072 * interface in the I2C constructor and the setting of the pin 00073 * modes in the beyond constructor might occur, the i2c 00074 * communication might be compromised. 00075 */ 00076 DevI2C(PinName sda, int mode_sda, PinName scl, int mode_scl) : I2C(sda, scl) { 00077 pin_mode(sda, (PinMode)mode_sda); 00078 pin_mode(scl, (PinMode)mode_scl); 00079 } 00080 00081 /** 00082 * @brief Writes a buffer towards the I2C peripheral device. 00083 * @param pBuffer pointer to the byte-array data to send 00084 * @param DeviceAddr specifies the peripheral device slave address. 00085 * @param RegisterAddr specifies the internal address register 00086 * where to start writing to (must be correctly masked). 00087 * @param NumByteToWrite number of bytes to be written. 00088 * @retval 0 if ok, 00089 * @retval -1 if an I2C error has occured, or 00090 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) 00091 * @note On some devices if NumByteToWrite is greater 00092 * than one, the RegisterAddr must be masked correctly! 00093 */ 00094 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00095 uint16_t NumByteToWrite) { 00096 int ret; 00097 uint8_t tmp[TEMP_BUF_SIZE]; 00098 00099 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; 00100 00101 /* First, send device address. Then, send data and STOP condition */ 00102 tmp[0] = RegisterAddr; 00103 memcpy(tmp+1, pBuffer, NumByteToWrite); 00104 00105 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); 00106 00107 if(ret) return -1; 00108 return 0; 00109 } 00110 00111 /** 00112 * @brief Reads a buffer from the I2C peripheral device. 00113 * @param pBuffer pointer to the byte-array to read data in to 00114 * @param DaviceAddr specifies the peripheral device slave address. 00115 * @param RegisterAddr specifies the internal address register 00116 * where to start reading from (must be correctly masked). 00117 * @param NumByteToRead number of bytes to be read. 00118 * @retval 0 if ok, 00119 * @retval -1 if an I2C error has occured 00120 * @note On some devices if NumByteToWrite is greater 00121 * than one, the RegisterAddr must be masked correctly! 00122 */ 00123 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00124 uint16_t NumByteToRead) { 00125 int ret; 00126 00127 /* Send device address, with no STOP condition */ 00128 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); 00129 if(!ret) { 00130 /* Read data, with STOP condition */ 00131 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); 00132 } 00133 00134 if(ret) return -1; 00135 return 0; 00136 } 00137 00138 private: 00139 static const unsigned int TEMP_BUF_SIZE = 32; 00140 }; 00141 00142 #endif /* __DEV_I2C_H */
Generated on Wed Jul 13 2022 23:42:58 by
1.7.2
