Computes Euler angles
Fork of X_NUCLEO_COMMON by
DevI2C/DevI2C.h@0:5f1934431e71, 2015-03-30 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Mon Mar 30 11:46:53 2015 +0200
- Revision:
- 0:5f1934431e71
- Child:
- 1:ca975408dd4e
Moved X_NUCLEO_COMMON to libraries/tests/libs
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 |
0:5f1934431e71 | 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 |
0:5f1934431e71 | 83 | if(ret) { |
Wolfgang Betz |
0:5f1934431e71 | 84 | error("%s: dev = %d, reg = %d, num = %d\n", |
Wolfgang Betz |
0:5f1934431e71 | 85 | __func__, DeviceAddr, RegisterAddr, NumByteToWrite); |
Wolfgang Betz |
0:5f1934431e71 | 86 | return -1; |
Wolfgang Betz |
0:5f1934431e71 | 87 | } |
Wolfgang Betz |
0:5f1934431e71 | 88 | return 0; |
Wolfgang Betz |
0:5f1934431e71 | 89 | } |
Wolfgang Betz |
0:5f1934431e71 | 90 | |
Wolfgang Betz |
0:5f1934431e71 | 91 | /** |
Wolfgang Betz |
0:5f1934431e71 | 92 | * @brief Reads a buffer from the I2C peripheral device. |
Wolfgang Betz |
0:5f1934431e71 | 93 | * @param pBuffer pointer to data to be read. |
Wolfgang Betz |
0:5f1934431e71 | 94 | * @param DaviceAddr specifies the peripheral device slave address. |
Wolfgang Betz |
0:5f1934431e71 | 95 | * @param RegisterAddr specifies the internal address register |
Wolfgang Betz |
0:5f1934431e71 | 96 | * where to start reading from (must be correctly masked). |
Wolfgang Betz |
0:5f1934431e71 | 97 | * @param NumByteToRead number of bytes to be read. |
Wolfgang Betz |
0:5f1934431e71 | 98 | * @retval 0 if ok, -1 if an I2C error has occured |
Wolfgang Betz |
0:5f1934431e71 | 99 | * @note On some devices if NumByteToWrite is greater |
Wolfgang Betz |
0:5f1934431e71 | 100 | * than one, the RegisterAddr must be masked correctly! |
Wolfgang Betz |
0:5f1934431e71 | 101 | */ |
Wolfgang Betz |
0:5f1934431e71 | 102 | int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, |
Wolfgang Betz |
0:5f1934431e71 | 103 | uint16_t NumByteToRead) |
Wolfgang Betz |
0:5f1934431e71 | 104 | { |
Wolfgang Betz |
0:5f1934431e71 | 105 | int ret; |
Wolfgang Betz |
0:5f1934431e71 | 106 | |
Wolfgang Betz |
0:5f1934431e71 | 107 | /* Send device address, with no STOP condition */ |
Wolfgang Betz |
0:5f1934431e71 | 108 | ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, 1); |
Wolfgang Betz |
0:5f1934431e71 | 109 | if(!ret) { |
Wolfgang Betz |
0:5f1934431e71 | 110 | /* Read data, with STOP condition */ |
Wolfgang Betz |
0:5f1934431e71 | 111 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, 0); |
Wolfgang Betz |
0:5f1934431e71 | 112 | } |
Wolfgang Betz |
0:5f1934431e71 | 113 | |
Wolfgang Betz |
0:5f1934431e71 | 114 | if(ret) { |
Wolfgang Betz |
0:5f1934431e71 | 115 | error("%s: dev = %d, reg = %d, num = %d\n", |
Wolfgang Betz |
0:5f1934431e71 | 116 | __func__, DeviceAddr, RegisterAddr, NumByteToRead); |
Wolfgang Betz |
0:5f1934431e71 | 117 | return -1; |
Wolfgang Betz |
0:5f1934431e71 | 118 | } |
Wolfgang Betz |
0:5f1934431e71 | 119 | return 0; |
Wolfgang Betz |
0:5f1934431e71 | 120 | } |
Wolfgang Betz |
0:5f1934431e71 | 121 | }; |
Wolfgang Betz |
0:5f1934431e71 | 122 | |
Wolfgang Betz |
0:5f1934431e71 | 123 | #endif /* __DEV_I2C_H */ |