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