-
Notifications
You must be signed in to change notification settings - Fork 0
/
PN532.h
157 lines (133 loc) · 4.98 KB
/
PN532.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef PN532_H
#define PN532_H
#include <stdbool.h>
#include <stdint.h>
#include <Arduino.h>
#include "SPI_Device.h"
void _print_uint8_hex(uint8_t b);
class PN532 {
public:
// METHODS
PN532(pin_size_t cs_pin, bool debug=false); // UNIMPLEMENTED: pin_size_t irq_pin
bool get_firmware_version(uint32_t * version);
bool SAM_disable(void);
bool read_passive_target_id(uint8_t * uid, uint8_t * uid_len, uint8_t card_baud_rate=ISO14443A_BAUD, uint16_t timeout=100);
bool power_down(void);
// UNIMPLEMENTED: ntag2xx_write_block(block_number, data) and ntag2xx_read_block(block_number)
bool wake_success(void);
private:
// MEMBERS
bool _debug;
static const SPISettings _spi_settings;
SPI_Device _spi;
// UNIMPLEMENTED: pin_size_t _irq_pin;
bool _wake_success;
// METHODS
void _wakeup(void);
bool _wait_ready(uint16_t timeout=1000);
void _read_data(uint8_t * data, uint8_t count);
void _write_data(uint8_t * data, uint8_t count);
uint8_t _read_frame(uint8_t * response, uint8_t * count);
void _write_frame(uint8_t * data, uint8_t count);
uint8_t _call_function(uint8_t command, uint8_t * params, uint8_t params_len, uint8_t * response, uint8_t * response_len, uint16_t timeout=1000);
uint8_t _call_function_try(uint8_t command, uint8_t * params, uint8_t params_len, uint8_t * response, uint8_t * response_len, uint16_t timeout);
// CONSTANTS
enum ERROR_CODES_E : uint8_t {
SUCCESS = 0x00,
// _read_frame
ERR_BAD_PREAMBLE = 0x01,
ERR_BAD_PACKET_START = 0x02,
ERR_EMPTY_PACKET = 0x03,
ERR_EMPTY_RESPONSE = 0x04,
ERR_BAD_LENGTH_CHECKSUM = 0x05,
ERR_LONG_RESPONSE = 0x06,
ERR_BAD_DATA_CHECKSUM = 0x07,
ERR_BAD_TFI = 0x08,
ERR_BAD_POSTAMBLE = 0x09,
// _call_function
ERR_ACK_TIMEOUT = 0x0A,
ERR_NO_COMMAND_ACK = 0x0B,
ERR_RESPONSE_TIMEOUT = 0x0C,
ERR_WRONG_COMMAND_RESPONSE = 0x0D
};
// frame markers
enum FRAME_MARKERS_E : uint8_t {
PREAMBLE = 0x00,
STARTCODE1 = 0x00,
STARTCODE2 = 0xFF,
POSTAMBLE = 0x00
};
// frame identifier: to/from
enum FRAME_IDENTIFIERS_E : uint8_t {
HOSTTOPN532 = 0xD4,
PN532TOHOST = 0xD5
};
// special frames
static const uint8_t ACK_LEN = 6;
static const uint8_t ACK_FRAME[];
static const uint8_t NACK_FRAME[];
static const uint8_t ERROR_FRAME[];
// PN532 over SPI protocol: see section 6.2.5 of manual
enum SPI_E : uint8_t {
SPI_STATUSREAD = 0x02,
SPI_DATAWRITE = 0x01,
SPI_DATAREAD = 0x03,
SPI_READY = 0x01,
SPI_STATUSPOLLINTERVAL = 5 // milliseconds
};
// command set
enum COMMANDS_E : uint8_t {
COMMAND_DIAGNOSE = 0x00,
COMMAND_GETFIRMWAREVERSION = 0x02,
COMMAND_GETGENERALSTATUS = 0x04,
COMMAND_READREGISTER = 0x06,
COMMAND_WRITEREGISTER = 0x08,
COMMAND_READGPIO = 0x0C,
COMMAND_WRITEGPIO = 0x0E,
COMMAND_SETSERIALBAUDRATE = 0x10,
COMMAND_SETPARAMETERS = 0x12,
COMMAND_SAMCONFIGURATION = 0x14,
COMMAND_POWERDOWN = 0x16,
COMMAND_RFCONFIGURATION = 0x32,
COMMAND_INDATAEXCHANGE = 0x40,
COMMAND_INCOMMUNICATETHRU = 0x42,
COMMAND_INDESELECT = 0x44,
COMMAND_INJUMPFORPSL = 0x46,
COMMAND_INLISTPASSIVETARGET = 0x4A,
COMMAND_INPSL = 0x4E,
COMMAND_INATR = 0x50,
COMMAND_INRELEASE = 0x52,
COMMAND_INSELECT = 0x54,
COMMAND_INJUMPFORDEP = 0x56,
COMMAND_RFREGULATIONTEST = 0x58,
COMMAND_INAUTOPOLL = 0x60,
COMMAND_TGGETDATA = 0x86,
COMMAND_TGGETINITIATORCOMMAND = 0x88,
COMMAND_TGGETTARGETSTATUS = 0x8A,
COMMAND_TGINITASTARGET = 0x8C,
COMMAND_TGSETDATA = 0x8E,
COMMAND_TGRESPONSETOINITIATOR = 0x90,
COMMAND_TGSETGENERALBYTES = 0x92,
COMMAND_TGSETMETADATA = 0x94
};
// constants for powering down and waking up
enum POWERDOWN_E : uint8_t {
POWERDOWN_WAKEFROM_SPI = 0x20,
POWERDOWN_NO_IRQ = 0x00,
POWERDOWN_DELAY = 1, // milliseconds
WAKE_DELAY = 2 // milliseconds (max), or 100µs min
};
// baud rate for InListPassiveTarget
enum BAUD_E : uint8_t {
// ISO/IEC 14443 Type A such as MIFARE and NTAG: 106 kbps
ISO14443A_BAUD = 0x00,
// FeliCa polling: 212 and 424 kbps
FELICA_212_BAUD = 0x01,
FELICA_424_BAUD = 0x02,
// ISO/IEC 14443-3 Type B: 106 kbps
ISO14443B_BAUD = 0x03,
// Innovision Jewel tag: 106 kbps
INNOVISION_JEWEL_BAUD = 0x04
};
};
#endif