changeable spi class and settings
This commit is contained in:
@@ -11,16 +11,6 @@
|
|||||||
// Functions for setting up the Arduino
|
// 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
|
// Basic interface functions for communicating with the MFRC522
|
||||||
@@ -33,12 +23,12 @@ MFRC522::MFRC522( byte chipSelectPin, ///< Arduino pin connected to MFRC522's S
|
|||||||
void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to. One of the PCD_Register enums.
|
void MFRC522::PCD_WriteRegister( PCD_Register reg, ///< The register to write to. One of the PCD_Register enums.
|
||||||
byte value ///< The value to write.
|
byte value ///< The value to write.
|
||||||
) {
|
) {
|
||||||
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
|
_spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus
|
||||||
digitalWrite(_chipSelectPin, LOW); // Select slave
|
digitalWrite(_chipSelectPin, LOW); // Select slave
|
||||||
SPI.transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
|
_spiClass->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
|
||||||
SPI.transfer(value);
|
_spiClass->transfer(value);
|
||||||
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
||||||
SPI.endTransaction(); // Stop using the SPI bus
|
_spiClass->endTransaction(); // Stop using the SPI bus
|
||||||
} // End PCD_WriteRegister()
|
} // End PCD_WriteRegister()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,14 +39,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 count, ///< The number of bytes to write to the register
|
||||||
byte *values ///< The values to write. Byte array.
|
byte *values ///< The values to write. Byte array.
|
||||||
) {
|
) {
|
||||||
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
|
_spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus
|
||||||
digitalWrite(_chipSelectPin, LOW); // Select slave
|
digitalWrite(_chipSelectPin, LOW); // Select slave
|
||||||
SPI.transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
|
_spiClass->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++) {
|
for (byte index = 0; index < count; index++) {
|
||||||
SPI.transfer(values[index]);
|
_spiClass->transfer(values[index]);
|
||||||
}
|
}
|
||||||
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
||||||
SPI.endTransaction(); // Stop using the SPI bus
|
_spiClass->endTransaction(); // Stop using the SPI bus
|
||||||
} // End PCD_WriteRegister()
|
} // End PCD_WriteRegister()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,12 +56,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 MFRC522::PCD_ReadRegister( PCD_Register reg ///< The register to read from. One of the PCD_Register enums.
|
||||||
) {
|
) {
|
||||||
byte value;
|
byte value;
|
||||||
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
|
_spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus
|
||||||
digitalWrite(_chipSelectPin, LOW); // Select slave
|
digitalWrite(_chipSelectPin, LOW); // Select slave
|
||||||
SPI.transfer(0x80 | reg); // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
|
_spiClass->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.
|
value = _spiClass->transfer(0); // Read the value back. Send 0 to stop reading.
|
||||||
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
||||||
SPI.endTransaction(); // Stop using the SPI bus
|
_spiClass->endTransaction(); // Stop using the SPI bus
|
||||||
return value;
|
return value;
|
||||||
} // End PCD_ReadRegister()
|
} // End PCD_ReadRegister()
|
||||||
|
|
||||||
@@ -90,26 +80,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."));
|
//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 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.
|
byte index = 0; // Index in values array.
|
||||||
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
|
_spiClass->beginTransaction(_spiSettings); // Set the settings to work with SPI bus
|
||||||
digitalWrite(_chipSelectPin, LOW); // Select slave
|
digitalWrite(_chipSelectPin, LOW); // Select slave
|
||||||
count--; // One read is performed outside of the loop
|
count--; // One read is performed outside of the loop
|
||||||
SPI.transfer(address); // Tell MFRC522 which address we want to read
|
_spiClass->transfer(address); // Tell MFRC522 which address we want to read
|
||||||
if (rxAlign) { // Only update bit positions rxAlign..7 in values[0]
|
if (rxAlign) { // Only update bit positions rxAlign..7 in values[0]
|
||||||
// Create bit mask for bit positions rxAlign..7
|
// Create bit mask for bit positions rxAlign..7
|
||||||
byte mask = (0xFF << rxAlign) & 0xFF;
|
byte mask = (0xFF << rxAlign) & 0xFF;
|
||||||
// Read value and tell that we want to read the same address again.
|
// Read value and tell that we want to read the same address again.
|
||||||
byte value = SPI.transfer(address);
|
byte value = _spiClass->transfer(address);
|
||||||
// Apply mask to both current value of values[0] and the new data in value.
|
// Apply mask to both current value of values[0] and the new data in value.
|
||||||
values[0] = (values[0] & ~mask) | (value & mask);
|
values[0] = (values[0] & ~mask) | (value & mask);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
while (index < count) {
|
while (index < count) {
|
||||||
values[index] = SPI.transfer(address); // Read value and tell that we want to read the same address again.
|
values[index] = _spiClass->transfer(address); // Read value and tell that we want to read the same address again.
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
values[index] = SPI.transfer(0); // Read the final byte. Send 0 to stop reading.
|
values[index] = _spiClass->transfer(0); // Read the final byte. Send 0 to stop reading.
|
||||||
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
digitalWrite(_chipSelectPin, HIGH); // Release slave again
|
||||||
SPI.endTransaction(); // Stop using the SPI bus
|
_spiClass->endTransaction(); // Stop using the SPI bus
|
||||||
} // End PCD_ReadRegister()
|
} // End PCD_ReadRegister()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -83,8 +83,6 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
|
|
||||||
#define MFRC522_SPICLOCK SPI_CLOCK_DIV4 // MFRC522 accept upto 10MHz
|
|
||||||
|
|
||||||
// Firmware data for self-test
|
// Firmware data for self-test
|
||||||
// Reference values based on firmware version
|
// Reference values based on firmware version
|
||||||
// Hint: if needed, you can remove unused self-test data to save flash memory
|
// Hint: if needed, you can remove unused self-test data to save flash memory
|
||||||
@@ -327,13 +325,15 @@ public:
|
|||||||
} MIFARE_Key;
|
} MIFARE_Key;
|
||||||
|
|
||||||
// Member variables
|
// Member variables
|
||||||
Uid uid; // Used by PICC_ReadCardSerial().
|
Uid uid; // Used by PICC_ReadCardSerial().
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Functions for setting up the Arduino
|
// Functions for setting up the Arduino
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
MFRC522() : MFRC522(UNUSED_PIN, UNUSED_PIN) {};
|
MFRC522(SPIClass *spiClass = &SPI, const SPISettings spiSettings = SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0))
|
||||||
MFRC522(byte chipSelectPin, byte resetPowerDownPin);
|
: _spiClass(spiClass), _spiSettings(spiSettings), _chipSelectPin(UNUSED_PIN), _resetPowerDownPin(UNUSED_PIN) {};
|
||||||
|
MFRC522(byte chipSelectPin, byte resetPowerDownPin)
|
||||||
|
: _chipSelectPin(chipSelectPin), _resetPowerDownPin(resetPowerDownPin) { MFRC522(); };
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Basic interface functions for communicating with the MFRC522
|
// Basic interface functions for communicating with the MFRC522
|
||||||
@@ -421,8 +421,15 @@ public:
|
|||||||
virtual bool PICC_ReadCardSerial();
|
virtual bool PICC_ReadCardSerial();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// Pins
|
||||||
byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low)
|
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)
|
||||||
|
|
||||||
|
// 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);
|
StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, int32_t data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user