X_NUCLEO_NFC02A1 library for M24LR

Dependencies:   ST_INTERFACES

Dependents:   HelloWorld_NFC02A1_mbedOS HelloWorld_NFC02A1laatste HelloWorld_NFC02A1

Fork of X_NUCLEO_NFC02A1 by ST Expansion SW Team

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.

Revision:
4:0287f5476fe0
Parent:
3:6d76c75e3eb4
Child:
5:900640bf1cff
--- a/m24lr/NDefNfcTagM24LR.cpp	Thu Sep 29 05:48:12 2016 +0000
+++ b/m24lr/NDefNfcTagM24LR.cpp	Fri Sep 30 11:46:18 2016 +0000
@@ -142,7 +142,7 @@
   if( ((Offset == 0) && (DataSize > 0)) || (Offset > 0) )
   {
     /* Read NDEF */
-    if( NDefReadByte(CCFileStruct.NDEF_offset + index + Offset, DataSize, (pData + FIRST_RECORD_OFFSET)) != NFC_OK )
+    if( NDefReadByte(CCFileStruct.NDEF_offset + index + Offset, DataSize, (pData + FIRST_RECORD_OFFSET)) != NFC_SUCCESS )
     {
       return NDEF_ERROR;
     }
@@ -215,7 +215,7 @@
   
   /* Start write NDEF message to EEPROM */
   status = NDefWriteByte( (pData + FIRST_RECORD_OFFSET), DataSize, CCFileStruct.NDEF_offset + index + Offset);
-  if( status != NFC_OK )
+  if( status != NFC_SUCCESS )
   {
     return NDEF_ERROR;
   }
@@ -262,12 +262,12 @@
   ret_value = NDefWriteByte( pCCBuffer, 0x4, 0x00);
  
   /* If extended memory writes the next 4 bytes */
-  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_OK) )
+  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) )
   {
     ret_value = NDefWriteByte( pCCBuffer + 4, 4, 0x04);
   }
 
-  if( ret_value != NFC_OK )
+  if( ret_value != NFC_SUCCESS )
   {
     return NDEF_ERROR;
   }
@@ -286,12 +286,12 @@
   ret_value = NDefReadByte(0x00, 4, pCCBuffer);
 
   /* If extended memory reads the next 4 bytes */
-  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_OK) )
+  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) )
   {
     ret_value = NDefReadByte(0x04, 4, pCCBuffer + 4 );
   }
   
-  if( ret_value != NFC_OK )
+  if( ret_value != NFC_SUCCESS )
   {
     return NDEF_ERROR;
   }
@@ -300,7 +300,7 @@
 }
 uint16_t NDefNfcTagM24LR::NfcType5_TT5Init( void )
 {
-  bool ret_value = NFC_OK;
+  bool ret_value ;
   uint16_t status;
   uint8_t accbuffer[8];
   uint8_t cdata;
@@ -331,7 +331,7 @@
   /* Update T */
   cdata = NFCT5_NDEF_MSG_TLV;
   ret_value = NDefWriteByte( &cdata, 1, CCFileStruct.NDEF_offset);
-  if( ret_value != NFC_OK )
+  if( ret_value != NFC_SUCCESS )
   {
     return NDEF_ERROR;
   }
@@ -339,7 +339,7 @@
   /* Update L */
   cdata = 0x00;
   ret_value = NDefWriteByte( &cdata, 1, (CCFileStruct.NDEF_offset + 1));
-  if( ret_value != NFC_OK )
+  if( ret_value != NFC_SUCCESS )
   {
     return NDEF_ERROR;
   }
@@ -400,7 +400,7 @@
   CCFileStruct.TT5Tag = acc_buffer[3];
   
   /* Search for position of NDEF TLV in memory and tag status */
-  while( ( NDefReadByte(CCFileStruct.NDEF_offset, 4, atlv_detect) == NFC_OK ) && ( CCFileStruct.NDEF_offset < memory_size ) )
+  while( ( NDefReadByte(CCFileStruct.NDEF_offset, 4, atlv_detect) == NFC_SUCCESS ) && ( CCFileStruct.NDEF_offset < memory_size ) )
   {
     /* Detect first NDEF Message in memory */
     if( atlv_detect[0] == NFCT5_NDEF_MSG_TLV )
@@ -449,43 +449,61 @@
 }
 
 bool NDefNfcTagM24LR::openSession(bool force) {
-bool status;
+uint16_t status;
 
       status = NfcType5_NDEFDetection();
 
-      if ( status == NFC_SUCCESS ) 
+      if ( status != NDEF_OK ) 
       {
         CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE;
       CCFileStruct.Version = NFCT5_VERSION_V1_0;
       CCFileStruct.MemorySize = ( M24LR_MAX_SIZE / 8 ) & 0xFF;
       CCFileStruct.TT5Tag = 0x05;
       /* Init of the Type Tag 5 component (M24LR) */
-      while( NfcType5_TT5Init( ) != NFC_OK );
+      while( NfcType5_TT5Init( ) != NDEF_OK );
       }
       mIsSessionOpen = 1;
-      return NFC_SUCCESS; 
+      return true; 
 
 }
 
-bool NDefNfcTagM24LR::closeSession() {
+bool NDefNfcTagM24LR::closeSession() 
+{
     
-    return NFC_SUCCESS;
+    return true;
 }
+
 bool NDefNfcTagM24LR::writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
-        byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
-                  return WriteData(offset, length, (uint8_t*)buffer);
-                }
+        byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)
+{
+  
+  if(WriteData(offset, length, (uint8_t*)buffer)== NDEF_OK){
+    callback(callbackStatus,true,buffer,length);
+    return true;
+  }
+  callback(callbackStatus,false,buffer,length);
+  return false;
+}
+                
 bool NDefNfcTagM24LR::readByte(const uint16_t byteOffset, const uint16_t length,
-        uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
-                  return ReadData(byteOffset, length, (uint8_t*)buffer);
-                }
+        uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)
+{
+  
+  //first it reads the 2 byte for the length then when it will have the call back it reads the real message
+  if(ReadData(byteOffset, length, (uint8_t*)buffer)== NDEF_OK){
+    callback(callbackStatus,true,buffer,length);
+    return true;
+  }
+  callback(callbackStatus,false,buffer,length);
+  return false;
+}
 
-bool NDefNfcTagM24LR::NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset){
+uint16_t NDefNfcTagM24LR::NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset){
       return mDevice.UpdateBinary(offset, length, (uint8_t*)buffer);
 }
 
 
-bool NDefNfcTagM24LR::NDefReadByte(const uint16_t byteOffset, const uint16_t length, uint8_t *buffer){
+uint16_t NDefNfcTagM24LR::NDefReadByte(const uint16_t byteOffset, const uint16_t length, uint8_t *buffer){
   return mDevice.ReadBinary(byteOffset, length, (uint8_t*)buffer);
 }