Better implementation of the PCD_Init() method. Thanks to Rotzbua
to point this out.
This commit is contained in:
48
MFRC522.cpp
48
MFRC522.cpp
@@ -14,7 +14,6 @@
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
MFRC522::MFRC522() {
|
MFRC522::MFRC522() {
|
||||||
|
|
||||||
} // End constructor
|
} // End constructor
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +44,7 @@ void MFRC522::setSPIConfig() {
|
|||||||
* Writes a byte to the specified register in the MFRC522 chip.
|
* Writes a byte to the specified register in the MFRC522 chip.
|
||||||
* The interface is described in the datasheet section 8.1.2.
|
* The interface is described in the datasheet section 8.1.2.
|
||||||
*/
|
*/
|
||||||
void MFRC522::PCD_WriteRegister(byte reg, ///< The register to write to. One of the PCD_Register enums.
|
void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums.
|
||||||
byte value ///< The value to write.
|
byte value ///< The value to write.
|
||||||
) {
|
) {
|
||||||
digitalWrite(_chipSelectPin, LOW); // Select slave
|
digitalWrite(_chipSelectPin, LOW); // Select slave
|
||||||
@@ -58,7 +57,7 @@ void MFRC522::PCD_WriteRegister(byte reg, ///< The register to write to. One of
|
|||||||
* Writes a number of bytes to the specified register in the MFRC522 chip.
|
* Writes a number of bytes to the specified register in the MFRC522 chip.
|
||||||
* The interface is described in the datasheet section 8.1.2.
|
* The interface is described in the datasheet section 8.1.2.
|
||||||
*/
|
*/
|
||||||
void MFRC522::PCD_WriteRegister(byte reg, ///< The register to write to. One of the PCD_Register enums.
|
void MFRC522::PCD_WriteRegister( byte reg, ///< The register to write to. One of the PCD_Register enums.
|
||||||
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.
|
||||||
) {
|
) {
|
||||||
@@ -225,43 +224,15 @@ void MFRC522::PCD_Init() {
|
|||||||
/**
|
/**
|
||||||
* Initializes the MFRC522 chip.
|
* Initializes the MFRC522 chip.
|
||||||
*/
|
*/
|
||||||
void MFRC522::PCD_Init(byte chipSelectPin, byte resetPowerDownPin) {
|
void MFRC522::PCD_Init( 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)
|
||||||
|
) {
|
||||||
_chipSelectPin = chipSelectPin;
|
_chipSelectPin = chipSelectPin;
|
||||||
_resetPowerDownPin = resetPowerDownPin;
|
_resetPowerDownPin = resetPowerDownPin;
|
||||||
|
|
||||||
// Set the chipSelectPin as digital output, do not select the slave yet
|
// Set the chipSelectPin as digital output, do not select the slave yet
|
||||||
pinMode(_chipSelectPin, OUTPUT);
|
PCD_Init();
|
||||||
digitalWrite(_chipSelectPin, HIGH);
|
|
||||||
|
|
||||||
// Set the resetPowerDownPin as digital output, do not reset or power down.
|
|
||||||
pinMode(_resetPowerDownPin, OUTPUT);
|
|
||||||
|
|
||||||
// Set SPI bus to work with MFRC522 chip.
|
|
||||||
setSPIConfig();
|
|
||||||
|
|
||||||
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<37>s. Let us be generous: 50ms.
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
else { // Perform a soft reset
|
|
||||||
PCD_Reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// When communicating with a PICC we need a timeout if something goes wrong.
|
|
||||||
// f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo].
|
|
||||||
// TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg.
|
|
||||||
PCD_WriteRegister(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds
|
|
||||||
PCD_WriteRegister(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25<32>s.
|
|
||||||
PCD_WriteRegister(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout.
|
|
||||||
PCD_WriteRegister(TReloadRegL, 0xE8);
|
|
||||||
|
|
||||||
PCD_WriteRegister(TxASKReg, 0x40); // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting
|
|
||||||
PCD_WriteRegister(ModeReg, 0x3D); // Default 0x3F. Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4)
|
|
||||||
PCD_AntennaOn(); // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset)
|
|
||||||
} // End PCD_Init()
|
} // End PCD_Init()
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
|
* Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
|
||||||
*/
|
*/
|
||||||
@@ -406,7 +377,7 @@ bool MFRC522::PCD_PerformSelfTest() {
|
|||||||
*
|
*
|
||||||
* @return STATUS_OK on success, STATUS_??? otherwise.
|
* @return STATUS_OK on success, STATUS_??? otherwise.
|
||||||
*/
|
*/
|
||||||
byte MFRC522::PCD_TransceiveData(byte *sendData, ///< Pointer to the data to transfer to the FIFO.
|
byte MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to transfer to the FIFO.
|
||||||
byte sendLen, ///< Number of bytes to transfer to the FIFO.
|
byte sendLen, ///< Number of bytes to transfer to the FIFO.
|
||||||
byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command.
|
byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command.
|
||||||
byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned.
|
byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned.
|
||||||
@@ -1221,7 +1192,10 @@ byte MFRC522::PICC_GetType(byte sak ///< The SAK byte returned from PICC_Select
|
|||||||
if (sak & 0x04) { // UID not complete
|
if (sak & 0x04) { // UID not complete
|
||||||
return PICC_TYPE_NOT_COMPLETE;
|
return PICC_TYPE_NOT_COMPLETE;
|
||||||
}
|
}
|
||||||
|
//http://www.nxp.com/documents/application_note/AN10833.pdf
|
||||||
|
//3.2 Coding of Select Acknowledge (SAK)
|
||||||
|
//ignore 8-bit
|
||||||
|
sak&=0x7F;
|
||||||
switch (sak) {
|
switch (sak) {
|
||||||
case 0x09: return PICC_TYPE_MIFARE_MINI; break;
|
case 0x09: return PICC_TYPE_MIFARE_MINI; break;
|
||||||
case 0x08: return PICC_TYPE_MIFARE_1K; break;
|
case 0x08: return PICC_TYPE_MIFARE_1K; break;
|
||||||
|
|||||||
@@ -339,7 +339,7 @@ public:
|
|||||||
// Functions for manipulating the MFRC522
|
// Functions for manipulating the MFRC522
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
void PCD_Init();
|
void PCD_Init();
|
||||||
void PCD_Init(byte, byte);
|
void PCD_Init(byte chipSelectPin, byte resetPowerDownPin);
|
||||||
void PCD_Reset();
|
void PCD_Reset();
|
||||||
void PCD_AntennaOn();
|
void PCD_AntennaOn();
|
||||||
void PCD_AntennaOff();
|
void PCD_AntennaOff();
|
||||||
|
|||||||
@@ -10,6 +10,9 @@
|
|||||||
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
|
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
|
||||||
* (= card/tag).
|
* (= card/tag).
|
||||||
*
|
*
|
||||||
|
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
|
||||||
|
*
|
||||||
|
*
|
||||||
* Typical pin layout used:
|
* Typical pin layout used:
|
||||||
* -----------------------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------------------
|
||||||
* MFRC522 Arduino Arduino Arduino Arduino Arduino
|
* MFRC522 Arduino Arduino Arduino Arduino Arduino
|
||||||
@@ -17,7 +20,7 @@
|
|||||||
* Signal Pin Pin Pin Pin Pin Pin
|
* Signal Pin Pin Pin Pin Pin Pin
|
||||||
* -----------------------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------------------
|
||||||
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
||||||
* SPI SS 1 SDA(SS) 5 53 D10 10 10
|
* SPI SS 1 SDA(SS) 10 53 D10 10 10
|
||||||
* SPI SS 2 SDA(SS) 2 53 D10 10 10
|
* SPI SS 2 SDA(SS) 2 53 D10 10 10
|
||||||
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
||||||
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
||||||
@@ -28,9 +31,9 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <MFRC522.h>
|
#include <MFRC522.h>
|
||||||
|
|
||||||
#define RST_PIN 9 // Configurable, see typical pin layout above
|
#define RST_PIN 10 // Configurable, see typical pin layout above
|
||||||
#define SS_1_PIN 5 // Configurable, see typical pin layout above
|
#define SS_1_PIN 5 // Configurable, see typical pin layout above
|
||||||
#define SS_2_PIN 2 // Configurable, see typical pin layout above
|
#define SS_2_PIN 3 // Configurable, see typical pin layout above
|
||||||
|
|
||||||
#define NR_OF_READERS 2
|
#define NR_OF_READERS 2
|
||||||
|
|
||||||
@@ -38,12 +41,11 @@ byte ssPins[] = {SS_1_PIN, SS_2_PIN};
|
|||||||
|
|
||||||
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
|
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
|
||||||
|
|
||||||
MFRC522::MIFARE_Key key;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize.
|
* Initialize.
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200); // Initialize serial communications with the PC
|
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)
|
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||||
|
|
||||||
@@ -61,10 +63,12 @@ void loop() {
|
|||||||
|
|
||||||
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
|
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
|
||||||
// Look for new cards
|
// Look for new cards
|
||||||
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
|
|
||||||
|
|
||||||
|
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
|
||||||
|
Serial.print(F("Reader: "));
|
||||||
|
Serial.print(reader);
|
||||||
// Show some details of the PICC (that is: the tag/card)
|
// Show some details of the PICC (that is: the tag/card)
|
||||||
Serial.print(F("Card UID:"));
|
Serial.print(F(" Card UID:"));
|
||||||
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
|
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print(F("PICC type: "));
|
Serial.print(F("PICC type: "));
|
||||||
|
|||||||
Reference in New Issue
Block a user