mbed OS 5 example application using X-NUCLEO-NFC02A1
Dependencies: NDefLib X_NUCLEO_NFC02A1
Fork of HelloWorld_NFC02A1 by
X_NUCLEO_NFC02A1/X_NUCLEO_COMMON/DevI2C/DevI2C.h@1:11ae12d41082, 2016-08-30 (annotated)
- Committer:
- rosarium
- Date:
- Tue Aug 30 09:18:50 2016 +0000
- Revision:
- 1:11ae12d41082
- Parent:
- 0:892175366555
NDefLib middle-ware library ported on HelloWorld_NFC02A1. Now NFC02A1 using the same middle-ware library as NFC01A1.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rosarium | 0:892175366555 | 1 | /** |
| rosarium | 0:892175366555 | 2 | ****************************************************************************** |
| rosarium | 0:892175366555 | 3 | * @file DevI2C.h |
| rosarium | 0:892175366555 | 4 | * @author AST / EST / AMG-CL |
| rosarium | 0:892175366555 | 5 | * @version V0.0.2 |
| rosarium | 0:892175366555 | 6 | * @date 25-July-2016 |
| rosarium | 0:892175366555 | 7 | * @brief Header file for a special I2C class DevI2C which provides some |
| rosarium | 0:892175366555 | 8 | * helper function for on-board communication |
| rosarium | 0:892175366555 | 9 | ****************************************************************************** |
| rosarium | 0:892175366555 | 10 | * @attention |
| rosarium | 0:892175366555 | 11 | * |
| rosarium | 0:892175366555 | 12 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
| rosarium | 0:892175366555 | 13 | * |
| rosarium | 0:892175366555 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
| rosarium | 0:892175366555 | 15 | * are permitted provided that the following conditions are met: |
| rosarium | 0:892175366555 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
| rosarium | 0:892175366555 | 17 | * this list of conditions and the following disclaimer. |
| rosarium | 0:892175366555 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| rosarium | 0:892175366555 | 19 | * this list of conditions and the following disclaimer in the documentation |
| rosarium | 0:892175366555 | 20 | * and/or other materials provided with the distribution. |
| rosarium | 0:892175366555 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| rosarium | 0:892175366555 | 22 | * may be used to endorse or promote products derived from this software |
| rosarium | 0:892175366555 | 23 | * without specific prior written permission. |
| rosarium | 0:892175366555 | 24 | * |
| rosarium | 0:892175366555 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| rosarium | 0:892175366555 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| rosarium | 0:892175366555 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| rosarium | 0:892175366555 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| rosarium | 0:892175366555 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| rosarium | 0:892175366555 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| rosarium | 0:892175366555 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| rosarium | 0:892175366555 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| rosarium | 0:892175366555 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| rosarium | 0:892175366555 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| rosarium | 0:892175366555 | 35 | * |
| rosarium | 0:892175366555 | 36 | ****************************************************************************** |
| rosarium | 0:892175366555 | 37 | */ |
| rosarium | 0:892175366555 | 38 | |
| rosarium | 0:892175366555 | 39 | /* Define to prevent from recursive inclusion --------------------------------*/ |
| rosarium | 0:892175366555 | 40 | #ifndef __DEV_I2C_H |
| rosarium | 0:892175366555 | 41 | #define __DEV_I2C_H |
| rosarium | 0:892175366555 | 42 | |
| rosarium | 0:892175366555 | 43 | /* Includes ------------------------------------------------------------------*/ |
| rosarium | 0:892175366555 | 44 | #include "mbed.h" |
| rosarium | 0:892175366555 | 45 | |
| rosarium | 0:892175366555 | 46 | /* Classes -------------------------------------------------------------------*/ |
| rosarium | 0:892175366555 | 47 | /** Helper class DevI2C providing functions for multi-register I2C communication |
| rosarium | 0:892175366555 | 48 | * common for a series of I2C devices |
| rosarium | 0:892175366555 | 49 | */ |
| rosarium | 0:892175366555 | 50 | class DevI2C : public I2C |
| rosarium | 0:892175366555 | 51 | { |
| rosarium | 0:892175366555 | 52 | public: |
| rosarium | 0:892175366555 | 53 | |
| rosarium | 0:892175366555 | 54 | /** Create a DevI2C Master interface, connected to the specified pins |
| rosarium | 0:892175366555 | 55 | * |
| rosarium | 0:892175366555 | 56 | * @param sda I2C data line pin |
| rosarium | 0:892175366555 | 57 | * @param scl I2C clock line pin |
| rosarium | 0:892175366555 | 58 | */ |
| rosarium | 0:892175366555 | 59 | DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} |
| rosarium | 0:892175366555 | 60 | |
| rosarium | 0:892175366555 | 61 | /** |
| rosarium | 0:892175366555 | 62 | * @brief Writes a buffer towards the I2C peripheral device. |
| rosarium | 0:892175366555 | 63 | * @param pBuffer pointer to the byte-array data to send |
| rosarium | 0:892175366555 | 64 | * @param DeviceAddr specifies the peripheral device slave address. |
| rosarium | 0:892175366555 | 65 | * @param RegisterAddr specifies the internal address register |
| rosarium | 0:892175366555 | 66 | * where to start writing to (must be correctly masked). |
| rosarium | 0:892175366555 | 67 | * @param NumByteToWrite number of bytes to be written. |
| rosarium | 0:892175366555 | 68 | * @retval 0 if ok, |
| rosarium | 0:892175366555 | 69 | * @retval -1 if an I2C error has occured, or |
| rosarium | 0:892175366555 | 70 | * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) |
| rosarium | 0:892175366555 | 71 | * @note On some devices if NumByteToWrite is greater |
| rosarium | 0:892175366555 | 72 | * than one, the RegisterAddr must be masked correctly! |
| rosarium | 0:892175366555 | 73 | */ |
| rosarium | 0:892175366555 | 74 | int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, |
| rosarium | 0:892175366555 | 75 | uint16_t NumByteToWrite) |
| rosarium | 0:892175366555 | 76 | { |
| rosarium | 0:892175366555 | 77 | int ret; |
| rosarium | 0:892175366555 | 78 | uint8_t tmp[TEMP_BUF_SIZE]; |
| rosarium | 0:892175366555 | 79 | |
| rosarium | 0:892175366555 | 80 | if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; |
| rosarium | 0:892175366555 | 81 | |
| rosarium | 0:892175366555 | 82 | /* First, send device address. Then, send data and STOP condition */ |
| rosarium | 0:892175366555 | 83 | tmp[0] = RegisterAddr; |
| rosarium | 0:892175366555 | 84 | memcpy(tmp+1, pBuffer, NumByteToWrite); |
| rosarium | 0:892175366555 | 85 | |
| rosarium | 0:892175366555 | 86 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); |
| rosarium | 0:892175366555 | 87 | |
| rosarium | 0:892175366555 | 88 | if(ret) return -1; |
| rosarium | 0:892175366555 | 89 | return 0; |
| rosarium | 0:892175366555 | 90 | } |
| rosarium | 0:892175366555 | 91 | |
| rosarium | 0:892175366555 | 92 | /** |
| rosarium | 0:892175366555 | 93 | * @brief Writes a buffer towards the I2C peripheral device. |
| rosarium | 0:892175366555 | 94 | * @param pBuffer pointer to the byte-array data to send |
| rosarium | 0:892175366555 | 95 | * @param DeviceAddr specifies the peripheral device slave address. |
| rosarium | 0:892175366555 | 96 | * @param RegisterAddr specifies the internal address register |
| rosarium | 0:892175366555 | 97 | * where to start writing to (must be correctly masked). |
| rosarium | 0:892175366555 | 98 | * @param NumByteToWrite number of bytes to be written. |
| rosarium | 0:892175366555 | 99 | * @retval 0 if ok, |
| rosarium | 0:892175366555 | 100 | * @retval -1 if an I2C error has occured, or |
| rosarium | 0:892175366555 | 101 | * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) |
| rosarium | 0:892175366555 | 102 | * @note On some devices if NumByteToWrite is greater |
| rosarium | 0:892175366555 | 103 | * than one, the RegisterAddr must be masked correctly! |
| rosarium | 0:892175366555 | 104 | */ |
| rosarium | 0:892175366555 | 105 | int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
| rosarium | 0:892175366555 | 106 | uint16_t NumByteToWrite) |
| rosarium | 0:892175366555 | 107 | { |
| rosarium | 0:892175366555 | 108 | int ret; |
| rosarium | 0:892175366555 | 109 | uint8_t tmp[TEMP_BUF_SIZE]; |
| rosarium | 0:892175366555 | 110 | |
| rosarium | 0:892175366555 | 111 | if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; |
| rosarium | 0:892175366555 | 112 | |
| rosarium | 0:892175366555 | 113 | /* First, send device address. Then, send data and STOP condition */ |
| rosarium | 0:892175366555 | 114 | tmp[0] = (RegisterAddr >> 8) & 0xFF; |
| rosarium | 0:892175366555 | 115 | tmp[1] = (RegisterAddr) & 0xFF; |
| rosarium | 0:892175366555 | 116 | memcpy(tmp+2, pBuffer, NumByteToWrite); |
| rosarium | 0:892175366555 | 117 | |
| rosarium | 0:892175366555 | 118 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false); |
| rosarium | 0:892175366555 | 119 | |
| rosarium | 0:892175366555 | 120 | if(ret) return -1; |
| rosarium | 0:892175366555 | 121 | return 0; |
| rosarium | 0:892175366555 | 122 | } |
| rosarium | 0:892175366555 | 123 | /** |
| rosarium | 0:892175366555 | 124 | * @brief Reads a buffer from the I2C peripheral device. |
| rosarium | 0:892175366555 | 125 | * @param pBuffer pointer to the byte-array to read data in to |
| rosarium | 0:892175366555 | 126 | * @param DaviceAddr specifies the peripheral device slave address. |
| rosarium | 0:892175366555 | 127 | * @param RegisterAddr specifies the internal address register |
| rosarium | 0:892175366555 | 128 | * where to start reading from (must be correctly masked). |
| rosarium | 0:892175366555 | 129 | * @param NumByteToRead number of bytes to be read. |
| rosarium | 0:892175366555 | 130 | * @retval 0 if ok, |
| rosarium | 0:892175366555 | 131 | * @retval -1 if an I2C error has occured |
| rosarium | 0:892175366555 | 132 | * @note On some devices if NumByteToWrite is greater |
| rosarium | 0:892175366555 | 133 | * than one, the RegisterAddr must be masked correctly! |
| rosarium | 0:892175366555 | 134 | */ |
| rosarium | 0:892175366555 | 135 | int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, |
| rosarium | 0:892175366555 | 136 | uint16_t NumByteToRead) |
| rosarium | 0:892175366555 | 137 | { |
| rosarium | 0:892175366555 | 138 | int ret; |
| rosarium | 0:892175366555 | 139 | uint8_t reg_addr[2]; |
| rosarium | 0:892175366555 | 140 | reg_addr[0] = (RegisterAddr >> 8) & 0xFF; |
| rosarium | 0:892175366555 | 141 | reg_addr[1] = (RegisterAddr) & 0xFF; |
| rosarium | 0:892175366555 | 142 | /* Send device address, with no STOP condition */ |
| rosarium | 0:892175366555 | 143 | ret = write(DeviceAddr, (const char*)reg_addr, 2, true); |
| rosarium | 0:892175366555 | 144 | if(!ret) { |
| rosarium | 0:892175366555 | 145 | /* Read data, with STOP condition */ |
| rosarium | 0:892175366555 | 146 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); |
| rosarium | 0:892175366555 | 147 | } |
| rosarium | 0:892175366555 | 148 | |
| rosarium | 0:892175366555 | 149 | if(ret) |
| rosarium | 0:892175366555 | 150 | return -1; |
| rosarium | 0:892175366555 | 151 | else |
| rosarium | 0:892175366555 | 152 | return 0; |
| rosarium | 0:892175366555 | 153 | } |
| rosarium | 0:892175366555 | 154 | |
| rosarium | 0:892175366555 | 155 | /** |
| rosarium | 0:892175366555 | 156 | * @brief Reads a buffer from the I2C peripheral device. |
| rosarium | 0:892175366555 | 157 | * @param pBuffer pointer to the byte-array to read data in to |
| rosarium | 0:892175366555 | 158 | * @param DaviceAddr specifies the peripheral device slave address. |
| rosarium | 0:892175366555 | 159 | * @param RegisterAddr specifies the internal address register |
| rosarium | 0:892175366555 | 160 | * where to start reading from (must be correctly masked). |
| rosarium | 0:892175366555 | 161 | * @param NumByteToRead number of bytes to be read. |
| rosarium | 0:892175366555 | 162 | * @retval 0 if ok, |
| rosarium | 0:892175366555 | 163 | * @retval -1 if an I2C error has occured |
| rosarium | 0:892175366555 | 164 | * @note On some devices if NumByteToWrite is greater |
| rosarium | 0:892175366555 | 165 | * than one, the RegisterAddr must be masked correctly! |
| rosarium | 0:892175366555 | 166 | */ |
| rosarium | 0:892175366555 | 167 | int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, |
| rosarium | 0:892175366555 | 168 | uint16_t NumByteToRead) |
| rosarium | 0:892175366555 | 169 | { |
| rosarium | 0:892175366555 | 170 | int ret; |
| rosarium | 0:892175366555 | 171 | |
| rosarium | 0:892175366555 | 172 | /* Send device address, with no STOP condition */ |
| rosarium | 0:892175366555 | 173 | ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); |
| rosarium | 0:892175366555 | 174 | if(!ret) { |
| rosarium | 0:892175366555 | 175 | /* Read data, with STOP condition */ |
| rosarium | 0:892175366555 | 176 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); |
| rosarium | 0:892175366555 | 177 | } |
| rosarium | 0:892175366555 | 178 | |
| rosarium | 0:892175366555 | 179 | if(ret) return -1; |
| rosarium | 0:892175366555 | 180 | return 0; |
| rosarium | 0:892175366555 | 181 | } |
| rosarium | 0:892175366555 | 182 | |
| rosarium | 0:892175366555 | 183 | private: |
| rosarium | 0:892175366555 | 184 | static const unsigned int TEMP_BUF_SIZE = 32; |
| rosarium | 0:892175366555 | 185 | }; |
| rosarium | 0:892175366555 | 186 | |
| rosarium | 0:892175366555 | 187 | #endif /* __DEV_I2C_H */ |
