Computes Euler angles
Fork of X_NUCLEO_COMMON by
DevI2C/DevI2C.h@2:c1cac955d4bd, 2015-04-14 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Tue Apr 14 15:32:06 2015 +0200
- Revision:
- 2:c1cac955d4bd
- Parent:
- 1:ca975408dd4e
- Child:
- 3:bc5fc631e9c5
X_NUCLEO_IKS01A1: first version of main & hts221 component
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
0:5f1934431e71 | 1 | /** |
Wolfgang Betz |
0:5f1934431e71 | 2 | ****************************************************************************** |
Wolfgang Betz |
0:5f1934431e71 | 3 | * @file DevI2C.h |
Wolfgang Betz |
0:5f1934431e71 | 4 | * @author AST / EST |
Wolfgang Betz |
0:5f1934431e71 | 5 | * @version V0.0.1 |
Wolfgang Betz |
0:5f1934431e71 | 6 | * @date 21-January-2015 |
Wolfgang Betz |
0:5f1934431e71 | 7 | * @brief Header file for a special I2C class DecI2C which provides some |
Wolfgang Betz |
0:5f1934431e71 | 8 | * helper function for on-board communication |
Wolfgang Betz |
0:5f1934431e71 | 9 | ****************************************************************************** |
Wolfgang Betz |
0:5f1934431e71 | 10 | * @attention |
Wolfgang Betz |
0:5f1934431e71 | 11 | * |
Wolfgang Betz |
0:5f1934431e71 | 12 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
Wolfgang Betz |
0:5f1934431e71 | 13 | * |
Wolfgang Betz |
0:5f1934431e71 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
Wolfgang Betz |
0:5f1934431e71 | 15 | * are permitted provided that the following conditions are met: |
Wolfgang Betz |
0:5f1934431e71 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Wolfgang Betz |
0:5f1934431e71 | 17 | * this list of conditions and the following disclaimer. |
Wolfgang Betz |
0:5f1934431e71 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Wolfgang Betz |
0:5f1934431e71 | 19 | * this list of conditions and the following disclaimer in the documentation |
Wolfgang Betz |
0:5f1934431e71 | 20 | * and/or other materials provided with the distribution. |
Wolfgang Betz |
0:5f1934431e71 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Wolfgang Betz |
0:5f1934431e71 | 22 | * may be used to endorse or promote products derived from this software |
Wolfgang Betz |
0:5f1934431e71 | 23 | * without specific prior written permission. |
Wolfgang Betz |
0:5f1934431e71 | 24 | * |
Wolfgang Betz |
0:5f1934431e71 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Wolfgang Betz |
0:5f1934431e71 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Wolfgang Betz |
0:5f1934431e71 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Wolfgang Betz |
0:5f1934431e71 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Wolfgang Betz |
0:5f1934431e71 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Wolfgang Betz |
0:5f1934431e71 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Wolfgang Betz |
0:5f1934431e71 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Wolfgang Betz |
0:5f1934431e71 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Wolfgang Betz |
0:5f1934431e71 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Wolfgang Betz |
0:5f1934431e71 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Wolfgang Betz |
0:5f1934431e71 | 35 | * |
Wolfgang Betz |
0:5f1934431e71 | 36 | ****************************************************************************** |
Wolfgang Betz |
0:5f1934431e71 | 37 | */ |
Wolfgang Betz |
0:5f1934431e71 | 38 | |
Wolfgang Betz |
0:5f1934431e71 | 39 | /* Define to prevent from recursive inclusion --------------------------------*/ |
Wolfgang Betz |
0:5f1934431e71 | 40 | #ifndef __DEV_I2C_H |
Wolfgang Betz |
0:5f1934431e71 | 41 | #define __DEV_I2C_H |
Wolfgang Betz |
0:5f1934431e71 | 42 | |
Wolfgang Betz |
0:5f1934431e71 | 43 | /* Includes ------------------------------------------------------------------*/ |
Wolfgang Betz |
0:5f1934431e71 | 44 | #include "mbed.h" |
Wolfgang Betz |
0:5f1934431e71 | 45 | |
Wolfgang Betz |
0:5f1934431e71 | 46 | /* Classes -------------------------------------------------------------------*/ |
Wolfgang Betz |
0:5f1934431e71 | 47 | /** Helper class DevI2C providing functions for multi-register I2C communication |
Wolfgang Betz |
0:5f1934431e71 | 48 | * common for a series of I2C devices |
Wolfgang Betz |
0:5f1934431e71 | 49 | */ |
Wolfgang Betz |
0:5f1934431e71 | 50 | class DevI2C : public I2C |
Wolfgang Betz |
0:5f1934431e71 | 51 | { |
Wolfgang Betz |
0:5f1934431e71 | 52 | public: |
Wolfgang Betz |
0:5f1934431e71 | 53 | /** Create a DevI2C Master interface, connected to the specified pins |
Wolfgang Betz |
0:5f1934431e71 | 54 | * |
Wolfgang Betz |
0:5f1934431e71 | 55 | * @param sda I2C data line pin |
Wolfgang Betz |
0:5f1934431e71 | 56 | * @param scl I2C clock line pin |
Wolfgang Betz |
0:5f1934431e71 | 57 | */ |
Wolfgang Betz |
1:ca975408dd4e | 58 | DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} |
Wolfgang Betz |
0:5f1934431e71 | 59 | |
Wolfgang Betz |
0:5f1934431e71 | 60 | /** |
Wolfgang Betz |
0:5f1934431e71 | 61 | * @brief Writes a buffer from the I2C peripheral device. |
Wolfgang Betz |
0:5f1934431e71 | 62 | * @param pBuffer pointer to data to be written. |
Wolfgang Betz |
0:5f1934431e71 | 63 | * @param DeviceAddr specifies the peripheral device slave address. |
Wolfgang Betz |
0:5f1934431e71 | 64 | * @param RegisterAddr specifies the internal address register |
Wolfgang Betz |
0:5f1934431e71 | 65 | * where to start writing to (must be correctly masked). |
Wolfgang Betz |
0:5f1934431e71 | 66 | * @param NumByteToWrite number of bytes to be written. |
Wolfgang Betz |
0:5f1934431e71 | 67 | * @retval 0 if ok, -1 if an I2C error has occured |
Wolfgang Betz |
0:5f1934431e71 | 68 | * @note On some devices if NumByteToWrite is greater |
Wolfgang Betz |
0:5f1934431e71 | 69 | * than one, the RegisterAddr must be masked correctly! |
Wolfgang Betz |
0:5f1934431e71 | 70 | */ |
Wolfgang Betz |
0:5f1934431e71 | 71 | int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, |
Wolfgang Betz |
0:5f1934431e71 | 72 | uint16_t NumByteToWrite) |
Wolfgang Betz |
0:5f1934431e71 | 73 | { |
Wolfgang Betz |
0:5f1934431e71 | 74 | int ret; |
Wolfgang Betz |
0:5f1934431e71 | 75 | uint8_t tmp[32]; |
Wolfgang Betz |
0:5f1934431e71 | 76 | |
Wolfgang Betz |
0:5f1934431e71 | 77 | /* First, send device address. Then, send data and STOP condition */ |
Wolfgang Betz |
0:5f1934431e71 | 78 | tmp[0] = RegisterAddr; |
Wolfgang Betz |
0:5f1934431e71 | 79 | memcpy(tmp+1, pBuffer, NumByteToWrite); |
Wolfgang Betz |
0:5f1934431e71 | 80 | |
Wolfgang Betz |
0:5f1934431e71 | 81 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, 0); |
Wolfgang Betz |
0:5f1934431e71 | 82 | |
Wolfgang Betz |
2:c1cac955d4bd | 83 | if(ret) return -1; |
Wolfgang Betz |
0:5f1934431e71 | 84 | return 0; |
Wolfgang Betz |
0:5f1934431e71 | 85 | } |
Wolfgang Betz |
0:5f1934431e71 | 86 | |
Wolfgang Betz |
0:5f1934431e71 | 87 | /** |
Wolfgang Betz |
0:5f1934431e71 | 88 | * @brief Reads a buffer from the I2C peripheral device. |
Wolfgang Betz |
0:5f1934431e71 | 89 | * @param pBuffer pointer to data to be read. |
Wolfgang Betz |
0:5f1934431e71 | 90 | * @param DaviceAddr specifies the peripheral device slave address. |
Wolfgang Betz |
0:5f1934431e71 | 91 | * @param RegisterAddr specifies the internal address register |
Wolfgang Betz |
0:5f1934431e71 | 92 | * where to start reading from (must be correctly masked). |
Wolfgang Betz |
0:5f1934431e71 | 93 | * @param NumByteToRead number of bytes to be read. |
Wolfgang Betz |
0:5f1934431e71 | 94 | * @retval 0 if ok, -1 if an I2C error has occured |
Wolfgang Betz |
0:5f1934431e71 | 95 | * @note On some devices if NumByteToWrite is greater |
Wolfgang Betz |
0:5f1934431e71 | 96 | * than one, the RegisterAddr must be masked correctly! |
Wolfgang Betz |
0:5f1934431e71 | 97 | */ |
Wolfgang Betz |
0:5f1934431e71 | 98 | int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, |
Wolfgang Betz |
0:5f1934431e71 | 99 | uint16_t NumByteToRead) |
Wolfgang Betz |
0:5f1934431e71 | 100 | { |
Wolfgang Betz |
0:5f1934431e71 | 101 | int ret; |
Wolfgang Betz |
0:5f1934431e71 | 102 | |
Wolfgang Betz |
0:5f1934431e71 | 103 | /* Send device address, with no STOP condition */ |
Wolfgang Betz |
0:5f1934431e71 | 104 | ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, 1); |
Wolfgang Betz |
0:5f1934431e71 | 105 | if(!ret) { |
Wolfgang Betz |
0:5f1934431e71 | 106 | /* Read data, with STOP condition */ |
Wolfgang Betz |
0:5f1934431e71 | 107 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, 0); |
Wolfgang Betz |
0:5f1934431e71 | 108 | } |
Wolfgang Betz |
0:5f1934431e71 | 109 | |
Wolfgang Betz |
2:c1cac955d4bd | 110 | if(ret) return -1; |
Wolfgang Betz |
0:5f1934431e71 | 111 | return 0; |
Wolfgang Betz |
0:5f1934431e71 | 112 | } |
Wolfgang Betz |
0:5f1934431e71 | 113 | }; |
Wolfgang Betz |
0:5f1934431e71 | 114 | |
Wolfgang Betz |
0:5f1934431e71 | 115 | #endif /* __DEV_I2C_H */ |