Local changes to increase sample rate
Fork of X_NUCLEO_COMMON by
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 /** 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 //frequency(400000); 00080 00081 } 00082 00083 /** 00084 * @brief Writes a buffer towards the I2C peripheral device. 00085 * @param pBuffer pointer to the byte-array data to send 00086 * @param DeviceAddr specifies the peripheral device slave address. 00087 * @param RegisterAddr specifies the internal address register 00088 * where to start writing to (must be correctly masked). 00089 * @param NumByteToWrite number of bytes to be written. 00090 * @retval 0 if ok, 00091 * @retval -1 if an I2C error has occured, or 00092 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) 00093 * @note On some devices if NumByteToWrite is greater 00094 * than one, the RegisterAddr must be masked correctly! 00095 */ 00096 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00097 uint16_t NumByteToWrite) { 00098 int ret; 00099 uint8_t tmp[TEMP_BUF_SIZE]; 00100 00101 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; 00102 00103 /* First, send device address. Then, send data and STOP condition */ 00104 tmp[0] = RegisterAddr; 00105 memcpy(tmp+1, pBuffer, NumByteToWrite); 00106 00107 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); 00108 00109 if(ret) return -1; 00110 return 0; 00111 } 00112 00113 /** 00114 * @brief Reads a buffer from the I2C peripheral device. 00115 * @param pBuffer pointer to the byte-array to read data in to 00116 * @param DaviceAddr specifies the peripheral device slave address. 00117 * @param RegisterAddr specifies the internal address register 00118 * where to start reading from (must be correctly masked). 00119 * @param NumByteToRead number of bytes to be read. 00120 * @retval 0 if ok, 00121 * @retval -1 if an I2C error has occured 00122 * @note On some devices if NumByteToWrite is greater 00123 * than one, the RegisterAddr must be masked correctly! 00124 */ 00125 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00126 uint16_t NumByteToRead) { 00127 int ret; 00128 00129 /* Send device address, with no STOP condition */ 00130 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); 00131 if(!ret) { 00132 /* Read data, with STOP condition */ 00133 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); 00134 } 00135 00136 if(ret) return -1; 00137 return 0; 00138 } 00139 00140 private: 00141 static const unsigned int TEMP_BUF_SIZE = 32; 00142 }; 00143 00144 #endif /* __DEV_I2C_H */
Generated on Tue Jul 12 2022 21:22:23 by
1.7.2
