diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56b89ad --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +MFRC522.zip diff --git a/.travis.yml b/.travis.yml index 1857565..1071bd9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,22 +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/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 e5b871c..6979b8e 100644 --- a/MFRC522.cpp +++ b/MFRC522.cpp @@ -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 57ca216..e003451 100644 --- a/MFRC522.h +++ b/MFRC522.h @@ -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 d9428bf..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 | 3.1 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+ -| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin | -+===========+==========+=============+=========+=========+===============+===========+========+ -| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET/ICSP-5 | RST | 9 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+ -| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 10 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+ -| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 11 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+ -| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 12 | -+-----------+----------+-------------+---------+---------+---------------+-----------+--------+ -| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 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/changes.txt b/changes.txt index 5e99ffe..efcfba5 100644 --- a/changes.txt +++ b/changes.txt @@ -1,3 +1,8 @@ +11 Feb 2016, v1.1.8 +- Added examples/MinimalInterrupt/MinimalInterrupt.ino example, Interrupt example @lmmeng +- Added .gitignore file allows the project to be more easily used as a subproject. @BenWiederhake +- Added Added Teensy 2.0 & Tensy++ 2.0 pinouts to README.rst @jkutianski + 16 Jan 2016, v1.1.7 - README.rst Spelling and Grammar Tweak @cuthbertnibbles - Added examples/servo_motor/servo_motor.ino example, Arduino RFID Access Control with a Servo Motor @techied 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/examples/MinimalInterrupt/MinimalInterrupt.ino b/examples/MinimalInterrupt/MinimalInterrupt.ino new file mode 100644 index 0000000..480fe0a --- /dev/null +++ b/examples/MinimalInterrupt/MinimalInterrupt.ino @@ -0,0 +1,137 @@ +/** + * ---------------------------------------------------------------------------- + * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid + * for further details and other examples. + * + * NOTE: The library file MFRC522.h has a lot of useful info. Please read it. + * + * Released into the public domain. + * ---------------------------------------------------------------------------- + * Minimal example how to use the interrupts to read the UID of a MIFARE Classic PICC + * (= card/tag). + * + * + * Typical pin layout used: + * ----------------------------------------------------------------------------------------- + * MFRC522 Arduino Arduino Arduino Arduino Arduino + * Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro + * Signal Pin Pin Pin Pin Pin Pin + * ----------------------------------------------------------------------------------------- + * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST + * SPI SS SDA(SS) 10 53 D10 3 10 + * IRQ ? ? ? ? 2 10 + * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 + * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 + * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 + * + */ + +#include +#include + +#define RST_PIN 9 // Configurable, see typical pin layout above +#define SS_PIN 3 // Configurable, see typical pin layout above +#define IRQ_PIN 2 + +MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. + +MFRC522::MIFARE_Key key; + +volatile boolean bNewInt = false; +unsigned char regVal = 0x7F; +void activateRec(MFRC522 mfrc522); +void clearInt(MFRC522 mfrc522); + +/** + * Initialize. + */ +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) + SPI.begin(); // Init SPI bus + + mfrc522.PCD_Init(); // Init MFRC522 card + + /* read and printout the MFRC522 version (valid values 0x91 & 0x92)*/ + Serial.print("Ver: 0x"); + byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + Serial.println(readReg, HEX); + + /* setup the IRQ pin*/ + pinMode(IRQ_PIN, INPUT_PULLUP); + + /* + * Allow the ... irq to be propagated to the IRQ pin + * For test purposes propagate the IdleIrq and loAlert + */ + regVal = 0xA0; //rx irq + mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg,regVal); + + bNewInt = false; //interrupt flag + + /*Activate the interrupt*/ + attachInterrupt(digitalPinToInterrupt(IRQ_PIN), readCard, FALLING); + + Serial.println("End setup"); + + do{ //clear a spourious interrupt at start + ; + }while(!bNewInt); + bNewInt = false; +} + +/** + * Main loop. + */ +void loop() { + + if(bNewInt){ //new read interrupt + bNewInt = false; + Serial.print("Interrupt. "); + mfrc522.PICC_ReadCardSerial(); //read the tag data + // Show some details of the PICC (that is: the tag/card) + Serial.print(F("Card UID:")); + dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); + Serial.println(); + + clearInt(mfrc522); + } + +// The receiving block needs regular retriggering (tell the tag it should transmit??) +// (mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg,mfrc522.PICC_CMD_REQA);) + activateRec(mfrc522); + delay(100); +} //loop() + +/** + * Helper routine to dump a byte array as hex values to Serial. + */ +void dump_byte_array(byte *buffer, byte bufferSize) { + for (byte i = 0; i < bufferSize; i++) { + Serial.print(buffer[i] < 0x10 ? " 0" : " "); + Serial.print(buffer[i], HEX); + } +} +/** + * MFRC522 interrupt serving routine + */ +void readCard(){ + bNewInt = true; +} + +/* + * The function sending to the MFRC522 the needed commands to activate the reception + */ +void activateRec(MFRC522 mfrc522){ + mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg,mfrc522.PICC_CMD_REQA); + mfrc522.PCD_WriteRegister(mfrc522.CommandReg,mfrc522.PCD_Transceive); + mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87); +} + +/* + * The function to clear the pending interrupt bits after interrupt serving routine + */ +void clearInt(MFRC522 mfrc522){ + mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg,0x7F); +} + 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 diff --git a/library.json b/library.json index 2f52a82..01a2794 100644 --- a/library.json +++ b/library.json @@ -7,7 +7,7 @@ "type": "git", "url": "https://github.com/miguelbalboa/rfid.git" }, - "version": "1.1.7", + "version": "1.1.8", "exclude": "doc", "frameworks": "arduino", "platforms": ["atmelavr", "ststm32", "teensy"] diff --git a/library.properties b/library.properties index 422b26b..85adb37 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MFRC522 -version=1.1.7 +version=1.1.8 author=GithubCommunity maintainer=miguelbalboa sentence=Arduino RFID Library for MFRC522 (SPI)