PN532 NFC Reader

PN532 NFC radio device driver.

PN532_IC_VERSION( fwver)
1
((fwver >> 24) & 0xff)
PN532_FW_VERSION( fwver)
1
((fwver >> 16) & 0xff)
PN532_FW_REVISION( fwver)
1
((fwver >>  8) & 0xff)
PN532_FW_FEATURES( fwver)
1
((fwver) & 0xff)
enum pn532_mode_t
PN532_I2C
PN532_SPI
enum pn532_sam_conf_mode_t
PN532_SAM_NORMAL = 1
PN532_SAM_VIRTUAL
PN532_SAM_WIRED
PN532_SAM_DUAL
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
enum nfc_iso14443a_type_t
ISO14443A_UNKNOWN
ISO14443A_MIFARE
ISO14443A_TYPE4
enum pn532_mifare_key_t
PN532_MIFARE_KEY_A = 0x60
PN532_MIFARE_KEY_B = 0x61
void pn532_reset(const pn532_t * dev)

Hard reset the chipset.

The chipset is reset by toggling the reset pins

Parameters

dev:target device

int pn532_init(pn532_t * dev, const pn532_params_t * params, pn532.h::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

dev:target device
params:configuration parameters
mode:initialization mode

Return values

  • 0 on success
  • <0 i2c/spi initialization error, the value is given by the i2c/spi init method.
int pn532_init_i2c(pn532_t * dev, const pn532_params_t * params)

Initialization of PN532 using i2c.

See also

pn532.h::pn532_init() for parameter and return value details

int pn532_init_spi(pn532_t * dev, const pn532_params_t * params)

Initialization of PN532 using spi.

See also

pn532.h::pn532_init() for parameter and return value details

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

dev:target device
fw_ver:encoded firmware version

Return values

  • 0 on success
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

dev:target device
out:value of the register
addr:address of the register to read

Return values

  • 0 on success
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

dev:target device
addr:address of the register to read
val:value to write in the register

Return values

  • 0 on success
int pn532_sam_configuration(pn532_t * dev, pn532.h::pn532_sam_conf_mode_t mode, unsigned timeout)

Set new settings for the Security Access Module.

Parameters

dev:target device
mode:new mode for the SAM
timeout:timeout for Virtual Card mode with LSB of 50ms. (0 = infinite and 0xFF = 12.75s)

Return values

  • 0 on success
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

dev:target device
out:target to be stored
max_retries:maximum number of attempts to activate a target (0xff blocks indefinitely)

Return values

  • 0 on success
  • -1 when no card detected (if non blocking)
int pn532_mifareclassic_authenticate(pn532_t * dev, nfc_iso14443a_t * card, pn532.h::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

dev:target device
card:card to use
keyid:which key to use
key:buffer containing the key
block:which block to authenticate

Return values

  • 0 on success
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

dev:target device
odata:buffer where to store the data
card:card to use
block:which block to read

Return values

  • 0 on success
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

dev:target device
idata:buffer containing the data to write
card:card to use
block:which block to write to

Return values

  • 0 on success
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

dev:target device
odata:buffer where to store the data
card:card to use
page:which block to read

Return values

  • 0 on success
int pn532_iso14443a_4_activate(pn532_t * dev, nfc_iso14443a_t * card)

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

Parameters

dev:target device
card:card to activate

Return values

  • 0 on success
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

dev:target device
odata:buffer where to store the data
card:card to activate
offset:offset where to start reading
len:length to read

Return values

  • 0 on success
void pn532_deselect_passive(pn532_t * dev, unsigned target_id)

Deselect a previously selected passive card.

Parameters

dev:target device
target_id:id of the target to deselect (0x00 for all)

void pn532_release_passive(pn532_t * dev, unsigned target_id)

Release an active passive card.

Parameters

dev:target device
target_id:id of the target to release (0x00 for all)

PN532_BUFFER_LEN

Internal buffer size.

1
(64)

A small buffer size is enough for most applications, however if large NDEF files are to be written this size shall be increased. Otherwise the files can be written in chunks.

PN532_ISO14443A_4_LEN_FROM_BUFFER( b)

Obtain Tag 4 data length from buffer.

1
((b[0] << 8) | b[1])

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

struct pn532_params_t

Data structure with the configuration parameters.

i2c.h::i2c_t i2c

I2C device.

spi.h::spi_t spi

SPI device.

union pn532_params_t::@152 @153
gpio.h::gpio_t reset

Reset pin.

gpio.h::gpio_t irq

Interrupt pin.

gpio.h::gpio_t nss

Chip Select pin (only SPI)

struct pn532_t

Device descriptor for the PN532.

const pn532_params_t * conf

Configuration struct.

pn532.h::pn532_mode_t mode

Working mode (i2c, spi)

mutex_t trap

Mutex to wait for chip response.

struct nfc_iso14443a_t

ISO14443A tag description.

char target

Target.

char auth

Card has been authenticated.

Do not modify manually

char id_len

Length of the ID field.

char sel_res

SEL_RES.

unsigned sns_res

SNS_RES.

pn532.h::nfc_iso14443a_type_t type

Type of ISO14443A card.

char id()

Card ID (length given by id_len)