From a78cfa44dabb8224e77c5f5094d5b2e21b591d23 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:23:54 +0100 Subject: [PATCH 01/11] adding custum wire-object --- MFRC522_I2C.cpp | 43 ++++++++++++++++++++++--------------------- MFRC522_I2C.h | 2 +- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 252256f..c1d0931 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -19,7 +19,8 @@ * Prepares the output pins. */ MFRC522::MFRC522( byte chipAddress, - 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) + TwoWire myWire ) { _chipAddress = chipAddress; _resetPowerDownPin = resetPowerDownPin; @@ -37,10 +38,10 @@ MFRC522::MFRC522( byte chipAddress, void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums. byte value ///< The value to write. ) { - Wire.beginTransmission(_chipAddress); - Wire.write(reg); - Wire.write(value); - Wire.endTransmission(); + myWire.beginTransmission(_chipAddress); + myWire.write(reg); + myWire.write(value); + myWire.endTransmission(); } // End PCD_WriteRegister() /** @@ -51,12 +52,12 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o byte count, ///< The number of bytes to write to the register byte *values ///< The values to write. Byte array. ) { - Wire.beginTransmission(_chipAddress); - Wire.write(reg); + myWire.beginTransmission(_chipAddress); + myWire.write(reg); for (byte index = 0; index < count; index++) { - Wire.write(values[index]); + myWire.write(values[index]); } - Wire.endTransmission(); + myWire.endTransmission(); } // End PCD_WriteRegister() /** @@ -67,12 +68,12 @@ byte MFRC522::PCD_ReadRegister( byte reg ///< The register to read from. One of ) { byte value; //digitalWrite(_chipSelectPin, LOW); // Select slave - Wire.beginTransmission(_chipAddress); - Wire.write(reg); - Wire.endTransmission(); + myWire.beginTransmission(_chipAddress); + myWire.write(reg); + myWire.endTransmission(); - Wire.requestFrom(_chipAddress, 1); - value = Wire.read(); + myWire.requestFrom(_chipAddress, 1); + value = myWire.read(); return value; } // End PCD_ReadRegister() @@ -90,11 +91,11 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o } byte address = reg; byte index = 0; // Index in values array. - Wire.beginTransmission(_chipAddress); - Wire.write(address); - Wire.endTransmission(); - Wire.requestFrom(_chipAddress, count); - while (Wire.available()) { + myWire.beginTransmission(_chipAddress); + myWire.write(address); + myWire.endTransmission(); + myWire.requestFrom(_chipAddress, count); + while (myWire.available()) { if (index == 0 && rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 byte mask = 0; @@ -102,12 +103,12 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o mask |= (1 << i); } // Read value and tell that we want to read the same address again. - byte value = Wire.read(); + byte value = myWire.read(); // Apply mask to both current value of values[0] and the new data in value. values[0] = (values[index] & ~mask) | (value & mask); } else { // Normal case - values[index] = Wire.read(); + values[index] = myWire.read(); } index++; } diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index 298e7f6..f8b8e11 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -321,7 +321,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(byte chipAddress, byte resetPowerDownPin); + MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire = Wire); ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 From a2c9c18e83b678b35902f5e7bf15790715a6588e Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:34:17 +0100 Subject: [PATCH 02/11] adding PCD_DumpVersionToSerial --- MFRC522_I2C.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index c1d0931..6cf346a 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -1213,6 +1213,29 @@ const __FlashStringHelper *MFRC522::PICC_GetTypeName(byte piccType ///< One of t } } // End PICC_GetTypeName() +/** + * Dumps debug info about the connected PCD to Serial. + * Shows all known firmware versions + */ +void MFRC522::PCD_DumpVersionToSerial() { + // Get the MFRC522 firmware version + byte v = _dev->PCD_ReadRegister(VersionReg); + Serial.print(F("Firmware Version: 0x")); + Serial.print(v, HEX); + // Lookup which version + switch(v) { + case 0x88: Serial.println(F(" = (clone)")); break; + case 0x90: Serial.println(F(" = v0.0")); break; + case 0x91: Serial.println(F(" = v1.0")); break; + case 0x92: Serial.println(F(" = v2.0")); break; + case 0x12: Serial.println(F(" = counterfeit chip")); break; + default: Serial.println(F(" = (unknown)")); + } + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); +} // End PCD_DumpVersionToSerial() + /** * Dumps debug info about the selected PICC to Serial. * On success the PICC is halted after dumping the data. From 11e1d469c843ee3dc96975147efecdf149e880df Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:39:44 +0100 Subject: [PATCH 03/11] adding missing member definition --- MFRC522_I2C.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index f8b8e11..3703393 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -383,6 +383,9 @@ public: // 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); + + // Support functions for debuging + void PCD_DumpVersionToSerial(); void PICC_DumpToSerial(Uid *uid); void PICC_DumpMifareClassicToSerial(Uid *uid, byte piccType, MIFARE_Key *key); void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector); From ae8faf0387dca2b6629c354a960041aaea43ccd7 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:51:15 +0100 Subject: [PATCH 04/11] Bugfixes --- MFRC522_I2C.cpp | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 6cf346a..0cf419a 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -20,10 +20,11 @@ */ MFRC522::MFRC522( byte chipAddress, byte resetPowerDownPin, ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire myWire + TwoWire TwoWireInstance ) { _chipAddress = chipAddress; _resetPowerDownPin = resetPowerDownPin; + _myWire = TwoWireInstance; } // End constructor @@ -38,10 +39,10 @@ MFRC522::MFRC522( byte chipAddress, void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums. byte value ///< The value to write. ) { - myWire.beginTransmission(_chipAddress); - myWire.write(reg); - myWire.write(value); - myWire.endTransmission(); + _myWire.beginTransmission(_chipAddress); + _myWire.write(reg); + _myWire.write(value); + _myWire.endTransmission(); } // End PCD_WriteRegister() /** @@ -52,12 +53,12 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o byte count, ///< The number of bytes to write to the register byte *values ///< The values to write. Byte array. ) { - myWire.beginTransmission(_chipAddress); - myWire.write(reg); + _myWire.beginTransmission(_chipAddress); + _myWire.write(reg); for (byte index = 0; index < count; index++) { - myWire.write(values[index]); + _myWire.write(values[index]); } - myWire.endTransmission(); + _myWire.endTransmission(); } // End PCD_WriteRegister() /** @@ -68,12 +69,12 @@ byte MFRC522::PCD_ReadRegister( byte reg ///< The register to read from. One of ) { byte value; //digitalWrite(_chipSelectPin, LOW); // Select slave - myWire.beginTransmission(_chipAddress); - myWire.write(reg); - myWire.endTransmission(); + _myWire.beginTransmission(_chipAddress); + _myWire.write(reg); + _myWire.endTransmission(); - myWire.requestFrom(_chipAddress, 1); - value = myWire.read(); + _myWire.requestFrom(_chipAddress, 1); + value = _myWire.read(); return value; } // End PCD_ReadRegister() @@ -91,11 +92,11 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o } byte address = reg; byte index = 0; // Index in values array. - myWire.beginTransmission(_chipAddress); - myWire.write(address); - myWire.endTransmission(); - myWire.requestFrom(_chipAddress, count); - while (myWire.available()) { + _myWire.beginTransmission(_chipAddress); + _myWire.write(address); + _myWire.endTransmission(); + _myWire.requestFrom(_chipAddress, count); + while (_myWire.available()) { if (index == 0 && rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 byte mask = 0; @@ -103,12 +104,12 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o mask |= (1 << i); } // Read value and tell that we want to read the same address again. - byte value = myWire.read(); + byte value = _myWire.read(); // Apply mask to both current value of values[0] and the new data in value. values[0] = (values[index] & ~mask) | (value & mask); } else { // Normal case - values[index] = myWire.read(); + values[index] = _myWire.read(); } index++; } @@ -1219,7 +1220,7 @@ const __FlashStringHelper *MFRC522::PICC_GetTypeName(byte piccType ///< One of t */ void MFRC522::PCD_DumpVersionToSerial() { // Get the MFRC522 firmware version - byte v = _dev->PCD_ReadRegister(VersionReg); + byte v = PCD_ReadRegister(VersionReg); Serial.print(F("Firmware Version: 0x")); Serial.print(v, HEX); // Lookup which version From 3572ad0f64dcd76ce1af1c9560043e84a0fe209a Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:55:15 +0100 Subject: [PATCH 05/11] additional cleanup --- MFRC522_I2C.cpp | 42 +++++++++++++++++++++--------------------- MFRC522_I2C.h | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 0cf419a..8bf2cf9 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -24,7 +24,7 @@ MFRC522::MFRC522( byte chipAddress, ) { _chipAddress = chipAddress; _resetPowerDownPin = resetPowerDownPin; - _myWire = TwoWireInstance; + _TwoWireInstance = TwoWireInstance; } // End constructor @@ -39,10 +39,10 @@ MFRC522::MFRC522( byte chipAddress, void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums. byte value ///< The value to write. ) { - _myWire.beginTransmission(_chipAddress); - _myWire.write(reg); - _myWire.write(value); - _myWire.endTransmission(); + _TwoWireInstance.beginTransmission(_chipAddress); + _TwoWireInstance.write(reg); + _TwoWireInstance.write(value); + _TwoWireInstance.endTransmission(); } // End PCD_WriteRegister() /** @@ -53,12 +53,12 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o byte count, ///< The number of bytes to write to the register byte *values ///< The values to write. Byte array. ) { - _myWire.beginTransmission(_chipAddress); - _myWire.write(reg); + _TwoWireInstance.beginTransmission(_chipAddress); + _TwoWireInstance.write(reg); for (byte index = 0; index < count; index++) { - _myWire.write(values[index]); + _TwoWireInstance.write(values[index]); } - _myWire.endTransmission(); + _TwoWireInstance.endTransmission(); } // End PCD_WriteRegister() /** @@ -69,12 +69,12 @@ byte MFRC522::PCD_ReadRegister( byte reg ///< The register to read from. One of ) { byte value; //digitalWrite(_chipSelectPin, LOW); // Select slave - _myWire.beginTransmission(_chipAddress); - _myWire.write(reg); - _myWire.endTransmission(); + _TwoWireInstance.beginTransmission(_chipAddress); + _TwoWireInstance.write(reg); + _TwoWireInstance.endTransmission(); - _myWire.requestFrom(_chipAddress, 1); - value = _myWire.read(); + _TwoWireInstance.requestFrom(_chipAddress, 1); + value = _TwoWireInstance.read(); return value; } // End PCD_ReadRegister() @@ -92,11 +92,11 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o } byte address = reg; byte index = 0; // Index in values array. - _myWire.beginTransmission(_chipAddress); - _myWire.write(address); - _myWire.endTransmission(); - _myWire.requestFrom(_chipAddress, count); - while (_myWire.available()) { + _TwoWireInstance.beginTransmission(_chipAddress); + _TwoWireInstance.write(address); + _TwoWireInstance.endTransmission(); + _TwoWireInstance.requestFrom(_chipAddress, count); + while (_TwoWireInstance.available()) { if (index == 0 && rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 byte mask = 0; @@ -104,12 +104,12 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o mask |= (1 << i); } // Read value and tell that we want to read the same address again. - byte value = _myWire.read(); + byte value = _TwoWireInstance.read(); // Apply mask to both current value of values[0] and the new data in value. values[0] = (values[index] & ~mask) | (value & mask); } else { // Normal case - values[index] = _myWire.read(); + values[index] = _TwoWireInstance.read(); } index++; } diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index 3703393..5d6cb82 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -321,7 +321,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire = Wire); + MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire TwoWireInstance); ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 From 95862a4e20ad58e3bb6c55de63180b7dde35fbd0 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 21:58:12 +0100 Subject: [PATCH 06/11] missing declaration --- MFRC522_I2C.h | 1 + 1 file changed, 1 insertion(+) diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index 5d6cb82..1b720a6 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -404,6 +404,7 @@ public: private: byte _chipAddress; byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) + TwoWire _TwoWireInstance; // TwoWire Instance byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); }; From d0138d78d8c9f1f89ebcd50e8ebd0a92a4ed9794 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 22:11:30 +0100 Subject: [PATCH 07/11] testst --- MFRC522_I2C.cpp | 5 ++--- MFRC522_I2C.h | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 8bf2cf9..cda1f85 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -20,11 +20,10 @@ */ MFRC522::MFRC522( byte chipAddress, byte resetPowerDownPin, ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire TwoWireInstance - ) { + TwoWire * TwoWireInstance + ) : _TwoWireInstance(TwoWireInstance) { _chipAddress = chipAddress; _resetPowerDownPin = resetPowerDownPin; - _TwoWireInstance = TwoWireInstance; } // End constructor diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index 1b720a6..b9c15f6 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -321,7 +321,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire TwoWireInstance); + MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire & TwoWireInstance = Wire); ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 @@ -404,7 +404,7 @@ public: private: byte _chipAddress; byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire _TwoWireInstance; // TwoWire Instance + TwoWire & _TwoWireInstance; // TwoWire Instance byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); }; From 33d637822da1bb49c79da7696ed9fffcf7a54870 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 3 Nov 2020 22:13:48 +0100 Subject: [PATCH 08/11] typo --- MFRC522_I2C.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index cda1f85..69b6c3c 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -20,7 +20,7 @@ */ MFRC522::MFRC522( byte chipAddress, byte resetPowerDownPin, ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire * TwoWireInstance + TwoWire & TwoWireInstance ) : _TwoWireInstance(TwoWireInstance) { _chipAddress = chipAddress; _resetPowerDownPin = resetPowerDownPin; From 70489f12c271aad51ca370721ab75f0a0c3bb7fe Mon Sep 17 00:00:00 2001 From: kkloesener Date: Mon, 21 Dec 2020 21:05:29 +0100 Subject: [PATCH 09/11] Rewrite of Basic Functions to work with TwoWire --- MFRC522_I2C.cpp | 27 ++-- MFRC522_I2C.h | 2 +- examples/MFRC522_i2c.ino | 54 ++++++++ examples/dual_i2c_scanner.ino | 113 +++++++++++++++ .../dual_i2c_scanner/dual_i2c_scanner.ino | 113 +++++++++++++++ .../dual_i2c_scanner_new.ino | 131 ++++++++++++++++++ library.properties | 6 +- 7 files changed, 432 insertions(+), 14 deletions(-) create mode 100644 examples/MFRC522_i2c.ino create mode 100644 examples/dual_i2c_scanner.ino create mode 100644 examples/dual_i2c_scanner/dual_i2c_scanner.ino create mode 100644 examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 69b6c3c..2f827c3 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -22,7 +22,7 @@ MFRC522::MFRC522( byte chipAddress, byte resetPowerDownPin, ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) TwoWire & TwoWireInstance ) : _TwoWireInstance(TwoWireInstance) { - _chipAddress = chipAddress; + _chipAddress = (uint8_t) chipAddress; _resetPowerDownPin = resetPowerDownPin; } // End constructor @@ -52,8 +52,12 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o byte count, ///< The number of bytes to write to the register byte *values ///< The values to write. Byte array. ) { + if (count == 0) { + return; + } + uint8_t regist = (uint8_t) reg; _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(reg); + _TwoWireInstance.write(regist); for (byte index = 0; index < count; index++) { _TwoWireInstance.write(values[index]); } @@ -67,12 +71,15 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o byte MFRC522::PCD_ReadRegister( byte reg ///< The register to read from. One of the PCD_Register enums. ) { byte value; + uint8_t _size = 1; + uint8_t regist; + regist = (uint8_t) reg; //digitalWrite(_chipSelectPin, LOW); // Select slave _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(reg); + _TwoWireInstance.write(regist); _TwoWireInstance.endTransmission(); - _TwoWireInstance.requestFrom(_chipAddress, 1); + _TwoWireInstance.requestFrom(_chipAddress, _size); value = _TwoWireInstance.read(); return value; } // End PCD_ReadRegister() @@ -89,12 +96,13 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o if (count == 0) { return; } - byte address = reg; + uint8_t _count = (uint8_t) count; + uint8_t regist = (uint8_t) reg; byte index = 0; // Index in values array. _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(address); + _TwoWireInstance.write(regist); _TwoWireInstance.endTransmission(); - _TwoWireInstance.requestFrom(_chipAddress, count); + _TwoWireInstance.requestFrom(_chipAddress, _count); while (_TwoWireInstance.available()) { if (index == 0 && rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 @@ -181,8 +189,6 @@ byte MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to the data to tra * Initializes the MFRC522 chip. */ void MFRC522::PCD_Init() { - // Set the chipSelectPin as digital output, do not select the slave yet - // Set the resetPowerDownPin as digital output, do not reset or power down. pinMode(_resetPowerDownPin, OUTPUT); @@ -190,7 +196,7 @@ void MFRC522::PCD_Init() { 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. - delay(50); + delay(100); } else { // Perform a soft reset PCD_Reset(); @@ -220,6 +226,7 @@ void MFRC522::PCD_Reset() { delay(50); // Wait for the PowerDown bit in CommandReg to be cleared while (PCD_ReadRegister(CommandReg) & (1<<4)) { + Serial.println("PCD Still restarting after SoftReset"); // PCD still restarting - unlikely after waiting 50ms, but better safe than sorry. } } // End PCD_Reset() diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index b9c15f6..82520eb 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -402,7 +402,7 @@ public: bool PICC_ReadCardSerial(); private: - byte _chipAddress; + uint16_t _chipAddress; byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) TwoWire & _TwoWireInstance; // TwoWire Instance byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); diff --git a/examples/MFRC522_i2c.ino b/examples/MFRC522_i2c.ino new file mode 100644 index 0000000..5bb4ee0 --- /dev/null +++ b/examples/MFRC522_i2c.ino @@ -0,0 +1,54 @@ +#include +#include "MFRC522_I2C.h" + +#define RST_PIN 6 // Arduino UNO Pin +// #define RST_PIN 14 // D5 Pin on NodeMCU + + +// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match. +MFRC522 mfrc522(0x28, RST_PIN); // Create MFRC522 instance. + +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + Wire.begin(); // Initialize I2C + mfrc522.PCD_Init(); // Init MFRC522 + ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details + Serial.println(F("Scan PICC to see UID, type, and data blocks...")); +} + +void loop() { + // Look for new cards, and select one if present + if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { + delay(50); + return; + } + + // Now a card is selected. The UID and SAK is in mfrc522.uid. + + // Dump UID + Serial.print(F("Card UID:")); + for (byte i = 0; i < mfrc522.uid.size; i++) { + Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); + Serial.print(mfrc522.uid.uidByte[i], HEX); + } + Serial.println(); +} + +void ShowReaderDetails() { + // Get the MFRC522 software version + byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + Serial.print(F("MFRC522 Software Version: 0x")); + Serial.print(v, HEX); + if (v == 0x91) + Serial.print(F(" = v1.0")); + else if (v == 0x92) + Serial.print(F(" = v2.0")); + else + Serial.print(F(" (unknown)")); + Serial.println(""); + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) { + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); + } +} + diff --git a/examples/dual_i2c_scanner.ino b/examples/dual_i2c_scanner.ino new file mode 100644 index 0000000..a6d2714 --- /dev/null +++ b/examples/dual_i2c_scanner.ino @@ -0,0 +1,113 @@ +#include +#include "MFRC522_I2C.h" + +// I2C GPIOs +#define IIC_CLK 32 // internal +#define IIC_DATA 33 // internal + +// 2nd I2C GPIOs +#define RST_PIN 12 +#define ext_IIC_CLK 23 // 14-pin-header +#define ext_IIC_DATA 18 // 14-pin-header + +TwoWire i2cBusOne = TwoWire(0); +TwoWire i2cBusTwo = TwoWire(1); +MFRC522 mfrc522(RST_PIN , 0x28, i2cBusTwo); + +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000); + i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000); +} + +void loop() { + scanBus1(); + delay(3000); + scanBus2(); + mfrc522.PCD_Init(); // Init MFRC522 + ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details + +} + +void scanBus1 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusOne.beginTransmission(address); + error = i2cBusOne.endTransmission(); + if (error == 0) { + Serial.print("Bus1: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } + else { + Serial.println("done\n"); + } +} + +void scanBus2 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusTwo.beginTransmission(address); + error = i2cBusTwo.endTransmission(); + if (error == 0) { + Serial.print("Bus2: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } + else { + Serial.println("done\n"); + } +} + +void ShowReaderDetails() { + // Get the MFRC522 software version + byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + Serial.print(F("MFRC522 Software Version: 0x")); + Serial.print(v, HEX); + if (v == 0x91) + Serial.print(F(" = v1.0")); + else if (v == 0x92) + Serial.print(F(" = v2.0")); + else + Serial.print(F(" (unknown)")); + Serial.println(""); + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) { + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); + } +} + diff --git a/examples/dual_i2c_scanner/dual_i2c_scanner.ino b/examples/dual_i2c_scanner/dual_i2c_scanner.ino new file mode 100644 index 0000000..a6d2714 --- /dev/null +++ b/examples/dual_i2c_scanner/dual_i2c_scanner.ino @@ -0,0 +1,113 @@ +#include +#include "MFRC522_I2C.h" + +// I2C GPIOs +#define IIC_CLK 32 // internal +#define IIC_DATA 33 // internal + +// 2nd I2C GPIOs +#define RST_PIN 12 +#define ext_IIC_CLK 23 // 14-pin-header +#define ext_IIC_DATA 18 // 14-pin-header + +TwoWire i2cBusOne = TwoWire(0); +TwoWire i2cBusTwo = TwoWire(1); +MFRC522 mfrc522(RST_PIN , 0x28, i2cBusTwo); + +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000); + i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000); +} + +void loop() { + scanBus1(); + delay(3000); + scanBus2(); + mfrc522.PCD_Init(); // Init MFRC522 + ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details + +} + +void scanBus1 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusOne.beginTransmission(address); + error = i2cBusOne.endTransmission(); + if (error == 0) { + Serial.print("Bus1: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } + else { + Serial.println("done\n"); + } +} + +void scanBus2 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusTwo.beginTransmission(address); + error = i2cBusTwo.endTransmission(); + if (error == 0) { + Serial.print("Bus2: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } + else { + Serial.println("done\n"); + } +} + +void ShowReaderDetails() { + // Get the MFRC522 software version + byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + Serial.print(F("MFRC522 Software Version: 0x")); + Serial.print(v, HEX); + if (v == 0x91) + Serial.print(F(" = v1.0")); + else if (v == 0x92) + Serial.print(F(" = v2.0")); + else + Serial.print(F(" (unknown)")); + Serial.println(""); + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) { + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); + } +} + diff --git a/examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino b/examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino new file mode 100644 index 0000000..9ea5b87 --- /dev/null +++ b/examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino @@ -0,0 +1,131 @@ +#include +#include "MFRC522_I2C.h" + +// I2C GPIOs +#define IIC_CLK 32 // internal +#define IIC_DATA 33 // internal + +// 2nd I2C GPIOs +#define RST_PIN 12 +#define ext_IIC_CLK 23 // 14-pin-header +#define ext_IIC_DATA 18 // 14-pin-header + +byte chipAddress = 0x28; + +TwoWire i2cBusOne = TwoWire(0); +TwoWire i2cBusTwo = TwoWire(1); +MFRC522 mfrc522(chipAddress, RST_PIN, i2cBusTwo); + +void setup() { + byte version; + byte value; + Serial.begin(115200); // Initialize serial communications with the PC + Serial.println("TwoWire Bus 1 initiieren"); + i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000); + Serial.println("TwoWire Bus 2 initiieren"); + i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000); + Serial.println("MFRC522 initiieren"); + mfrc522.PCD_Init(); + Serial.println("MFRC522 Selbsttest"); +} + +void loop() { +// scanBus1(); + scanBus2(); + ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details + if (mfrc522.PCD_PerformSelfTest()) { + Serial.println("Selftest OK"); + } else Serial.println("Selftest not OK"); + delay(5000); +} + +void scanBus1 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusOne.beginTransmission(address); + error = i2cBusOne.endTransmission(); + if (error == 0) { + Serial.print("Bus1: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } + else { + Serial.println("done\n"); + } +} + +void scanBus2 () { + byte error, address; + int nDevices; + Serial.println("Scanning..."); + nDevices = 0; + for(address = 1; address < 127; address++ ) { + i2cBusTwo.beginTransmission(address); + error = i2cBusTwo.endTransmission(); + if (error == 0) { + Serial.print("Bus2: I2C device found at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + nDevices++; + } + else if (error==4) { + Serial.print("Unknow error at address 0x"); + if (address<16) { + Serial.print("0"); + } + Serial.println(address,HEX); + } + } +/* if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + pinMode(RST_PIN, OUTPUT); + if (digitalRead(RST_PIN) == LOW) { //The MFRC522 chip is in power down mode. + digitalWrite(RST_PIN, 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. + delay(200); + } + else { + Serial.println("done\n"); + } + } */ +} + +void ShowReaderDetails() { + byte version = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + + switch (version) { + case 0x88: // Fudan Semiconductor FM17522 clone + Serial.println("Fudan Semiconductor FM17522 clone"); + break; + case 0x90: // Version 0.0 + Serial.println("Version 0.0"); + break; + case 0x91: // Version 1.0 + Serial.println("Version 1.0"); + break; + case 0x92: // Version 2.0 + Serial.println("Version 2.0"); + break; + default: // Unknown version + Serial.println("Unknown version"); + } +} \ No newline at end of file diff --git a/library.properties b/library.properties index d153156..35ef7c5 100644 --- a/library.properties +++ b/library.properties @@ -2,8 +2,8 @@ name=MFRC522 i2c Library version=1.0 author=arozcan semaf maintainer=Semaf Electronics -sentence=MFR522 i2c Library to read NFC Tags with Arduino or ESP2866 (NodeMCU) -paragraph=MFR522 i2c Library to read NFC Tags with Arduino or ESP2866 (NodeMCU) +sentence=MFR522 i2c Library to read NFC Tags with Arduino or ESP2866/ESP32 (NodeMCU) +paragraph=MFR522 i2c Library to read NFC Tags with Arduino or ESP2866/ESP32 (NodeMCU) category=Communication -url=https://github.com/semaf/mfrc522_i2c +url=https://github.com/kkloesener/MFRC522_I2C_Library architectures=* From 6ec5c97abb819c377977958edb3bdc9ae38b0310 Mon Sep 17 00:00:00 2001 From: kkloesener Date: Tue, 23 Mar 2021 13:47:45 +0100 Subject: [PATCH 10/11] Correcting PointerUsage with new Constructor --- MFRC522_I2C.cpp | 45 +++++++++++++++++++++++---------------------- MFRC522_I2C.h | 5 +++-- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/MFRC522_I2C.cpp b/MFRC522_I2C.cpp index 2f827c3..9c4675b 100644 --- a/MFRC522_I2C.cpp +++ b/MFRC522_I2C.cpp @@ -20,10 +20,11 @@ */ MFRC522::MFRC522( byte chipAddress, byte resetPowerDownPin, ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire & TwoWireInstance - ) : _TwoWireInstance(TwoWireInstance) { + TwoWire *TwoWireInstance + ) { _chipAddress = (uint8_t) chipAddress; _resetPowerDownPin = resetPowerDownPin; + _TwoWireInstance = TwoWireInstance; } // End constructor @@ -38,10 +39,10 @@ MFRC522::MFRC522( byte chipAddress, void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums. byte value ///< The value to write. ) { - _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(reg); - _TwoWireInstance.write(value); - _TwoWireInstance.endTransmission(); + _TwoWireInstance->beginTransmission(_chipAddress); + _TwoWireInstance->write(reg); + _TwoWireInstance->write(value); + _TwoWireInstance->endTransmission(); } // End PCD_WriteRegister() /** @@ -56,12 +57,12 @@ void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One o return; } uint8_t regist = (uint8_t) reg; - _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(regist); + _TwoWireInstance->beginTransmission(_chipAddress); + _TwoWireInstance->write(regist); for (byte index = 0; index < count; index++) { - _TwoWireInstance.write(values[index]); + _TwoWireInstance->write(values[index]); } - _TwoWireInstance.endTransmission(); + _TwoWireInstance->endTransmission(); } // End PCD_WriteRegister() /** @@ -75,12 +76,12 @@ byte MFRC522::PCD_ReadRegister( byte reg ///< The register to read from. One of uint8_t regist; regist = (uint8_t) reg; //digitalWrite(_chipSelectPin, LOW); // Select slave - _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(regist); - _TwoWireInstance.endTransmission(); + _TwoWireInstance->beginTransmission(_chipAddress); + _TwoWireInstance->write(regist); + _TwoWireInstance->endTransmission(); - _TwoWireInstance.requestFrom(_chipAddress, _size); - value = _TwoWireInstance.read(); + _TwoWireInstance->requestFrom(_chipAddress, _size); + value = _TwoWireInstance->read(); return value; } // End PCD_ReadRegister() @@ -99,11 +100,11 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o uint8_t _count = (uint8_t) count; uint8_t regist = (uint8_t) reg; byte index = 0; // Index in values array. - _TwoWireInstance.beginTransmission(_chipAddress); - _TwoWireInstance.write(regist); - _TwoWireInstance.endTransmission(); - _TwoWireInstance.requestFrom(_chipAddress, _count); - while (_TwoWireInstance.available()) { + _TwoWireInstance->beginTransmission(_chipAddress); + _TwoWireInstance->write(regist); + _TwoWireInstance->endTransmission(); + _TwoWireInstance->requestFrom(_chipAddress, _count); + while (_TwoWireInstance->available()) { if (index == 0 && rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 byte mask = 0; @@ -111,12 +112,12 @@ void MFRC522::PCD_ReadRegister( byte reg, ///< The register to read from. One o mask |= (1 << i); } // Read value and tell that we want to read the same address again. - byte value = _TwoWireInstance.read(); + byte value = _TwoWireInstance->read(); // Apply mask to both current value of values[0] and the new data in value. values[0] = (values[index] & ~mask) | (value & mask); } else { // Normal case - values[index] = _TwoWireInstance.read(); + values[index] = _TwoWireInstance->read(); } index++; } diff --git a/MFRC522_I2C.h b/MFRC522_I2C.h index 82520eb..8076829 100644 --- a/MFRC522_I2C.h +++ b/MFRC522_I2C.h @@ -321,7 +321,8 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire & TwoWireInstance = Wire); + // MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire & TwoWireInstance = Wire); + MFRC522(byte chipAddress, byte resetPowerDownPin, TwoWire *TwoWireInstance = &Wire); ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 @@ -404,7 +405,7 @@ public: private: uint16_t _chipAddress; byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) - TwoWire & _TwoWireInstance; // TwoWire Instance + TwoWire *_TwoWireInstance = NULL; // TwoWire Instance byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); }; From b03a842257a4ca78a9630c782f935db7a70d711e Mon Sep 17 00:00:00 2001 From: kkloesener Date: Thu, 25 Mar 2021 12:36:56 +0100 Subject: [PATCH 11/11] Moved Sources in Subfolder --- MFRC522_I2C.cpp => src/MFRC522_I2C.cpp | 0 MFRC522_I2C.h => src/MFRC522_I2C.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename MFRC522_I2C.cpp => src/MFRC522_I2C.cpp (100%) rename MFRC522_I2C.h => src/MFRC522_I2C.h (100%) diff --git a/MFRC522_I2C.cpp b/src/MFRC522_I2C.cpp similarity index 100% rename from MFRC522_I2C.cpp rename to src/MFRC522_I2C.cpp diff --git a/MFRC522_I2C.h b/src/MFRC522_I2C.h similarity index 100% rename from MFRC522_I2C.h rename to src/MFRC522_I2C.h