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|
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:
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:
|01h||Lock Control Block|
|02h||Memory Control Block|
|03h||NDEF Message 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|
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.