diff --git a/src/MFRC522.cpp b/src/MFRC522.cpp index 8119f70..f68afa5 100644 --- a/src/MFRC522.cpp +++ b/src/MFRC522.cpp @@ -11,6 +11,16 @@ // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// +/** + * Constructor. + * Prepares the output pins. + */ +MFRC522::MFRC522( 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). If there is no connection from the CPU to NRSTPD, set this to constant UNUSED_PIN. In this case, only soft reset will be used in PCD_Init(). + ) { + _chipSelectPin = chipSelectPin; + _resetPowerDownPin = resetPowerDownPin; +} // End constructor ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 @@ -23,12 +33,12 @@ void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to. One of the PCD_Register enums. byte value ///< The value to write. ) { - _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus digitalWrite(_chipSelectPin, LOW); // Select slave - _spiClass->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. - _spiClass->transfer(value); + SPI.transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. + SPI.transfer(value); digitalWrite(_chipSelectPin, HIGH); // Release slave again - _spiClass->endTransaction(); // Stop using the SPI bus + SPI.endTransaction(); // Stop using the SPI bus } // End PCD_WriteRegister() /** @@ -39,14 +49,14 @@ void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to byte count, ///< The number of bytes to write to the register byte *values ///< The values to write. Byte array. ) { - _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus digitalWrite(_chipSelectPin, LOW); // Select slave - _spiClass->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. + SPI.transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. for (byte index = 0; index < count; index++) { - _spiClass->transfer(values[index]); + SPI.transfer(values[index]); } digitalWrite(_chipSelectPin, HIGH); // Release slave again - _spiClass->endTransaction(); // Stop using the SPI bus + SPI.endTransaction(); // Stop using the SPI bus } // End PCD_WriteRegister() /** @@ -56,12 +66,12 @@ void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to byte MFRC522::PCD_ReadRegister( PCD_Register reg ///< The register to read from. One of the PCD_Register enums. ) { byte value; - _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus digitalWrite(_chipSelectPin, LOW); // Select slave - _spiClass->transfer(0x80 | reg); // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. - value = _spiClass->transfer(0); // Read the value back. Send 0 to stop reading. + SPI.transfer(0x80 | reg); // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. + value = SPI.transfer(0); // Read the value back. Send 0 to stop reading. digitalWrite(_chipSelectPin, HIGH); // Release slave again - _spiClass->endTransaction(); // Stop using the SPI bus + SPI.endTransaction(); // Stop using the SPI bus return value; } // End PCD_ReadRegister() @@ -80,26 +90,26 @@ void MFRC522::PCD_ReadRegister( PCD_Register reg, ///< The register to read from //Serial.print(F("Reading ")); Serial.print(count); Serial.println(F(" bytes from register.")); byte address = 0x80 | reg; // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. byte index = 0; // Index in values array. - _spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus + SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus digitalWrite(_chipSelectPin, LOW); // Select slave count--; // One read is performed outside of the loop - _spiClass->transfer(address); // Tell MFRC522 which address we want to read + SPI.transfer(address); // Tell MFRC522 which address we want to read if (rxAlign) { // Only update bit positions rxAlign..7 in values[0] // Create bit mask for bit positions rxAlign..7 byte mask = (0xFF << rxAlign) & 0xFF; // Read value and tell that we want to read the same address again. - byte value = _spiClass->transfer(address); + byte value = SPI.transfer(address); // Apply mask to both current value of values[0] and the new data in value. values[0] = (values[0] & ~mask) | (value & mask); index++; } while (index < count) { - values[index] = _spiClass->transfer(address); // Read value and tell that we want to read the same address again. + values[index] = SPI.transfer(address); // Read value and tell that we want to read the same address again. index++; } - values[index] = _spiClass->transfer(0); // Read the final byte. Send 0 to stop reading. + values[index] = SPI.transfer(0); // Read the final byte. Send 0 to stop reading. digitalWrite(_chipSelectPin, HIGH); // Release slave again - _spiClass->endTransaction(); // Stop using the SPI bus + SPI.endTransaction(); // Stop using the SPI bus } // End PCD_ReadRegister() /** diff --git a/src/MFRC522.h b/src/MFRC522.h index 8bb8f52..de25ece 100644 --- a/src/MFRC522.h +++ b/src/MFRC522.h @@ -83,6 +83,8 @@ #include #include +#define MFRC522_SPICLOCK SPI_CLOCK_DIV4 // MFRC522 accept upto 10MHz + // Firmware data for self-test // Reference values based on firmware version // Hint: if needed, you can remove unused self-test data to save flash memory @@ -325,15 +327,13 @@ public: } MIFARE_Key; // Member variables - Uid uid; // Used by PICC_ReadCardSerial(). + Uid uid; // Used by PICC_ReadCardSerial(). ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// - MFRC522(SPIClass *spiClass = &SPI, const SPISettings spiSettings = SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0)) - : _spiClass(spiClass), _spiSettings(spiSettings), _chipSelectPin(UNUSED_PIN), _resetPowerDownPin(UNUSED_PIN) {}; - MFRC522(byte chipSelectPin, byte resetPowerDownPin) - : _chipSelectPin(chipSelectPin), _resetPowerDownPin(resetPowerDownPin) { MFRC522(); }; + MFRC522() : MFRC522(UNUSED_PIN, UNUSED_PIN) {}; + MFRC522(byte chipSelectPin, byte resetPowerDownPin); ///////////////////////////////////////////////////////////////////////////////////// // Basic interface functions for communicating with the MFRC522 @@ -427,15 +427,8 @@ public: virtual bool PICC_ReadCardSerial(); protected: - // Pins 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) - - // SPI communication - SPIClass *_spiClass; // SPI class which abstracts hardware. - const SPISettings _spiSettings; // SPI settings. - - // Functions for communicating with MIFARE PICCs StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, int32_t data); };