Library for supporting the Nucleo Sensor Shield.
Dependents: Nucleo_Sensors_Demo m2x-temp_ethernet_demo m2x-MEMS_ACKme_Wifi_demo m2x_MEMS_Ublox_Cellular_demo ... more
Fork of Nucleo_Sensor_Shield by
x_cube_mems_i2c.h
00001 /** 00002 ****************************************************************************** 00003 * @file x_cube_mems_i2c.h 00004 * @author AST / EST 00005 * @version V0.0.1 00006 * @date 28-November-2014 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) 2014 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 __X_CUBE_MEMS_I2C_H 00041 #define __X_CUBE_MEMS_I2C_H 00042 00043 /* Includes ------------------------------------------------------------------*/ 00044 #include "mbed.h" 00045 00046 typedef struct { 00047 int32_t AXIS_X; 00048 int32_t AXIS_Y; 00049 int32_t AXIS_Z; 00050 } AxesRaw_TypeDef; 00051 00052 /* Classes -------------------------------------------------------------------*/ 00053 /** Helper class DevI2C providing some common functionality useful for on-board 00054 * communication. 00055 */ 00056 class DevI2C : public I2C 00057 { 00058 public: 00059 /** Create a DevI2C Master interface, connected to the specified pins 00060 * 00061 * @param sda I2C data line pin 00062 * @param scl I2C clock line pin 00063 */ 00064 DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}; 00065 00066 /** 00067 * @brief Writes a buffer from the I2C peripheral device. 00068 * @param pBuffer pointer to data to be read. 00069 * @param DeviceAddr specifies the peripheral device slave address 00070 * (correctly masked). 00071 * @param RegisterAddr specifies internal address register to read from. 00072 * @param NumByteToWrite number of bytes to be written. 00073 * @retval 0 if ok, -1 if an I2C error has occured 00074 * @note on some devices if NumByteToWrite is greater 00075 * than one, the DeviceAddr must be masked correctly 00076 */ 00077 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00078 uint16_t NumByteToWrite) 00079 { 00080 int ret; 00081 uint8_t tmp[32]; 00082 00083 //Acquire mbed mutex/semaphore/lock? 00084 00085 /* First, send device address. Then, send data and STOP condition */ 00086 tmp[0] = RegisterAddr; 00087 memcpy(tmp+1, pBuffer, NumByteToWrite); 00088 00089 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, 0); 00090 00091 //Release mbed mutex/semaphore/lock? 00092 00093 if(ret) { 00094 error("%s: dev = %d, reg = %d, num = %d\n", 00095 __func__, DeviceAddr, RegisterAddr, NumByteToWrite); 00096 return -1; 00097 } 00098 return 0; 00099 } 00100 00101 /** 00102 * @brief Reads a buffer from the I2C peripheral device. 00103 * @param pBuffer pointer to data to be read. 00104 * @param DaviceAddr specifies the peripheral device slave address 00105 * (correctly masked).. 00106 * @param RegisterAddr specifies internal address register to read from. 00107 * @param NumByteToRead number of bytes to be read. 00108 * @retval 0 if ok, -1 if an I2C error has occured 00109 * @note on some devices if NumByteToRead is greater 00110 * than one, the DeviceAddr must be masked correctly 00111 */ 00112 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 00113 uint16_t NumByteToRead) 00114 { 00115 int ret; 00116 00117 //Acquire mbed mutex/semaphore/lock? 00118 00119 /* Send device address, with no STOP condition */ 00120 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, 1); 00121 if(!ret) { 00122 /* Read data, with STOP condition */ 00123 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, 0); 00124 } 00125 00126 //Release mbed mutex/semaphore/lock? 00127 00128 if(ret) { 00129 error("%s: dev = %d, reg = %d, num = %d\n", 00130 __func__, DeviceAddr, RegisterAddr, NumByteToRead); 00131 return -1; 00132 } 00133 return 0; 00134 } 00135 }; 00136 00137 #endif /* __X_CUBE_MEMS_I2C_H */
Generated on Tue Jul 12 2022 14:47:53 by 1.7.2