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