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 {
71  pn532_mode_t mode;
73 } pn532_t;
74 
82 #ifndef PN532_BUFFER_LEN
83 #define PN532_BUFFER_LEN (64)
84 #endif
85 
90 #define PN532_IC_VERSION(fwver) ((fwver >> 24) & 0xff)
91 #define PN532_FW_VERSION(fwver) ((fwver >> 16) & 0xff)
92 #define PN532_FW_REVISION(fwver) ((fwver >> 8) & 0xff)
93 #define PN532_FW_FEATURES(fwver) ((fwver) & 0xff)
94 
99 typedef enum {
100  PN532_SAM_NORMAL = 1,
101  PN532_SAM_VIRTUAL,
102  PN532_SAM_WIRED,
103  PN532_SAM_DUAL
105 
109 typedef enum {
110  PN532_BR_106_ISO_14443_A = 0,
111  PN532_BR_212_FELICA,
112  PN532_BR_424_FELICA,
113  PN532_BR_106_ISO_14443_B,
114  PN532_BR_106_JEWEL
116 
120 typedef enum {
121  ISO14443A_UNKNOWN,
122  ISO14443A_MIFARE,
123  ISO14443A_TYPE4
125 
129 typedef struct {
130  char target;
131  char auth;
132  char id_len;
133  char sel_res;
134  unsigned sns_res;
135  nfc_iso14443a_type_t type;
136  char id[8];
138 
142 typedef enum {
143  PN532_MIFARE_KEY_A = 0x60,
144  PN532_MIFARE_KEY_B = 0x61
146 
153 #define PN532_ISO14443A_4_LEN_FROM_BUFFER(b) ((b[0] << 8) | b[1])
154 
163 void pn532_reset(const pn532_t *dev);
164 
179 int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode);
180 
181 
182 #if defined(PN532_SUPPORT_I2C) || DOXYGEN
183 
188 static inline int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
189 {
190  return pn532_init(dev, params, PN532_I2C);
191 }
192 #endif
193 
194 #if defined(PN532_SUPPORT_SPI) || DOXYGEN
195 
200 static inline int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
201 {
202  return pn532_init(dev, params, PN532_SPI);
203 }
204 #endif
205 
220 int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver);
221 
236 int pn532_read_reg(pn532_t *dev, char *out, unsigned addr);
237 
251 int pn532_write_reg(pn532_t *dev, unsigned addr, char val);
252 
263 int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout);
264 
278 int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries);
279 
294  pn532_mifare_key_t keyid, char *key, unsigned block);
295 
308 int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block);
309 
322 int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block);
323 
336 int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page);
337 
347 
363 int pn532_iso14443a_4_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned offset,
364  char len);
365 
372 void pn532_deselect_passive(pn532_t *dev, unsigned target_id);
373 
380 void pn532_release_passive(pn532_t *dev, unsigned target_id);
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif /* PN532_H */
387 
spi_t spi
SPI device.
Definition: pn532.h:48
int pn532_write_reg(pn532_t *dev, unsigned addr, char val)
Write register of the pn532.
Low-level GPIO peripheral driver interface definitions.
int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page)
Read a block of a Mifare Ultralight card.
void pn532_deselect_passive(pn532_t *dev, unsigned target_id)
Deselect a previously selected passive card.
nfc_iso14443a_type_t
ISO14443A Card types.
Definition: pn532.h:120
char sel_res
SEL_RES.
Definition: pn532.h:133
int pn532_read_reg(pn532_t *dev, char *out, unsigned addr)
Read register of the pn532.
unsigned int i2c_t
Default i2c_t type definition.
Definition: i2c.h:113
Device descriptor for the PN532.
Definition: pn532.h:69
gpio_t nss
Chip Select pin (only SPI)
Definition: pn532.h:54
Low-level SPI peripheral driver interface definition.
i2c_t i2c
I2C device.
Definition: pn532.h:45
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.
int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode)
Initialize the module and peripherals.
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.
ISO14443A tag description.
Definition: pn532.h:129
unsigned sns_res
SNS_RES.
Definition: pn532.h:134
gpio_t irq
Interrupt pin.
Definition: pn532.h:52
const pn532_params_t * conf
Configuration struct.
Definition: pn532.h:70
nfc_iso14443a_type_t type
Type of ISO14443A card.
Definition: pn532.h:135
char id_len
Length of the ID field.
Definition: pn532.h:132
pn532_mifare_key_t
Mifare keys.
Definition: pn532.h:142
pn532_mode_t
Working mode of the PN532.
Definition: pn532.h:61
static int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using i2c.
Definition: pn532.h:188
RIOT synchronization API.
mutex_t trap
Mutex to wait for chip response.
Definition: pn532.h:72
void pn532_reset(const pn532_t *dev)
Hard reset the chipset.
int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries)
Get one ISO14443-A passive target.
int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block)
Write a block of a Mifare classic card.
pn532_mode_t mode
Working mode (i2c, spi)
Definition: pn532.h:71
void pn532_release_passive(pn532_t *dev, unsigned target_id)
Release an active passive card.
char target
Target.
Definition: pn532.h:130
char auth
Card has been authenticated.
Definition: pn532.h:131
Mutex structure.
Definition: mutex.h:36
int pn532_iso14443a_4_activate(pn532_t *dev, nfc_iso14443a_t *card)
Activate the NDEF file of a ISO14443-A Type 4 tag.
gpio_t reset
Reset pin.
Definition: pn532.h:51
Data structure with the configuration parameters.
Definition: pn532.h:42
int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block)
Read a block of a Mifare classic card.
pn532_sam_conf_mode_t
Possible SAM configurations.
Definition: pn532.h:99
static int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using spi.
Definition: pn532.h:200
pn532_target_t
PN532 supported targets.
Definition: pn532.h:109
int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout)
Set new settings for the Security Access Module.
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:105
int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver)
Get the firmware version of the pn532.
Low-level I2C peripheral driver interface definition.