libnfc  1.4.2
pn53x.h
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library
3  *
4  * Copyright (C) 2009, 2010, Roel Verdult, Romuald Conty
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>
18  */
19 
25 #ifndef __NFC_CHIPS_PN53X_H__
26 # define __NFC_CHIPS_PN53X_H__
27 
28 # include <nfc/nfc-types.h>
29 
30 # define PN53x_NORMAL_FRAME_MAX_LEN 255
31 # define PN53x_NORMAL_FRAME_OVERHEAD 7
32 # define PN53x_EXTENDED_FRAME_MAX_LEN 264
33 # define PN53x_EXTENDED_FRAME_OVERHEAD 10
34 
35 // Registers and symbols masks used to covers parts within a register
36 # define REG_CIU_TX_MODE 0x6302
37 # define SYMBOL_TX_CRC_ENABLE 0x80
38 // TX_FRAMING bits explanation:
39 // 00 : ISO/IEC 14443A/MIFARE and Passive Communication mode 106 kbit/s
40 // 01 : Active Communication mode
41 // 10 : FeliCa and Passive Communication mode at 212 kbit/s and 424 kbit/s
42 // 11 : ISO/IEC 14443B
43 # define SYMBOL_TX_FRAMING 0x03
44 
45 # define REG_CIU_RX_MODE 0x6303
46 # define SYMBOL_RX_CRC_ENABLE 0x80
47 # define SYMBOL_RX_NO_ERROR 0x08
48 # define SYMBOL_RX_MULTIPLE 0x04
49 // RX_FRAMING follow same scheme than TX_FRAMING
50 # define SYMBOL_RX_FRAMING 0x03
51 
52 # define REG_CIU_TX_AUTO 0x6305
53 # define SYMBOL_FORCE_100_ASK 0x40
54 # define SYMBOL_AUTO_WAKE_UP 0x20
55 # define SYMBOL_INITIAL_RF_ON 0x04
56 
57 # define REG_CIU_MANUAL_RCV 0x630D
58 # define SYMBOL_PARITY_DISABLE 0x10
59 
60 # define REG_CIU_STATUS2 0x6338
61 # define SYMBOL_MF_CRYPTO1_ON 0x08
62 
63 # define REG_CIU_CONTROL 0x633C
64 # define SYMBOL_INITIATOR 0x10
65 # define SYMBOL_RX_LAST_BITS 0x07
66 
67 # define REG_CIU_BIT_FRAMING 0x633D
68 # define SYMBOL_TX_LAST_BITS 0x07
69 
70 // PN53X Support Byte flags
71 #define SUPPORT_ISO14443A 0x01
72 #define SUPPORT_ISO14443B 0x02
73 #define SUPPORT_ISO18092 0x04
74 
75 // Internal parameters flags
76 # define PARAM_NONE 0x00
77 # define PARAM_NAD_USED 0x01
78 # define PARAM_DID_USED 0x02
79 # define PARAM_AUTO_ATR_RES 0x04
80 # define PARAM_AUTO_RATS 0x10
81 # define PARAM_14443_4_PICC 0x20 /* Only for PN532 */
82 # define PARAM_NFC_SECURE 0x20 /* Only for PN533 */
83 # define PARAM_NO_AMBLE 0x40 /* Only for PN532 */
84 
85 // Radio Field Configure Items // Configuration Data length
86 # define RFCI_FIELD 0x01 // 1
87 # define RFCI_TIMING 0x02 // 3
88 # define RFCI_RETRY_DATA 0x04 // 1
89 # define RFCI_RETRY_SELECT 0x05 // 3
90 # define RFCI_ANALOG_TYPE_A_106 0x0A // 11
91 # define RFCI_ANALOG_TYPE_A_212_424 0x0B // 8
92 # define RFCI_ANALOG_TYPE_B 0x0C // 3
93 # define RFCI_ANALOG_TYPE_14443_4 0x0D // 9
94 
95 /* PN53x specific device-level errors */
96 # define DENACK 0x0100/* NACK */
97 # define DEACKMISMATCH 0x0200/* Unexpected data */
98 # define DEISERRFRAME 0x0300/* Error frame */
99 # define DENOTSUP 0x0400/* Not supported */
100 
101 /* PN53x specific types */
106 typedef enum {
118  PM_JEWEL_106 = 0x04,
126 
131 typedef enum {
145  PTT_MIFARE = 0x10,
167 
172 typedef enum {
174  PTM_NORMAL = 0x00,
178  PTM_DEP_ONLY = 0x02,
182 
183 bool pn53x_init(nfc_device_t * pnd);
184 bool pn53x_check_ack_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame,
185  const size_t szRxFrameLen);
186 bool pn53x_check_error_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame,
187  const size_t szRxFrameLen);
188 bool pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx,
189  size_t * pszRx);
190 bool pn53x_get_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t * ui8Value);
191 bool pn53x_set_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value);
192 bool pn53x_set_parameter (nfc_device_t * pnd, const uint8_t ui8Value, const bool bEnable);
193 bool pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits);
194 bool pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtFrame,
195  size_t * pszFrameBits);
196 bool pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits,
197  byte_t * pbtRxPar);
198 bool pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData,
199  nfc_chip_t nc, nfc_modulation_type_t nmt,
200  nfc_target_info_t * pnti);
201 bool pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18]);
202 bool pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable);
203 
204 // NFC device as Initiator functions
205 bool pn53x_initiator_select_passive_target (nfc_device_t * pnd,
206  const nfc_modulation_t nm,
207  const byte_t * pbtInitData, const size_t szInitData,
208  nfc_target_t * pnt);
209 bool pn53x_initiator_poll_targets (nfc_device_t * pnd,
210  const nfc_modulation_t * pnmModulations, const size_t szModulations,
211  const byte_t btPollNr, const byte_t btPeriod,
212  nfc_target_t * pntTargets, size_t * pszTargetFound);
213 bool pn53x_initiator_select_dep_target (nfc_device_t * pnd,
214  const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr,
215  const nfc_dep_info_t * pndiInitiator,
216  nfc_target_t * pnt);
217 bool pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits,
218  const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits,
219  byte_t * pbtRxPar);
220 bool pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx,
221  byte_t * pbtRx, size_t * pszRx);
222 // NFC device as Target functions
223 bool pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx);
224 bool pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar);
225 bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx);
226 bool pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits,
227  const byte_t * pbtTxPar);
228 bool pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx);
229 // Error handling functions
230 const char *pn53x_strerror (const nfc_device_t * pnd);
231 static const struct chip_callbacks pn53x_callbacks_list = {
232  pn53x_strerror
233 };
234 
235 // C wrappers for PN53x commands
236 bool pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value);
237 bool pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation,
238  const byte_t szMaxTargets, const byte_t * pbtInitiatorData,
239  const size_t szInitiatorDataLen, byte_t * pbtTargetsData, size_t * pszTargetsData);
240 bool pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target);
241 bool pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target);
242 bool pn53x_InAutoPoll (nfc_device_t * pnd, const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes,
243  const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets,
244  size_t * pszTargetFound);
245 bool pn53x_InJumpForDEP (nfc_device_t * pnd,
246  const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr,
247  const byte_t * pbtPassiveInitiatorData,
248  const byte_t * pbtNFCID3i,
249  const byte_t * pbtGB, const size_t szGB,
250  nfc_target_t * pnt);
251 bool pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm,
252  const byte_t * pbtMifareParams,
253  const byte_t * pbtTkt, size_t szTkt,
254  const byte_t * pbtFeliCaParams,
255  const byte_t * pbtNFCID3t, const byte_t * pbtGB, const size_t szGB,
256  byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte);
257 
258 
259 #endif // __NFC_CHIPS_PN53X_H__