diff --git a/MFRC522.cpp b/MFRC522.cpp index 01adb66..ff3defa 100644 --- a/MFRC522.cpp +++ b/MFRC522.cpp @@ -150,9 +150,9 @@ void MFRC522::PCD_ClearRegisterBitMask( byte reg, ///< The register to update. O * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to the data to transfer to the FIFO for CRC calculation. - byte length, ///< In: The number of bytes to transfer. - byte *result ///< Out: Pointer to result buffer. Result is written to result[0..1], low byte first. +MFRC522::StatusCode MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to the data to transfer to the FIFO for CRC calculation. + byte length, ///< In: The number of bytes to transfer. + byte *result ///< Out: Pointer to result buffer. Result is written to result[0..1], low byte first. ) { PCD_WriteRegister(CommandReg, PCD_Idle); // Stop any active command. PCD_WriteRegister(DivIrqReg, 0x04); // Clear the CRCIRq interrupt request bit @@ -192,13 +192,13 @@ void MFRC522::PCD_Init() { // Set the chipSelectPin as digital output, do not select the slave yet pinMode(_chipSelectPin, OUTPUT); digitalWrite(_chipSelectPin, HIGH); - + // Set the resetPowerDownPin as digital output, do not reset or power down. pinMode(_resetPowerDownPin, OUTPUT); - + // Set SPI bus to work with MFRC522 chip. setSPIConfig(); - + if (digitalRead(_resetPowerDownPin) == LOW) { //The MFRC522 chip is in power down mode. digitalWrite(_resetPowerDownPin, HIGH); // Exit power down mode. This triggers a hard reset. // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74�s. Let us be generous: 50ms. @@ -225,8 +225,8 @@ void MFRC522::PCD_Init() { * Initializes the MFRC522 chip. */ void MFRC522::PCD_Init( byte chipSelectPin, ///< Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) - byte resetPowerDownPin ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - ) { + byte resetPowerDownPin ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) + ) { _chipSelectPin = chipSelectPin; _resetPowerDownPin = resetPowerDownPin; // Set the chipSelectPin as digital output, do not select the slave yet @@ -377,13 +377,13 @@ bool MFRC522::PCD_PerformSelfTest() { * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to transfer to the FIFO. - byte sendLen, ///< Number of bytes to transfer to the FIFO. - byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command. - byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. - byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL. - byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. - bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. +MFRC522::StatusCode MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to transfer to the FIFO. + byte sendLen, ///< Number of bytes to transfer to the FIFO. + byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command. + byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. + byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL. + byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. + bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. ) { byte waitIRq = 0x30; // RxIRq and IdleIRq return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC); @@ -395,15 +395,15 @@ byte MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to t * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PCD_CommunicateWithPICC( byte command, ///< The command to execute. One of the PCD_Command enums. - byte waitIRq, ///< The bits in the ComIrqReg register that signals successful completion of the command. - byte *sendData, ///< Pointer to the data to transfer to the FIFO. - byte sendLen, ///< Number of bytes to transfer to the FIFO. - byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command. - byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. - byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. - byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. - bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. +MFRC522::StatusCode MFRC522::PCD_CommunicateWithPICC( byte command, ///< The command to execute. One of the PCD_Command enums. + byte waitIRq, ///< The bits in the ComIrqReg register that signals successful completion of the command. + byte *sendData, ///< Pointer to the data to transfer to the FIFO. + byte sendLen, ///< Number of bytes to transfer to the FIFO. + byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command. + byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned. + byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. + byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0. + bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. ) { byte n, _validBits; unsigned int i; @@ -476,9 +476,9 @@ byte MFRC522::PCD_CommunicateWithPICC( byte command, ///< The command to execut } // Verify CRC_A - do our own calculation and store the control in controlBuffer. byte controlBuffer[2]; - n = PCD_CalculateCRC(&backData[0], *backLen - 2, &controlBuffer[0]); - if (n != STATUS_OK) { - return n; + MFRC522::StatusCode status = PCD_CalculateCRC(&backData[0], *backLen - 2, &controlBuffer[0]); + if (status != STATUS_OK) { + return status; } if ((backData[*backLen - 2] != controlBuffer[0]) || (backData[*backLen - 1] != controlBuffer[1])) { return STATUS_CRC_WRONG; @@ -494,9 +494,9 @@ byte MFRC522::PCD_CommunicateWithPICC( byte command, ///< The command to execut * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PICC_RequestA(byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in - byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. - ) { +MFRC522::StatusCode MFRC522::PICC_RequestA( byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { return PICC_REQA_or_WUPA(PICC_CMD_REQA, bufferATQA, bufferSize); } // End PICC_RequestA() @@ -506,9 +506,9 @@ byte MFRC522::PICC_RequestA(byte *bufferATQA, ///< The buffer to store the ATQA * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PICC_WakeupA( byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in - byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. - ) { +MFRC522::StatusCode MFRC522::PICC_WakeupA( byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { return PICC_REQA_or_WUPA(PICC_CMD_WUPA, bufferATQA, bufferSize); } // End PICC_WakeupA() @@ -518,12 +518,12 @@ byte MFRC522::PICC_WakeupA( byte *bufferATQA, ///< The buffer to store the ATQA * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PICC_REQA_or_WUPA( byte command, ///< The command to send - PICC_CMD_REQA or PICC_CMD_WUPA - byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in - byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. - ) { +MFRC522::StatusCode MFRC522::PICC_REQA_or_WUPA( byte command, ///< The command to send - PICC_CMD_REQA or PICC_CMD_WUPA + byte *bufferATQA, ///< The buffer to store the ATQA (Answer to request) in + byte *bufferSize ///< Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK. + ) { byte validBits; - byte status; + MFRC522::StatusCode status; if (bufferATQA == NULL || *bufferSize < 2) { // The ATQA response is 2 bytes long. return STATUS_NO_ROOM; @@ -557,14 +557,14 @@ byte MFRC522::PICC_REQA_or_WUPA( byte command, ///< The command to send - PICC * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally output, but can also be used to supply a known UID. - byte validBits ///< The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size. - ) { +MFRC522::StatusCode MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally output, but can also be used to supply a known UID. + byte validBits ///< The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size. + ) { bool uidComplete; bool selectDone; bool useCascadeTag; byte cascadeLevel = 1; - byte result; + MFRC522::StatusCode result; byte count; byte index; byte uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level. @@ -698,8 +698,8 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp // Transmit the buffer and receive the response. result = PCD_TransceiveData(buffer, bufferUsed, responseBuffer, &responseLength, &txLastBits, rxAlign); if (result == STATUS_COLLISION) { // More than one PICC in the field => collision. - result = PCD_ReadRegister(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0] - if (result & 0x20) { // CollPosNotValid + byte valueOfCollReg = PCD_ReadRegister(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0] + if (valueOfCollReg & 0x20) { // CollPosNotValid return STATUS_COLLISION; // Without a valid collision position we cannot continue } byte collisionPos = result & 0x1F; // Values 0-31, 0 means bit 32. @@ -772,8 +772,8 @@ byte MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct. Normally outp * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PICC_HaltA() { - byte result; +MFRC522::StatusCode MFRC522::PICC_HaltA() { + MFRC522::StatusCode result; byte buffer[4]; // Build command buffer @@ -817,11 +817,11 @@ byte MFRC522::PICC_HaltA() { * * @return STATUS_OK on success, STATUS_??? otherwise. Probably STATUS_TIMEOUT if you supply the wrong key. */ -byte MFRC522::PCD_Authenticate(byte command, ///< PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B - byte blockAddr, ///< The block number. See numbering in the comments in the .h file. - MIFARE_Key *key, ///< Pointer to the Crypteo1 key to use (6 bytes) - Uid *uid ///< Pointer to Uid struct. The first 4 bytes of the UID is used. - ) { +MFRC522::StatusCode MFRC522::PCD_Authenticate(byte command, ///< PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B + byte blockAddr, ///< The block number. See numbering in the comments in the .h file. + MIFARE_Key *key, ///< Pointer to the Crypteo1 key to use (6 bytes) + Uid *uid ///< Pointer to Uid struct. The first 4 bytes of the UID is used. + ) { byte waitIRq = 0x10; // IdleIRq // Build command buffer @@ -864,11 +864,11 @@ void MFRC522::PCD_StopCrypto1() { * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Read( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from. - byte *buffer, ///< The buffer to store the data in - byte *bufferSize ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK. - ) { - byte result; +MFRC522::StatusCode MFRC522::MIFARE_Read( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from. + byte *buffer, ///< The buffer to store the data in + byte *bufferSize ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK. + ) { + MFRC522::StatusCode result; // Sanity check if (buffer == NULL || *bufferSize < 18) { @@ -899,11 +899,11 @@ byte MFRC522::MIFARE_Read( byte blockAddr, ///< MIFARE Classic: The block (0-0x * * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Write( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to. - byte *buffer, ///< The 16 bytes to write to the PICC - byte bufferSize ///< Buffer size, must be at least 16 bytes. Exactly 16 bytes are written. - ) { - byte result; +MFRC522::StatusCode MFRC522::MIFARE_Write( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to. + byte *buffer, ///< The 16 bytes to write to the PICC + byte bufferSize ///< Buffer size, must be at least 16 bytes. Exactly 16 bytes are written. + ) { + MFRC522::StatusCode result; // Sanity check if (buffer == NULL || bufferSize < 16) { @@ -934,11 +934,11 @@ byte MFRC522::MIFARE_Write( byte blockAddr, ///< MIFARE Classic: The block (0-0x * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Ultralight_Write( byte page, ///< The page (2-15) to write to. - byte *buffer, ///< The 4 bytes to write to the PICC - byte bufferSize ///< Buffer size, must be at least 4 bytes. Exactly 4 bytes are written. - ) { - byte result; +MFRC522::StatusCode MFRC522::MIFARE_Ultralight_Write( byte page, ///< The page (2-15) to write to. + byte *buffer, ///< The 4 bytes to write to the PICC + byte bufferSize ///< Buffer size, must be at least 4 bytes. Exactly 4 bytes are written. + ) { + MFRC522::StatusCode result; // Sanity check if (buffer == NULL || bufferSize < 4) { @@ -967,9 +967,9 @@ byte MFRC522::MIFARE_Ultralight_Write( byte page, ///< The page (2-15) to writ * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number. - long delta ///< This number is subtracted from the value of block blockAddr. - ) { +MFRC522::StatusCode MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number. + long delta ///< This number is subtracted from the value of block blockAddr. + ) { return MIFARE_TwoStepHelper(PICC_CMD_MF_DECREMENT, blockAddr, delta); } // End MIFARE_Decrement() @@ -981,9 +981,9 @@ byte MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number. * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number. - long delta ///< This number is added to the value of block blockAddr. - ) { +MFRC522::StatusCode MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number. + long delta ///< This number is added to the value of block blockAddr. + ) { return MIFARE_TwoStepHelper(PICC_CMD_MF_INCREMENT, blockAddr, delta); } // End MIFARE_Increment() @@ -995,8 +995,8 @@ byte MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number. * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Restore( byte blockAddr ///< The block (0-0xff) number. - ) { +MFRC522::StatusCode MFRC522::MIFARE_Restore( byte blockAddr ///< The block (0-0xff) number. + ) { // The datasheet describes Restore as a two step operation, but does not explain what data to transfer in step 2. // Doing only a single step does not work, so I chose to transfer 0L in step two. return MIFARE_TwoStepHelper(PICC_CMD_MF_RESTORE, blockAddr, 0L); @@ -1007,11 +1007,11 @@ byte MFRC522::MIFARE_Restore( byte blockAddr ///< The block (0-0xff) number. * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use - byte blockAddr, ///< The block (0-0xff) number. - long data ///< The data to transfer in step 2 - ) { - byte result; +MFRC522::StatusCode MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use + byte blockAddr, ///< The block (0-0xff) number. + long data ///< The data to transfer in step 2 + ) { + MFRC522::StatusCode result; byte cmdBuffer[2]; // We only need room for 2 bytes. // Step 1: Tell the PICC the command and block address @@ -1038,9 +1038,9 @@ byte MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_Transfer( byte blockAddr ///< The block (0-0xff) number. - ) { - byte result; +MFRC522::StatusCode MFRC522::MIFARE_Transfer( byte blockAddr ///< The block (0-0xff) number. + ) { + MFRC522::StatusCode result; byte cmdBuffer[2]; // We only need room for 2 bytes. // Tell the PICC we want to transfer the result into block blockAddr. @@ -1064,8 +1064,8 @@ byte MFRC522::MIFARE_Transfer( byte blockAddr ///< The block (0-0xff) number. * @param[out] value Current value of the Value Block. * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_GetValue(byte blockAddr, long *value) { - byte status; +MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, long *value) { + MFRC522::StatusCode status; byte buffer[18]; byte size = sizeof(buffer); @@ -1089,7 +1089,7 @@ byte MFRC522::MIFARE_GetValue(byte blockAddr, long *value) { * @param[in] value New value of the Value Block. * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::MIFARE_SetValue(byte blockAddr, long value) { +MFRC522::StatusCode MFRC522::MIFARE_SetValue(byte blockAddr, long value) { byte buffer[18]; // Translate the long into 4 bytes; repeated 2x in value block @@ -1120,11 +1120,11 @@ byte MFRC522::MIFARE_SetValue(byte blockAddr, long value) { * * @return STATUS_OK on success, STATUS_??? otherwise. */ -byte MFRC522::PCD_MIFARE_Transceive( byte *sendData, ///< Pointer to the data to transfer to the FIFO. Do NOT include the CRC_A. - byte sendLen, ///< Number of bytes in sendData. - bool acceptTimeout ///< True => A timeout is also success - ) { - byte result; +MFRC522::StatusCode MFRC522::PCD_MIFARE_Transceive( byte *sendData, ///< Pointer to the data to transfer to the FIFO. Do NOT include the CRC_A. + byte sendLen, ///< Number of bytes in sendData. + bool acceptTimeout ///< True => A timeout is also success + ) { + MFRC522::StatusCode result; byte cmdBuffer[18]; // We need room for 16 bytes data and 2 bytes CRC_A. // Sanity check @@ -1166,19 +1166,19 @@ byte MFRC522::PCD_MIFARE_Transceive( byte *sendData, ///< Pointer to the data t * * @return const __FlashStringHelper * */ -const __FlashStringHelper *MFRC522::GetStatusCodeName(byte code ///< One of the StatusCode enums. +const __FlashStringHelper *MFRC522::GetStatusCodeName(MFRC522::StatusCode code ///< One of the StatusCode enums. ) { switch (code) { - case STATUS_OK: return F("Success."); break; - case STATUS_ERROR: return F("Error in communication."); break; - case STATUS_COLLISION: return F("Collission detected."); break; - case STATUS_TIMEOUT: return F("Timeout in communication."); break; - case STATUS_NO_ROOM: return F("A buffer is not big enough."); break; - case STATUS_INTERNAL_ERROR: return F("Internal error in the code. Should not happen."); break; - case STATUS_INVALID: return F("Invalid argument."); break; - case STATUS_CRC_WRONG: return F("The CRC_A does not match."); break; - case STATUS_MIFARE_NACK: return F("A MIFARE PICC responded with NAK."); break; - default: return F("Unknown error"); break; + case STATUS_OK: return F("Success."); + case STATUS_ERROR: return F("Error in communication."); + case STATUS_COLLISION: return F("Collission detected."); + case STATUS_TIMEOUT: return F("Timeout in communication."); + case STATUS_NO_ROOM: return F("A buffer is not big enough."); + case STATUS_INTERNAL_ERROR: return F("Internal error in the code. Should not happen."); + case STATUS_INVALID: return F("Invalid argument."); + case STATUS_CRC_WRONG: return F("The CRC_A does not match."); + case STATUS_MIFARE_NACK: return F("A MIFARE PICC responded with NAK."); + default: return F("Unknown error"); } } // End GetStatusCodeName() @@ -1187,35 +1187,26 @@ const __FlashStringHelper *MFRC522::GetStatusCodeName(byte code ///< One of the * * @return PICC_Type */ -byte MFRC522::PICC_GetType(byte sak ///< The SAK byte returned from PICC_Select(). - ) { - if (sak & 0x04) { // UID not complete - return PICC_TYPE_NOT_COMPLETE; - } - //http://www.nxp.com/documents/application_note/AN10833.pdf - //3.2 Coding of Select Acknowledge (SAK) - //ignore 8-bit - sak&=0x7F; +MFRC522::PICC_Type MFRC522::PICC_GetType(byte sak ///< The SAK byte returned from PICC_Select(). + ) { + // http://www.nxp.com/documents/application_note/AN10833.pdf + // 3.2 Coding of Select Acknowledge (SAK) + // ignore 8-bit (iso14443 starts with LSBit = bit 1) + // fixes wrong type for manufacturer Infineon (http://nfc-tools.org/index.php?title=ISO14443A) + sak &= 0x7F; switch (sak) { - case 0x09: return PICC_TYPE_MIFARE_MINI; break; - case 0x08: return PICC_TYPE_MIFARE_1K; break; - case 0x18: return PICC_TYPE_MIFARE_4K; break; - case 0x00: return PICC_TYPE_MIFARE_UL; break; + case 0x04: return PICC_TYPE_NOT_COMPLETE; // UID not complete + case 0x09: return PICC_TYPE_MIFARE_MINI; + case 0x08: return PICC_TYPE_MIFARE_1K; + case 0x18: return PICC_TYPE_MIFARE_4K; + case 0x00: return PICC_TYPE_MIFARE_UL; case 0x10: - case 0x11: return PICC_TYPE_MIFARE_PLUS; break; - case 0x01: return PICC_TYPE_TNP3XXX; break; - default: break; + case 0x11: return PICC_TYPE_MIFARE_PLUS; + case 0x01: return PICC_TYPE_TNP3XXX; + case 0x20: return PICC_TYPE_ISO_14443_4; + case 0x40: return PICC_TYPE_ISO_18092; + default: return PICC_TYPE_UNKNOWN; } - - if (sak & 0x20) { - return PICC_TYPE_ISO_14443_4; - } - - if (sak & 0x40) { - return PICC_TYPE_ISO_18092; - } - - return PICC_TYPE_UNKNOWN; } // End PICC_GetType() /** @@ -1224,19 +1215,19 @@ byte MFRC522::PICC_GetType(byte sak ///< The SAK byte returned from PICC_Select * @return const __FlashStringHelper * */ const __FlashStringHelper *MFRC522::PICC_GetTypeName(byte piccType ///< One of the PICC_Type enums. - ) { + ) { switch (piccType) { - case PICC_TYPE_ISO_14443_4: return F("PICC compliant with ISO/IEC 14443-4"); break; - case PICC_TYPE_ISO_18092: return F("PICC compliant with ISO/IEC 18092 (NFC)");break; - case PICC_TYPE_MIFARE_MINI: return F("MIFARE Mini, 320 bytes"); break; - case PICC_TYPE_MIFARE_1K: return F("MIFARE 1KB"); break; - case PICC_TYPE_MIFARE_4K: return F("MIFARE 4KB"); break; - case PICC_TYPE_MIFARE_UL: return F("MIFARE Ultralight or Ultralight C"); break; - case PICC_TYPE_MIFARE_PLUS: return F("MIFARE Plus"); break; - case PICC_TYPE_TNP3XXX: return F("MIFARE TNP3XXX"); break; - case PICC_TYPE_NOT_COMPLETE: return F("SAK indicates UID is not complete."); break; + case PICC_TYPE_ISO_14443_4: return F("PICC compliant with ISO/IEC 14443-4"); + case PICC_TYPE_ISO_18092: return F("PICC compliant with ISO/IEC 18092 (NFC)"); + case PICC_TYPE_MIFARE_MINI: return F("MIFARE Mini, 320 bytes"); + case PICC_TYPE_MIFARE_1K: return F("MIFARE 1KB"); + case PICC_TYPE_MIFARE_4K: return F("MIFARE 4KB"); + case PICC_TYPE_MIFARE_UL: return F("MIFARE Ultralight or Ultralight C"); + case PICC_TYPE_MIFARE_PLUS: return F("MIFARE Plus"); + case PICC_TYPE_TNP3XXX: return F("MIFARE TNP3XXX"); + case PICC_TYPE_NOT_COMPLETE: return F("SAK indicates UID is not complete."); case PICC_TYPE_UNKNOWN: - default: return F("Unknown type"); break; + default: return F("Unknown type"); } } // End PICC_GetTypeName() @@ -1347,7 +1338,7 @@ void MFRC522::PICC_DumpMifareClassicSectorToSerial(Uid *uid, ///< Pointer to U MIFARE_Key *key, ///< Key A for the sector. byte sector ///< The sector to dump, 0..39. ) { - byte status; + MFRC522::StatusCode status; byte firstBlock; // Address of lowest address to dump actually last block dumped) byte no_of_blocks; // Number of blocks in sector bool isSectorTrailer; // Set to true while handling the "last" (ie highest address) in the sector. @@ -1491,7 +1482,7 @@ void MFRC522::PICC_DumpMifareClassicSectorToSerial(Uid *uid, ///< Pointer to U * Dumps memory contents of a MIFARE Ultralight PICC. */ void MFRC522::PICC_DumpMifareUltralightToSerial() { - byte status; + MFRC522::StatusCode status; byte byteCount; byte buffer[18]; byte i; @@ -1575,7 +1566,7 @@ bool MFRC522::MIFARE_OpenUidBackdoor(bool logErrors) { this will contain amount of valid response bits. */ byte response[32]; // Card's response is written here byte received; - byte status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 40 + MFRC522::StatusCode status = PCD_TransceiveData(&cmd, (byte)1, response, &received, &validBits, (byte)0, false); // 40 if(status != STATUS_OK) { if(logErrors) { Serial.println(F("Card did not respond to 0x40 after HALT command. Are you sure it is a UID changeable one?")); @@ -1641,7 +1632,7 @@ bool MFRC522::MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors) { // Authenticate for reading MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - byte status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid); + MFRC522::StatusCode status = PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte)1, &key, &uid); if (status != STATUS_OK) { if (status == STATUS_TIMEOUT) { @@ -1737,7 +1728,7 @@ bool MFRC522::MIFARE_UnbrickUidSector(bool logErrors) { byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Write modified block 0 back to card - byte status = MIFARE_Write((byte)0, block0_buffer, (byte)16); + MFRC522::StatusCode status = MIFARE_Write((byte)0, block0_buffer, (byte)16); if (status != STATUS_OK) { if (logErrors) { Serial.print(F("MIFARE_Write() failed: ")); diff --git a/MFRC522.h b/MFRC522.h index 00a0dd6..bbe1963 100644 --- a/MFRC522.h +++ b/MFRC522.h @@ -319,7 +319,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(); + MFRC522(); MFRC522(byte chipSelectPin, byte resetPowerDownPin); void setSPIConfig(); @@ -333,7 +333,7 @@ public: void setBitMask(unsigned char reg, unsigned char mask); void PCD_SetRegisterBitMask(byte reg, byte mask); void PCD_ClearRegisterBitMask(byte reg, byte mask); - byte PCD_CalculateCRC(byte *data, byte length, byte *result); + MFRC522::StatusCode PCD_CalculateCRC(byte *data, byte length, byte *result); ///////////////////////////////////////////////////////////////////////////////////// // Functions for manipulating the MFRC522 @@ -350,37 +350,37 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for communicating with PICCs ///////////////////////////////////////////////////////////////////////////////////// - byte PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); - byte PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); - byte PICC_RequestA(byte *bufferATQA, byte *bufferSize); - byte PICC_WakeupA(byte *bufferATQA, byte *bufferSize); - byte PICC_REQA_or_WUPA(byte command, byte *bufferATQA, byte *bufferSize); - byte PICC_Select(Uid *uid, byte validBits = 0); - byte PICC_HaltA(); + MFRC522::StatusCode PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); + MFRC522::StatusCode PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); + MFRC522::StatusCode PICC_RequestA(byte *bufferATQA, byte *bufferSize); + MFRC522::StatusCode PICC_WakeupA(byte *bufferATQA, byte *bufferSize); + MFRC522::StatusCode PICC_REQA_or_WUPA(byte command, byte *bufferATQA, byte *bufferSize); + MFRC522::StatusCode PICC_Select(Uid *uid, byte validBits = 0); + MFRC522::StatusCode PICC_HaltA(); ///////////////////////////////////////////////////////////////////////////////////// // Functions for communicating with MIFARE PICCs ///////////////////////////////////////////////////////////////////////////////////// - byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid); + MFRC522::StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid); void PCD_StopCrypto1(); - byte MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize); - byte MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize); - byte MIFARE_Decrement(byte blockAddr, long delta); - byte MIFARE_Increment(byte blockAddr, long delta); - byte MIFARE_Restore(byte blockAddr); - byte MIFARE_Transfer(byte blockAddr); - byte MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize); - byte MIFARE_GetValue(byte blockAddr, long *value); - byte MIFARE_SetValue(byte blockAddr, long value); + MFRC522::StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize); + MFRC522::StatusCode MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize); + MFRC522::StatusCode MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize); + MFRC522::StatusCode MIFARE_Decrement(byte blockAddr, long delta); + MFRC522::StatusCode MIFARE_Increment(byte blockAddr, long delta); + MFRC522::StatusCode MIFARE_Restore(byte blockAddr); + MFRC522::StatusCode MIFARE_Transfer(byte blockAddr); + MFRC522::StatusCode MIFARE_GetValue(byte blockAddr, long *value); + MFRC522::StatusCode MIFARE_SetValue(byte blockAddr, long value); ///////////////////////////////////////////////////////////////////////////////////// // Support functions ///////////////////////////////////////////////////////////////////////////////////// - byte PCD_MIFARE_Transceive(byte *sendData, byte sendLen, bool acceptTimeout = false); + MFRC522::StatusCode PCD_MIFARE_Transceive(byte *sendData, byte sendLen, bool acceptTimeout = false); // old function used too much memory, now name moved to flash; if you need char, copy from flash to memory //const char *GetStatusCodeName(byte code); - const __FlashStringHelper *GetStatusCodeName(byte code); - byte PICC_GetType(byte sak); + const __FlashStringHelper *GetStatusCodeName(MFRC522::StatusCode code); + MFRC522::PICC_Type PICC_GetType(byte sak); // old function used too much memory, now name moved to flash; if you need char, copy from flash to memory //const char *PICC_GetTypeName(byte type); const __FlashStringHelper *PICC_GetTypeName(byte type); @@ -402,7 +402,7 @@ public: private: byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); + MFRC522::StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); }; #endif diff --git a/changes.txt b/changes.txt index 18c2617..0618674 100644 --- a/changes.txt +++ b/changes.txt @@ -1,3 +1,8 @@ +5 Dec 2015 +- recognize infineon cards correctly @mayatforest +- added multi reader support, see example @lmmeng +- functions return MFRC522::StatusCode instead of generic byte @rotzbua + 10 Nov 2014 - Updated the changelog. - Added makefile. diff --git a/examples/MifareClassicValueBlock/MifareClassicValueBlock.ino b/examples/MifareClassicValueBlock/MifareClassicValueBlock.ino index b78f913..53b6e51 100644 --- a/examples/MifareClassicValueBlock/MifareClassicValueBlock.ino +++ b/examples/MifareClassicValueBlock/MifareClassicValueBlock.ino @@ -95,7 +95,7 @@ void loop() { byte valueBlockA = 5; byte valueBlockB = 6; byte trailerBlock = 7; - byte status; + MFRC522::StatusCode status; byte buffer[18]; byte size = sizeof(buffer); long value; @@ -278,7 +278,7 @@ void dump_byte_array(byte *buffer, byte bufferSize) { void formatValueBlock(byte blockAddr) { byte buffer[18]; byte size = sizeof(buffer); - byte status; + MFRC522::StatusCode status; Serial.print(F("Reading block ")); Serial.println(blockAddr); status = mfrc522.MIFARE_Read(blockAddr, buffer, &size); diff --git a/examples/ReadAndWrite/ReadAndWrite.ino b/examples/ReadAndWrite/ReadAndWrite.ino index 950e441..5d9e896 100644 --- a/examples/ReadAndWrite/ReadAndWrite.ino +++ b/examples/ReadAndWrite/ReadAndWrite.ino @@ -99,7 +99,7 @@ void loop() { 0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16 }; byte trailerBlock = 7; - byte status; + MFRC522::StatusCode status; byte buffer[18]; byte size = sizeof(buffer); diff --git a/examples/rfid_default_keys/rfid_default_keys.ino b/examples/rfid_default_keys/rfid_default_keys.ino index 4b5bfff..32fb6c5 100644 --- a/examples/rfid_default_keys/rfid_default_keys.ino +++ b/examples/rfid_default_keys/rfid_default_keys.ino @@ -80,7 +80,7 @@ boolean try_key(MFRC522::MIFARE_Key *key) boolean result = false; byte buffer[18]; byte block = 0; - byte status; + MFRC522::StatusCode status; // Serial.println(F("Authenticating using key A...")); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid)); diff --git a/examples/rfid_write_personal_data/rfid_write_personal_data.ino b/examples/rfid_write_personal_data/rfid_write_personal_data.ino index dda943a..ffbec07 100644 --- a/examples/rfid_write_personal_data/rfid_write_personal_data.ino +++ b/examples/rfid_write_personal_data/rfid_write_personal_data.ino @@ -58,7 +58,8 @@ void loop() { byte buffer[34]; byte block; - byte status, len; + MFRC522::StatusCode status; + byte len; Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial // Ask personal data: Family name