Cleanup of initializations

This commit is contained in:
Philippe Teuwen 2011-05-05 10:12:26 +00:00
parent 5f890fedcc
commit 4124dc662d
11 changed files with 85 additions and 159 deletions

View file

@ -53,6 +53,7 @@ static const byte_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 };
static const byte_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 };
/* prototypes */
bool pn53x_reset_settings(nfc_device_t * pnd);
nfc_modulation_t pn53x_ptt_to_nm (const pn53x_target_type_t ptt);
pn53x_modulation_t pn53x_nm_to_pm (const nfc_modulation_t nm);
pn53x_target_type_t pn53x_nm_to_ptt (const nfc_modulation_t nm);
@ -74,18 +75,14 @@ pn53x_init(nfc_device_t * pnd)
// Parity handling is enabled by default
pnd->bPar = true;
// Reset the ending transmission bits register, it is unknown what the last tranmission used there
CHIP_DATA (pnd)->ui8TxBits = 0;
if (!pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, 0x00)) {
return false;
}
// We can't read these parameters, so we set a default config by using the SetParameters wrapper
// Note: pn53x_SetParameters() will save the sent value in pnd->ui8Parameters cache
if(!pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS)) {
return false;
}
pn53x_reset_settings(pnd);
// Add the firmware revision to the device name
char *pcName;
pcName = strdup (pnd->acName);
@ -94,6 +91,16 @@ pn53x_init(nfc_device_t * pnd)
return true;
}
bool
pn53x_reset_settings(nfc_device_t * pnd)
{
// Reset the ending transmission bits register, it is unknown what the last tranmission used there
CHIP_DATA (pnd)->ui8TxBits = 0;
if (!pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, 0x00)) {
return false;
}
}
bool
pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx)
{
@ -571,6 +578,10 @@ pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool b
if (!pn53x_write_register (pnd, REG_CIU_RX_MODE, SYMBOL_RX_FRAMING, 0x00)) {
return false;
}
// Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards)
if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_FORCE_100_ASK, 0x40))
return false;
return true;
break;
}
@ -596,9 +607,7 @@ pn53x_check_communication (nfc_device_t *pnd)
bool
pn53x_initiator_init (nfc_device_t * pnd)
{
// Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards)
if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_FORCE_100_ASK, 0x40))
return false;
pn53x_reset_settings(pnd);
// Configure the PN53X to be an Initiator or Reader/Writer
if (!pn53x_write_register (pnd, REG_CIU_CONTROL, SYMBOL_INITIATOR, 0x10))
@ -1001,10 +1010,7 @@ pn53x_initiator_deselect_target (nfc_device_t * pnd)
bool
pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx)
{
// Save the current configuration settings
bool bCrc = pnd->bCrc;
bool bPar = pnd->bPar;
pn53x_reset_settings(pnd);
pn53x_target_mode_t ptm = PTM_NORMAL;
switch (pnt->nm.nmt) {
case NMT_ISO14443A:
@ -1041,12 +1047,6 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_
break;
}
// Make sure the CRC & parity are handled by the device, this is needed for target_init to work properly
if (!bCrc)
pn53x_configure (pnd, NDO_HANDLE_CRC, true);
if (!bPar)
pn53x_configure (pnd, NDO_HANDLE_PARITY, true);
// Let the PN53X be activated by the RF level detector from power down mode
if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_INITIAL_RF_ON, 0x04))
return false;
@ -1170,12 +1170,6 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_
}
}
// Restore the CRC & parity setting to the original value (if needed)
if (!bCrc)
pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_CRC, false);
if (!bPar)
pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_PARITY, false);
return true;
}

View file

@ -74,7 +74,7 @@ const struct nfc_driver_t *nfc_drivers[] = {
* When it has successfully claimed a NFC device, memory is allocated to save the device information. It will return a pointer to a \a nfc_device_t struct.
* This pointer should be supplied by every next functions of libnfc that should perform an action with this device.
*
* @note During this function, the device will be configured with default options:
* @note During this function, the device will be configured with default initiator options, cf nfc_initiator_init:
* - Crc is handled by the device (NDO_HANDLE_CRC = true)
* - Parity is handled the device (NDO_HANDLE_PARITY = true)
* - Cryto1 cipher is disabled (NDO_ACTIVATE_CRYPTO1 = false)
@ -82,6 +82,9 @@ const struct nfc_driver_t *nfc_drivers[] = {
* - Auto-switching in ISO14443-4 mode is enabled (NDO_AUTO_ISO14443_4 = true)
* - Invalid frames are not accepted (NDO_ACCEPT_INVALID_FRAMES = false)
* - Multiple frames are not accepted (NDO_ACCEPT_MULTIPLE_FRAMES = false)
* - 14443-A mode is activated (NDO_FORCE_ISO14443_A = true)
* - Let the device try forever to find a target (NDO_INFINITE_SELECT = true)
* - RF field is shortly dropped (if it was enabled) then activated again
*/
nfc_device_t *
nfc_connect (nfc_device_desc_t * pndd)
@ -111,30 +114,7 @@ nfc_connect (nfc_device_desc_t * pndd)
DBG ("[%s] has been claimed.", pnd->acName);
// Set default configuration options
// Make sure we reset the CRC and parity to chip handling.
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true))
return NULL;
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true))
return NULL;
// Deactivate the CRYPTO1 cipher, it may could cause problems when still active
if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false))
return NULL;
// Activate "easy framing" feature by default
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true))
return NULL;
// Activate auto ISO14443-4 switching by default
if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true))
return NULL;
// Disallow invalid frame
if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false))
return NULL;
// Disallow multiple frames
if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false))
if (!nfc_initiator_init(pnd))
return NULL;
return pnd;
@ -255,7 +235,41 @@ nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEn
bool
nfc_initiator_init (nfc_device_t * pnd)
{
// Drop the field for a while
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false))
return false;
// Force 14443-A mode
if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true))
return false;
// Disallow invalid frame
if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false))
return false;
// Disallow multiple frames
if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false))
return false;
// Make sure we reset the CRC and parity to chip handling.
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true))
return false;
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true))
return false;
// Activate "easy framing" feature by default
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true))
return false;
// Activate auto ISO14443-4 switching by default
if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true))
return false;
// Deactivate the CRYPTO1 cipher, it may could cause problems when still active
if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false))
return false;
// Let the device try forever to find a target/tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, true))
return false;
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true))
return false;
HAL (initiator_init, pnd);
return true;
}
/**
@ -351,18 +365,10 @@ nfc_initiator_list_passive_targets (nfc_device_t * pnd,
pnd->iLastError = 0;
// Drop the field for a while
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
return false;
}
// Let the reader only try once to find a tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
return false;
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
return false;
}
switch (nm.nmt) {
case NMT_ISO14443B: {
@ -600,7 +606,26 @@ nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, c
bool
nfc_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx)
{
// Disallow invalid frame
if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false))
return false;
// Disallow multiple frames
if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false))
return false;
// Make sure we reset the CRC and parity to chip handling.
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true))
return false;
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true))
return false;
// Activate "easy framing" feature by default
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true))
return false;
// Deactivate the CRYPTO1 cipher, it may could cause problems when still active
if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false))
return false;
HAL (target_init, pnd, pnt, pbtRx, pszRx);
return true;
}
/* TODO Document this function */