libnfc  1.4.2
pn53x.h
Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library
00003  * 
00004  * Copyright (C) 2009, 2010, Roel Verdult, Romuald Conty
00005  * 
00006  * This program is free software: you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation, either version 3 of the License, or (at your
00009  * option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014  * more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00025 #ifndef __NFC_CHIPS_PN53X_H__
00026 #  define __NFC_CHIPS_PN53X_H__
00027 
00028 #  include <nfc/nfc-types.h>
00029 
00030 #  define PN53x_NORMAL_FRAME_MAX_LEN    255
00031 #  define PN53x_NORMAL_FRAME_OVERHEAD   7
00032 #  define PN53x_EXTENDED_FRAME_MAX_LEN    264
00033 #  define PN53x_EXTENDED_FRAME_OVERHEAD   10
00034 
00035 // Registers and symbols masks used to covers parts within a register
00036 #  define REG_CIU_TX_MODE           0x6302
00037 #  define SYMBOL_TX_CRC_ENABLE      0x80
00038 // TX_FRAMING bits explanation:
00039 //   00 : ISO/IEC 14443A/MIFARE and Passive Communication mode 106 kbit/s
00040 //   01 : Active Communication mode
00041 //   10 : FeliCa and Passive Communication mode at 212 kbit/s and 424 kbit/s
00042 //   11 : ISO/IEC 14443B
00043 #  define SYMBOL_TX_FRAMING         0x03
00044 
00045 #  define REG_CIU_RX_MODE           0x6303
00046 #  define SYMBOL_RX_CRC_ENABLE      0x80
00047 #  define SYMBOL_RX_NO_ERROR        0x08
00048 #  define SYMBOL_RX_MULTIPLE        0x04
00049 // RX_FRAMING follow same scheme than TX_FRAMING
00050 #  define SYMBOL_RX_FRAMING         0x03
00051 
00052 #  define REG_CIU_TX_AUTO           0x6305
00053 #  define SYMBOL_FORCE_100_ASK      0x40
00054 #  define SYMBOL_AUTO_WAKE_UP       0x20
00055 #  define SYMBOL_INITIAL_RF_ON      0x04
00056 
00057 #  define REG_CIU_MANUAL_RCV        0x630D
00058 #  define SYMBOL_PARITY_DISABLE     0x10
00059 
00060 #  define REG_CIU_STATUS2           0x6338
00061 #  define SYMBOL_MF_CRYPTO1_ON      0x08
00062 
00063 #  define REG_CIU_CONTROL           0x633C
00064 #  define SYMBOL_INITIATOR          0x10
00065 #  define SYMBOL_RX_LAST_BITS       0x07
00066 
00067 #  define REG_CIU_BIT_FRAMING       0x633D
00068 #  define SYMBOL_TX_LAST_BITS       0x07
00069 
00070 // PN53X Support Byte flags
00071 #define SUPPORT_ISO14443A             0x01
00072 #define SUPPORT_ISO14443B             0x02
00073 #define SUPPORT_ISO18092              0x04
00074 
00075 // Internal parameters flags
00076 #  define PARAM_NONE                  0x00
00077 #  define PARAM_NAD_USED              0x01
00078 #  define PARAM_DID_USED              0x02
00079 #  define PARAM_AUTO_ATR_RES          0x04
00080 #  define PARAM_AUTO_RATS             0x10
00081 #  define PARAM_14443_4_PICC          0x20 /* Only for PN532 */
00082 #  define PARAM_NFC_SECURE            0x20 /* Only for PN533 */
00083 #  define PARAM_NO_AMBLE              0x40 /* Only for PN532 */
00084 
00085 // Radio Field Configure Items           // Configuration Data length
00086 #  define RFCI_FIELD                  0x01      //  1
00087 #  define RFCI_TIMING                 0x02      //  3
00088 #  define RFCI_RETRY_DATA             0x04      //  1
00089 #  define RFCI_RETRY_SELECT           0x05      //  3
00090 #  define RFCI_ANALOG_TYPE_A_106      0x0A      // 11
00091 #  define RFCI_ANALOG_TYPE_A_212_424  0x0B      //  8
00092 #  define RFCI_ANALOG_TYPE_B          0x0C      //  3
00093 #  define RFCI_ANALOG_TYPE_14443_4    0x0D      //  9
00094 
00095 /* PN53x specific device-level errors */
00096 #  define DENACK          0x0100/* NACK */
00097 #  define DEACKMISMATCH   0x0200/* Unexpected data */
00098 #  define DEISERRFRAME    0x0300/* Error frame */
00099 #  define DENOTSUP        0x0400/* Not supported */
00100 
00101 /* PN53x specific types */
00106 typedef enum {
00108   PM_UNDEFINED = -1,
00110   PM_ISO14443A_106 = 0x00,
00112   PM_FELICA_212 = 0x01,
00114   PM_FELICA_424 = 0x02,
00116   PM_ISO14443B_106 = 0x03, 
00118   PM_JEWEL_106 = 0x04,
00120   PM_ISO14443B_212 = 0x06,
00122   PM_ISO14443B_424 = 0x07,
00124   PM_ISO14443B_847 = 0x08,
00125 } pn53x_modulation_t;
00126 
00131 typedef enum {
00133   PTT_UNDEFINED = -1,
00135   PTT_GENERIC_PASSIVE_106 = 0x00,
00137   PTT_GENERIC_PASSIVE_212 = 0x01,
00139   PTT_GENERIC_PASSIVE_424 = 0x02,
00141   PTT_ISO14443_4B_106 = 0x03,
00143   PTT_JEWEL_106 = 0x04,
00145   PTT_MIFARE = 0x10,
00147   PTT_FELICA_212 = 0x11,
00149   PTT_FELICA_424 = 0x12,
00151   PTT_ISO14443_4A_106 = 0x20,
00153   PTT_ISO14443_4B_TCL_106 = 0x23,
00155   PTT_DEP_PASSIVE_106 = 0x40,
00157   PTT_DEP_PASSIVE_212 = 0x41,
00159   PTT_DEP_PASSIVE_424 = 0x42,
00161   PTT_DEP_ACTIVE_106 = 0x80,
00163   PTT_DEP_ACTIVE_212 = 0x81,
00165   PTT_DEP_ACTIVE_424 = 0x82,
00166 } pn53x_target_type_t;
00167 
00172 typedef enum {
00174   PTM_NORMAL = 0x00,
00176   PTM_PASSIVE_ONLY = 0x01,
00178   PTM_DEP_ONLY = 0x02,
00180   PTM_ISO14443_4_PICC_ONLY = 0x04
00181 } pn53x_target_mode_t;
00182 
00183 bool  pn53x_init(nfc_device_t * pnd);
00184 bool    pn53x_check_ack_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame,
00185                                                    const size_t szRxFrameLen);
00186 bool    pn53x_check_error_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame,
00187                                                      const size_t szRxFrameLen);
00188 bool    pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx,
00189                           size_t * pszRx);
00190 bool    pn53x_get_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t * ui8Value);
00191 bool    pn53x_set_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value);
00192 bool    pn53x_set_parameter (nfc_device_t * pnd, const uint8_t ui8Value, const bool bEnable);
00193 bool    pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits);
00194 bool    pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtFrame,
00195                           size_t * pszFrameBits);
00196 bool    pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits,
00197                             byte_t * pbtRxPar);
00198 bool    pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData,
00199                                   nfc_chip_t nc, nfc_modulation_type_t nmt,
00200                                   nfc_target_info_t * pnti);
00201 bool    pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18]);
00202 bool    pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable);
00203 
00204 // NFC device as Initiator functions
00205 bool    pn53x_initiator_select_passive_target (nfc_device_t * pnd,
00206                                                const nfc_modulation_t nm,
00207                                                const byte_t * pbtInitData, const size_t szInitData,
00208                                                nfc_target_t * pnt);
00209 bool    pn53x_initiator_poll_targets (nfc_device_t * pnd,
00210                                       const nfc_modulation_t * pnmModulations, const size_t szModulations,
00211                                       const byte_t btPollNr, const byte_t btPeriod,
00212                                       nfc_target_t * pntTargets, size_t * pszTargetFound);
00213 bool    pn53x_initiator_select_dep_target (nfc_device_t * pnd,
00214                                            const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr,
00215                                            const nfc_dep_info_t * pndiInitiator, 
00216                                            nfc_target_t * pnt);
00217 bool    pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits,
00218                                          const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits,
00219                                          byte_t * pbtRxPar);
00220 bool    pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx,
00221                                           byte_t * pbtRx, size_t * pszRx);
00222 // NFC device as Target functions
00223 bool    pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx);
00224 bool    pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar);
00225 bool    pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx);
00226 bool    pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits,
00227                                 const byte_t * pbtTxPar);
00228 bool    pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx);
00229 // Error handling functions
00230 const char *pn53x_strerror (const nfc_device_t * pnd);
00231 static const struct chip_callbacks pn53x_callbacks_list = {
00232   pn53x_strerror
00233 };
00234 
00235 // C wrappers for PN53x commands
00236 bool    pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value);
00237 bool    pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation,
00238                                    const byte_t szMaxTargets, const byte_t * pbtInitiatorData,
00239                                    const size_t szInitiatorDataLen, byte_t * pbtTargetsData, size_t * pszTargetsData);
00240 bool    pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target);
00241 bool    pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target);
00242 bool    pn53x_InAutoPoll (nfc_device_t * pnd, const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes,
00243                           const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets,
00244                           size_t * pszTargetFound);
00245 bool    pn53x_InJumpForDEP (nfc_device_t * pnd,
00246                             const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr,
00247                             const byte_t * pbtPassiveInitiatorData,
00248                             const byte_t * pbtNFCID3i,
00249                             const byte_t * pbtGB, const size_t szGB,
00250                             nfc_target_t * pnt);
00251 bool  pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm,
00252                               const byte_t * pbtMifareParams,
00253                               const byte_t * pbtTkt, size_t szTkt,
00254                               const byte_t * pbtFeliCaParams,
00255                               const byte_t * pbtNFCID3t, const byte_t * pbtGB, const size_t szGB,
00256                               byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte);
00257 
00258 
00259 #endif // __NFC_CHIPS_PN53X_H__