PN532 NFC library for Seeed Studio's NFC Shield

Fork of PN532 by Yihui Xiong

Embed: (wiki syntax)

« Back to documentation index

PN532 Class Reference

The PN532 class. More...

#include <PN532.h>

Public Member Functions

void begin (void)
 Setups the HW.
bool SAMConfig (void)
 Configures the SAM (Secure Access Module)
uint32_t getFirmwareVersion (void)
 Checks the firmware version of the PN5xx chip.
bool writeGPIO (uint8_t pinstate)
uint8_t readGPIO (void)
bool setPassiveActivationRetries (uint8_t maxRetries)
int8_t tgInitAsTarget (uint16_t timeout=0)
 Init PN532 as a target.
bool inListPassiveTarget ()
 'InLists' a passive target. PN532 acting as reader/initiator, peer acting as card/responder.
bool readPassiveTargetID (uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t *atqa, uint8_t *sak, uint16_t timeout=1000)
bool inDataExchange (uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength)
 Exchanges an APDU with the currently inlisted peer.
bool mifareclassic_IsFirstBlock (uint32_t uiBlock)
bool mifareclassic_IsTrailerBlock (uint32_t uiBlock)
uint8_t mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data)
uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data)
uint8_t mifareclassic_FormatNDEF (void)
uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char *url)
uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t *buffer)
uint8_t mifareultralight_WritePage (uint8_t page, uint8_t *buffer)

Static Public Member Functions

static void PrintHex (const uint8_t *data, const uint32_t numBytes)
 Prints a hexadecimal value in plain characters.
static void PrintHexChar (const uint8_t *pbtData, const uint32_t numBytes)
 Prints a hexadecimal value in plain characters, along with the char equivalents in the following format.

Detailed Description

The PN532 class.

Definition at line 114 of file PN532.h.


Member Function Documentation

void begin ( void   )

Setups the HW.

Definition at line 29 of file PN532.cpp.

uint32_t getFirmwareVersion ( void   )

Checks the firmware version of the PN5xx chip.

Returns:
The chip's firmware version and ID

Definition at line 119 of file PN532.cpp.

bool inDataExchange ( uint8_t *  send,
uint8_t  sendLength,
uint8_t *  response,
uint8_t *  responseLength 
)

Exchanges an APDU with the currently inlisted peer.

Parameters:
sendPointer to data to send
sendLengthLength of the data to send
responsePointer to response data
responseLengthPointer to the response data length

Definition at line 696 of file PN532.cpp.

bool inListPassiveTarget (  )

'InLists' a passive target. PN532 acting as reader/initiator, peer acting as card/responder.

Definition at line 738 of file PN532.cpp.

uint8_t mifareclassic_AuthenticateBlock ( uint8_t *  uid,
uint8_t  uidLen,
uint32_t  blockNumber,
uint8_t  keyNumber,
uint8_t *  keyData 
)

Tries to authenticate a block of memory on a MIFARE card using the INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual for more information on sending MIFARE and other commands.

Parameters:
uidPointer to a byte array containing the card UID
uidLenThe length (in bytes) of the card's UID (Should be 4 for MIFARE Classic)
blockNumberThe block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards).
keyNumberWhich key type to use during authentication (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)
keyDataPointer to a byte array containing the 6 bytes key value
Returns:
1 if everything executed properly, 0 for an error

Definition at line 391 of file PN532.cpp.

uint8_t mifareclassic_FormatNDEF ( void   )

Formats a Mifare Classic card to store NDEF Records

Returns:
1 if everything executed properly, 0 for an error

Definition at line 537 of file PN532.cpp.

bool mifareclassic_IsFirstBlock ( uint32_t  uiBlock )

Indicates whether the specified block number is the first block in the sector (block 0 relative to the current sector)

Definition at line 349 of file PN532.cpp.

bool mifareclassic_IsTrailerBlock ( uint32_t  uiBlock )

Indicates whether the specified block number is the sector trailer

Definition at line 363 of file PN532.cpp.

uint8_t mifareclassic_ReadDataBlock ( uint8_t  blockNumber,
uint8_t *  data 
)

Tries to read an entire 16-bytes data block at the specified block address.

Parameters:
blockNumberThe block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards).
dataPointer to the byte array that will hold the retrieved data (if any)
Returns:
1 if everything executed properly, 0 for an error

Definition at line 440 of file PN532.cpp.

uint8_t mifareclassic_WriteDataBlock ( uint8_t  blockNumber,
uint8_t *  data 
)

Tries to write an entire 16-bytes data block at the specified block address.

Parameters:
blockNumberThe block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards).
dataThe byte array that contains the data to write.
Returns:
1 if everything executed properly, 0 for an error

Definition at line 483 of file PN532.cpp.

uint8_t mifareclassic_WriteNDEFURI ( uint8_t  sectorNumber,
uint8_t  uriIdentifier,
const char *  url 
)

Writes an NDEF URI Record to the specified sector (1..15)

Note that this function assumes that the Mifare Classic card is already formatted to work as an "NFC Forum Tag" and uses a MAD1 file system. You can use the NXP TagWriter app on Android to properly format cards for this.

Parameters:
sectorNumberThe sector that the URI record should be written to (can be 1..15 for a 1K card)
uriIdentifierThe uri identifier code (0 = none, 0x01 = "http://www.", etc.)
urlThe uri text to write (max 38 characters).
Returns:
1 if everything executed properly, 0 for an error

Definition at line 577 of file PN532.cpp.

uint8_t mifareultralight_ReadPage ( uint8_t  page,
uint8_t *  buffer 
)

Tries to read an entire 4-bytes page at the specified address.

Parameters:
pageThe page number (0..63 in most cases)
bufferPointer to the byte array that will hold the retrieved data (if any)

Definition at line 649 of file PN532.cpp.

uint8_t mifareultralight_WritePage ( uint8_t  blockNumber,
uint8_t *  data 
)

Tries to write an entire 4-bytes data block at the specified block address.

Parameters:
blockNumberThe block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards).
dataThe byte array that contains the data to write.
Returns:
1 if everything executed properly, 0 for an error

Definition at line 512 of file PN532.cpp.

void PrintHex ( const uint8_t *  data,
const uint32_t  numBytes 
) [static]

Prints a hexadecimal value in plain characters.

Parameters:
dataPointer to the uint8_t data
numBytesData length in bytes

Definition at line 43 of file PN532.cpp.

void PrintHexChar ( const uint8_t *  data,
const uint32_t  numBytes 
) [static]

Prints a hexadecimal value in plain characters, along with the char equivalents in the following format.

00 00 00 00 00 00 ......

Parameters:
dataPointer to the data
numBytesData length in bytes

Definition at line 74 of file PN532.cpp.

uint8_t readGPIO ( void   )

Reads the state of the PN532's GPIO pins

Returns:
An 8-bit value containing the pin state where:

pinState[0] = P30 pinState[1] = P31 pinState[2] = P32 pinState[3] = P33 pinState[4] = P34 pinState[5] = P35

Definition at line 203 of file PN532.cpp.

bool readPassiveTargetID ( uint8_t  cardbaudrate,
uint8_t *  uid,
uint8_t *  uidLength,
uint16_t *  atqa,
uint8_t *  sak,
uint16_t  timeout = 1000 
)

Waits for an ISO14443A target to enter the field

Parameters:
cardBaudRateBaud rate of the card
uidPointer to the array that will be populated with the card's UID (up to 7 bytes)
uidLengthPointer to the variable that will hold the length of the card's UID.
Returns:
1 if everything executed properly, 0 for an error

Definition at line 290 of file PN532.cpp.

bool SAMConfig ( void   )

Configures the SAM (Secure Access Module)

Definition at line 236 of file PN532.cpp.

bool setPassiveActivationRetries ( uint8_t  maxRetries )

Sets the MxRtyPassiveActivation uint8_t of the RFConfiguration register

Parameters:
maxRetries0xFF to wait forever, 0x00..0xFE to timeout after mxRetries
Returns:
1 if everything executed properly, 0 for an error

Definition at line 261 of file PN532.cpp.

int8_t tgInitAsTarget ( uint16_t  timeout = 0 )

Init PN532 as a target.

Peer to Peer.

Parameters:
timeoutmax time to wait, 0 means no timeout
Returns:
> 0 success = 0 timeout < 0 failed

Definition at line 784 of file PN532.cpp.

bool writeGPIO ( uint8_t  pinstate )

Writes an 8-bit value that sets the state of the PN532's GPIO pins

Warning:
This function is provided exclusively for board testing and is dangerous since it will throw an error if any pin other than the ones marked "Can be used as GPIO" are modified! All pins that can not be used as GPIO should ALWAYS be left high (value = 1) or the system will become unstable and a HW reset will be required to recover the PN532.

pinState[0] = P30 Can be used as GPIO pinState[1] = P31 Can be used as GPIO pinState[2] = P32 *** RESERVED (Must be 1!) *** pinState[3] = P33 Can be used as GPIO pinState[4] = P34 *** RESERVED (Must be 1!) *** pinState[5] = P35 Can be used as GPIO

Returns:
1 if everything executed properly, 0 for an error

Definition at line 168 of file PN532.cpp.