diff --git a/.travis.yml b/.travis.yml index 306b838..1071bd9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,23 +9,30 @@ cache: - "~/.platformio" env: - - PLATFORMIO_CI_SRC=examples/ChangeUID/ChangeUID.ino - - PLATFORMIO_CI_SRC=examples/DumpInfo/DumpInfo.ino - - PLATFORMIO_CI_SRC=examples/firmware_check/firmware_check.ino - - PLATFORMIO_CI_SRC=examples/FixBrickedUID/FixBrickedUID.ino - - PLATFORMIO_CI_SRC=examples/MifareClassicValueBlock/MifareClassicValueBlock.ino - - PLATFORMIO_CI_SRC=examples/MinimalInterrupt/MinimalInterrupt.ino - - PLATFORMIO_CI_SRC=examples/ReadAndWrite/ReadAndWrite.ino - - PLATFORMIO_CI_SRC=examples/ReadUidMultiReader/ReadUidMultiReader.ino - - PLATFORMIO_CI_SRC=examples/rfid_default_keys/rfid_default_keys.ino - - PLATFORMIO_CI_SRC=examples/rfid_write_personal_data/rfid_write_personal_data.ino - - PLATFORMIO_CI_SRC=examples/Ntag216_AUTH/Ntag216_AUTH.ino - - PLATFORMIO_CI_SRC=examples/ReadNUID/ReadNUID.ino - - PLATFORMIO_CI_SRC=examples/servo_motor/servo_motor.ino - - PLATFORMIO_CI_SRC=examples/RFID-Cloner/RFID-Cloner.ino + # add examples here and define which boards should be tested (only compile test) + - PLATFORMIO_CI_SRC=examples/ChangeUID/ChangeUID.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/DumpInfo/DumpInfo.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/firmware_check/firmware_check.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/FixBrickedUID/FixBrickedUID.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/MifareClassicValueBlock/MifareClassicValueBlock.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/MinimalInterrupt/MinimalInterrupt.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/ReadAndWrite/ReadAndWrite.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/ReadUidMultiReader/ReadUidMultiReader.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/rfid_default_keys/rfid_default_keys.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/rfid_write_personal_data/rfid_write_personal_data.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/Ntag216_AUTH/Ntag216_AUTH.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/ReadNUID/ReadNUID.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp + - PLATFORMIO_CI_SRC=examples/servo_motor/servo_motor.ino TESTBOARD=arduino_avr,teensy + - PLATFORMIO_CI_SRC=examples/RFID-Cloner/RFID-Cloner.ino TESTBOARD=arduino_avr,arduino_arm,teensy,esp install: - pip install -U platformio script: - - platformio ci --lib=. --board=uno --board=megaatmega1280 --board=teensy31 + # short the string comparison + - stringContain() { [ -z "${2##*$1*}" ]; } + # selectable board tests @Rotzbua + - board="arduino_avr"; if stringContain "$board" "$TESTBOARD"; then echo "check board $board"; platformio ci --lib=. --board=uno --board=megaatmega1280; else echo "skip board test of $board"; fi + - board="arduino_arm"; if stringContain "$board" "$TESTBOARD"; then echo "check board $board"; platformio ci --lib=. --board=due --board=zero; else echo "skip board test of $board"; fi + - board="teensy"; if stringContain "$board" "$TESTBOARD"; then echo "check board $board"; platformio ci --lib=. --board=teensy31; else echo "skip board test of $board"; fi + - board="esp"; if stringContain "$board" "$TESTBOARD"; then echo "check board $board"; platformio ci --lib=. --board=d1_mini; else echo "skip board test of $board"; fi diff --git a/MFRC522.cpp b/MFRC522.cpp index 1d91670..6979b8e 100644 --- a/MFRC522.cpp +++ b/MFRC522.cpp @@ -698,7 +698,7 @@ MFRC522::StatusCode MFRC522::PICC_Select( Uid *uid, ///< Pointer to Uid struct 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. + byte collisionPos = valueOfCollReg & 0x1F; // Values 0-31, 0 means bit 32. if (collisionPos == 0) { collisionPos = 32; } @@ -1301,23 +1301,11 @@ void MFRC522::PICC_DumpToSerial(Uid *uid ///< Pointer to Uid struct returned fro ) { MIFARE_Key key; - // UID - Serial.print(F("Card UID:")); - for (byte i = 0; i < uid->size; i++) { - if(uid->uidByte[i] < 0x10) - Serial.print(F(" 0")); - else - Serial.print(F(" ")); - Serial.print(uid->uidByte[i], HEX); - } - Serial.println(); - - // PICC type - PICC_Type piccType = PICC_GetType(uid->sak); - Serial.print(F("PICC type: ")); - Serial.println(PICC_GetTypeName(piccType)); + // Dump UID, SAK and Type + PICC_DumpDetailsToSerial(uid); // Dump contents + PICC_Type piccType = PICC_GetType(uid->sak); switch (piccType) { case PICC_TYPE_MIFARE_MINI: case PICC_TYPE_MIFARE_1K: @@ -1350,6 +1338,34 @@ void MFRC522::PICC_DumpToSerial(Uid *uid ///< Pointer to Uid struct returned fro PICC_HaltA(); // Already done if it was a MIFARE Classic PICC. } // End PICC_DumpToSerial() +/** + * Dumps card info (UID,SAK,Type) about the selected PICC to Serial. + */ +void MFRC522::PICC_DumpDetailsToSerial(Uid *uid ///< Pointer to Uid struct returned from a successful PICC_Select(). + ) { + // UID + Serial.print(F("Card UID:")); + for (byte i = 0; i < uid->size; i++) { + if(uid->uidByte[i] < 0x10) + Serial.print(F(" 0")); + else + Serial.print(F(" ")); + Serial.print(uid->uidByte[i], HEX); + } + Serial.println(); + + // SAK + Serial.print(F("Card SAK: ")); + if(uid->sak < 0x10) + Serial.print(F("0")); + Serial.println(uid->sak, HEX); + + // (suggested) PICC type + PICC_Type piccType = PICC_GetType(uid->sak); + Serial.print(F("PICC type: ")); + Serial.println(PICC_GetTypeName(piccType)); +} // End PICC_DumpDetailsToSerial() + /** * Dumps memory contents of a MIFARE Classic PICC. * On success the PICC is halted after dumping the data. diff --git a/MFRC522.h b/MFRC522.h index 6c3da7f..e003451 100644 --- a/MFRC522.h +++ b/MFRC522.h @@ -272,8 +272,8 @@ public: }; // PICC types we can detect. Remember to update PICC_GetTypeName() if you add more. - // last value set to 255, hints compiler to use byte instead of default integer, works for arduino ide to save ram and flash - enum PICC_Type { + // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered + enum PICC_Type : byte { PICC_TYPE_UNKNOWN , PICC_TYPE_ISO_14443_4 , // PICC compliant with ISO/IEC 14443-4 PICC_TYPE_ISO_18092 , // PICC compliant with ISO/IEC 18092 (NFC) @@ -283,12 +283,12 @@ public: PICC_TYPE_MIFARE_UL , // MIFARE Ultralight or Ultralight C PICC_TYPE_MIFARE_PLUS , // MIFARE Plus PICC_TYPE_TNP3XXX , // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure - PICC_TYPE_NOT_COMPLETE = 255 // SAK indicates UID is not complete. + PICC_TYPE_NOT_COMPLETE = 0xff // SAK indicates UID is not complete. }; // Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more. - // last value set to 255, hints compiler to use byte instead of default integer, works for arduino ide to save ram and flash - enum StatusCode { + // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered + enum StatusCode : byte { STATUS_OK , // Success STATUS_ERROR , // Error in communication STATUS_COLLISION , // Collission detected @@ -297,7 +297,7 @@ public: STATUS_INTERNAL_ERROR , // Internal error in the code. Should not happen ;-) STATUS_INVALID , // Invalid argument. STATUS_CRC_WRONG , // The CRC_A does not match - STATUS_MIFARE_NACK = 255 // A MIFARE PICC responded with NAK. + STATUS_MIFARE_NACK = 0xff // A MIFARE PICC responded with NAK. }; // A struct used for passing the UID of a PICC. @@ -381,16 +381,21 @@ public: 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(StatusCode code); - PICC_Type PICC_GetType(byte sak); + static const __FlashStringHelper *GetStatusCodeName(StatusCode code); + static 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(PICC_Type type); + static const __FlashStringHelper *PICC_GetTypeName(PICC_Type type); + + // Support functions for debuging void PCD_DumpVersionToSerial(); void PICC_DumpToSerial(Uid *uid); + void PICC_DumpDetailsToSerial(Uid *uid); void PICC_DumpMifareClassicToSerial(Uid *uid, PICC_Type piccType, MIFARE_Key *key); void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector); void PICC_DumpMifareUltralightToSerial(); + + // Advanced functions for MIFARE void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3); bool MIFARE_OpenUidBackdoor(bool logErrors); bool MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors); diff --git a/README.rst b/README.rst index ecb8958..87ec696 100644 --- a/README.rst +++ b/README.rst @@ -13,36 +13,43 @@ Interface (SPI) interface. Set the UID, write to sector 0, and unbrick Chinese UID changeable MIFARE cards. -.. _pin layout: +.. _compatible boards: +Compatible boards +---------- +This library is compatible to Teensy and ESP8266, but not all examples are available for every board. Also you have to change pins, see `pin layout`_. + + +.. _pin layout: Pin Layout ---------- The following table shows the typical pin layout used: -+-----------+----------+-------------------------------------------------------------+--------------------------+ -| | PCD | Arduino | Teensy | -| +----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| | MFRC522 | Uno | Mega | Nano v3 |Leonardo/Micro | Pro Micro | 2.0 | ++ 2.0 | 3.1 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | -+===========+==========+=============+=========+=========+===============+===========+========+========+========+ -| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET/ICSP-5 | RST | 7 | 4 | 9 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 0 | 20 | 10 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 2 | 22 | 11 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 3 | 23 | 12 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ -| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 1 | 21 | 13 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+--------+--------+ ++-----------+----------+---------------------------------------------------------------+--------------------------+ +| | PCD | Arduino | Teensy | +| +----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| | MFRC522 | Uno | Mega | Nano v3 |Leonardo / Micro | Pro Micro | 2.0 | ++ 2.0 | 3.1 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | ++===========+==========+=============+=========+=========+=================+===========+========+========+========+ +| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET / ICSP-5 | RST | 7 | 4 | 9 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 0 | 20 | 10 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 2 | 22 | 11 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 3 | 23 | 12 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ +| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 1 | 21 | 13 | ++-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+ .. [1] Configurable, typically defined as RST_PIN in sketch/program. .. [2] Configurable, typically defined as SS_PIN in sketch/program. .. [3] The SDA pin might be labeled SS on some/older MFRC522 boards. +.. _hardware: Hardware -------- @@ -133,6 +140,7 @@ Troubleshooting #. Hardware may be corrupted, most products are from china and sometimes the quality is really poor. Contact your seller. #. Newer versions of Mifare cards like DESFire/Ultralight maybe not work according to missing authentification, see `security`_ or different `protocol`_. + * **My mobile phone doesn't recognize the MFRC522** or **my MFRC522 can't read data from other MFRC522** #. Card simmulation is not supported. @@ -146,6 +154,7 @@ Troubleshooting #. If hardware: buy a more expensive like PN532 (supports NFC and many more, but costs about $15) +.. _license: License ------- This is free and unencumbered software released into the public domain. diff --git a/examples/DumpInfo/DumpInfo.ino b/examples/DumpInfo/DumpInfo.ino index a6345b1..325b7ca 100644 --- a/examples/DumpInfo/DumpInfo.ino +++ b/examples/DumpInfo/DumpInfo.ino @@ -47,7 +47,7 @@ void setup() { SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details - Serial.println(F("Scan PICC to see UID, type, and data blocks...")); + Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); } void loop() { diff --git a/keywords.txt b/keywords.txt index 07fea88..ae2a39e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -64,11 +64,16 @@ PCD_MIFARE_Transceive KEYWORD2 GetStatusCodeName KEYWORD2 PICC_GetType KEYWORD2 PICC_GetTypeName KEYWORD2 + +# Support functions for debuging PCD_DumpVersionToSerial KEYWORD2 PICC_DumpToSerial KEYWORD2 +PICC_DumpDetailsToSerial KEYWORD2 PICC_DumpMifareClassicToSerial KEYWORD2 PICC_DumpMifareClassicSectorToSerial KEYWORD2 PICC_DumpMifareUltralightToSerial KEYWORD2 + +# Advanced functions for MIFARE MIFARE_SetAccessBits KEYWORD2 MIFARE_OpenUidBackdoor KEYWORD2 MIFARE_SetUid KEYWORD2