Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to use diferent SPI bus #571

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/MFRC522.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ MFRC522::MFRC522( byte chipSelectPin, ///< Arduino pin connected to MFRC522's S
_resetPowerDownPin = resetPowerDownPin;
} // End constructor

/**
* 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 UINT8_MAX. In this case, only soft reset will be used in PCD_Init().
SPIClass& selectedSPI ///< SPIClass pointer for using different SPI ports.
) {
_chipSelectPin = chipSelectPin;
_resetPowerDownPin = resetPowerDownPin;
_rfidSPI = &selectedSPI;
} // End constructor

/////////////////////////////////////////////////////////////////////////////////////
// Basic interface functions for communicating with the MFRC522
/////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -46,12 +59,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.
byte value ///< The value to write.
) {
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
_rfidSPI->beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
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.
SPI.transfer(value);
_rfidSPI->transfer(reg); // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
_rfidSPI->transfer(value);
digitalWrite(_chipSelectPin, HIGH); // Release slave again
SPI.endTransaction(); // Stop using the SPI bus
_rfidSPI->endTransaction(); // Stop using the SPI bus
} // End PCD_WriteRegister()

/**
Expand All @@ -62,14 +75,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.
) {
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
_rfidSPI->beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
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.
_rfidSPI->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++) {
SPI.transfer(values[index]);
_rfidSPI->transfer(values[index]);
}
digitalWrite(_chipSelectPin, HIGH); // Release slave again
SPI.endTransaction(); // Stop using the SPI bus
_rfidSPI->endTransaction(); // Stop using the SPI bus
} // End PCD_WriteRegister()

/**
Expand All @@ -79,12 +92,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;
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
_rfidSPI->beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
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.
value = SPI.transfer(0); // Read the value back. Send 0 to stop reading.
_rfidSPI->transfer(0x80 | reg); // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
value = _rfidSPI->transfer(0); // Read the value back. Send 0 to stop reading.
digitalWrite(_chipSelectPin, HIGH); // Release slave again
SPI.endTransaction(); // Stop using the SPI bus
_rfidSPI->endTransaction(); // Stop using the SPI bus
return value;
} // End PCD_ReadRegister()

Expand All @@ -103,26 +116,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.
SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0)); // Set the settings to work with SPI bus
_rfidSPI->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
SPI.transfer(address); // Tell MFRC522 which address we want to read
_rfidSPI->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 = SPI.transfer(address);
byte value = _rfidSPI->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] = SPI.transfer(address); // Read value and tell that we want to read the same address again.
values[index] = _rfidSPI->transfer(address); // Read value and tell that we want to read the same address again.
index++;
}
values[index] = SPI.transfer(0); // Read the final byte. Send 0 to stop reading.
values[index] = _rfidSPI->transfer(0); // Read the final byte. Send 0 to stop reading.
digitalWrite(_chipSelectPin, HIGH); // Release slave again
SPI.endTransaction(); // Stop using the SPI bus
_rfidSPI->endTransaction(); // Stop using the SPI bus
} // End PCD_ReadRegister()

/**
Expand All @@ -147,7 +160,6 @@ void MFRC522::PCD_ClearRegisterBitMask( PCD_Register reg, ///< The register to u
PCD_WriteRegister(reg, tmp & (~mask)); // clear bit mask
} // End PCD_ClearRegisterBitMask()


/**
* Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
*
Expand Down
4 changes: 3 additions & 1 deletion src/MFRC522.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class MFRC522 {
MFRC522();
MFRC522(byte resetPowerDownPin);
MFRC522(byte chipSelectPin, byte resetPowerDownPin);
MFRC522(byte chipSelectPin, byte resetPowerDownPin, SPIClass& selectedSPI);

/////////////////////////////////////////////////////////////////////////////////////
// Basic interface functions for communicating with the MFRC522
Expand Down Expand Up @@ -365,7 +366,8 @@ class MFRC522 {
protected:
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)
SPIClass * _rfidSPI = &SPI; // SPIClass pointer for using a different SPI port. SPI is the default class.
StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, int32_t data);
};

#endif
#endif