TOP

NFC Shield V2.0

From Wiki 来自痴汉的爱
Jump to: navigation, search

[中文]


NFCshield.jpg

Contents

Introduction

Near Field Communication, NFC is a widely used technology around us. It's behind daily applications such as access control system and mobile payment system. NFC Shield features a highly integrated transceiver module PN532 which handles contactless communication at 13.56MHz. You can read and write a 13.56MHz tag with this shield or implement point to point data exchange with two NFC Shields. In this new version, we assign an independent PCB antenna which can easily stretch out of any enclosure you use, leaving more room for you to design the exterior of your project.

The PCB comes stuck on the shield, but you can easily peel it off.
Model: SLD01097P




Features

  • Input Voltage: 5V
  • Typical Current: 100mA
  • 5cm max effective range
  • SPI pin saving interface
  • Serve for contactless communication at 13.56MHz
  • Support P2P communication
  • Support ISO14443 Type A and Type B protocols




Interface


NFC Shield Interface.jpgNFC_Antanna_28x30.5.jpg
U1: 74VHC125PW IC, Quad Buffer with 3-STATE Outputs.
U2: PN532 IC, Near Field Communication(NFC)Controller.

SPI Interface: Used for Arduino communicates with NFC shield.

Pins usage on Arduino:

D10 : SPI chip select
D11 : SPI MOSI pin
D12: SPI MISO pin
D13: SPI serial clock pin

Usage

If you want to use the latest library, please go to https://github.com/Seeed-Studio/PN532. Have fun!

1. Connect NFC Shield to Seeeduino.
2. Download the File: PN532_SPI V2.0 Library.
3. Unzip and put them in the libraries file of Arduino IDE by the path: ..\arduino-1.0.1\libraries.

Note: The PN532_SPI is derived from Adafruits PN532 Library.The original library provides API for reading Passive Target ID of Mifare Card/Tags. This is enough for card/tag identification purpose. We have added APIs for authentication, reading from and writing to Mifare Cards/Tags. The software library only provides low level functionality. Users have to implement NFC application layer(if required).


Demo 1:Read the complete memory of a MIFARE card
1. Open and upload the example readAllMemoryBlocks example as shown below.

//This example reads all MIFARE memory block from 0x00 to 0x63. 
//It is tested with a new MIFARE 1K cards. Uses default keys for authenication.
//Contributed by Seeed Technology Inc (www.seeedstudio.com)
 
#include <PN532.h>
#include <SPI.h>
 
/*Chip select pin can be connected to D10 or D9 which is hareware optional*/
/*if you the version of NFC Shield from SeeedStudio is v2.0.*/
#define PN532_CS 10
 
PN532 nfc(PN532_CS);
 
#define  NFC_DEMO_DEBUG 1
 
void setup(void) {
#ifdef NFC_DEMO_DEBUG
  Serial.begin(9600);
  Serial.println("Hello!");
#endif
  nfc.begin();
 
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Didn't find PN53x board");
#endif
    while (1); // halt
  }
#ifdef NFC_DEMO_DEBUG
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); 
  Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); 
  Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); 
  Serial.println((versiondata>>8) & 0xFF, DEC);
  Serial.print("Supports "); 
  Serial.println(versiondata & 0xFF, HEX);
#endif
  // configure board to read RFID tags and cards
  nfc.SAMConfig();
}
 
void loop(void) {
  uint32_t id;
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
 
  if (id != 0)
  {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Read card #");
    Serial.println(id);
    Serial.println();
#endif
    uint8_t keys[]= { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };// default key of a fresh card
    for(uint8_t blockn=0;blockn<64;blockn++) 
    {
      if(nfc.authenticateBlock(1, id ,blockn,KEY_A,keys)) //authenticate block blockn
      {
        //if authentication successful
        uint8_t block[16];
        //read memory block blockn
        if(nfc.readMemoryBlock(1,blockn,block))
        {
#ifdef NFC_DEMO_DEBUG
          //if read operation is successful
          for(uint8_t i=0;i<16;i++)
          {
            //print memory block
            Serial.print(block[i],HEX);
            if(block[i] <= 0xF) //Data arrangement / beautify
            {
              Serial.print("  ");
            }
            else
            {
              Serial.print(" ");
            }
          }
 
          Serial.print("| Block ");
          if(blockn <= 9) //Data arrangement / beautify
          {
            Serial.print(" ");
          }
          Serial.print(blockn,DEC);
          Serial.print(" | ");
 
          if(blockn == 0)
          {
            Serial.println("Manufacturer Block");
          }
          else
          {
            if(((blockn + 1) % 4) == 0)
            {
              Serial.println("Sector Trailer");
            }
            else
            {
              Serial.println("Data Block");
            }
          }
#endif
        }
      }
    }
  }
  delay(2000);
}

This sketch reads the complete memory of a MIFARE Standard card using default authentication keys. The output gives typical memory layout of fresh MIFARE Standard card.

2.Bring a Mifare Card near the NFC Antenna.


NFC Shield3.jpg

3.Open the serial monitor, you can see the score as shown below:

 
Score 2.jpg

Note: Blocks are classified as Manufacturer Block(read-only), Data Block (user/application writable area), and Sector Trailer(authentication and access bits for that sector)



Demo 2: Write data to MIFARE Memory Block

1. Open the example writeMifareMemory. This example writes a MIFARE memory block 0x08 of a new MIFARE 1K cards using default authentication keys. Read the Block Memory after complete writing data to MIFARE Memory Block.

Note: Memory block 0 is read only and contains manufacturer data. Do not write to Sector Trailer block unless you know what you are doing. Otherwise, the MIFARE card may be unusable in the future.

2. Compile and upload the example. If you do not know how to upload, please click here.

3. Bring a Mifare Card near the NFC Antenna..
4. Open the serial monitor, you can see the score as show below:
Score 1.jpg



Demo 3: Peer to Peer communication

An experimental NFC Peer to Peer communication is implemented in the latest NFC library. This uses Active Initiator for P2P communication.

1. Download the example “PtoPInitiator” and “PtoPTarge” into two different Arduinos with NFC Shields.

2. Place Antenna close to each other. See hardware setup as below.   


Two NFC Shield.jpg

3. Open the serial terminal and observe the data trans-reception.

Output Score:

P2P Initiator:
NFC Shield PtoPInitiator.jpg


P2P Target:
NFC Shield PtoPTarget.jpg

Reference Command Set

The APIs make use of the commands to invoke the interfaces provided by PN532 firmware via SPI. All these commands are documented in PN532 User Manual. The following APIs are provided by PN532_SPI Library.


1. boolean SAMConfig(void)

This API invokes the SAMConfiguration command of PN532 and sets it to Normal Mode. SAM stands for Security Access Module (i.e the PN532 system). PN532 system can work in Normal mode, Virtual Card mode, Wired Card mode and Dual Card mode.

Usage:

nfc.SAMConfig(); // Call this before any read/write operation

2. uint32_t readPassiveTargetID(uint8_t cardbaudrate)

This method reads the Passive Target ID and returns it as a 32-bit number. At the moment only reading MIFARE ISO14443A cards/tags are supported. Hence use PN532_MIFARE_ISO14443A as parameter. Returns 32 bit card number

Usage:

uint32_t cid;
// look for MiFare type cards/tags
cid = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);

3. uint32_t authenticateBlock(uint8_t cardnumber, uint32_t cid, uint8_t blockaddress ,uint8_t authtype, uint8_t * keys)

This method is used to authenticate a memory block with key before read/write operation. Returns true when successful.

  • cardnumber can be 1 or 2
  • cid is 32-bit Card ID
  • blockaddress is block number (any number between 0 - 63 for MIFARE card)
  • authtype is which key is to be used for authentication (either KEY_A or KEY_B)
  • keys points to the byte-array holding 6 keys.

Usage:

uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};  // default key of a fresh card
nfc.authenticateBlock(1, id ,3,KEY_A,keys); ////authenticate block 3, id is 32-bit passive target id.

4. uint32_t readMemoryBlock(uint8_t cardnumber,uint8_t blockaddress, uint8_t * block)

This method reads a memory block after authentication with the key. Returns true when successful.

  • cardnumber can be 1 or 2
  • blockaddress is block number (any number between 0 - 63 for MIFARE card) to read. Each block is 16bytes long in case of MIFARE Standard card.
  • block points to buffer(byte-array)to hold 16 bytes of block-data.

Usage:

uint8_t block[16];
nfc.readMemoryBlock(1,3,block); //Read can be performed only when authentication was successful.


Commands 5 - 9 are experimental. They support P2P communication but are not tested on mobile devices yet. Use with care.


5. uint32_t writeMemoryBlock(uint8_t cardnumber,uint8_t blockaddress, uint8_t * block)

This method writes data to a memory block after authentication with the key. Returns true when successful.

  • cardnumber can be 1 or 2
  • blockaddress is block number (any number between 0 - 63 for MIFARE card) to write. Each block is 16bytes long in case of MIFARE Standard card.
  • block points to buffer(byte-array) which holds 16 bytes of block-data to write.Usage:
uint8_t writeBuffer[16];
    for(uint8_t ii=0;ii<16;ii++)
     { 
       writeBuffer[ii]=ii; //Fill buffer with 0,1,2....F
     }
nfc.writeMemoryBlock(1,0x08,writeBuffer); //Write writeBuffer[] to block address 0x08. Read can be performed only when authentication was successful.

6. uint32_t PN532::configurePeerAsInitiator(uint8_t baudrate)

This method implements a Peer to Peer Initiator. Returns true when successful.

  • baudrate can be any number from 0-2. 0 for 106kbps or 1 for 201kbps or 2 for 424kbps. At the moment only 1 and 2 are supported.

Usage:

// Configure PN532 as Peer to Peer Initiator
if( nfc.configurePeerAsInitiator(2) ) //if connection is error-free
    {
     //Your Send  Receive code here
    }

7. uint32_t configurePeerAsTarget()

This method implements a Peer to Peer Target. Returns true when successful.

Usage:

// Configure PN532 as Peer to Peer Target
if(nfc.configurePeerAsTarget()) //if connection is error-free
   {
        //You code to trans-receive data
   }

8. uint32_t initiatorTxRx(char *DataOut,char *DataIn)

This method is used to transmit and receive data to and from target. This command is used by NFC Peer to Peer Initiator. Returns true when successful.

  • DataOut is pointer and array of chars (16 bytes) transmit data.
  • DataIn is pointer and array of chars (16 bytes) receive data.

Usage:

    // Configure PN532 as Peer to Peer Initiator in active mode
    if( nfc.configurePeerAsInitiator(2) ) //if connection is error-free
    {
        //trans-receive data
        if(nfc.initiatorTxRx(DataOut,DataIn))
        {
         Serial.print("Data Sent and Received: ");
         Serial.println(DataIn);
        }
    }

9. uint32_t targetTxRx(char *DataOut,char *DataIn)

This method is used to transmit and receive data to and from initiator. This code is used by NFC Peer to Peer Target to respond to Initiator. Returns true when successful.

  • DataOut is pointer and array of chars (16 bytes) transmit data.
  • DataIn is pointer and array of chars (16 bytes) receive data.

Usage:

    // Configure PN532 as Peer to Peer Target
    if(nfc.configurePeerAsTarget()) //if connection is error-free
    {
        //trans-receive data
        if(nfc.targetTxRx(DataOut,DataIn))
        {
         Serial.print("Data Received: ");
         Serial.println(DataIn);
        }
    }


Resource

NFC Shield v2.0 Schematic
NFC Shield v2.0 Eagle File
PN532_SPI Library For NFC Shield v2.0
PN532 Datasheet

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Start here|开始探索
Learn|学习
Product|产品线
Category|分类
Toolbox