Clone wiki

arduino-pn532 / NFC Forum Type 2 tags

NFC Forum Type 2 tags

NFC Forum Type 2 tags use a pretty simple communication protocol. Basically, you need two commands: 30h for reading a block of data from the tag and A2h to write a block to the tag. (One block is 4 bytes.)

In the PN532 package, they are defined as

MIFARE_READ_CARD_CMD : constant := 16#30#;
MIFARE_WRITE_CARD_CMD : constant := 16#A2#;

and implementation details are in functions:

procedure PN532_Read_NFC_Forum_Type_2_Tag_Block
  (Block      : NFC_Forum_Type_2_Block;
   Buf        : out PN532_Buf;
   Byte_Count : out Interfaces.Unsigned_8;
   Status     : out Boolean);

procedure PN532_Write_NFC_Forum_Type_2_Tag_Block
  (Block_Number : NFC_Forum_Type_2_Block;
   Buf          : PN532_Buf_4;
   Status       : out Boolean);

Reading NFC Forum Type 2 tag

When you are reading one or more blocks from the tag, you also need to specify the block where you want to start the reading.

So, the read command will be 2 bytes long.

byte 1 byte 2
30h POS

You get back 4 blocks (16 bytes) at once.

In the PN532 package, if the Buf parameter given to the PN532_Read_NFC_Forum_Type_2_Tag_Block procedure is smaller than 16 bytes, the data, which does not fit into the buffer, is ignored.

NFC Forum Type 2 tags are structured so that first 4 block are read-only and have metadata about the tag.

The format of the first blocks is following:

Description 00 11 22 33 Block
UID xx xx xx xx 0
Serial xx xx xx xx 1
Internal/Lock xx xx xx LL 2
Capabilities CC CC CC CC 3

For us, the capabilities block is the most interesting one. The 3rd byte of the capabilities block (marked as "22") will tell the size of the tag (you need to multiple the value by 8).

The rest of the data is formatted as TLV blocks.

In TLV block, the first byte tells the type of the block. The most common types are:

00h NULL Block
01h Lock Control Block
02h Memory Control Block
03h NDEF Message Block
FEh Terminator Block

In practice, you need to care only about NDEF message blocks, and terminator blocks.

NDEF message blocks will contain the NDEF messages and the terminator block will tell the end of the blocks.

Writing NFC Forum Type 2 tag

Writing NFC Forum Type 2 tag happens with command byte A2h, followed byte the number of the block and data (4 bytes) for the block.

byte 1 byte 2 byte 1 byte 2 byte 3 byte 4
A2h POS aa bb cc dd

The data should follow the TLV block format described above. If you don't have enough data for full block, you can put NULL blocks after the terminator block.