diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index f7f06a2..388592a 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -25,8 +25,6 @@ #ifndef __PN53X_INTERNAL_H__ #define __PN53X_INTERNAL_H__ -#include "pn53x.h" - // Miscellaneous #define Diagnose 0x00 #define GetFirmwareVersion 0x02 @@ -118,11 +116,11 @@ typedef struct { #endif } pn53x_command; -/* -#define PN531 0x01 -#define PN532 0x02 -#define PN533 0X04 -*/ +typedef enum { + PN531 = 0x01, + PN532 = 0x02, + PN533 = 0x04 +} pn53x_type; #ifndef DEBUG # define PNCMD( X, Y ) { X , Y } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 7563bdb..3d6c73f 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -67,12 +67,6 @@ pn53x_init(nfc_device_t * pnd) return false; } - // PN53x starts in initiator mode - CHIP_DATA (pnd)->operating_mode = INITIATOR; - - // Set current target to NULL - CHIP_DATA (pnd)->current_target = NULL; - // CRC handling is enabled by default pnd->bCrc = true; // Parity handling is enabled by default @@ -500,8 +494,6 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18]) byte_t abtFw[4]; size_t szFwLen = sizeof (abtFw); if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen)) { - // Failed to get firmware revision??, whatever...let's disconnect and clean up and return err - // FIXME: Wtf? return false; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC @@ -2237,3 +2229,22 @@ pn53x_nm_to_ptt(const nfc_modulation_t nm) } return PTT_UNDEFINED; } + +void +pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io) +{ + pnd->chip_data = malloc(sizeof(struct pn53x_data)); + + // Keep I/O functions + CHIP_DATA (pnd)->io = io; + + // Set power mode to normal, if your device starts in LowVBat (ie. PN532 + // UART) the driver layer have to correctly set it. + CHIP_DATA (pnd)->power_mode = NORMAL; + + // PN53x starts in initiator mode + CHIP_DATA (pnd)->operating_mode = INITIATOR; + + // Set current target to NULL + CHIP_DATA (pnd)->current_target = NULL; +} diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 11458f5..54dd155 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -28,6 +28,7 @@ # define __NFC_CHIPS_PN53X_H__ # include +# include "pn53x-internal.h" // TODO Remove double register address defines // Registers and symbols masks used to covers parts within a register @@ -133,12 +134,6 @@ # define DEACKMISMATCH 0x0200/* Unexpected data */ # define DEISERRFRAME 0x0300/* Error frame */ -typedef enum { - PN531 = 0x01, - PN532 = 0x02, - PN533 = 0x04 -} pn53x_type; - typedef enum { NORMAL, // In that case, there is no power saved but the PN53x reacts as fast as possible on the host controller interface. POWERDOWN, // Only on PN532, need to be wake up to process commands with a long preamble @@ -312,6 +307,7 @@ bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * bool pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); bool pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx); + // Error handling functions const char *pn53x_strerror (const nfc_device_t * pnd); @@ -345,4 +341,6 @@ bool pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, co bool pn53x_check_error_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); bool pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, const size_t szData); +void pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io); + #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 7151c84..4065d01 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -193,7 +193,9 @@ acr122_connect (const nfc_device_desc_t * pndd) char *pcFirmware; nfc_device_t *pnd = nfc_device_new (); pnd->driver_data = malloc (sizeof (struct acr122_data)); - pnd->chip_data = malloc (sizeof (struct pn53x_data)); + + // Alloc and init chip's data + pn53x_data_new (pnd, &acr122_io); SCARDCONTEXT *pscc; @@ -220,8 +222,6 @@ acr122_connect (const nfc_device_desc_t * pndd) // Done, we found the reader we are looking for snprintf (pnd->acName, sizeof (pnd->acName), "%s / %s", pndd->acDevice, pcFirmware); - CHIP_DATA (pnd)->power_mode = NORMAL; - CHIP_DATA (pnd)->io = &acr122_io; // 50: empirical tuning on Touchatag // 46: empirical tuning on ACR122U CHIP_DATA (pnd)->timer_correction = 50; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 13b314d..9b6fb58 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -117,9 +117,9 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); DRIVER_DATA (pnd)->port = sp; - pnd->chip_data = malloc(sizeof(struct pn53x_data)); - CHIP_DATA (pnd)->power_mode = NORMAL; - CHIP_DATA (pnd)->io = &arygon_tama_io; + + // Alloc and init chip's data + pn53x_data_new (pnd, &arygon_tama_io); bool res = arygon_reset_tama (pnd); nfc_device_free (pnd); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 591dada..0f99c17 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -98,10 +98,11 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA (pnd)->port = sp; - pnd->chip_data = malloc(sizeof(struct pn53x_data)); - CHIP_DATA (pnd)->type = PN532; + + // Alloc and init chip's data + pn53x_data_new (pnd, &pn532_uart_io); + // This device starts in LowVBat power mode CHIP_DATA (pnd)->power_mode = LOWVBAT; - CHIP_DATA (pnd)->io = &pn532_uart_io; // Check communication using "Diagnose" command, with "Communication test" (0x00) bool res = pn53x_check_communication (pnd); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index ec3576f..559b6ea 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -290,10 +290,9 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd) pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); *DRIVER_DATA (pnd) = data; - pnd->chip_data = malloc(sizeof(struct pn53x_data)); - CHIP_DATA (pnd)->power_mode = NORMAL; - CHIP_DATA (pnd)->io = &pn53x_usb_io; + // Alloc and init chip's data + pn53x_data_new (pnd, &pn53x_usb_io); switch (DRIVER_DATA (pnd)->model) { // empirical tuning