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:
8:7c4cf671960b
Parent:
6:8c1eca41b3a9
--- a/m24lr/NDefNfcTagM24LR.cpp	Tue Jul 11 15:23:56 2017 +0000
+++ b/m24lr/NDefNfcTagM24LR.cpp	Wed Jul 12 12:34:12 2017 +0000
@@ -37,7 +37,6 @@
 
 
 #include <cmath>
-
 #include "NDefNfcTagM24LR.h"
 
 
@@ -75,11 +74,11 @@
 #define NFCTAG_16M_SIZE           0x800
 #define NFCTAG_64M_SIZE           0x2000
 
-    
 #define MAX_NDEF_MEM                 0x200
 #define M24LR_MAX_SIZE               NFCTAG_4M_SIZE
 #define M24LR_NDEF_MAX_SIZE          MIN(M24LR_MAX_SIZE,MAX_NDEF_MEM)
 #define NFC_DEVICE_MAX_NDEFMEMORY    M24LR_NDEF_MAX_SIZE
+
 /**
   * @brief  This function read the data stored in NDEF file at defined offset.
   * @param  Offset : Offset in the NDEF file.
@@ -90,63 +89,53 @@
   * @retval NDEF_ERROR : No NDEF in the tag.
   * @retval NDEF_OK : The operation is completed.
   */
-uint16_t  NDefNfcTagM24LR::ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData )
+uint16_t NDefNfcTagM24LR::ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData )
 {
   uint16_t status = NDEF_ERROR;
   uint8_t atlv_detect[4];
   uint8_t index = 0;
   
   /* If too many data to write return error */
-  if( DataSize > NDEF_MAX_SIZE )
-  {
+  if ( DataSize > NDEF_MAX_SIZE ) {
     return NDEF_ERROR_MEMORY_INTERNAL;
   }
   
   /* Detect NDEF message in memory */
   status = NfcType5_NDEFDetection( );
-  if( status != NDEF_OK )
-  {
+  if ( status != NDEF_OK ) {
     return status;
   }
 
   /* Read TL of Type 5 */
   status = NDefReadByte(CCFileStruct.NDEF_offset, 4, atlv_detect);
-  if( status != NDEF_OK )
-  {
+  if ( status != NDEF_OK ){
     return status;
   }
   
-  if( atlv_detect[1] == NFCT5_3_BYTES_L_TLV )
-      index=2;
-
-  if(Offset==0 && DataSize>=2){
-      if( atlv_detect[1] == NFCT5_3_BYTES_L_TLV )
-      {
-        pData[0] = atlv_detect[2];
-        pData[1] = atlv_detect[3];
+  if ( atlv_detect[1] == NFCT5_3_BYTES_L_TLV ) {
+    index = 2;
+  }
 
-      }
-      else
-      {
-        pData[0] = 0x00;
-        pData[1] = atlv_detect[1];
-
-      }
-      DataSize-=2;
-      pData+=2;
+  if (Offset==0 && DataSize>=2) {
+    if ( atlv_detect[1] == NFCT5_3_BYTES_L_TLV ) {
+      pData[0] = atlv_detect[2];
+      pData[1] = atlv_detect[3];
+    } else {
+      pData[0] = 0x00;
+      pData[1] = atlv_detect[1];
+    }
+    DataSize -= 2;
+    pData += 2;
   }
 
   /* Check CC file is in the correct mode to proceed */
-  if( CCFileStruct.State ==  TT5_INITIALIZED )
-  {
+  if ( CCFileStruct.State ==  TT5_INITIALIZED ) {
     return NDEF_ERROR;
   }
 
-  if(DataSize > 0)
-  {
+  if (DataSize > 0) {
     /* Read NDEF */
-    if( NDefReadByte(CCFileStruct.NDEF_offset + index + Offset, DataSize, pData) != NFC_SUCCESS )
-    {
+    if ( NDefReadByte(CCFileStruct.NDEF_offset + index + Offset, DataSize, pData) != NFC_SUCCESS ) {
       return NDEF_ERROR;
     }
   }
@@ -175,14 +164,12 @@
   DataSize -= FIRST_RECORD_OFFSET;
   
   /* If too many data to write return error */
-  if( DataSize > NDEF_MAX_SIZE )
-  {
+  if ( DataSize > NDEF_MAX_SIZE ) {
     return NDEF_ERROR_MEMORY_INTERNAL;
   }
   
   /* Detect NDEF message in memory */
-  if( NfcType5_NDEFDetection( ) != NDEF_OK )
-  {
+  if ( NfcType5_NDEFDetection( ) != NDEF_OK ) {
     return NDEF_ERROR;
   }
  
@@ -191,56 +178,41 @@
   NDEF_Size = NDEF_Size | (uint16_t)(pData[1] );
   
   /* If entire NDEF is written, update Length of Type 5 */
-  if( DataSize == NDEF_Size )
-  {
+  if ( DataSize == NDEF_Size ) {
     /* Check if L is on 3 or 1 byte */
-    if( NDEF_Size >= NFCT5_3_BYTES_L_TLV )
-    {
+    if ( NDEF_Size >= NFCT5_3_BYTES_L_TLV ) {
       /* First init L with 0, will be updated at the end */
       atlv[1] = 0x00;
       atlv[2] = 0x00;
       atlv[3] = 0x00;
-      
       status = NDefWriteByte( (atlv + 1), 3, (CCFileStruct.NDEF_offset + 1));
-      
       index += 4; 
-    }
-    else
-    {
+    } else {
       /* First inti L with 0, will be updated at the end */
       atlv[1] = 0x00;
-      
       status = NDefWriteByte( (atlv + 1), 1, (CCFileStruct.NDEF_offset + 1));
-      
       index += 2;
     }
   }
   
   /* Start write NDEF message to EEPROM */
   status = NDefWriteByte( (pData + FIRST_RECORD_OFFSET), DataSize, CCFileStruct.NDEF_offset + index + Offset);
-  if( status != NFC_SUCCESS )
-  {
+  if ( status != NFC_SUCCESS ) {
     return NDEF_ERROR;
   }
   
   /* If entire NDEF is written, update Length of Type 5 */
-  if( DataSize == NDEF_Size )
-  {
+  if ( DataSize == NDEF_Size ) {
     /* Check if L is on 3 or 1 byte */
-    if( NDEF_Size >= NFCT5_3_BYTES_L_TLV )
-    {
+    if ( NDEF_Size >= NFCT5_3_BYTES_L_TLV ) {
       /* Update Length value */
       atlv[1] = NFCT5_3_BYTES_L_TLV;
       atlv[2] = pData[0];
       atlv[3] = pData[1];
-      
       status = NDefWriteByte( (atlv + 1), 3, (CCFileStruct.NDEF_offset + 1));
-    }
-    else
-    {
+    } else {
       /* Update Length value */
       atlv[1] = pData[1];
-      
       status = NDefWriteByte( (atlv + 1), 1, (CCFileStruct.NDEF_offset + 1));
     }
 
@@ -254,7 +226,7 @@
 
 /**
   * @brief  This functions writes CCFile in EEPROM.
-  * @Param  pCCBuffer : pointer on the buffer containnig CC file.
+  * @param  pCCBuffer : pointer on the buffer containnig CC file.
   * @retval NFCTAG status.
   */
 uint16_t NDefNfcTagM24LR::NfcType5_WriteCCFile( const uint8_t * const pCCBuffer )
@@ -265,22 +237,22 @@
   ret_value = NDefWriteByte( pCCBuffer, 0x4, 0x00);
  
   /* If extended memory writes the next 4 bytes */
-  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) )
-  {
+  if ( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) ) {
     ret_value = NDefWriteByte( pCCBuffer + 4, 4, 0x04);
   }
 
-  if( ret_value != NFC_SUCCESS )
-  {
+  if ( ret_value != NFC_SUCCESS ) {
     return NDEF_ERROR;
   }
   
-    return NDEF_OK;
+  return NDEF_OK;
 }
 
-
-
-
+/**
+  * @brief  This functions reads CCFile from EEPROM.
+  * @param  pCCBuffer : pointer on the buffer containnig CC file.
+  * @retval NFCTAG status.
+  */
 uint16_t NDefNfcTagM24LR::NfcType5_ReadCCFile( uint8_t * const pCCBuffer )
 {
   bool ret_value;
@@ -289,18 +261,22 @@
   ret_value = NDefReadByte(0x00, 4, pCCBuffer);
 
   /* If extended memory reads the next 4 bytes */
-  if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) )
-  {
+  if ( (pCCBuffer[2] == 0x00) && (ret_value == NFC_SUCCESS) ) {
     ret_value = NDefReadByte(0x04, 4, pCCBuffer + 4 );
   }
   
-  if( ret_value != NFC_SUCCESS )
-  {
+  if ( ret_value != NFC_SUCCESS ) {
     return NDEF_ERROR;
   }
   
   return NDEF_OK;
 }
+
+/**
+  * @brief  This functions initializes Nfc Type.
+  * @param  None.
+  * @retval NFCTAG status.
+  */
 uint16_t NDefNfcTagM24LR::NfcType5_TT5Init( void )
 {
   bool ret_value ;
@@ -316,8 +292,7 @@
   CCFileStruct.NDEF_offset = 0x04;
   
   /* If extended memory prepare the length bytes */
-  if( CCFileStruct.MemorySize == NFCT5_EXTENDED_CCFILE )
-  {
+  if ( CCFileStruct.MemorySize == NFCT5_EXTENDED_CCFILE ) {
     accbuffer[6] = (uint8_t)(CCFileStruct.ExtMemorySize >> 8);
     accbuffer[7] = (uint8_t)(CCFileStruct.ExtMemorySize & 0xFF);
     CCFileStruct.NDEF_offset = 0x08;
@@ -325,8 +300,7 @@
   
   /* Update CCFile */
   status = NfcType5_WriteCCFile( accbuffer );
-  if( status != NDEF_OK )
-  {
+  if ( status != NDEF_OK ) {
     return status;
   }
   
@@ -334,23 +308,26 @@
   /* Update T */
   cdata = NFCT5_NDEF_MSG_TLV;
   ret_value = NDefWriteByte( &cdata, 1, CCFileStruct.NDEF_offset);
-  if( ret_value != NFC_SUCCESS )
-  {
+  if ( ret_value != NFC_SUCCESS ) {
     return NDEF_ERROR;
   }
 
   /* Update L */
   cdata = 0x00;
   ret_value = NDefWriteByte( &cdata, 1, (CCFileStruct.NDEF_offset + 1));
-  if( ret_value != NFC_SUCCESS )
-  {
+  if ( ret_value != NFC_SUCCESS ) {
     return NDEF_ERROR;
   }
   
   return NDEF_OK;
 }
 
-
+/**
+  * @brief  This functions detects Nfc Tyoe.
+  * @param  None.
+  * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported.
+  * @retval NDEF_OK : The operation is completed.
+  */
 uint16_t NDefNfcTagM24LR::NfcType5_NDEFDetection( void )
 {
   uint8_t acc_buffer[8];
@@ -362,34 +339,26 @@
   
   /* Read CCFile */
   status = NfcType5_ReadCCFile( acc_buffer );
-  if( status != NDEF_OK )
-  {
+  if ( status != NDEF_OK ) {
     return status;
   }
   
   /* Check Byte 0 is equal to magic number */
-  if( ( acc_buffer[0] != NFCT5_MAGICNUMBER_E1_CCFILE ) && ( acc_buffer[0] != NFCT5_MAGICNUMBER_E2_CCFILE ) )
-  {
+  if ( ( acc_buffer[0] != NFCT5_MAGICNUMBER_E1_CCFILE ) && ( acc_buffer[0] != NFCT5_MAGICNUMBER_E2_CCFILE ) ) {
     return NDEF_ERROR_NOT_FORMATED;
-  }
-  /* Check Version number */
-  else if( ( (acc_buffer[1]&0xFC) != 0x40 ) )
-  {
+  } else if ( ( (acc_buffer[1]&0xFC) != 0x40 ) ) { /* Check Version number */
     return NDEF_ERROR_NOT_FORMATED;
   }
   
   /* Check if CCFile is on 4 Bytes or 8 Bytes */
-  if( acc_buffer[2] == 0x00 )
-  {
+  if ( acc_buffer[2] == 0x00 ) {
     /* Update CCFIle structure */
     CCFileStruct.MemorySize = 0x0;
     CCFileStruct.ExtMemorySize = (uint16_t)acc_buffer[6];
     CCFileStruct.ExtMemorySize = ( CCFileStruct.ExtMemorySize << 8 ) |  acc_buffer[7];
     memory_size = CCFileStruct.ExtMemorySize;
     CCFileStruct.NDEF_offset = 8;
-  }
-  else
-  {
+  } else {
     /* Update CCFIle structure */
     CCFileStruct.MemorySize = acc_buffer[2];
     CCFileStruct.ExtMemorySize = 0x0;
@@ -403,127 +372,153 @@
   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_SUCCESS ) && ( 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 )
-    {
-      if( atlv_detect[1] == 0x00 )
-      {
+    if ( atlv_detect[0] == NFCT5_NDEF_MSG_TLV ) {
+      if ( atlv_detect[1] == 0x00 ) {
         CCFileStruct.State = TT5_INITIALIZED;
-      }
-      else
-      {
-        if( CCFileStruct.Version & 0x3 )
-        {
+      } else {
+        if ( CCFileStruct.Version & 0x3 ) {
           CCFileStruct.State = TT5_READ;
-        }
-        else
-        {
+        } else {
           CCFileStruct.State = TT5_READ_WRITE;
         }
       }
       return NDEF_OK;
-    }
-    /* If Proprietary NDEF jump to end of proprietary message */
-    else if( atlv_detect[0] == NFCT5_PROPRIETARY_TLV )
-    {
-      if( atlv_detect[1] == NFCT5_3_BYTES_L_TLV )
-      {
+    } else if ( atlv_detect[0] == NFCT5_PROPRIETARY_TLV ) { /* If Proprietary NDEF jump to end of proprietary message */
+      if ( atlv_detect[1] == NFCT5_3_BYTES_L_TLV ) {
         CCFileStruct.NDEF_offset = CCFileStruct.NDEF_offset + ( (uint32_t)atlv_detect[2] << 8 ) + atlv_detect[3];
         continue;
-      }
-      else
-      {
+      } else {
         CCFileStruct.NDEF_offset = CCFileStruct.NDEF_offset + atlv_detect[1];
         continue;
       }
-    }
-    /* if Terminator no NDEF detected */
-    else if( atlv_detect[0] == NFCT5_TERMINATOR_TLV )
-    {
+    } else if ( atlv_detect[0] == NFCT5_TERMINATOR_TLV ) { /* if Terminator no NDEF detected */
       return NDEF_ERROR_NOT_FORMATED;
     }
-      
+
     CCFileStruct.NDEF_offset++;
   }
   
   return NDEF_ERROR_NOT_FORMATED;
 }
 
+/**
+  * @brief  This functions opens a session.
+  * @param  force : force the opening.
+  * @retval true.
+  */
 bool NDefNfcTagM24LR::open_session(bool force) {
-uint16_t status;
-
-      status = NfcType5_NDEFDetection();
+  uint16_t status = NfcType5_NDEFDetection();
 
-      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( ) != NDEF_OK );
-      }
-      mIsSessionOpen = 1;
-      return true; 
+  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( ) != NDEF_OK );
+  }
 
+  mIsSessionOpen = 1;
+
+  return true;
 }
 
+
+/**
+  * @brief  This functions closes a session.
+  * @param  None.
+  * @retval true.
+  */
 bool NDefNfcTagM24LR::close_session() 
 {
-    
     return true;
 }
 
-bool NDefNfcTagM24LR::writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
-        byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)
+/**
+  * @brief  This functions writes bytes.
+  * @param  byffer : the buffer containing data.
+  * @param  length: the length of the buffer in bytes.
+  * @param  offset: the offset of the buffer in bytes.
+  * @retval true if written, false otherwise.
+  */
+bool NDefNfcTagM24LR::writeByte(const uint8_t *buffer, uint16_t length, uint16_t offset,
+    byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)
 {
-  
-  if(WriteData(offset, length, (uint8_t*)buffer)== NDEF_OK){
+  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)
-{
-  
-  //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;
 }
 
-uint16_t NDefNfcTagM24LR::NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset){
+/**
+  * @brief  This functions reads bytes.
+  * @param  byteOffset : the offset of the buffer in bytes.
+  * @param  length: the length of the buffer in bytes.
+  * @param  buffer : the buffer containing data.
+  * @param  callback : a function to call after reading data.
+  * @param  callbackStatus : the status of the callback.
+  * @retval true if read, false otherwise.
+  */       
+bool NDefNfcTagM24LR::readByte(const uint16_t byteOffset, const uint16_t length,
+    uint8_t *buffer, byteOperationCallback_t callback, CallbackStatus_t *callbackStatus)
+{
+  
+  //first it reads the 2 byte for the length then when it will have the callback 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;
+}
+
+/**
+  * @brief  This functions writes bytes.
+  * @param  byffer : the buffer containing data.
+  * @param  length: the length of the buffer in bytes.
+  * @param  offset: the offset of the buffer in bytes.
+  * @retval NFCTAG status.
+  */
+uint16_t NDefNfcTagM24LR::NDefWriteByte(const uint8_t *buffer, uint16_t length, uint16_t offset)
+{
     uint16_t status;
-    do{
+    do {
         uint8_t writeLength =(uint8_t) std::min<uint16_t>(0xFF,length);
-        status= mDevice.update_binary(offset, writeLength, (uint8_t*)buffer);
+        status = mDevice.update_binary(offset, writeLength, (uint8_t*)buffer);
         offset+=writeLength;
         buffer+=writeLength;
         length-=writeLength;
-    }while(status==NDEF_OK && length!=0);
+    } while (status==NDEF_OK && length!=0);
+
     return status;
 }
 
-
-uint16_t NDefNfcTagM24LR::NDefReadByte(uint16_t byteOffset, uint16_t length, uint8_t *buffer){
+/**
+  * @brief  This functions reads bytes.
+  * @param  byteOffset : the offset of the buffer in bytes.
+  * @param  length: the length of the buffer in bytes.
+  * @param  buffer : the buffer containing data.
+  * @retval NFCTAG status.
+  */
+uint16_t NDefNfcTagM24LR::NDefReadByte(uint16_t byteOffset, uint16_t length, uint8_t *buffer)
+{
     uint16_t status;
-    do{
+    do {
         uint8_t readBuffer = (uint8_t)std::min<uint16_t>(0xFF,length);
         status= mDevice.read_binary(byteOffset, readBuffer, (uint8_t*)buffer);
         byteOffset+=readBuffer;
         buffer+=readBuffer;
         length-=readBuffer;
-    }while(status==NDEF_OK && length!=0);
+    } while (status==NDEF_OK && length!=0);
+
   return status;
 }
 
+
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/