pn532.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 TriaGnoSys GmbH
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef PN532_H
22 #define PN532_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "mutex.h"
29 #include "periph/i2c.h"
30 #include "periph/spi.h"
31 #include "periph/gpio.h"
32 #include <stdint.h>
33 
34 #if !defined(PN532_SUPPORT_I2C) && !defined(PN532_SUPPORT_SPI)
35 #error Please define PN532_SUPPORT_I2C and/or PN532_SUPPORT_SPI to enable \
36  the functionality on this device
37 #endif
38 
42 typedef struct {
43  union {
44 #if defined(PN532_SUPPORT_I2C) || DOXYGEN
46 #endif
47 #if defined(PN532_SUPPORT_SPI) || DOXYGEN
49 #endif
50  };
51  gpio_t reset;
52  gpio_t irq;
53 #if defined(PN532_SUPPORT_SPI) || DOXYGEN
54  gpio_t nss;
55 #endif
57 
61 typedef enum {
62  PN532_I2C,
63  PN532_SPI
64 } pn532_mode_t;
65 
69 typedef struct {
73 } pn532_t;
74 
87 #ifndef CONFIG_PN532_BUFFER_LEN
88 #define CONFIG_PN532_BUFFER_LEN (64)
89 #endif
90 
96 #define PN532_IC_VERSION(fwver) ((fwver >> 24) & 0xff)
97 #define PN532_FW_VERSION(fwver) ((fwver >> 16) & 0xff)
98 #define PN532_FW_REVISION(fwver) ((fwver >> 8) & 0xff)
99 #define PN532_FW_FEATURES(fwver) ((fwver) & 0xff)
100 
105 typedef enum {
106  PN532_SAM_NORMAL = 1,
107  PN532_SAM_VIRTUAL,
108  PN532_SAM_WIRED,
109  PN532_SAM_DUAL
111 
115 typedef enum {
116  PN532_BR_106_ISO_14443_A = 0,
117  PN532_BR_212_FELICA,
118  PN532_BR_424_FELICA,
119  PN532_BR_106_ISO_14443_B,
120  PN532_BR_106_JEWEL
122 
126 typedef enum {
127  ISO14443A_UNKNOWN,
128  ISO14443A_MIFARE,
129  ISO14443A_TYPE4
131 
135 typedef struct {
136  char target;
137  char auth;
138  char id_len;
139  char sel_res;
140  unsigned sns_res;
142  char id[8];
144 
148 typedef enum {
149  PN532_MIFARE_KEY_A = 0x60,
150  PN532_MIFARE_KEY_B = 0x61
152 
159 #define PN532_ISO14443A_4_LEN_FROM_BUFFER(b) ((b[0] << 8) | b[1])
160 
169 void pn532_reset(const pn532_t *dev);
170 
185 int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode);
186 
187 
188 #if defined(PN532_SUPPORT_I2C) || DOXYGEN
189 
194 static inline int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
195 {
196  return pn532_init(dev, params, PN532_I2C);
197 }
198 #endif
199 
200 #if defined(PN532_SUPPORT_SPI) || DOXYGEN
201 
206 static inline int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
207 {
208  return pn532_init(dev, params, PN532_SPI);
209 }
210 #endif
211 
226 int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver);
227 
242 int pn532_read_reg(pn532_t *dev, char *out, unsigned addr);
243 
257 int pn532_write_reg(pn532_t *dev, unsigned addr, char val);
258 
269 int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout);
270 
284 int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries);
285 
300  pn532_mifare_key_t keyid, char *key, unsigned block);
301 
314 int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block);
315 
328 int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block);
329 
342 int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page);
343 
353 
369 int pn532_iso14443a_4_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned offset,
370  char len);
371 
378 void pn532_deselect_passive(pn532_t *dev, unsigned target_id);
379 
386 void pn532_release_passive(pn532_t *dev, unsigned target_id);
387 
388 #ifdef __cplusplus
389 }
390 #endif
391 
392 #endif /* PN532_H */
393 
pn532_init_spi
static int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using spi.
Definition: pn532.h:206
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.
nfc_iso14443a_t::sel_res
char sel_res
SEL_RES.
Definition: pn532.h:139
nfc_iso14443a_t
ISO14443A tag description.
Definition: pn532.h:135
pn532_t::trap
mutex_t trap
Mutex to wait for chip response.
Definition: pn532.h:72
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.
spi.h
Low-level SPI peripheral driver interface definition.
nfc_iso14443a_type_t
nfc_iso14443a_type_t
ISO14443A Card types.
Definition: pn532.h:126
pn532_params_t::nss
gpio_t nss
Chip Select pin (only SPI)
Definition: pn532.h:54
pn532_write_reg
int pn532_write_reg(pn532_t *dev, unsigned addr, char val)
Write register of the pn532.
i2c_t
unsigned int i2c_t
Default i2c_t type definition.
Definition: i2c.h:151
pn532_params_t::i2c
i2c_t i2c
I2C device.
Definition: pn532.h:45
pn532_deselect_passive
void pn532_deselect_passive(pn532_t *dev, unsigned target_id)
Deselect a previously selected passive card.
nfc_iso14443a_t::sns_res
unsigned sns_res
SNS_RES.
Definition: pn532.h:140
nfc_iso14443a_t::type
nfc_iso14443a_type_t type
Type of ISO14443A card.
Definition: pn532.h:141
pn532_read_reg
int pn532_read_reg(pn532_t *dev, char *out, unsigned addr)
Read register of the pn532.
pn532_t
Device descriptor for the PN532.
Definition: pn532.h:69
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.
nfc_iso14443a_t::auth
char auth
Card has been authenticated.
Definition: pn532.h:137
pn532_mifare_key_t
pn532_mifare_key_t
Mifare keys.
Definition: pn532.h:148
pn532_init
int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode)
Initialize the module and peripherals.
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.
nfc_iso14443a_t::id_len
char id_len
Length of the ID field.
Definition: pn532.h:138
pn532_params_t::irq
gpio_t irq
Interrupt pin.
Definition: pn532.h:52
nfc_iso14443a_t::target
char target
Target.
Definition: pn532.h:136
pn532_mode_t
pn532_mode_t
Working mode of the PN532.
Definition: pn532.h:61
pn532_init_i2c
static int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using i2c.
Definition: pn532.h:194
pn532_reset
void pn532_reset(const pn532_t *dev)
Hard reset the chipset.
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.
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.
pn532_release_passive
void pn532_release_passive(pn532_t *dev, unsigned target_id)
Release an active passive card.
mutex.h
Mutex for thread synchronization.
gpio.h
Low-level GPIO peripheral driver interface definitions.
pn532_params_t::reset
gpio_t reset
Reset pin.
Definition: pn532.h:51
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.
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.
pn532_target_t
pn532_target_t
PN532 supported targets.
Definition: pn532.h:115
i2c.h
Low-level I2C peripheral driver interface definition.
pn532_t::mode
pn532_mode_t mode
Working mode (i2c, spi)
Definition: pn532.h:71
pn532_fw_version
int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver)
Get the firmware version of the pn532.
mutex_t
Mutex structure.
Definition: mutex.h:120
pn532_t::conf
const pn532_params_t * conf
Configuration struct.
Definition: pn532.h:70
spi_t
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
pn532_sam_conf_mode_t
pn532_sam_conf_mode_t
Possible SAM configurations.
Definition: pn532.h:105
pn532_params_t
Data structure with the configuration parameters.
Definition: pn532.h:42
pn532_params_t::spi
spi_t spi
SPI device.
Definition: pn532.h:48