Loading...
Searching...
No Matches
PN532 NFC Reader

PN532 NFC radio device driver. More...

Detailed Description

PN532 NFC radio device driver.

Modules

 PN532 NFC Radio driver compile configuration
 

Files

file  pn532.h
 PN532 driver.
 

Data Structures

struct  pn532_params_t
 Data structure with the configuration parameters. More...
 
struct  pn532_t
 Device descriptor for the PN532. More...
 
struct  nfc_iso14443a_t
 ISO14443A tag description. More...
 

Macros

#define PN532_ISO14443A_4_LEN_FROM_BUFFER(b)   ((b[0] << 8) | b[1])
 Obtain Tag 4 data length from buffer.
 

Enumerations

enum  pn532_mode_t { PN532_I2C , PN532_SPI }
 Working mode of the PN532. More...
 
enum  pn532_sam_conf_mode_t { PN532_SAM_NORMAL = 1 , PN532_SAM_VIRTUAL , PN532_SAM_WIRED , PN532_SAM_DUAL }
 Possible SAM configurations. More...
 
enum  pn532_target_t {
  PN532_BR_106_ISO_14443_A = 0 , PN532_BR_212_FELICA , PN532_BR_424_FELICA , PN532_BR_106_ISO_14443_B ,
  PN532_BR_106_JEWEL
}
 PN532 supported targets. More...
 
enum  nfc_iso14443a_type_t { ISO14443A_UNKNOWN , ISO14443A_MIFARE , ISO14443A_TYPE4 }
 ISO14443A Card types. More...
 
enum  pn532_mifare_key_t { PN532_MIFARE_KEY_A = 0x60 , PN532_MIFARE_KEY_B = 0x61 }
 Mifare keys. More...
 

Functions

void pn532_reset (const pn532_t *dev)
 Hard reset the chipset.
 
int pn532_init (pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode)
 Initialize the module and peripherals.
 
static int pn532_init_i2c (pn532_t *dev, const pn532_params_t *params)
 Initialization of PN532 using i2c.
 
static int pn532_init_spi (pn532_t *dev, const pn532_params_t *params)
 Initialization of PN532 using spi.
 
int pn532_fw_version (pn532_t *dev, uint32_t *fw_ver)
 Get the firmware version of the pn532.
 
int pn532_read_reg (pn532_t *dev, char *out, unsigned addr)
 Read register of the pn532.
 
int pn532_write_reg (pn532_t *dev, unsigned addr, char val)
 Write register of the pn532.
 
int pn532_sam_configuration (pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout)
 Set new settings for the Security Access Module.
 
int pn532_get_passive_iso14443a (pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries)
 Get one ISO14443-A passive target.
 
int pn532_mifareclassic_authenticate (pn532_t *dev, nfc_iso14443a_t *card, pn532_mifare_key_t keyid, char *key, unsigned block)
 Authenticate a Mifare classic card.
 
int pn532_mifareclassic_read (pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block)
 Read a block of a Mifare classic card.
 
int pn532_mifareclassic_write (pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block)
 Write a block of a Mifare classic card.
 
int pn532_mifareulight_read (pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page)
 Read a block of a Mifare Ultralight card.
 
int pn532_iso14443a_4_activate (pn532_t *dev, nfc_iso14443a_t *card)
 Activate the NDEF file of a ISO14443-A Type 4 tag.
 
int pn532_iso14443a_4_read (pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned offset, char len)
 Read data from the NDEF file of a ISO14443-A Type 4 tag.
 
void pn532_deselect_passive (pn532_t *dev, unsigned target_id)
 Deselect a previously selected passive card.
 
void pn532_release_passive (pn532_t *dev, unsigned target_id)
 Release an active passive card.
 

Helpers to extract firmware information from word

#define PN532_IC_VERSION(fwver)   ((fwver >> 24) & 0xff)
 
#define PN532_FW_VERSION(fwver)   ((fwver >> 16) & 0xff)
 
#define PN532_FW_REVISION(fwver)   ((fwver >> 8) & 0xff)
 
#define PN532_FW_FEATURES(fwver)   ((fwver) & 0xff)
 

Macro Definition Documentation

◆ PN532_FW_FEATURES

#define PN532_FW_FEATURES (   fwver)    ((fwver) & 0xff)

Definition at line 100 of file pn532.h.

◆ PN532_FW_REVISION

#define PN532_FW_REVISION (   fwver)    ((fwver >> 8) & 0xff)

Definition at line 99 of file pn532.h.

◆ PN532_FW_VERSION

#define PN532_FW_VERSION (   fwver)    ((fwver >> 16) & 0xff)

Definition at line 98 of file pn532.h.

◆ PN532_IC_VERSION

#define PN532_IC_VERSION (   fwver)    ((fwver >> 24) & 0xff)

Definition at line 97 of file pn532.h.

◆ PN532_ISO14443A_4_LEN_FROM_BUFFER

#define PN532_ISO14443A_4_LEN_FROM_BUFFER (   b)    ((b[0] << 8) | b[1])

Obtain Tag 4 data length from buffer.

This is useful in case the length has been read and one intents to read the data.

Definition at line 160 of file pn532.h.

Enumeration Type Documentation

◆ nfc_iso14443a_type_t

ISO14443A Card types.

Definition at line 127 of file pn532.h.

◆ pn532_mifare_key_t

Mifare keys.

Definition at line 149 of file pn532.h.

◆ pn532_mode_t

Working mode of the PN532.

Definition at line 62 of file pn532.h.

◆ pn532_sam_conf_mode_t

Possible SAM configurations.

Definition at line 106 of file pn532.h.

◆ pn532_target_t

PN532 supported targets.

Definition at line 116 of file pn532.h.

Function Documentation

◆ pn532_deselect_passive()

void pn532_deselect_passive ( pn532_t dev,
unsigned  target_id 
)

Deselect a previously selected passive card.

Parameters
[in]devtarget device
[in]target_idid of the target to deselect (0x00 for all)

◆ pn532_fw_version()

int pn532_fw_version ( pn532_t dev,
uint32_t *  fw_ver 
)

Get the firmware version of the pn532.

The firmware version returned is a 4 byte long value:

  • ic version,
  • fw version,
  • fw revision
  • feature support
Parameters
[in]devtarget device
[out]fw_verencoded firmware version
Returns
0 on success

◆ pn532_get_passive_iso14443a()

int pn532_get_passive_iso14443a ( pn532_t dev,
nfc_iso14443a_t out,
unsigned  max_retries 
)

Get one ISO14443-A passive target.

This method blocks until a target is detected.

Parameters
[in]devtarget device
[out]outtarget to be stored
[in]max_retriesmaximum number of attempts to activate a target (0xff blocks indefinitely)
Returns
0 on success
-1 when no card detected (if non blocking)

◆ pn532_init()

int pn532_init ( pn532_t dev,
const pn532_params_t params,
pn532_mode_t  mode 
)

Initialize the module and peripherals.

This is the first method to be called in order to interact with the pn532. It configures the GPIOs and the i2c/spi interface (depending on mode).

Parameters
[in]devtarget device
[in]paramsconfiguration parameters
[in]modeinitialization mode
Returns
0 on success
<0 i2c/spi initialization error, the value is given by the i2c/spi init method.

◆ pn532_init_i2c()

static int pn532_init_i2c ( pn532_t dev,
const pn532_params_t params 
)
inlinestatic

Initialization of PN532 using i2c.

See also
pn532_init for parameter and return value details
Note
Use pn532_i2c module to use this function.

Definition at line 195 of file pn532.h.

◆ pn532_init_spi()

static int pn532_init_spi ( pn532_t dev,
const pn532_params_t params 
)
inlinestatic

Initialization of PN532 using spi.

See also
pn532_init for parameter and return value details
Note
Use pn532_spi module to use this function.

Definition at line 208 of file pn532.h.

◆ pn532_iso14443a_4_activate()

int pn532_iso14443a_4_activate ( pn532_t dev,
nfc_iso14443a_t card 
)

Activate the NDEF file of a ISO14443-A Type 4 tag.

Parameters
[in]devtarget device
[in]cardcard to activate
Returns
0 on success

◆ pn532_iso14443a_4_read()

int pn532_iso14443a_4_read ( pn532_t dev,
char *  odata,
nfc_iso14443a_t card,
unsigned  offset,
char  len 
)

Read data from the NDEF file of a ISO14443-A Type 4 tag.

The first two bytes of an NDEF file are the length of the data. Afterwards, at offset 0x02 starts the data itself. If one tries to read further than the end of the data no data is returned.

Parameters
[in]devtarget device
[out]odatabuffer where to store the data
[in]cardcard to activate
[in]offsetoffset where to start reading
[in]lenlength to read
Returns
0 on success

◆ pn532_mifareclassic_authenticate()

int pn532_mifareclassic_authenticate ( pn532_t dev,
nfc_iso14443a_t card,
pn532_mifare_key_t  keyid,
char *  key,
unsigned  block 
)

Authenticate a Mifare classic card.

This operation must be done before reading or writing the segment.

Parameters
[in]devtarget device
[in]cardcard to use
[in]keyidwhich key to use
[in]keybuffer containing the key
[in]blockwhich block to authenticate
Returns
0 on success

◆ pn532_mifareclassic_read()

int pn532_mifareclassic_read ( pn532_t dev,
char *  odata,
nfc_iso14443a_t card,
unsigned  block 
)

Read a block of a Mifare classic card.

The block size is 16 bytes and it must be authenticated before read.

Parameters
[in]devtarget device
[out]odatabuffer where to store the data
[in]cardcard to use
[in]blockwhich block to read
Returns
0 on success

◆ pn532_mifareclassic_write()

int pn532_mifareclassic_write ( pn532_t dev,
char *  idata,
nfc_iso14443a_t card,
unsigned  block 
)

Write a block of a Mifare classic card.

The block size is 16 bytes and it must be authenticated before written.

Parameters
[in]devtarget device
[in]idatabuffer containing the data to write
[in]cardcard to use
[in]blockwhich block to write to
Returns
0 on success

◆ pn532_mifareulight_read()

int pn532_mifareulight_read ( pn532_t dev,
char *  odata,
nfc_iso14443a_t card,
unsigned  page 
)

Read a block of a Mifare Ultralight card.

The block size is 32 bytes and it must be authenticated before read.

Parameters
[in]devtarget device
[out]odatabuffer where to store the data
[in]cardcard to use
[in]pagewhich block to read
Returns
0 on success

◆ pn532_read_reg()

int pn532_read_reg ( pn532_t dev,
char *  out,
unsigned  addr 
)

Read register of the pn532.

Refer to the datasheet for a comprehensive list of registers and meanings. For SFR registers the high byte must be set to 0xff.

http://www.nxp.com/documents/user_manual/141520.pdf

Parameters
[in]devtarget device
[out]outvalue of the register
[in]addraddress of the register to read
Returns
0 on success

◆ pn532_release_passive()

void pn532_release_passive ( pn532_t dev,
unsigned  target_id 
)

Release an active passive card.

Parameters
[in]devtarget device
[in]target_idid of the target to release (0x00 for all)

◆ pn532_reset()

void pn532_reset ( const pn532_t dev)

Hard reset the chipset.

The chipset is reset by toggling the reset pins

Parameters
[in]devtarget device

◆ pn532_sam_configuration()

int pn532_sam_configuration ( pn532_t dev,
pn532_sam_conf_mode_t  mode,
unsigned  timeout 
)

Set new settings for the Security Access Module.

Parameters
[in]devtarget device
[in]modenew mode for the SAM
[in]timeouttimeout for Virtual Card mode with LSB of 50ms. (0 = infinite and 0xFF = 12.75s)
Returns
0 on success

◆ pn532_write_reg()

int pn532_write_reg ( pn532_t dev,
unsigned  addr,
char  val 
)

Write register of the pn532.

Refer to the datasheet for a comprehensive list of registers and meanings.

http://www.nxp.com/documents/user_manual/141520.pdf

Parameters
[in]devtarget device
[in]addraddress of the register to read
[in]valvalue to write in the register
Returns
0 on success