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