Loading...
Searching...
No Matches
pn532.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 TriaGnoSys GmbH
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#include "kernel_defines.h"
25#include "mutex.h"
26#include "periph/i2c.h"
27#include "periph/spi.h"
28#include "periph/gpio.h"
29#include <stdint.h>
30
31#if !IS_USED(MODULE_PN532_I2C) && !IS_USED(MODULE_PN532_SPI)
32#error Please use either pn532_i2c and/or pn532_spi module to enable \
33 the functionality on this device
34#endif
35
39typedef struct {
40 union {
41#if IS_USED(MODULE_PN532_I2C) || DOXYGEN
43#endif
44#if IS_USED(MODULE_PN532_SPI) || DOXYGEN
45 spi_t spi;
46#endif
47 };
48 gpio_t reset;
49 gpio_t irq;
50#if IS_USED(MODULE_PN532_SPI) || DOXYGEN
51 gpio_t nss;
52#endif
54
58typedef enum {
59 PN532_I2C,
60 PN532_SPI
62
71
84#ifndef CONFIG_PN532_BUFFER_LEN
85#define CONFIG_PN532_BUFFER_LEN (64)
86#endif
88
93#define PN532_IC_VERSION(fwver) ((fwver >> 24) & 0xff)
94#define PN532_FW_VERSION(fwver) ((fwver >> 16) & 0xff)
95#define PN532_FW_REVISION(fwver) ((fwver >> 8) & 0xff)
96#define PN532_FW_FEATURES(fwver) ((fwver) & 0xff)
98
102typedef enum {
103 PN532_SAM_NORMAL = 1,
104 PN532_SAM_VIRTUAL,
105 PN532_SAM_WIRED,
106 PN532_SAM_DUAL
108
112typedef enum {
113 PN532_BR_106_ISO_14443_A = 0,
114 PN532_BR_212_FELICA,
115 PN532_BR_424_FELICA,
116 PN532_BR_106_ISO_14443_B,
117 PN532_BR_106_JEWEL
119
123typedef enum {
124 ISO14443A_UNKNOWN,
125 ISO14443A_MIFARE,
126 ISO14443A_TYPE4
128
132typedef struct {
133 char target;
134 char auth;
135 char id_len;
136 char sel_res;
137 unsigned sns_res;
139 char id[8];
141
145typedef enum {
146 PN532_MIFARE_KEY_A = 0x60,
147 PN532_MIFARE_KEY_B = 0x61
149
156#define PN532_ISO14443A_4_LEN_FROM_BUFFER(b) ((b[0] << 8) | b[1])
157
166void pn532_reset(const pn532_t *dev);
167
182int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode);
183
184#if IS_USED(MODULE_PN532_I2C) || DOXYGEN
191static inline int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
192{
193 return pn532_init(dev, params, PN532_I2C);
194}
195#endif
196
197#if IS_USED(MODULE_PN532_SPI) || DOXYGEN
204static inline int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
205{
206 return pn532_init(dev, params, PN532_SPI);
207}
208#endif
209
224int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver);
225
240int pn532_read_reg(pn532_t *dev, char *out, unsigned addr);
241
255int pn532_write_reg(pn532_t *dev, unsigned addr, char val);
256
267int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout);
268
282int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries);
283
298 pn532_mifare_key_t keyid, char *key, unsigned block);
299
312int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block);
313
326int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block);
327
340int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page);
341
351
367int pn532_iso14443a_4_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned offset,
368 char len);
369
376void pn532_deselect_passive(pn532_t *dev, unsigned target_id);
377
384void pn532_release_passive(pn532_t *dev, unsigned target_id);
385
386#ifdef __cplusplus
387}
388#endif
389
Low-level GPIO peripheral driver interface definitions.
uint_fast8_t i2c_t
Default i2c_t type definition.
Definition i2c.h:155
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.
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_fw_version(pn532_t *dev, uint32_t *fw_ver)
Get the firmware version of the pn532.
int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode)
Initialize the module and peripherals.
pn532_sam_conf_mode_t
Possible SAM configurations.
Definition pn532.h:102
pn532_mifare_key_t
Mifare keys.
Definition pn532.h:145
int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout)
Set new settings for the Security Access Module.
pn532_mode_t
Working mode of the PN532.
Definition pn532.h:58
int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card, unsigned block)
Write a block of a Mifare classic card.
void pn532_release_passive(pn532_t *dev, unsigned target_id)
Release an active passive card.
int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out, unsigned max_retries)
Get one ISO14443-A passive target.
pn532_target_t
PN532 supported targets.
Definition pn532.h:112
nfc_iso14443a_type_t
ISO14443A Card types.
Definition pn532.h:123
int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned block)
Read a block of a Mifare classic card.
void pn532_reset(const pn532_t *dev)
Hard reset the chipset.
int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card, unsigned page)
Read a block of a Mifare Ultralight card.
static int pn532_init_i2c(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using i2c.
Definition pn532.h:191
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_read_reg(pn532_t *dev, char *out, unsigned addr)
Read register of the pn532.
static int pn532_init_spi(pn532_t *dev, const pn532_params_t *params)
Initialization of PN532 using spi.
Definition pn532.h:204
int pn532_write_reg(pn532_t *dev, unsigned addr, char val)
Write register of the pn532.
Low-level I2C peripheral driver interface definition.
Common macros and compiler attributes/pragmas configuration.
Mutex for thread synchronization.
Low-level SPI peripheral driver interface definition.
Mutex structure.
Definition mutex.h:36
ISO14443A tag description.
Definition pn532.h:132
char target
Target.
Definition pn532.h:133
char auth
Card has been authenticated.
Definition pn532.h:134
char sel_res
SEL_RES.
Definition pn532.h:136
unsigned sns_res
SNS_RES.
Definition pn532.h:137
nfc_iso14443a_type_t type
Type of ISO14443A card.
Definition pn532.h:138
char id_len
Length of the ID field.
Definition pn532.h:135
Data structure with the configuration parameters.
Definition pn532.h:39
gpio_t nss
Chip Select pin (only SPI)
Definition pn532.h:51
gpio_t reset
Reset pin.
Definition pn532.h:48
i2c_t i2c
I2C device.
Definition pn532.h:42
spi_t spi
SPI device.
Definition pn532.h:45
gpio_t irq
Interrupt pin.
Definition pn532.h:49
Device descriptor for the PN532.
Definition pn532.h:66
const pn532_params_t * conf
Configuration struct.
Definition pn532.h:67
pn532_mode_t mode
Working mode (i2c, spi)
Definition pn532.h:68
mutex_t trap
Mutex to wait for chip response.
Definition pn532.h:69