STMicroelectronics' M24SR NFC Dynamic Tag Library.

Dependencies:   ST_INTERFACES

Dependents:   X_NUCLEO_NFC01A1

Fork of M24SR by ST Expansion SW Team

M24SR series Dynamic NFC Tags

The M24SR series provides an NFC forum tag type 4 RF interface and supports the NFC data exchange format (NDEF). This enables NFC use cases such as simple Bluetooth pairing and other connection handovers, automatic links to URLs, storage of Vcard and other types of information. It can be used in a wide variety of applications, including consumer electronics, computer peripherals, home appliances, industrial automation and healthcare products.

  • NFC forum tag type 4 based on ISO 14443 RF interface
  • 1 MHz I²C serial interface operating from 2.7 to 5.5 V
  • EEPROM memory density from 2 Kbits to 64 Kbits with built-in NDEF message support
  • RF disable pin allowing the application to control RF access from NFC phones
  • 128-bit password protection
  • General-purpose output pin allowing flexibility for the applications (wake up on several types of events)
  • Simple antenna design, backward compatible with M24LR series

For further information and ordering please refer to the ST Page.

HelloWorld application

Import programHelloWorld_Async_M24SR

M24SR NFC example. Simple application to asynchronously write and read an URL from a M24SR tag.

Committer:
nikapov
Date:
Mon Jul 31 12:26:02 2017 +0000
Revision:
0:11161008d77a
First revision.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:11161008d77a 1 /**
nikapov 0:11161008d77a 2 ******************************************************************************
nikapov 0:11161008d77a 3 * @file Type4NfcTagM24SR.h
nikapov 0:11161008d77a 4 * @author ST Central Labs
nikapov 0:11161008d77a 5 * @version V2.0.0
nikapov 0:11161008d77a 6 * @date 28 Apr 2017
nikapov 0:11161008d77a 7 * @brief M24SR specific NDefLib derived class
nikapov 0:11161008d77a 8 ******************************************************************************
nikapov 0:11161008d77a 9 * @attention
nikapov 0:11161008d77a 10 *
nikapov 0:11161008d77a 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
nikapov 0:11161008d77a 12 *
nikapov 0:11161008d77a 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:11161008d77a 14 * are permitted provided that the following conditions are met:
nikapov 0:11161008d77a 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:11161008d77a 16 * this list of conditions and the following disclaimer.
nikapov 0:11161008d77a 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:11161008d77a 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:11161008d77a 19 * and/or other materials provided with the distribution.
nikapov 0:11161008d77a 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:11161008d77a 21 * may be used to endorse or promote products derived from this software
nikapov 0:11161008d77a 22 * without specific prior written permission.
nikapov 0:11161008d77a 23 *
nikapov 0:11161008d77a 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:11161008d77a 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:11161008d77a 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:11161008d77a 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:11161008d77a 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:11161008d77a 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:11161008d77a 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:11161008d77a 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:11161008d77a 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:11161008d77a 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:11161008d77a 34 *
nikapov 0:11161008d77a 35 ******************************************************************************
nikapov 0:11161008d77a 36 */
nikapov 0:11161008d77a 37
nikapov 0:11161008d77a 38 #ifndef X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
nikapov 0:11161008d77a 39 #define X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
nikapov 0:11161008d77a 40
nikapov 0:11161008d77a 41 #include <cstdlib>
nikapov 0:11161008d77a 42 #include <stdint.h>
nikapov 0:11161008d77a 43
nikapov 0:11161008d77a 44 #include "NDefLib/NDefNfcTag.h"
nikapov 0:11161008d77a 45
nikapov 0:11161008d77a 46 #include "M24SR.h"
nikapov 0:11161008d77a 47
nikapov 0:11161008d77a 48 /**
nikapov 0:11161008d77a 49 * Helper class to use the NDefLib
nikapov 0:11161008d77a 50 */
nikapov 0:11161008d77a 51 class NDefNfcTagM24SR: public NDefLib::NDefNfcTag {
nikapov 0:11161008d77a 52
nikapov 0:11161008d77a 53 public:
nikapov 0:11161008d77a 54
nikapov 0:11161008d77a 55 /**
nikapov 0:11161008d77a 56 *
nikapov 0:11161008d77a 57 * @param device device to use
nikapov 0:11161008d77a 58 */
nikapov 0:11161008d77a 59 NDefNfcTagM24SR(M24SR &device):
nikapov 0:11161008d77a 60 NDefLib::NDefNfcTag(),
nikapov 0:11161008d77a 61 mDevice(device),mIsSessionOpen(false),
nikapov 0:11161008d77a 62 mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF),
nikapov 0:11161008d77a 63 mOpenSessionCallback(*this),
nikapov 0:11161008d77a 64 mCloseSessionCallback(*this),
nikapov 0:11161008d77a 65 mWriteByteCallback(*this),
nikapov 0:11161008d77a 66 mReadByteCallback(*this){}
nikapov 0:11161008d77a 67
nikapov 0:11161008d77a 68 virtual bool open_session(bool force = false);
nikapov 0:11161008d77a 69
nikapov 0:11161008d77a 70 virtual bool close_session();
nikapov 0:11161008d77a 71
nikapov 0:11161008d77a 72 virtual bool is_session_open(){
nikapov 0:11161008d77a 73 return mIsSessionOpen;
nikapov 0:11161008d77a 74 }
nikapov 0:11161008d77a 75
nikapov 0:11161008d77a 76 /**
nikapov 0:11161008d77a 77 * Close the open session.
nikapov 0:11161008d77a 78 */
nikapov 0:11161008d77a 79 virtual ~NDefNfcTagM24SR() {
nikapov 0:11161008d77a 80 if (is_session_open()) {
nikapov 0:11161008d77a 81 close_session();
nikapov 0:11161008d77a 82 }
nikapov 0:11161008d77a 83 }//~NDefNfcTagM24SR
nikapov 0:11161008d77a 84
nikapov 0:11161008d77a 85 protected:
nikapov 0:11161008d77a 86
nikapov 0:11161008d77a 87 virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
nikapov 0:11161008d77a 88 byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
nikapov 0:11161008d77a 89
nikapov 0:11161008d77a 90 virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
nikapov 0:11161008d77a 91 uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
nikapov 0:11161008d77a 92
nikapov 0:11161008d77a 93 private:
nikapov 0:11161008d77a 94
nikapov 0:11161008d77a 95 M24SR &mDevice;
nikapov 0:11161008d77a 96
nikapov 0:11161008d77a 97 /**
nikapov 0:11161008d77a 98 * true if the session is open
nikapov 0:11161008d77a 99 */
nikapov 0:11161008d77a 100 bool mIsSessionOpen;
nikapov 0:11161008d77a 101
nikapov 0:11161008d77a 102 /**
nikapov 0:11161008d77a 103 * Max length for a read operation
nikapov 0:11161008d77a 104 */
nikapov 0:11161008d77a 105 uint16_t mMaxReadBytes;
nikapov 0:11161008d77a 106
nikapov 0:11161008d77a 107 /**
nikapov 0:11161008d77a 108 * Max length for a write operation
nikapov 0:11161008d77a 109 */
nikapov 0:11161008d77a 110 uint16_t mMaxWriteBytes;
nikapov 0:11161008d77a 111
nikapov 0:11161008d77a 112 /**
nikapov 0:11161008d77a 113 * Class containing the callback needed to open a session and read the max
nikapov 0:11161008d77a 114 * read/write size
nikapov 0:11161008d77a 115 */
nikapov 0:11161008d77a 116 class OpenSessionCallBack: public M24SR::Callbacks{
nikapov 0:11161008d77a 117 public:
nikapov 0:11161008d77a 118 OpenSessionCallBack(NDefNfcTagM24SR &sender);
nikapov 0:11161008d77a 119
nikapov 0:11161008d77a 120 virtual void on_session_open(M24SR *nfc,M24SR::StatusTypeDef status);
nikapov 0:11161008d77a 121 virtual void on_selected_application(M24SR *nfc,M24SR::StatusTypeDef status);
nikapov 0:11161008d77a 122 virtual void on_selected_CC_file(M24SR *nfc,M24SR::StatusTypeDef status);
nikapov 0:11161008d77a 123 virtual void on_read_byte(M24SR *nfc,M24SR::StatusTypeDef status,uint16_t offset,
nikapov 0:11161008d77a 124 uint8_t *readByte, uint16_t nReadByte);
nikapov 0:11161008d77a 125 virtual void on_selected_NDEF_file(M24SR *nfc,M24SR::StatusTypeDef status);
nikapov 0:11161008d77a 126
nikapov 0:11161008d77a 127 private:
nikapov 0:11161008d77a 128 /**
nikapov 0:11161008d77a 129 * Object that send the open session callback
nikapov 0:11161008d77a 130 */
nikapov 0:11161008d77a 131 NDefNfcTagM24SR &mSender;
nikapov 0:11161008d77a 132
nikapov 0:11161008d77a 133 /**
nikapov 0:11161008d77a 134 * number of trials done for open the session
nikapov 0:11161008d77a 135 */
nikapov 0:11161008d77a 136 uint32_t mNTrials;
nikapov 0:11161008d77a 137
nikapov 0:11161008d77a 138 /**
nikapov 0:11161008d77a 139 * buffer where read the CC file
nikapov 0:11161008d77a 140 */
nikapov 0:11161008d77a 141 uint8_t CCFile[15];
nikapov 0:11161008d77a 142 };
nikapov 0:11161008d77a 143
nikapov 0:11161008d77a 144 OpenSessionCallBack mOpenSessionCallback;
nikapov 0:11161008d77a 145 friend class OpenSessionCallBack;
nikapov 0:11161008d77a 146
nikapov 0:11161008d77a 147 /**
nikapov 0:11161008d77a 148 * Class containing the callback needed to close a session
nikapov 0:11161008d77a 149 */
nikapov 0:11161008d77a 150 class CloseSessionCallBack : public M24SR::Callbacks{
nikapov 0:11161008d77a 151 public:
nikapov 0:11161008d77a 152 CloseSessionCallBack(NDefNfcTagM24SR &sender):
nikapov 0:11161008d77a 153 mSender(sender){}
nikapov 0:11161008d77a 154
nikapov 0:11161008d77a 155 virtual void on_deselect(M24SR *nfc,M24SR::StatusTypeDef status){
nikapov 0:11161008d77a 156 (void)nfc;
nikapov 0:11161008d77a 157 if (status==M24SR::M24SR_SUCCESS) {
nikapov 0:11161008d77a 158 mSender.mIsSessionOpen = false;
nikapov 0:11161008d77a 159 mSender.mCallBack->on_session_close(&mSender,true);
nikapov 0:11161008d77a 160 } else {
nikapov 0:11161008d77a 161 mSender.mCallBack->on_session_close(&mSender,false);
nikapov 0:11161008d77a 162 }//if-else
nikapov 0:11161008d77a 163 }
nikapov 0:11161008d77a 164 private:
nikapov 0:11161008d77a 165
nikapov 0:11161008d77a 166 /**
nikapov 0:11161008d77a 167 * Object that send the open session callback
nikapov 0:11161008d77a 168 */
nikapov 0:11161008d77a 169 NDefNfcTagM24SR &mSender;
nikapov 0:11161008d77a 170 };
nikapov 0:11161008d77a 171
nikapov 0:11161008d77a 172 CloseSessionCallBack mCloseSessionCallback;
nikapov 0:11161008d77a 173 friend class OpenSessionCallBack;
nikapov 0:11161008d77a 174
nikapov 0:11161008d77a 175 /**
nikapov 0:11161008d77a 176 * Class containing the callback needed to write a buffer
nikapov 0:11161008d77a 177 */
nikapov 0:11161008d77a 178 class WriteByteCallback : public M24SR::Callbacks{
nikapov 0:11161008d77a 179 public:
nikapov 0:11161008d77a 180
nikapov 0:11161008d77a 181 /**
nikapov 0:11161008d77a 182 *
nikapov 0:11161008d77a 183 * @param sender tag where write the buffer
nikapov 0:11161008d77a 184 */
nikapov 0:11161008d77a 185 WriteByteCallback(NDefNfcTagM24SR &sender):
nikapov 0:11161008d77a 186 mByteToWrite(NULL),
nikapov 0:11161008d77a 187 mNByteToWrite(0),
nikapov 0:11161008d77a 188 mByteWrote(0),
nikapov 0:11161008d77a 189 mCallback(NULL),
nikapov 0:11161008d77a 190 mCallbackParam(NULL),
nikapov 0:11161008d77a 191 mSender(sender){}
nikapov 0:11161008d77a 192
nikapov 0:11161008d77a 193 /**
nikapov 0:11161008d77a 194 * Set the buffer to write and the function to call when finish
nikapov 0:11161008d77a 195 * @param buffer Buffer to write.
nikapov 0:11161008d77a 196 * @param nByte Number of bytes to write.
nikapov 0:11161008d77a 197 * @param callback Function to call when the write ends.
nikapov 0:11161008d77a 198 * @param param Parameter to pass to the callback function.
nikapov 0:11161008d77a 199 */
nikapov 0:11161008d77a 200 void set_task(const uint8_t *buffer,uint16_t nByte,
nikapov 0:11161008d77a 201 byteOperationCallback_t callback,CallbackStatus_t *param) {
nikapov 0:11161008d77a 202 mByteToWrite=buffer;
nikapov 0:11161008d77a 203 mNByteToWrite=nByte;
nikapov 0:11161008d77a 204 mByteWrote=0;
nikapov 0:11161008d77a 205 mCallback = callback;
nikapov 0:11161008d77a 206 mCallbackParam = param;
nikapov 0:11161008d77a 207 }
nikapov 0:11161008d77a 208
nikapov 0:11161008d77a 209 virtual void on_updated_binary(M24SR *nfc,M24SR::StatusTypeDef status,
nikapov 0:11161008d77a 210 uint16_t startOffset,uint8_t *writeByte,uint16_t nWriteByte);
nikapov 0:11161008d77a 211
nikapov 0:11161008d77a 212
nikapov 0:11161008d77a 213 private:
nikapov 0:11161008d77a 214
nikapov 0:11161008d77a 215 /** buffer to write */
nikapov 0:11161008d77a 216 const uint8_t *mByteToWrite;
nikapov 0:11161008d77a 217 /** length of the buffer */
nikapov 0:11161008d77a 218 uint16_t mNByteToWrite;
nikapov 0:11161008d77a 219 /** number of byte already wrote */
nikapov 0:11161008d77a 220 uint16_t mByteWrote;
nikapov 0:11161008d77a 221
nikapov 0:11161008d77a 222 /** function to call when all the bytes are write */
nikapov 0:11161008d77a 223 byteOperationCallback_t mCallback;
nikapov 0:11161008d77a 224 /** parameter to pass to the callback function*/
nikapov 0:11161008d77a 225 CallbackStatus_t* mCallbackParam;
nikapov 0:11161008d77a 226
nikapov 0:11161008d77a 227 /** tag where write the buffer*/
nikapov 0:11161008d77a 228 NDefNfcTagM24SR &mSender;
nikapov 0:11161008d77a 229 };
nikapov 0:11161008d77a 230
nikapov 0:11161008d77a 231 WriteByteCallback mWriteByteCallback;
nikapov 0:11161008d77a 232 friend class WriteByteCallback;
nikapov 0:11161008d77a 233
nikapov 0:11161008d77a 234 /**
nikapov 0:11161008d77a 235 * Class containing the callback needed to read a buffer
nikapov 0:11161008d77a 236 */
nikapov 0:11161008d77a 237 class ReadByteCallback : public M24SR::Callbacks{
nikapov 0:11161008d77a 238 public:
nikapov 0:11161008d77a 239
nikapov 0:11161008d77a 240 /**
nikapov 0:11161008d77a 241 *
nikapov 0:11161008d77a 242 * @param Sender tag where read the buffer
nikapov 0:11161008d77a 243 */
nikapov 0:11161008d77a 244 ReadByteCallback(NDefNfcTagM24SR &sender):
nikapov 0:11161008d77a 245 mBuffer(NULL),
nikapov 0:11161008d77a 246 mNByteToRead(0),
nikapov 0:11161008d77a 247 mByteRead(0),
nikapov 0:11161008d77a 248 mCallback(NULL),
nikapov 0:11161008d77a 249 mCallbackParam(NULL),
nikapov 0:11161008d77a 250 mSender(sender){}
nikapov 0:11161008d77a 251
nikapov 0:11161008d77a 252 /**
nikapov 0:11161008d77a 253 * Set the buffer where read the data and the function to call when finish
nikapov 0:11161008d77a 254 * @param buffer Buffer read
nikapov 0:11161008d77a 255 * @param nByte Number of bytes to read
nikapov 0:11161008d77a 256 * @param callback Function to call when the read ends
nikapov 0:11161008d77a 257 * @param param Parameter to pass to the callback function
nikapov 0:11161008d77a 258 */
nikapov 0:11161008d77a 259 void set_task(uint8_t *buffer,uint16_t nByte,
nikapov 0:11161008d77a 260 byteOperationCallback_t callback,CallbackStatus_t *param){
nikapov 0:11161008d77a 261 mBuffer=buffer;
nikapov 0:11161008d77a 262 mNByteToRead=nByte;
nikapov 0:11161008d77a 263 mByteRead=0;
nikapov 0:11161008d77a 264 mCallback = callback;
nikapov 0:11161008d77a 265 mCallbackParam = param;
nikapov 0:11161008d77a 266 }
nikapov 0:11161008d77a 267
nikapov 0:11161008d77a 268 virtual void on_read_byte(M24SR *nfc,M24SR::StatusTypeDef status,
nikapov 0:11161008d77a 269 uint16_t offset,uint8_t *readByte, uint16_t nReadByte);
nikapov 0:11161008d77a 270
nikapov 0:11161008d77a 271
nikapov 0:11161008d77a 272 private:
nikapov 0:11161008d77a 273
nikapov 0:11161008d77a 274 /**
nikapov 0:11161008d77a 275 * Buffer where read the data
nikapov 0:11161008d77a 276 */
nikapov 0:11161008d77a 277 uint8_t *mBuffer;
nikapov 0:11161008d77a 278
nikapov 0:11161008d77a 279 /**
nikapov 0:11161008d77a 280 * Number of bytes to read
nikapov 0:11161008d77a 281 */
nikapov 0:11161008d77a 282 uint16_t mNByteToRead;
nikapov 0:11161008d77a 283
nikapov 0:11161008d77a 284 /**
nikapov 0:11161008d77a 285 * Number of bytes already read
nikapov 0:11161008d77a 286 */
nikapov 0:11161008d77a 287 uint16_t mByteRead;
nikapov 0:11161008d77a 288
nikapov 0:11161008d77a 289 /**
nikapov 0:11161008d77a 290 * Function to call when all the bytes are read
nikapov 0:11161008d77a 291 */
nikapov 0:11161008d77a 292 byteOperationCallback_t mCallback;
nikapov 0:11161008d77a 293
nikapov 0:11161008d77a 294 /**
nikapov 0:11161008d77a 295 * Parameter to pass to the callback function
nikapov 0:11161008d77a 296 */
nikapov 0:11161008d77a 297 CallbackStatus_t* mCallbackParam;
nikapov 0:11161008d77a 298
nikapov 0:11161008d77a 299 /**
nikapov 0:11161008d77a 300 * Tag where the data are read
nikapov 0:11161008d77a 301 */
nikapov 0:11161008d77a 302 NDefNfcTagM24SR &mSender;
nikapov 0:11161008d77a 303 };
nikapov 0:11161008d77a 304
nikapov 0:11161008d77a 305 ReadByteCallback mReadByteCallback;
nikapov 0:11161008d77a 306 friend class ReadByteCallback;
nikapov 0:11161008d77a 307
nikapov 0:11161008d77a 308
nikapov 0:11161008d77a 309 };
nikapov 0:11161008d77a 310
nikapov 0:11161008d77a 311 #endif /* X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_ */