munfc stack

µNFC Stack

Created by AppNearMe

/media/uploads/AppNearMe/footer.png

Visit us at http://www.appnearme.com/

NFC with AppNearMe

This page will help you develop your first program using AppNearMe's µNFC stack.

First of all, be sure to get the proper hardware and wire it up to your mbed.

Importing and setting up the library

The mbed implementation of the µNFC stack is located here; import it in your program.

Import libraryAppNearMe_MuNFC_PN532

AppNearMe µNFC stack for the NXP PN532 chip License: You can use the stack free of charge to prototype with mbed; if you want to use the stack with your commercial product, get in touch!

The only thing you have to configure is whether you want to use a RTOS or not.

If so, import it from here:

Import librarymbed-rtos

Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

In your project, create a file named MuNFCConfig.h.

In this file, create on of the following defines:

#define MUNFC_RTOS 1 //Use RTOS
/* OR */
#define MUNFC_RTOS 0 //Do not use RTOS

Using the library

Include the libraries header file:

#include "MuNFC.h"

The main class that you will use is the MuNFC class:

Import library

Public Member Functions

  MuNFC (char appHash[16], uint32_t version, PinName mosi, PinName miso, PinName sclk, PinName cs, PinName isr)
  Instantiate the �NFC stack for the following mobile app and using the following PN512/PN532 chip.
void  encode (void(*fn)( TLVList *, void *), void *arg)
  Set Encode Callback.
template<class T >
void  encode (T *inst, void(T::*member)( TLVList *))
  Set Encode Callback.
void  decode (void(*fn)( TLVList *, void *), void *arg)
  Set Decode Callback.
template<class T >
void  decode (T *inst, void(T::*member)( TLVList *))
  Set Decode Callback.
void  event (void(*fn)( NFCEvent , void *), void *arg)
  Set Event Callback.
template<class T >
void  event (T *inst, void(T::*member)( NFCEvent ))
  Set Event Callback.
bool  init ()
  Initialize stack.
void  run ()
  Start NFC thread (threaded mode)
void  poll (int timeoutMs)
  Poll for NFC reader (polling mode).

Protected Member Functions

void  process ()
  NFC Thread.

Instantiate it:

MuNFC nfc("00000001aZSe2vF5", //Application hash
1,   // Application version
p11, // MOSI
p12, // MISO
p13, // SCK
p19, // /CS
p18  // /IRQ
);

The stack provides some callbacks that you can register to interact with the mobile phone during a NFC transaction.

The encode callback will be called before transferring data from the mbed to the mobile phone. In this callback, you will have to populate a TLVList object that will appear as a JSON object in your mobile application.

The decode callback handles the reverse process: the JSON object generated by the mobile application appears as a TLVList object that you can decode.

Import library

Public Types

enum   TLVType {
   UINT8 , UINT32 , UINT8_ARRAY , STRING ,
   NONE , UNKNOWN
}
 

Type of a TLV item.

More...

Public Member Functions

TLVType   getNext ()
  Iterate to next item.
TLVType   getType ()
  Get current item's type.
uint8_t  getUInt8 ()
  Get uint8_t value.
uint32_t  getUInt32 ()
  Get uint32_t value.
size_t  getArrayLength ()
  Get array length.
size_t  getArray (uint8_t *buf, size_t maxLen)
  Get array.
size_t  getStringLength ()
  Get string length.
size_t  getString (char *str, size_t maxLen)
  Get string.
bool  isSpace ()
  Check whether there is space left in list.
bool  putUInt8 (uint8_t value)
  Put uint8_t value.
bool  putUInt32 (uint32_t value)
  Put uint32_t value.
size_t  putArray (uint8_t *buf, size_t len)
  Put array.
size_t  putString (char *str)
  Put string.

Finally you can register the event callback to know when NFC transactions start/succeed/fail.

At this point, you can initialize the stack.

bool ret = nfc.init();
if(ret)
{
  printf("AppNearMe/MuNFC stack initialized\n");
  
}
else
{
  printf("Could not initialize stack\n");
}

Finally, depending on the fact that you are using a RTOS or not:

//RTOS
nfc.run(); //Start thread
//Your code goes next

//No RTOS
while(true)
{
  nfc.poll();
  //Your code here
}

Please not that for now, the poll() method will only return at the end of a NFC transaction, so it is only suitable for simple applications. In the next version of the stack this will only block if there is a pending transaction.

Example program

This example program provides a good starting point to use the stack with an RTOS:

Import programAppNearMe_MuNFC_PN532_Test

Test of the µNFC stack with the Adafruit PN532 board