![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
tfsd
Dependencies: Servo GroveGPS Cayenne-LPP driver_mbed_TH02
MFRC522.cpp@65:724a61ad654c, 2022-03-29 (annotated)
- Committer:
- younousleconquerant
- Date:
- Tue Mar 29 09:43:58 2022 +0000
- Revision:
- 65:724a61ad654c
- Parent:
- 64:4cd8fed3f437
12;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
younousleconquerant | 64:4cd8fed3f437 | 1 | /* |
younousleconquerant | 64:4cd8fed3f437 | 2 | * MFRC522.cpp - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. |
younousleconquerant | 64:4cd8fed3f437 | 3 | * _Please_ see the comments in MFRC522.h - they give useful hints and background. |
younousleconquerant | 64:4cd8fed3f437 | 4 | * Released into the public domain. |
younousleconquerant | 64:4cd8fed3f437 | 5 | */ |
younousleconquerant | 64:4cd8fed3f437 | 6 | |
younousleconquerant | 64:4cd8fed3f437 | 7 | #include "MFRC522.h" |
younousleconquerant | 64:4cd8fed3f437 | 8 | |
younousleconquerant | 64:4cd8fed3f437 | 9 | static const char* const _TypeNamePICC[] = |
younousleconquerant | 64:4cd8fed3f437 | 10 | { |
younousleconquerant | 64:4cd8fed3f437 | 11 | "Unknown type", |
younousleconquerant | 64:4cd8fed3f437 | 12 | "PICC compliant with ISO/IEC 14443-4", |
younousleconquerant | 64:4cd8fed3f437 | 13 | "PICC compliant with ISO/IEC 18092 (NFC)", |
younousleconquerant | 64:4cd8fed3f437 | 14 | "MIFARE Mini, 320 bytes", |
younousleconquerant | 64:4cd8fed3f437 | 15 | "MIFARE 1KB", |
younousleconquerant | 64:4cd8fed3f437 | 16 | "MIFARE 4KB", |
younousleconquerant | 64:4cd8fed3f437 | 17 | "MIFARE Ultralight or Ultralight C", |
younousleconquerant | 64:4cd8fed3f437 | 18 | "MIFARE Plus", |
younousleconquerant | 64:4cd8fed3f437 | 19 | "MIFARE TNP3XXX", |
younousleconquerant | 64:4cd8fed3f437 | 20 | |
younousleconquerant | 64:4cd8fed3f437 | 21 | /* not complete UID */ |
younousleconquerant | 64:4cd8fed3f437 | 22 | "SAK indicates UID is not complete" |
younousleconquerant | 64:4cd8fed3f437 | 23 | }; |
younousleconquerant | 64:4cd8fed3f437 | 24 | |
younousleconquerant | 64:4cd8fed3f437 | 25 | static const char* const _ErrorMessage[] = |
younousleconquerant | 64:4cd8fed3f437 | 26 | { |
younousleconquerant | 64:4cd8fed3f437 | 27 | "Unknown error", |
younousleconquerant | 64:4cd8fed3f437 | 28 | "Success", |
younousleconquerant | 64:4cd8fed3f437 | 29 | "Error in communication", |
younousleconquerant | 64:4cd8fed3f437 | 30 | "Collision detected", |
younousleconquerant | 64:4cd8fed3f437 | 31 | "Timeout in communication", |
younousleconquerant | 64:4cd8fed3f437 | 32 | "A buffer is not big enough", |
younousleconquerant | 64:4cd8fed3f437 | 33 | "Internal error in the code, should not happen", |
younousleconquerant | 64:4cd8fed3f437 | 34 | "Invalid argument", |
younousleconquerant | 64:4cd8fed3f437 | 35 | "The CRC_A does not match", |
younousleconquerant | 64:4cd8fed3f437 | 36 | "A MIFARE PICC responded with NAK" |
younousleconquerant | 64:4cd8fed3f437 | 37 | }; |
younousleconquerant | 64:4cd8fed3f437 | 38 | |
younousleconquerant | 64:4cd8fed3f437 | 39 | #define MFRC522_MaxPICCs (sizeof(_TypeNamePICC)/sizeof(_TypeNamePICC[0])) |
younousleconquerant | 64:4cd8fed3f437 | 40 | #define MFRC522_MaxError (sizeof(_ErrorMessage)/sizeof(_ErrorMessage[0])) |
younousleconquerant | 64:4cd8fed3f437 | 41 | |
younousleconquerant | 64:4cd8fed3f437 | 42 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 43 | // Functions for setting up the driver |
younousleconquerant | 64:4cd8fed3f437 | 44 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 45 | |
younousleconquerant | 64:4cd8fed3f437 | 46 | /** |
younousleconquerant | 64:4cd8fed3f437 | 47 | * Constructor. |
younousleconquerant | 64:4cd8fed3f437 | 48 | * Prepares the output pins. |
younousleconquerant | 64:4cd8fed3f437 | 49 | */ |
younousleconquerant | 64:4cd8fed3f437 | 50 | MFRC522::MFRC522(PinName mosi, |
younousleconquerant | 64:4cd8fed3f437 | 51 | PinName miso, |
younousleconquerant | 64:4cd8fed3f437 | 52 | PinName sclk, |
younousleconquerant | 64:4cd8fed3f437 | 53 | PinName cs, |
younousleconquerant | 64:4cd8fed3f437 | 54 | PinName reset) : m_SPI(mosi, miso, sclk), m_CS(cs), m_RESET(reset) |
younousleconquerant | 64:4cd8fed3f437 | 55 | { |
younousleconquerant | 64:4cd8fed3f437 | 56 | /* Configure SPI bus */ |
younousleconquerant | 64:4cd8fed3f437 | 57 | m_SPI.format(8, 0); |
younousleconquerant | 64:4cd8fed3f437 | 58 | m_SPI.frequency(8000000); |
younousleconquerant | 64:4cd8fed3f437 | 59 | |
younousleconquerant | 64:4cd8fed3f437 | 60 | /* Release SPI-CS pin */ |
younousleconquerant | 64:4cd8fed3f437 | 61 | m_CS = 1; |
younousleconquerant | 64:4cd8fed3f437 | 62 | |
younousleconquerant | 64:4cd8fed3f437 | 63 | /* Release RESET pin */ |
younousleconquerant | 64:4cd8fed3f437 | 64 | m_RESET = 1; |
younousleconquerant | 64:4cd8fed3f437 | 65 | } // End constructor |
younousleconquerant | 64:4cd8fed3f437 | 66 | |
younousleconquerant | 64:4cd8fed3f437 | 67 | |
younousleconquerant | 64:4cd8fed3f437 | 68 | /** |
younousleconquerant | 64:4cd8fed3f437 | 69 | * Destructor. |
younousleconquerant | 64:4cd8fed3f437 | 70 | */ |
younousleconquerant | 64:4cd8fed3f437 | 71 | MFRC522::~MFRC522() |
younousleconquerant | 64:4cd8fed3f437 | 72 | { |
younousleconquerant | 64:4cd8fed3f437 | 73 | |
younousleconquerant | 64:4cd8fed3f437 | 74 | } |
younousleconquerant | 64:4cd8fed3f437 | 75 | |
younousleconquerant | 64:4cd8fed3f437 | 76 | |
younousleconquerant | 64:4cd8fed3f437 | 77 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 78 | // Basic interface functions for communicating with the MFRC522 |
younousleconquerant | 64:4cd8fed3f437 | 79 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 80 | |
younousleconquerant | 64:4cd8fed3f437 | 81 | /** |
younousleconquerant | 64:4cd8fed3f437 | 82 | * Writes a byte to the specified register in the MFRC522 chip. |
younousleconquerant | 64:4cd8fed3f437 | 83 | * The interface is described in the datasheet section 8.1.2. |
younousleconquerant | 64:4cd8fed3f437 | 84 | */ |
younousleconquerant | 64:4cd8fed3f437 | 85 | void MFRC522::PCD_WriteRegister(uint8_t reg, uint8_t value) |
younousleconquerant | 64:4cd8fed3f437 | 86 | { |
younousleconquerant | 64:4cd8fed3f437 | 87 | m_CS = 0; /* Select SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 88 | |
younousleconquerant | 64:4cd8fed3f437 | 89 | // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. |
younousleconquerant | 64:4cd8fed3f437 | 90 | (void) m_SPI.write(reg & 0x7E); |
younousleconquerant | 64:4cd8fed3f437 | 91 | (void) m_SPI.write(value); |
younousleconquerant | 64:4cd8fed3f437 | 92 | |
younousleconquerant | 64:4cd8fed3f437 | 93 | m_CS = 1; /* Release SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 94 | } // End PCD_WriteRegister() |
younousleconquerant | 64:4cd8fed3f437 | 95 | |
younousleconquerant | 64:4cd8fed3f437 | 96 | /** |
younousleconquerant | 64:4cd8fed3f437 | 97 | * Writes a number of bytes to the specified register in the MFRC522 chip. |
younousleconquerant | 64:4cd8fed3f437 | 98 | * The interface is described in the datasheet section 8.1.2. |
younousleconquerant | 64:4cd8fed3f437 | 99 | */ |
younousleconquerant | 64:4cd8fed3f437 | 100 | void MFRC522::PCD_WriteRegister(uint8_t reg, uint8_t count, uint8_t *values) |
younousleconquerant | 64:4cd8fed3f437 | 101 | { |
younousleconquerant | 64:4cd8fed3f437 | 102 | m_CS = 0; /* Select SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 103 | |
younousleconquerant | 64:4cd8fed3f437 | 104 | // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3. |
younousleconquerant | 64:4cd8fed3f437 | 105 | (void) m_SPI.write(reg & 0x7E); |
younousleconquerant | 64:4cd8fed3f437 | 106 | for (uint8_t index = 0; index < count; index++) |
younousleconquerant | 64:4cd8fed3f437 | 107 | { |
younousleconquerant | 64:4cd8fed3f437 | 108 | (void) m_SPI.write(values[index]); |
younousleconquerant | 64:4cd8fed3f437 | 109 | } |
younousleconquerant | 64:4cd8fed3f437 | 110 | |
younousleconquerant | 64:4cd8fed3f437 | 111 | m_CS = 1; /* Release SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 112 | } // End PCD_WriteRegister() |
younousleconquerant | 64:4cd8fed3f437 | 113 | |
younousleconquerant | 64:4cd8fed3f437 | 114 | /** |
younousleconquerant | 64:4cd8fed3f437 | 115 | * Reads a byte from the specified register in the MFRC522 chip. |
younousleconquerant | 64:4cd8fed3f437 | 116 | * The interface is described in the datasheet section 8.1.2. |
younousleconquerant | 64:4cd8fed3f437 | 117 | */ |
younousleconquerant | 64:4cd8fed3f437 | 118 | uint8_t MFRC522::PCD_ReadRegister(uint8_t reg) |
younousleconquerant | 64:4cd8fed3f437 | 119 | { |
younousleconquerant | 64:4cd8fed3f437 | 120 | uint8_t value; |
younousleconquerant | 64:4cd8fed3f437 | 121 | m_CS = 0; /* Select SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 122 | |
younousleconquerant | 64:4cd8fed3f437 | 123 | // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. |
younousleconquerant | 64:4cd8fed3f437 | 124 | (void) m_SPI.write(0x80 | reg); |
younousleconquerant | 64:4cd8fed3f437 | 125 | |
younousleconquerant | 64:4cd8fed3f437 | 126 | // Read the value back. Send 0 to stop reading. |
younousleconquerant | 64:4cd8fed3f437 | 127 | value = m_SPI.write(0); |
younousleconquerant | 64:4cd8fed3f437 | 128 | |
younousleconquerant | 64:4cd8fed3f437 | 129 | m_CS = 1; /* Release SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 130 | |
younousleconquerant | 64:4cd8fed3f437 | 131 | return value; |
younousleconquerant | 64:4cd8fed3f437 | 132 | } // End PCD_ReadRegister() |
younousleconquerant | 64:4cd8fed3f437 | 133 | |
younousleconquerant | 64:4cd8fed3f437 | 134 | /** |
younousleconquerant | 64:4cd8fed3f437 | 135 | * Reads a number of bytes from the specified register in the MFRC522 chip. |
younousleconquerant | 64:4cd8fed3f437 | 136 | * The interface is described in the datasheet section 8.1.2. |
younousleconquerant | 64:4cd8fed3f437 | 137 | */ |
younousleconquerant | 64:4cd8fed3f437 | 138 | void MFRC522::PCD_ReadRegister(uint8_t reg, uint8_t count, uint8_t *values, uint8_t rxAlign) |
younousleconquerant | 64:4cd8fed3f437 | 139 | { |
younousleconquerant | 64:4cd8fed3f437 | 140 | if (count == 0) { return; } |
younousleconquerant | 64:4cd8fed3f437 | 141 | |
younousleconquerant | 64:4cd8fed3f437 | 142 | uint8_t address = 0x80 | reg; // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3. |
younousleconquerant | 64:4cd8fed3f437 | 143 | uint8_t index = 0; // Index in values array. |
younousleconquerant | 64:4cd8fed3f437 | 144 | |
younousleconquerant | 64:4cd8fed3f437 | 145 | m_CS = 0; /* Select SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 146 | count--; // One read is performed outside of the loop |
younousleconquerant | 64:4cd8fed3f437 | 147 | (void) m_SPI.write(address); // Tell MFRC522 which address we want to read |
younousleconquerant | 64:4cd8fed3f437 | 148 | |
younousleconquerant | 64:4cd8fed3f437 | 149 | while (index < count) |
younousleconquerant | 64:4cd8fed3f437 | 150 | { |
younousleconquerant | 64:4cd8fed3f437 | 151 | if ((index == 0) && rxAlign) // Only update bit positions rxAlign..7 in values[0] |
younousleconquerant | 64:4cd8fed3f437 | 152 | { |
younousleconquerant | 64:4cd8fed3f437 | 153 | // Create bit mask for bit positions rxAlign..7 |
younousleconquerant | 64:4cd8fed3f437 | 154 | uint8_t mask = 0; |
younousleconquerant | 64:4cd8fed3f437 | 155 | for (uint8_t i = rxAlign; i <= 7; i++) |
younousleconquerant | 64:4cd8fed3f437 | 156 | { |
younousleconquerant | 64:4cd8fed3f437 | 157 | mask |= (1 << i); |
younousleconquerant | 64:4cd8fed3f437 | 158 | } |
younousleconquerant | 64:4cd8fed3f437 | 159 | |
younousleconquerant | 64:4cd8fed3f437 | 160 | // Read value and tell that we want to read the same address again. |
younousleconquerant | 64:4cd8fed3f437 | 161 | uint8_t value = m_SPI.write(address); |
younousleconquerant | 64:4cd8fed3f437 | 162 | |
younousleconquerant | 64:4cd8fed3f437 | 163 | // Apply mask to both current value of values[0] and the new data in value. |
younousleconquerant | 64:4cd8fed3f437 | 164 | values[0] = (values[index] & ~mask) | (value & mask); |
younousleconquerant | 64:4cd8fed3f437 | 165 | } |
younousleconquerant | 64:4cd8fed3f437 | 166 | else |
younousleconquerant | 64:4cd8fed3f437 | 167 | { |
younousleconquerant | 64:4cd8fed3f437 | 168 | // Read value and tell that we want to read the same address again. |
younousleconquerant | 64:4cd8fed3f437 | 169 | values[index] = m_SPI.write(address); |
younousleconquerant | 64:4cd8fed3f437 | 170 | } |
younousleconquerant | 64:4cd8fed3f437 | 171 | |
younousleconquerant | 64:4cd8fed3f437 | 172 | index++; |
younousleconquerant | 64:4cd8fed3f437 | 173 | } |
younousleconquerant | 64:4cd8fed3f437 | 174 | |
younousleconquerant | 64:4cd8fed3f437 | 175 | values[index] = m_SPI.write(0); // Read the final byte. Send 0 to stop reading. |
younousleconquerant | 64:4cd8fed3f437 | 176 | |
younousleconquerant | 64:4cd8fed3f437 | 177 | m_CS = 1; /* Release SPI Chip MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 178 | } // End PCD_ReadRegister() |
younousleconquerant | 64:4cd8fed3f437 | 179 | |
younousleconquerant | 64:4cd8fed3f437 | 180 | /** |
younousleconquerant | 64:4cd8fed3f437 | 181 | * Sets the bits given in mask in register reg. |
younousleconquerant | 64:4cd8fed3f437 | 182 | */ |
younousleconquerant | 64:4cd8fed3f437 | 183 | void MFRC522::PCD_SetRegisterBits(uint8_t reg, uint8_t mask) |
younousleconquerant | 64:4cd8fed3f437 | 184 | { |
younousleconquerant | 64:4cd8fed3f437 | 185 | uint8_t tmp = PCD_ReadRegister(reg); |
younousleconquerant | 64:4cd8fed3f437 | 186 | PCD_WriteRegister(reg, tmp | mask); // set bit mask |
younousleconquerant | 64:4cd8fed3f437 | 187 | } // End PCD_SetRegisterBitMask() |
younousleconquerant | 64:4cd8fed3f437 | 188 | |
younousleconquerant | 64:4cd8fed3f437 | 189 | /** |
younousleconquerant | 64:4cd8fed3f437 | 190 | * Clears the bits given in mask from register reg. |
younousleconquerant | 64:4cd8fed3f437 | 191 | */ |
younousleconquerant | 64:4cd8fed3f437 | 192 | void MFRC522::PCD_ClrRegisterBits(uint8_t reg, uint8_t mask) |
younousleconquerant | 64:4cd8fed3f437 | 193 | { |
younousleconquerant | 64:4cd8fed3f437 | 194 | uint8_t tmp = PCD_ReadRegister(reg); |
younousleconquerant | 64:4cd8fed3f437 | 195 | PCD_WriteRegister(reg, tmp & (~mask)); // clear bit mask |
younousleconquerant | 64:4cd8fed3f437 | 196 | } // End PCD_ClearRegisterBitMask() |
younousleconquerant | 64:4cd8fed3f437 | 197 | |
younousleconquerant | 64:4cd8fed3f437 | 198 | |
younousleconquerant | 64:4cd8fed3f437 | 199 | /** |
younousleconquerant | 64:4cd8fed3f437 | 200 | * Use the CRC coprocessor in the MFRC522 to calculate a CRC_A. |
younousleconquerant | 64:4cd8fed3f437 | 201 | */ |
younousleconquerant | 64:4cd8fed3f437 | 202 | uint8_t MFRC522::PCD_CalculateCRC(uint8_t *data, uint8_t length, uint8_t *result) |
younousleconquerant | 64:4cd8fed3f437 | 203 | { |
younousleconquerant | 64:4cd8fed3f437 | 204 | PCD_WriteRegister(CommandReg, PCD_Idle); // Stop any active command. |
younousleconquerant | 64:4cd8fed3f437 | 205 | PCD_WriteRegister(DivIrqReg, 0x04); // Clear the CRCIRq interrupt request bit |
younousleconquerant | 64:4cd8fed3f437 | 206 | PCD_SetRegisterBits(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization |
younousleconquerant | 64:4cd8fed3f437 | 207 | PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO |
younousleconquerant | 64:4cd8fed3f437 | 208 | PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation |
younousleconquerant | 64:4cd8fed3f437 | 209 | |
younousleconquerant | 64:4cd8fed3f437 | 210 | // Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73us. |
younousleconquerant | 64:4cd8fed3f437 | 211 | uint16_t i = 5000; |
younousleconquerant | 64:4cd8fed3f437 | 212 | uint8_t n; |
younousleconquerant | 64:4cd8fed3f437 | 213 | while (1) |
younousleconquerant | 64:4cd8fed3f437 | 214 | { |
younousleconquerant | 64:4cd8fed3f437 | 215 | n = PCD_ReadRegister(DivIrqReg); // DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved |
younousleconquerant | 64:4cd8fed3f437 | 216 | if (n & 0x04) |
younousleconquerant | 64:4cd8fed3f437 | 217 | { |
younousleconquerant | 64:4cd8fed3f437 | 218 | // CRCIRq bit set - calculation done |
younousleconquerant | 64:4cd8fed3f437 | 219 | break; |
younousleconquerant | 64:4cd8fed3f437 | 220 | } |
younousleconquerant | 64:4cd8fed3f437 | 221 | |
younousleconquerant | 64:4cd8fed3f437 | 222 | if (--i == 0) |
younousleconquerant | 64:4cd8fed3f437 | 223 | { |
younousleconquerant | 64:4cd8fed3f437 | 224 | // The emergency break. We will eventually terminate on this one after 89ms. |
younousleconquerant | 64:4cd8fed3f437 | 225 | // Communication with the MFRC522 might be down. |
younousleconquerant | 64:4cd8fed3f437 | 226 | return STATUS_TIMEOUT; |
younousleconquerant | 64:4cd8fed3f437 | 227 | } |
younousleconquerant | 64:4cd8fed3f437 | 228 | } |
younousleconquerant | 64:4cd8fed3f437 | 229 | |
younousleconquerant | 64:4cd8fed3f437 | 230 | // Stop calculating CRC for new content in the FIFO. |
younousleconquerant | 64:4cd8fed3f437 | 231 | PCD_WriteRegister(CommandReg, PCD_Idle); |
younousleconquerant | 64:4cd8fed3f437 | 232 | |
younousleconquerant | 64:4cd8fed3f437 | 233 | // Transfer the result from the registers to the result buffer |
younousleconquerant | 64:4cd8fed3f437 | 234 | result[0] = PCD_ReadRegister(CRCResultRegL); |
younousleconquerant | 64:4cd8fed3f437 | 235 | result[1] = PCD_ReadRegister(CRCResultRegH); |
younousleconquerant | 64:4cd8fed3f437 | 236 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 237 | } // End PCD_CalculateCRC() |
younousleconquerant | 64:4cd8fed3f437 | 238 | |
younousleconquerant | 64:4cd8fed3f437 | 239 | |
younousleconquerant | 64:4cd8fed3f437 | 240 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 241 | // Functions for manipulating the MFRC522 |
younousleconquerant | 64:4cd8fed3f437 | 242 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 243 | |
younousleconquerant | 64:4cd8fed3f437 | 244 | /** |
younousleconquerant | 64:4cd8fed3f437 | 245 | * Initializes the MFRC522 chip. |
younousleconquerant | 64:4cd8fed3f437 | 246 | */ |
younousleconquerant | 64:4cd8fed3f437 | 247 | void MFRC522::PCD_Init() |
younousleconquerant | 64:4cd8fed3f437 | 248 | { |
younousleconquerant | 64:4cd8fed3f437 | 249 | /* Reset MFRC522 */ |
younousleconquerant | 64:4cd8fed3f437 | 250 | m_RESET = 0; |
younousleconquerant | 64:4cd8fed3f437 | 251 | wait_ms(10); |
younousleconquerant | 64:4cd8fed3f437 | 252 | m_RESET = 1; |
younousleconquerant | 64:4cd8fed3f437 | 253 | |
younousleconquerant | 64:4cd8fed3f437 | 254 | // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74us. Let us be generous: 50ms. |
younousleconquerant | 64:4cd8fed3f437 | 255 | wait_ms(50); |
younousleconquerant | 64:4cd8fed3f437 | 256 | |
younousleconquerant | 64:4cd8fed3f437 | 257 | // When communicating with a PICC we need a timeout if something goes wrong. |
younousleconquerant | 64:4cd8fed3f437 | 258 | // f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo]. |
younousleconquerant | 64:4cd8fed3f437 | 259 | // TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg. |
younousleconquerant | 64:4cd8fed3f437 | 260 | PCD_WriteRegister(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds |
younousleconquerant | 64:4cd8fed3f437 | 261 | PCD_WriteRegister(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25us. |
younousleconquerant | 64:4cd8fed3f437 | 262 | PCD_WriteRegister(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout. |
younousleconquerant | 64:4cd8fed3f437 | 263 | PCD_WriteRegister(TReloadRegL, 0xE8); |
younousleconquerant | 64:4cd8fed3f437 | 264 | |
younousleconquerant | 64:4cd8fed3f437 | 265 | PCD_WriteRegister(TxASKReg, 0x40); // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting |
younousleconquerant | 64:4cd8fed3f437 | 266 | 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) |
younousleconquerant | 64:4cd8fed3f437 | 267 | |
younousleconquerant | 64:4cd8fed3f437 | 268 | PCD_WriteRegister(RFCfgReg, (0x07<<4)); // Set Rx Gain to max |
younousleconquerant | 64:4cd8fed3f437 | 269 | |
younousleconquerant | 64:4cd8fed3f437 | 270 | PCD_AntennaOn(); // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset) |
younousleconquerant | 64:4cd8fed3f437 | 271 | } // End PCD_Init() |
younousleconquerant | 64:4cd8fed3f437 | 272 | |
younousleconquerant | 64:4cd8fed3f437 | 273 | /** |
younousleconquerant | 64:4cd8fed3f437 | 274 | * Performs a soft reset on the MFRC522 chip and waits for it to be ready again. |
younousleconquerant | 64:4cd8fed3f437 | 275 | */ |
younousleconquerant | 64:4cd8fed3f437 | 276 | void MFRC522::PCD_Reset() |
younousleconquerant | 64:4cd8fed3f437 | 277 | { |
younousleconquerant | 64:4cd8fed3f437 | 278 | PCD_WriteRegister(CommandReg, PCD_SoftReset); // Issue the SoftReset command. |
younousleconquerant | 64:4cd8fed3f437 | 279 | // The datasheet does not mention how long the SoftRest command takes to complete. |
younousleconquerant | 64:4cd8fed3f437 | 280 | // But the MFRC522 might have been in soft power-down mode (triggered by bit 4 of CommandReg) |
younousleconquerant | 64:4cd8fed3f437 | 281 | // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74us. Let us be generous: 50ms. |
younousleconquerant | 64:4cd8fed3f437 | 282 | wait_ms(50); |
younousleconquerant | 64:4cd8fed3f437 | 283 | |
younousleconquerant | 64:4cd8fed3f437 | 284 | // Wait for the PowerDown bit in CommandReg to be cleared |
younousleconquerant | 64:4cd8fed3f437 | 285 | while (PCD_ReadRegister(CommandReg) & (1<<4)) |
younousleconquerant | 64:4cd8fed3f437 | 286 | { |
younousleconquerant | 64:4cd8fed3f437 | 287 | // PCD still restarting - unlikely after waiting 50ms, but better safe than sorry. |
younousleconquerant | 64:4cd8fed3f437 | 288 | } |
younousleconquerant | 64:4cd8fed3f437 | 289 | } // End PCD_Reset() |
younousleconquerant | 64:4cd8fed3f437 | 290 | |
younousleconquerant | 64:4cd8fed3f437 | 291 | /** |
younousleconquerant | 64:4cd8fed3f437 | 292 | * Turns the antenna on by enabling pins TX1 and TX2. |
younousleconquerant | 64:4cd8fed3f437 | 293 | * After a reset these pins disabled. |
younousleconquerant | 64:4cd8fed3f437 | 294 | */ |
younousleconquerant | 64:4cd8fed3f437 | 295 | void MFRC522::PCD_AntennaOn() |
younousleconquerant | 64:4cd8fed3f437 | 296 | { |
younousleconquerant | 64:4cd8fed3f437 | 297 | uint8_t value = PCD_ReadRegister(TxControlReg); |
younousleconquerant | 64:4cd8fed3f437 | 298 | if ((value & 0x03) != 0x03) |
younousleconquerant | 64:4cd8fed3f437 | 299 | { |
younousleconquerant | 64:4cd8fed3f437 | 300 | PCD_WriteRegister(TxControlReg, value | 0x03); |
younousleconquerant | 64:4cd8fed3f437 | 301 | } |
younousleconquerant | 64:4cd8fed3f437 | 302 | } // End PCD_AntennaOn() |
younousleconquerant | 64:4cd8fed3f437 | 303 | |
younousleconquerant | 64:4cd8fed3f437 | 304 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 305 | // Functions for communicating with PICCs |
younousleconquerant | 64:4cd8fed3f437 | 306 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 307 | |
younousleconquerant | 64:4cd8fed3f437 | 308 | /** |
younousleconquerant | 64:4cd8fed3f437 | 309 | * Executes the Transceive command. |
younousleconquerant | 64:4cd8fed3f437 | 310 | * CRC validation can only be done if backData and backLen are specified. |
younousleconquerant | 64:4cd8fed3f437 | 311 | */ |
younousleconquerant | 64:4cd8fed3f437 | 312 | uint8_t MFRC522::PCD_TransceiveData(uint8_t *sendData, |
younousleconquerant | 64:4cd8fed3f437 | 313 | uint8_t sendLen, |
younousleconquerant | 64:4cd8fed3f437 | 314 | uint8_t *backData, |
younousleconquerant | 64:4cd8fed3f437 | 315 | uint8_t *backLen, |
younousleconquerant | 64:4cd8fed3f437 | 316 | uint8_t *validBits, |
younousleconquerant | 64:4cd8fed3f437 | 317 | uint8_t rxAlign, |
younousleconquerant | 64:4cd8fed3f437 | 318 | bool checkCRC) |
younousleconquerant | 64:4cd8fed3f437 | 319 | { |
younousleconquerant | 64:4cd8fed3f437 | 320 | uint8_t waitIRq = 0x30; // RxIRq and IdleIRq |
younousleconquerant | 64:4cd8fed3f437 | 321 | return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC); |
younousleconquerant | 64:4cd8fed3f437 | 322 | } // End PCD_TransceiveData() |
younousleconquerant | 64:4cd8fed3f437 | 323 | |
younousleconquerant | 64:4cd8fed3f437 | 324 | /** |
younousleconquerant | 64:4cd8fed3f437 | 325 | * Transfers data to the MFRC522 FIFO, executes a commend, waits for completion and transfers data back from the FIFO. |
younousleconquerant | 64:4cd8fed3f437 | 326 | * CRC validation can only be done if backData and backLen are specified. |
younousleconquerant | 64:4cd8fed3f437 | 327 | */ |
younousleconquerant | 64:4cd8fed3f437 | 328 | uint8_t MFRC522::PCD_CommunicateWithPICC(uint8_t command, |
younousleconquerant | 64:4cd8fed3f437 | 329 | uint8_t waitIRq, |
younousleconquerant | 64:4cd8fed3f437 | 330 | uint8_t *sendData, |
younousleconquerant | 64:4cd8fed3f437 | 331 | uint8_t sendLen, |
younousleconquerant | 64:4cd8fed3f437 | 332 | uint8_t *backData, |
younousleconquerant | 64:4cd8fed3f437 | 333 | uint8_t *backLen, |
younousleconquerant | 64:4cd8fed3f437 | 334 | uint8_t *validBits, |
younousleconquerant | 64:4cd8fed3f437 | 335 | uint8_t rxAlign, |
younousleconquerant | 64:4cd8fed3f437 | 336 | bool checkCRC) |
younousleconquerant | 64:4cd8fed3f437 | 337 | { |
younousleconquerant | 64:4cd8fed3f437 | 338 | uint8_t n, _validBits = 0; |
younousleconquerant | 64:4cd8fed3f437 | 339 | uint32_t i; |
younousleconquerant | 64:4cd8fed3f437 | 340 | |
younousleconquerant | 64:4cd8fed3f437 | 341 | // Prepare values for BitFramingReg |
younousleconquerant | 64:4cd8fed3f437 | 342 | uint8_t txLastBits = validBits ? *validBits : 0; |
younousleconquerant | 64:4cd8fed3f437 | 343 | uint8_t bitFraming = (rxAlign << 4) + txLastBits; // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] |
younousleconquerant | 64:4cd8fed3f437 | 344 | |
younousleconquerant | 64:4cd8fed3f437 | 345 | PCD_WriteRegister(CommandReg, PCD_Idle); // Stop any active command. |
younousleconquerant | 64:4cd8fed3f437 | 346 | PCD_WriteRegister(ComIrqReg, 0x7F); // Clear all seven interrupt request bits |
younousleconquerant | 64:4cd8fed3f437 | 347 | PCD_SetRegisterBits(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization |
younousleconquerant | 64:4cd8fed3f437 | 348 | PCD_WriteRegister(FIFODataReg, sendLen, sendData); // Write sendData to the FIFO |
younousleconquerant | 64:4cd8fed3f437 | 349 | PCD_WriteRegister(BitFramingReg, bitFraming); // Bit adjustments |
younousleconquerant | 64:4cd8fed3f437 | 350 | PCD_WriteRegister(CommandReg, command); // Execute the command |
younousleconquerant | 64:4cd8fed3f437 | 351 | if (command == PCD_Transceive) |
younousleconquerant | 64:4cd8fed3f437 | 352 | { |
younousleconquerant | 64:4cd8fed3f437 | 353 | PCD_SetRegisterBits(BitFramingReg, 0x80); // StartSend=1, transmission of data starts |
younousleconquerant | 64:4cd8fed3f437 | 354 | } |
younousleconquerant | 64:4cd8fed3f437 | 355 | |
younousleconquerant | 64:4cd8fed3f437 | 356 | // Wait for the command to complete. |
younousleconquerant | 64:4cd8fed3f437 | 357 | // In PCD_Init() we set the TAuto flag in TModeReg. This means the timer automatically starts when the PCD stops transmitting. |
younousleconquerant | 64:4cd8fed3f437 | 358 | // Each iteration of the do-while-loop takes 17.86us. |
younousleconquerant | 64:4cd8fed3f437 | 359 | i = 2000; |
younousleconquerant | 64:4cd8fed3f437 | 360 | while (1) |
younousleconquerant | 64:4cd8fed3f437 | 361 | { |
younousleconquerant | 64:4cd8fed3f437 | 362 | n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq |
younousleconquerant | 64:4cd8fed3f437 | 363 | if (n & waitIRq) |
younousleconquerant | 64:4cd8fed3f437 | 364 | { // One of the interrupts that signal success has been set. |
younousleconquerant | 64:4cd8fed3f437 | 365 | break; |
younousleconquerant | 64:4cd8fed3f437 | 366 | } |
younousleconquerant | 64:4cd8fed3f437 | 367 | |
younousleconquerant | 64:4cd8fed3f437 | 368 | if (n & 0x01) |
younousleconquerant | 64:4cd8fed3f437 | 369 | { // Timer interrupt - nothing received in 25ms |
younousleconquerant | 64:4cd8fed3f437 | 370 | return STATUS_TIMEOUT; |
younousleconquerant | 64:4cd8fed3f437 | 371 | } |
younousleconquerant | 64:4cd8fed3f437 | 372 | |
younousleconquerant | 64:4cd8fed3f437 | 373 | if (--i == 0) |
younousleconquerant | 64:4cd8fed3f437 | 374 | { // The emergency break. If all other condions fail we will eventually terminate on this one after 35.7ms. Communication with the MFRC522 might be down. |
younousleconquerant | 64:4cd8fed3f437 | 375 | return STATUS_TIMEOUT; |
younousleconquerant | 64:4cd8fed3f437 | 376 | } |
younousleconquerant | 64:4cd8fed3f437 | 377 | } |
younousleconquerant | 64:4cd8fed3f437 | 378 | |
younousleconquerant | 64:4cd8fed3f437 | 379 | // Stop now if any errors except collisions were detected. |
younousleconquerant | 64:4cd8fed3f437 | 380 | uint8_t errorRegValue = PCD_ReadRegister(ErrorReg); // ErrorReg[7..0] bits are: WrErr TempErr reserved BufferOvfl CollErr CRCErr ParityErr ProtocolErr |
younousleconquerant | 64:4cd8fed3f437 | 381 | if (errorRegValue & 0x13) |
younousleconquerant | 64:4cd8fed3f437 | 382 | { // BufferOvfl ParityErr ProtocolErr |
younousleconquerant | 64:4cd8fed3f437 | 383 | return STATUS_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 384 | } |
younousleconquerant | 64:4cd8fed3f437 | 385 | |
younousleconquerant | 64:4cd8fed3f437 | 386 | // If the caller wants data back, get it from the MFRC522. |
younousleconquerant | 64:4cd8fed3f437 | 387 | if (backData && backLen) |
younousleconquerant | 64:4cd8fed3f437 | 388 | { |
younousleconquerant | 64:4cd8fed3f437 | 389 | n = PCD_ReadRegister(FIFOLevelReg); // Number of bytes in the FIFO |
younousleconquerant | 64:4cd8fed3f437 | 390 | if (n > *backLen) |
younousleconquerant | 64:4cd8fed3f437 | 391 | { |
younousleconquerant | 64:4cd8fed3f437 | 392 | return STATUS_NO_ROOM; |
younousleconquerant | 64:4cd8fed3f437 | 393 | } |
younousleconquerant | 64:4cd8fed3f437 | 394 | |
younousleconquerant | 64:4cd8fed3f437 | 395 | *backLen = n; // Number of bytes returned |
younousleconquerant | 64:4cd8fed3f437 | 396 | PCD_ReadRegister(FIFODataReg, n, backData, rxAlign); // Get received data from FIFO |
younousleconquerant | 64:4cd8fed3f437 | 397 | _validBits = PCD_ReadRegister(ControlReg) & 0x07; // RxLastBits[2:0] indicates the number of valid bits in the last received byte. If this value is 000b, the whole byte is valid. |
younousleconquerant | 64:4cd8fed3f437 | 398 | if (validBits) |
younousleconquerant | 64:4cd8fed3f437 | 399 | { |
younousleconquerant | 64:4cd8fed3f437 | 400 | *validBits = _validBits; |
younousleconquerant | 64:4cd8fed3f437 | 401 | } |
younousleconquerant | 64:4cd8fed3f437 | 402 | } |
younousleconquerant | 64:4cd8fed3f437 | 403 | |
younousleconquerant | 64:4cd8fed3f437 | 404 | // Tell about collisions |
younousleconquerant | 64:4cd8fed3f437 | 405 | if (errorRegValue & 0x08) |
younousleconquerant | 64:4cd8fed3f437 | 406 | { // CollErr |
younousleconquerant | 64:4cd8fed3f437 | 407 | return STATUS_COLLISION; |
younousleconquerant | 64:4cd8fed3f437 | 408 | } |
younousleconquerant | 64:4cd8fed3f437 | 409 | |
younousleconquerant | 64:4cd8fed3f437 | 410 | // Perform CRC_A validation if requested. |
younousleconquerant | 64:4cd8fed3f437 | 411 | if (backData && backLen && checkCRC) |
younousleconquerant | 64:4cd8fed3f437 | 412 | { |
younousleconquerant | 64:4cd8fed3f437 | 413 | // In this case a MIFARE Classic NAK is not OK. |
younousleconquerant | 64:4cd8fed3f437 | 414 | if ((*backLen == 1) && (_validBits == 4)) |
younousleconquerant | 64:4cd8fed3f437 | 415 | { |
younousleconquerant | 64:4cd8fed3f437 | 416 | return STATUS_MIFARE_NACK; |
younousleconquerant | 64:4cd8fed3f437 | 417 | } |
younousleconquerant | 64:4cd8fed3f437 | 418 | |
younousleconquerant | 64:4cd8fed3f437 | 419 | // We need at least the CRC_A value and all 8 bits of the last byte must be received. |
younousleconquerant | 64:4cd8fed3f437 | 420 | if ((*backLen < 2) || (_validBits != 0)) |
younousleconquerant | 64:4cd8fed3f437 | 421 | { |
younousleconquerant | 64:4cd8fed3f437 | 422 | return STATUS_CRC_WRONG; |
younousleconquerant | 64:4cd8fed3f437 | 423 | } |
younousleconquerant | 64:4cd8fed3f437 | 424 | |
younousleconquerant | 64:4cd8fed3f437 | 425 | // Verify CRC_A - do our own calculation and store the control in controlBuffer. |
younousleconquerant | 64:4cd8fed3f437 | 426 | uint8_t controlBuffer[2]; |
younousleconquerant | 64:4cd8fed3f437 | 427 | n = PCD_CalculateCRC(&backData[0], *backLen - 2, &controlBuffer[0]); |
younousleconquerant | 64:4cd8fed3f437 | 428 | if (n != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 429 | { |
younousleconquerant | 64:4cd8fed3f437 | 430 | return n; |
younousleconquerant | 64:4cd8fed3f437 | 431 | } |
younousleconquerant | 64:4cd8fed3f437 | 432 | |
younousleconquerant | 64:4cd8fed3f437 | 433 | if ((backData[*backLen - 2] != controlBuffer[0]) || (backData[*backLen - 1] != controlBuffer[1])) |
younousleconquerant | 64:4cd8fed3f437 | 434 | { |
younousleconquerant | 64:4cd8fed3f437 | 435 | return STATUS_CRC_WRONG; |
younousleconquerant | 64:4cd8fed3f437 | 436 | } |
younousleconquerant | 64:4cd8fed3f437 | 437 | } |
younousleconquerant | 64:4cd8fed3f437 | 438 | |
younousleconquerant | 64:4cd8fed3f437 | 439 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 440 | } // End PCD_CommunicateWithPICC() |
younousleconquerant | 64:4cd8fed3f437 | 441 | |
younousleconquerant | 64:4cd8fed3f437 | 442 | /* |
younousleconquerant | 64:4cd8fed3f437 | 443 | * Transmits a REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame. |
younousleconquerant | 64:4cd8fed3f437 | 444 | * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. |
younousleconquerant | 64:4cd8fed3f437 | 445 | */ |
younousleconquerant | 64:4cd8fed3f437 | 446 | uint8_t MFRC522::PICC_RequestA(uint8_t *bufferATQA, uint8_t *bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 447 | { |
younousleconquerant | 64:4cd8fed3f437 | 448 | return PICC_REQA_or_WUPA(PICC_CMD_REQA, bufferATQA, bufferSize); |
younousleconquerant | 64:4cd8fed3f437 | 449 | } // End PICC_RequestA() |
younousleconquerant | 64:4cd8fed3f437 | 450 | |
younousleconquerant | 64:4cd8fed3f437 | 451 | /** |
younousleconquerant | 64:4cd8fed3f437 | 452 | * Transmits a Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. |
younousleconquerant | 64:4cd8fed3f437 | 453 | * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. |
younousleconquerant | 64:4cd8fed3f437 | 454 | */ |
younousleconquerant | 64:4cd8fed3f437 | 455 | uint8_t MFRC522::PICC_WakeupA(uint8_t *bufferATQA, uint8_t *bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 456 | { |
younousleconquerant | 64:4cd8fed3f437 | 457 | return PICC_REQA_or_WUPA(PICC_CMD_WUPA, bufferATQA, bufferSize); |
younousleconquerant | 64:4cd8fed3f437 | 458 | } // End PICC_WakeupA() |
younousleconquerant | 64:4cd8fed3f437 | 459 | |
younousleconquerant | 64:4cd8fed3f437 | 460 | /* |
younousleconquerant | 64:4cd8fed3f437 | 461 | * Transmits REQA or WUPA commands. |
younousleconquerant | 64:4cd8fed3f437 | 462 | * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design. |
younousleconquerant | 64:4cd8fed3f437 | 463 | */ |
younousleconquerant | 64:4cd8fed3f437 | 464 | uint8_t MFRC522::PICC_REQA_or_WUPA(uint8_t command, uint8_t *bufferATQA, uint8_t *bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 465 | { |
younousleconquerant | 64:4cd8fed3f437 | 466 | uint8_t validBits; |
younousleconquerant | 64:4cd8fed3f437 | 467 | uint8_t status; |
younousleconquerant | 64:4cd8fed3f437 | 468 | |
younousleconquerant | 64:4cd8fed3f437 | 469 | if (bufferATQA == NULL || *bufferSize < 2) |
younousleconquerant | 64:4cd8fed3f437 | 470 | { // The ATQA response is 2 bytes long. |
younousleconquerant | 64:4cd8fed3f437 | 471 | return STATUS_NO_ROOM; |
younousleconquerant | 64:4cd8fed3f437 | 472 | } |
younousleconquerant | 64:4cd8fed3f437 | 473 | |
younousleconquerant | 64:4cd8fed3f437 | 474 | // ValuesAfterColl=1 => Bits received after collision are cleared. |
younousleconquerant | 64:4cd8fed3f437 | 475 | PCD_ClrRegisterBits(CollReg, 0x80); |
younousleconquerant | 64:4cd8fed3f437 | 476 | |
younousleconquerant | 64:4cd8fed3f437 | 477 | // For REQA and WUPA we need the short frame format |
younousleconquerant | 64:4cd8fed3f437 | 478 | // - transmit only 7 bits of the last (and only) byte. TxLastBits = BitFramingReg[2..0] |
younousleconquerant | 64:4cd8fed3f437 | 479 | validBits = 7; |
younousleconquerant | 64:4cd8fed3f437 | 480 | |
younousleconquerant | 64:4cd8fed3f437 | 481 | status = PCD_TransceiveData(&command, 1, bufferATQA, bufferSize, &validBits); |
younousleconquerant | 64:4cd8fed3f437 | 482 | if (status != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 483 | { |
younousleconquerant | 64:4cd8fed3f437 | 484 | return status; |
younousleconquerant | 64:4cd8fed3f437 | 485 | } |
younousleconquerant | 64:4cd8fed3f437 | 486 | |
younousleconquerant | 64:4cd8fed3f437 | 487 | if ((*bufferSize != 2) || (validBits != 0)) |
younousleconquerant | 64:4cd8fed3f437 | 488 | { // ATQA must be exactly 16 bits. |
younousleconquerant | 64:4cd8fed3f437 | 489 | return STATUS_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 490 | } |
younousleconquerant | 64:4cd8fed3f437 | 491 | |
younousleconquerant | 64:4cd8fed3f437 | 492 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 493 | } // End PICC_REQA_or_WUPA() |
younousleconquerant | 64:4cd8fed3f437 | 494 | |
younousleconquerant | 64:4cd8fed3f437 | 495 | /* |
younousleconquerant | 64:4cd8fed3f437 | 496 | * Transmits SELECT/ANTICOLLISION commands to select a single PICC. |
younousleconquerant | 64:4cd8fed3f437 | 497 | */ |
younousleconquerant | 64:4cd8fed3f437 | 498 | uint8_t MFRC522::PICC_Select(Uid *uid, uint8_t validBits) |
younousleconquerant | 64:4cd8fed3f437 | 499 | { |
younousleconquerant | 64:4cd8fed3f437 | 500 | bool uidComplete; |
younousleconquerant | 64:4cd8fed3f437 | 501 | bool selectDone; |
younousleconquerant | 64:4cd8fed3f437 | 502 | bool useCascadeTag; |
younousleconquerant | 64:4cd8fed3f437 | 503 | uint8_t cascadeLevel = 1; |
younousleconquerant | 64:4cd8fed3f437 | 504 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 505 | uint8_t count; |
younousleconquerant | 64:4cd8fed3f437 | 506 | uint8_t index; |
younousleconquerant | 64:4cd8fed3f437 | 507 | uint8_t uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level. |
younousleconquerant | 64:4cd8fed3f437 | 508 | uint8_t currentLevelKnownBits; // The number of known UID bits in the current Cascade Level. |
younousleconquerant | 64:4cd8fed3f437 | 509 | uint8_t buffer[9]; // The SELECT/ANTICOLLISION commands uses a 7 byte standard frame + 2 bytes CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 510 | uint8_t bufferUsed; // The number of bytes used in the buffer, ie the number of bytes to transfer to the FIFO. |
younousleconquerant | 64:4cd8fed3f437 | 511 | uint8_t rxAlign; // Used in BitFramingReg. Defines the bit position for the first bit received. |
younousleconquerant | 64:4cd8fed3f437 | 512 | uint8_t txLastBits; // Used in BitFramingReg. The number of valid bits in the last transmitted byte. |
younousleconquerant | 64:4cd8fed3f437 | 513 | uint8_t *responseBuffer; |
younousleconquerant | 64:4cd8fed3f437 | 514 | uint8_t responseLength; |
younousleconquerant | 64:4cd8fed3f437 | 515 | |
younousleconquerant | 64:4cd8fed3f437 | 516 | // Description of buffer structure: |
younousleconquerant | 64:4cd8fed3f437 | 517 | // Byte 0: SEL Indicates the Cascade Level: PICC_CMD_SEL_CL1, PICC_CMD_SEL_CL2 or PICC_CMD_SEL_CL3 |
younousleconquerant | 64:4cd8fed3f437 | 518 | // Byte 1: NVB Number of Valid Bits (in complete command, not just the UID): High nibble: complete bytes, Low nibble: Extra bits. |
younousleconquerant | 64:4cd8fed3f437 | 519 | // Byte 2: UID-data or CT See explanation below. CT means Cascade Tag. |
younousleconquerant | 64:4cd8fed3f437 | 520 | // Byte 3: UID-data |
younousleconquerant | 64:4cd8fed3f437 | 521 | // Byte 4: UID-data |
younousleconquerant | 64:4cd8fed3f437 | 522 | // Byte 5: UID-data |
younousleconquerant | 64:4cd8fed3f437 | 523 | // Byte 6: BCC Block Check Character - XOR of bytes 2-5 |
younousleconquerant | 64:4cd8fed3f437 | 524 | // Byte 7: CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 525 | // Byte 8: CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 526 | // The BCC and CRC_A is only transmitted if we know all the UID bits of the current Cascade Level. |
younousleconquerant | 64:4cd8fed3f437 | 527 | // |
younousleconquerant | 64:4cd8fed3f437 | 528 | // Description of bytes 2-5: (Section 6.5.4 of the ISO/IEC 14443-3 draft: UID contents and cascade levels) |
younousleconquerant | 64:4cd8fed3f437 | 529 | // UID size Cascade level Byte2 Byte3 Byte4 Byte5 |
younousleconquerant | 64:4cd8fed3f437 | 530 | // ======== ============= ===== ===== ===== ===== |
younousleconquerant | 64:4cd8fed3f437 | 531 | // 4 bytes 1 uid0 uid1 uid2 uid3 |
younousleconquerant | 64:4cd8fed3f437 | 532 | // 7 bytes 1 CT uid0 uid1 uid2 |
younousleconquerant | 64:4cd8fed3f437 | 533 | // 2 uid3 uid4 uid5 uid6 |
younousleconquerant | 64:4cd8fed3f437 | 534 | // 10 bytes 1 CT uid0 uid1 uid2 |
younousleconquerant | 64:4cd8fed3f437 | 535 | // 2 CT uid3 uid4 uid5 |
younousleconquerant | 64:4cd8fed3f437 | 536 | // 3 uid6 uid7 uid8 uid9 |
younousleconquerant | 64:4cd8fed3f437 | 537 | |
younousleconquerant | 64:4cd8fed3f437 | 538 | // Sanity checks |
younousleconquerant | 64:4cd8fed3f437 | 539 | if (validBits > 80) |
younousleconquerant | 64:4cd8fed3f437 | 540 | { |
younousleconquerant | 64:4cd8fed3f437 | 541 | return STATUS_INVALID; |
younousleconquerant | 64:4cd8fed3f437 | 542 | } |
younousleconquerant | 64:4cd8fed3f437 | 543 | |
younousleconquerant | 64:4cd8fed3f437 | 544 | // Prepare MFRC522 |
younousleconquerant | 64:4cd8fed3f437 | 545 | // ValuesAfterColl=1 => Bits received after collision are cleared. |
younousleconquerant | 64:4cd8fed3f437 | 546 | PCD_ClrRegisterBits(CollReg, 0x80); |
younousleconquerant | 64:4cd8fed3f437 | 547 | |
younousleconquerant | 64:4cd8fed3f437 | 548 | // Repeat Cascade Level loop until we have a complete UID. |
younousleconquerant | 64:4cd8fed3f437 | 549 | uidComplete = false; |
younousleconquerant | 64:4cd8fed3f437 | 550 | while ( ! uidComplete) |
younousleconquerant | 64:4cd8fed3f437 | 551 | { |
younousleconquerant | 64:4cd8fed3f437 | 552 | // Set the Cascade Level in the SEL byte, find out if we need to use the Cascade Tag in byte 2. |
younousleconquerant | 64:4cd8fed3f437 | 553 | switch (cascadeLevel) |
younousleconquerant | 64:4cd8fed3f437 | 554 | { |
younousleconquerant | 64:4cd8fed3f437 | 555 | case 1: |
younousleconquerant | 64:4cd8fed3f437 | 556 | buffer[0] = PICC_CMD_SEL_CL1; |
younousleconquerant | 64:4cd8fed3f437 | 557 | uidIndex = 0; |
younousleconquerant | 64:4cd8fed3f437 | 558 | useCascadeTag = validBits && (uid->size > 4); // When we know that the UID has more than 4 bytes |
younousleconquerant | 64:4cd8fed3f437 | 559 | break; |
younousleconquerant | 64:4cd8fed3f437 | 560 | |
younousleconquerant | 64:4cd8fed3f437 | 561 | case 2: |
younousleconquerant | 64:4cd8fed3f437 | 562 | buffer[0] = PICC_CMD_SEL_CL2; |
younousleconquerant | 64:4cd8fed3f437 | 563 | uidIndex = 3; |
younousleconquerant | 64:4cd8fed3f437 | 564 | useCascadeTag = validBits && (uid->size > 7); // When we know that the UID has more than 7 bytes |
younousleconquerant | 64:4cd8fed3f437 | 565 | break; |
younousleconquerant | 64:4cd8fed3f437 | 566 | |
younousleconquerant | 64:4cd8fed3f437 | 567 | case 3: |
younousleconquerant | 64:4cd8fed3f437 | 568 | buffer[0] = PICC_CMD_SEL_CL3; |
younousleconquerant | 64:4cd8fed3f437 | 569 | uidIndex = 6; |
younousleconquerant | 64:4cd8fed3f437 | 570 | useCascadeTag = false; // Never used in CL3. |
younousleconquerant | 64:4cd8fed3f437 | 571 | break; |
younousleconquerant | 64:4cd8fed3f437 | 572 | |
younousleconquerant | 64:4cd8fed3f437 | 573 | default: |
younousleconquerant | 64:4cd8fed3f437 | 574 | return STATUS_INTERNAL_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 575 | //break; |
younousleconquerant | 64:4cd8fed3f437 | 576 | } |
younousleconquerant | 64:4cd8fed3f437 | 577 | |
younousleconquerant | 64:4cd8fed3f437 | 578 | // How many UID bits are known in this Cascade Level? |
younousleconquerant | 64:4cd8fed3f437 | 579 | if(validBits > (8 * uidIndex)) |
younousleconquerant | 64:4cd8fed3f437 | 580 | { |
younousleconquerant | 64:4cd8fed3f437 | 581 | currentLevelKnownBits = validBits - (8 * uidIndex); |
younousleconquerant | 64:4cd8fed3f437 | 582 | } |
younousleconquerant | 64:4cd8fed3f437 | 583 | else |
younousleconquerant | 64:4cd8fed3f437 | 584 | { |
younousleconquerant | 64:4cd8fed3f437 | 585 | currentLevelKnownBits = 0; |
younousleconquerant | 64:4cd8fed3f437 | 586 | } |
younousleconquerant | 64:4cd8fed3f437 | 587 | |
younousleconquerant | 64:4cd8fed3f437 | 588 | // Copy the known bits from uid->uidByte[] to buffer[] |
younousleconquerant | 64:4cd8fed3f437 | 589 | index = 2; // destination index in buffer[] |
younousleconquerant | 64:4cd8fed3f437 | 590 | if (useCascadeTag) |
younousleconquerant | 64:4cd8fed3f437 | 591 | { |
younousleconquerant | 64:4cd8fed3f437 | 592 | buffer[index++] = PICC_CMD_CT; |
younousleconquerant | 64:4cd8fed3f437 | 593 | } |
younousleconquerant | 64:4cd8fed3f437 | 594 | |
younousleconquerant | 64:4cd8fed3f437 | 595 | uint8_t bytesToCopy = currentLevelKnownBits / 8 + (currentLevelKnownBits % 8 ? 1 : 0); // The number of bytes needed to represent the known bits for this level. |
younousleconquerant | 64:4cd8fed3f437 | 596 | if (bytesToCopy) |
younousleconquerant | 64:4cd8fed3f437 | 597 | { |
younousleconquerant | 64:4cd8fed3f437 | 598 | // Max 4 bytes in each Cascade Level. Only 3 left if we use the Cascade Tag |
younousleconquerant | 64:4cd8fed3f437 | 599 | uint8_t maxBytes = useCascadeTag ? 3 : 4; |
younousleconquerant | 64:4cd8fed3f437 | 600 | if (bytesToCopy > maxBytes) |
younousleconquerant | 64:4cd8fed3f437 | 601 | { |
younousleconquerant | 64:4cd8fed3f437 | 602 | bytesToCopy = maxBytes; |
younousleconquerant | 64:4cd8fed3f437 | 603 | } |
younousleconquerant | 64:4cd8fed3f437 | 604 | |
younousleconquerant | 64:4cd8fed3f437 | 605 | for (count = 0; count < bytesToCopy; count++) |
younousleconquerant | 64:4cd8fed3f437 | 606 | { |
younousleconquerant | 64:4cd8fed3f437 | 607 | buffer[index++] = uid->uidByte[uidIndex + count]; |
younousleconquerant | 64:4cd8fed3f437 | 608 | } |
younousleconquerant | 64:4cd8fed3f437 | 609 | } |
younousleconquerant | 64:4cd8fed3f437 | 610 | |
younousleconquerant | 64:4cd8fed3f437 | 611 | // Now that the data has been copied we need to include the 8 bits in CT in currentLevelKnownBits |
younousleconquerant | 64:4cd8fed3f437 | 612 | if (useCascadeTag) |
younousleconquerant | 64:4cd8fed3f437 | 613 | { |
younousleconquerant | 64:4cd8fed3f437 | 614 | currentLevelKnownBits += 8; |
younousleconquerant | 64:4cd8fed3f437 | 615 | } |
younousleconquerant | 64:4cd8fed3f437 | 616 | |
younousleconquerant | 64:4cd8fed3f437 | 617 | // Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations. |
younousleconquerant | 64:4cd8fed3f437 | 618 | selectDone = false; |
younousleconquerant | 64:4cd8fed3f437 | 619 | while ( ! selectDone) |
younousleconquerant | 64:4cd8fed3f437 | 620 | { |
younousleconquerant | 64:4cd8fed3f437 | 621 | // Find out how many bits and bytes to send and receive. |
younousleconquerant | 64:4cd8fed3f437 | 622 | if (currentLevelKnownBits >= 32) |
younousleconquerant | 64:4cd8fed3f437 | 623 | { // All UID bits in this Cascade Level are known. This is a SELECT. |
younousleconquerant | 64:4cd8fed3f437 | 624 | //Serial.print("SELECT: currentLevelKnownBits="); Serial.println(currentLevelKnownBits, DEC); |
younousleconquerant | 64:4cd8fed3f437 | 625 | buffer[1] = 0x70; // NVB - Number of Valid Bits: Seven whole bytes |
younousleconquerant | 64:4cd8fed3f437 | 626 | |
younousleconquerant | 64:4cd8fed3f437 | 627 | // Calulate BCC - Block Check Character |
younousleconquerant | 64:4cd8fed3f437 | 628 | buffer[6] = buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]; |
younousleconquerant | 64:4cd8fed3f437 | 629 | |
younousleconquerant | 64:4cd8fed3f437 | 630 | // Calculate CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 631 | result = PCD_CalculateCRC(buffer, 7, &buffer[7]); |
younousleconquerant | 64:4cd8fed3f437 | 632 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 633 | { |
younousleconquerant | 64:4cd8fed3f437 | 634 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 635 | } |
younousleconquerant | 64:4cd8fed3f437 | 636 | |
younousleconquerant | 64:4cd8fed3f437 | 637 | txLastBits = 0; // 0 => All 8 bits are valid. |
younousleconquerant | 64:4cd8fed3f437 | 638 | bufferUsed = 9; |
younousleconquerant | 64:4cd8fed3f437 | 639 | |
younousleconquerant | 64:4cd8fed3f437 | 640 | // Store response in the last 3 bytes of buffer (BCC and CRC_A - not needed after tx) |
younousleconquerant | 64:4cd8fed3f437 | 641 | responseBuffer = &buffer[6]; |
younousleconquerant | 64:4cd8fed3f437 | 642 | responseLength = 3; |
younousleconquerant | 64:4cd8fed3f437 | 643 | } |
younousleconquerant | 64:4cd8fed3f437 | 644 | else |
younousleconquerant | 64:4cd8fed3f437 | 645 | { // This is an ANTICOLLISION. |
younousleconquerant | 64:4cd8fed3f437 | 646 | //Serial.print("ANTICOLLISION: currentLevelKnownBits="); Serial.println(currentLevelKnownBits, DEC); |
younousleconquerant | 64:4cd8fed3f437 | 647 | txLastBits = currentLevelKnownBits % 8; |
younousleconquerant | 64:4cd8fed3f437 | 648 | count = currentLevelKnownBits / 8; // Number of whole bytes in the UID part. |
younousleconquerant | 64:4cd8fed3f437 | 649 | index = 2 + count; // Number of whole bytes: SEL + NVB + UIDs |
younousleconquerant | 64:4cd8fed3f437 | 650 | buffer[1] = (index << 4) + txLastBits; // NVB - Number of Valid Bits |
younousleconquerant | 64:4cd8fed3f437 | 651 | bufferUsed = index + (txLastBits ? 1 : 0); |
younousleconquerant | 64:4cd8fed3f437 | 652 | |
younousleconquerant | 64:4cd8fed3f437 | 653 | // Store response in the unused part of buffer |
younousleconquerant | 64:4cd8fed3f437 | 654 | responseBuffer = &buffer[index]; |
younousleconquerant | 64:4cd8fed3f437 | 655 | responseLength = sizeof(buffer) - index; |
younousleconquerant | 64:4cd8fed3f437 | 656 | } |
younousleconquerant | 64:4cd8fed3f437 | 657 | |
younousleconquerant | 64:4cd8fed3f437 | 658 | // Set bit adjustments |
younousleconquerant | 64:4cd8fed3f437 | 659 | rxAlign = txLastBits; // Having a seperate variable is overkill. But it makes the next line easier to read. |
younousleconquerant | 64:4cd8fed3f437 | 660 | PCD_WriteRegister(BitFramingReg, (rxAlign << 4) + txLastBits); // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] |
younousleconquerant | 64:4cd8fed3f437 | 661 | |
younousleconquerant | 64:4cd8fed3f437 | 662 | // Transmit the buffer and receive the response. |
younousleconquerant | 64:4cd8fed3f437 | 663 | result = PCD_TransceiveData(buffer, bufferUsed, responseBuffer, &responseLength, &txLastBits, rxAlign); |
younousleconquerant | 64:4cd8fed3f437 | 664 | if (result == STATUS_COLLISION) |
younousleconquerant | 64:4cd8fed3f437 | 665 | { // More than one PICC in the field => collision. |
younousleconquerant | 64:4cd8fed3f437 | 666 | result = PCD_ReadRegister(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0] |
younousleconquerant | 64:4cd8fed3f437 | 667 | if (result & 0x20) |
younousleconquerant | 64:4cd8fed3f437 | 668 | { // CollPosNotValid |
younousleconquerant | 64:4cd8fed3f437 | 669 | return STATUS_COLLISION; // Without a valid collision position we cannot continue |
younousleconquerant | 64:4cd8fed3f437 | 670 | } |
younousleconquerant | 64:4cd8fed3f437 | 671 | |
younousleconquerant | 64:4cd8fed3f437 | 672 | uint8_t collisionPos = result & 0x1F; // Values 0-31, 0 means bit 32. |
younousleconquerant | 64:4cd8fed3f437 | 673 | if (collisionPos == 0) |
younousleconquerant | 64:4cd8fed3f437 | 674 | { |
younousleconquerant | 64:4cd8fed3f437 | 675 | collisionPos = 32; |
younousleconquerant | 64:4cd8fed3f437 | 676 | } |
younousleconquerant | 64:4cd8fed3f437 | 677 | |
younousleconquerant | 64:4cd8fed3f437 | 678 | if (collisionPos <= currentLevelKnownBits) |
younousleconquerant | 64:4cd8fed3f437 | 679 | { // No progress - should not happen |
younousleconquerant | 64:4cd8fed3f437 | 680 | return STATUS_INTERNAL_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 681 | } |
younousleconquerant | 64:4cd8fed3f437 | 682 | |
younousleconquerant | 64:4cd8fed3f437 | 683 | // Choose the PICC with the bit set. |
younousleconquerant | 64:4cd8fed3f437 | 684 | currentLevelKnownBits = collisionPos; |
younousleconquerant | 64:4cd8fed3f437 | 685 | count = (currentLevelKnownBits - 1) % 8; // The bit to modify |
younousleconquerant | 64:4cd8fed3f437 | 686 | index = 1 + (currentLevelKnownBits / 8) + (count ? 1 : 0); // First byte is index 0. |
younousleconquerant | 64:4cd8fed3f437 | 687 | buffer[index] |= (1 << count); |
younousleconquerant | 64:4cd8fed3f437 | 688 | } |
younousleconquerant | 64:4cd8fed3f437 | 689 | else if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 690 | { |
younousleconquerant | 64:4cd8fed3f437 | 691 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 692 | } |
younousleconquerant | 64:4cd8fed3f437 | 693 | else |
younousleconquerant | 64:4cd8fed3f437 | 694 | { // STATUS_OK |
younousleconquerant | 64:4cd8fed3f437 | 695 | if (currentLevelKnownBits >= 32) |
younousleconquerant | 64:4cd8fed3f437 | 696 | { // This was a SELECT. |
younousleconquerant | 64:4cd8fed3f437 | 697 | selectDone = true; // No more anticollision |
younousleconquerant | 64:4cd8fed3f437 | 698 | // We continue below outside the while. |
younousleconquerant | 64:4cd8fed3f437 | 699 | } |
younousleconquerant | 64:4cd8fed3f437 | 700 | else |
younousleconquerant | 64:4cd8fed3f437 | 701 | { // This was an ANTICOLLISION. |
younousleconquerant | 64:4cd8fed3f437 | 702 | // We now have all 32 bits of the UID in this Cascade Level |
younousleconquerant | 64:4cd8fed3f437 | 703 | currentLevelKnownBits = 32; |
younousleconquerant | 64:4cd8fed3f437 | 704 | // Run loop again to do the SELECT. |
younousleconquerant | 64:4cd8fed3f437 | 705 | } |
younousleconquerant | 64:4cd8fed3f437 | 706 | } |
younousleconquerant | 64:4cd8fed3f437 | 707 | } // End of while ( ! selectDone) |
younousleconquerant | 64:4cd8fed3f437 | 708 | |
younousleconquerant | 64:4cd8fed3f437 | 709 | // We do not check the CBB - it was constructed by us above. |
younousleconquerant | 64:4cd8fed3f437 | 710 | |
younousleconquerant | 64:4cd8fed3f437 | 711 | // Copy the found UID bytes from buffer[] to uid->uidByte[] |
younousleconquerant | 64:4cd8fed3f437 | 712 | index = (buffer[2] == PICC_CMD_CT) ? 3 : 2; // source index in buffer[] |
younousleconquerant | 64:4cd8fed3f437 | 713 | bytesToCopy = (buffer[2] == PICC_CMD_CT) ? 3 : 4; |
younousleconquerant | 64:4cd8fed3f437 | 714 | for (count = 0; count < bytesToCopy; count++) |
younousleconquerant | 64:4cd8fed3f437 | 715 | { |
younousleconquerant | 64:4cd8fed3f437 | 716 | uid->uidByte[uidIndex + count] = buffer[index++]; |
younousleconquerant | 64:4cd8fed3f437 | 717 | } |
younousleconquerant | 64:4cd8fed3f437 | 718 | |
younousleconquerant | 64:4cd8fed3f437 | 719 | // Check response SAK (Select Acknowledge) |
younousleconquerant | 64:4cd8fed3f437 | 720 | if (responseLength != 3 || txLastBits != 0) |
younousleconquerant | 64:4cd8fed3f437 | 721 | { // SAK must be exactly 24 bits (1 byte + CRC_A). |
younousleconquerant | 64:4cd8fed3f437 | 722 | return STATUS_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 723 | } |
younousleconquerant | 64:4cd8fed3f437 | 724 | |
younousleconquerant | 64:4cd8fed3f437 | 725 | // Verify CRC_A - do our own calculation and store the control in buffer[2..3] - those bytes are not needed anymore. |
younousleconquerant | 64:4cd8fed3f437 | 726 | result = PCD_CalculateCRC(responseBuffer, 1, &buffer[2]); |
younousleconquerant | 64:4cd8fed3f437 | 727 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 728 | { |
younousleconquerant | 64:4cd8fed3f437 | 729 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 730 | } |
younousleconquerant | 64:4cd8fed3f437 | 731 | |
younousleconquerant | 64:4cd8fed3f437 | 732 | if ((buffer[2] != responseBuffer[1]) || (buffer[3] != responseBuffer[2])) |
younousleconquerant | 64:4cd8fed3f437 | 733 | { |
younousleconquerant | 64:4cd8fed3f437 | 734 | return STATUS_CRC_WRONG; |
younousleconquerant | 64:4cd8fed3f437 | 735 | } |
younousleconquerant | 64:4cd8fed3f437 | 736 | |
younousleconquerant | 64:4cd8fed3f437 | 737 | if (responseBuffer[0] & 0x04) |
younousleconquerant | 64:4cd8fed3f437 | 738 | { // Cascade bit set - UID not complete yes |
younousleconquerant | 64:4cd8fed3f437 | 739 | cascadeLevel++; |
younousleconquerant | 64:4cd8fed3f437 | 740 | } |
younousleconquerant | 64:4cd8fed3f437 | 741 | else |
younousleconquerant | 64:4cd8fed3f437 | 742 | { |
younousleconquerant | 64:4cd8fed3f437 | 743 | uidComplete = true; |
younousleconquerant | 64:4cd8fed3f437 | 744 | uid->sak = responseBuffer[0]; |
younousleconquerant | 64:4cd8fed3f437 | 745 | } |
younousleconquerant | 64:4cd8fed3f437 | 746 | } // End of while ( ! uidComplete) |
younousleconquerant | 64:4cd8fed3f437 | 747 | |
younousleconquerant | 64:4cd8fed3f437 | 748 | // Set correct uid->size |
younousleconquerant | 64:4cd8fed3f437 | 749 | uid->size = 3 * cascadeLevel + 1; |
younousleconquerant | 64:4cd8fed3f437 | 750 | |
younousleconquerant | 64:4cd8fed3f437 | 751 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 752 | } // End PICC_Select() |
younousleconquerant | 64:4cd8fed3f437 | 753 | |
younousleconquerant | 64:4cd8fed3f437 | 754 | /* |
younousleconquerant | 64:4cd8fed3f437 | 755 | * Instructs a PICC in state ACTIVE(*) to go to state HALT. |
younousleconquerant | 64:4cd8fed3f437 | 756 | */ |
younousleconquerant | 64:4cd8fed3f437 | 757 | uint8_t MFRC522::PICC_HaltA() |
younousleconquerant | 64:4cd8fed3f437 | 758 | { |
younousleconquerant | 64:4cd8fed3f437 | 759 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 760 | uint8_t buffer[4]; |
younousleconquerant | 64:4cd8fed3f437 | 761 | |
younousleconquerant | 64:4cd8fed3f437 | 762 | // Build command buffer |
younousleconquerant | 64:4cd8fed3f437 | 763 | buffer[0] = PICC_CMD_HLTA; |
younousleconquerant | 64:4cd8fed3f437 | 764 | buffer[1] = 0; |
younousleconquerant | 64:4cd8fed3f437 | 765 | |
younousleconquerant | 64:4cd8fed3f437 | 766 | // Calculate CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 767 | result = PCD_CalculateCRC(buffer, 2, &buffer[2]); |
younousleconquerant | 64:4cd8fed3f437 | 768 | if (result == STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 769 | { |
younousleconquerant | 64:4cd8fed3f437 | 770 | // Send the command. |
younousleconquerant | 64:4cd8fed3f437 | 771 | // The standard says: |
younousleconquerant | 64:4cd8fed3f437 | 772 | // If the PICC responds with any modulation during a period of 1 ms after the end of the frame containing the |
younousleconquerant | 64:4cd8fed3f437 | 773 | // HLTA command, this response shall be interpreted as 'not acknowledge'. |
younousleconquerant | 64:4cd8fed3f437 | 774 | // We interpret that this way: Only STATUS_TIMEOUT is an success. |
younousleconquerant | 64:4cd8fed3f437 | 775 | result = PCD_TransceiveData(buffer, sizeof(buffer), NULL, 0); |
younousleconquerant | 64:4cd8fed3f437 | 776 | if (result == STATUS_TIMEOUT) |
younousleconquerant | 64:4cd8fed3f437 | 777 | { |
younousleconquerant | 64:4cd8fed3f437 | 778 | result = STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 779 | } |
younousleconquerant | 64:4cd8fed3f437 | 780 | else if (result == STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 781 | { // That is ironically NOT ok in this case ;-) |
younousleconquerant | 64:4cd8fed3f437 | 782 | result = STATUS_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 783 | } |
younousleconquerant | 64:4cd8fed3f437 | 784 | } |
younousleconquerant | 64:4cd8fed3f437 | 785 | |
younousleconquerant | 64:4cd8fed3f437 | 786 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 787 | } // End PICC_HaltA() |
younousleconquerant | 64:4cd8fed3f437 | 788 | |
younousleconquerant | 64:4cd8fed3f437 | 789 | |
younousleconquerant | 64:4cd8fed3f437 | 790 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 791 | // Functions for communicating with MIFARE PICCs |
younousleconquerant | 64:4cd8fed3f437 | 792 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 793 | |
younousleconquerant | 64:4cd8fed3f437 | 794 | /* |
younousleconquerant | 64:4cd8fed3f437 | 795 | * Executes the MFRC522 MFAuthent command. |
younousleconquerant | 64:4cd8fed3f437 | 796 | */ |
younousleconquerant | 64:4cd8fed3f437 | 797 | uint8_t MFRC522::PCD_Authenticate(uint8_t command, uint8_t blockAddr, MIFARE_Key *key, Uid *uid) |
younousleconquerant | 64:4cd8fed3f437 | 798 | { |
younousleconquerant | 64:4cd8fed3f437 | 799 | uint8_t i, waitIRq = 0x10; // IdleIRq |
younousleconquerant | 64:4cd8fed3f437 | 800 | |
younousleconquerant | 64:4cd8fed3f437 | 801 | // Build command buffer |
younousleconquerant | 64:4cd8fed3f437 | 802 | uint8_t sendData[12]; |
younousleconquerant | 64:4cd8fed3f437 | 803 | sendData[0] = command; |
younousleconquerant | 64:4cd8fed3f437 | 804 | sendData[1] = blockAddr; |
younousleconquerant | 64:4cd8fed3f437 | 805 | |
younousleconquerant | 64:4cd8fed3f437 | 806 | for (i = 0; i < MF_KEY_SIZE; i++) |
younousleconquerant | 64:4cd8fed3f437 | 807 | { // 6 key bytes |
younousleconquerant | 64:4cd8fed3f437 | 808 | sendData[2+i] = key->keyByte[i]; |
younousleconquerant | 64:4cd8fed3f437 | 809 | } |
younousleconquerant | 64:4cd8fed3f437 | 810 | |
younousleconquerant | 64:4cd8fed3f437 | 811 | for (i = 0; i < 4; i++) |
younousleconquerant | 64:4cd8fed3f437 | 812 | { // The first 4 bytes of the UID |
younousleconquerant | 64:4cd8fed3f437 | 813 | sendData[8+i] = uid->uidByte[i]; |
younousleconquerant | 64:4cd8fed3f437 | 814 | } |
younousleconquerant | 64:4cd8fed3f437 | 815 | |
younousleconquerant | 64:4cd8fed3f437 | 816 | // Start the authentication. |
younousleconquerant | 64:4cd8fed3f437 | 817 | return PCD_CommunicateWithPICC(PCD_MFAuthent, waitIRq, &sendData[0], sizeof(sendData)); |
younousleconquerant | 64:4cd8fed3f437 | 818 | } // End PCD_Authenticate() |
younousleconquerant | 64:4cd8fed3f437 | 819 | |
younousleconquerant | 64:4cd8fed3f437 | 820 | /* |
younousleconquerant | 64:4cd8fed3f437 | 821 | * Used to exit the PCD from its authenticated state. |
younousleconquerant | 64:4cd8fed3f437 | 822 | * Remember to call this function after communicating with an authenticated PICC - otherwise no new communications can start. |
younousleconquerant | 64:4cd8fed3f437 | 823 | */ |
younousleconquerant | 64:4cd8fed3f437 | 824 | void MFRC522::PCD_StopCrypto1() |
younousleconquerant | 64:4cd8fed3f437 | 825 | { |
younousleconquerant | 64:4cd8fed3f437 | 826 | // Clear MFCrypto1On bit |
younousleconquerant | 64:4cd8fed3f437 | 827 | PCD_ClrRegisterBits(Status2Reg, 0x08); // Status2Reg[7..0] bits are: TempSensClear I2CForceHS reserved reserved MFCrypto1On ModemState[2:0] |
younousleconquerant | 64:4cd8fed3f437 | 828 | } // End PCD_StopCrypto1() |
younousleconquerant | 64:4cd8fed3f437 | 829 | |
younousleconquerant | 64:4cd8fed3f437 | 830 | /* |
younousleconquerant | 64:4cd8fed3f437 | 831 | * Reads 16 bytes (+ 2 bytes CRC_A) from the active PICC. |
younousleconquerant | 64:4cd8fed3f437 | 832 | */ |
younousleconquerant | 64:4cd8fed3f437 | 833 | uint8_t MFRC522::MIFARE_Read(uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 834 | { |
younousleconquerant | 64:4cd8fed3f437 | 835 | uint8_t result = STATUS_NO_ROOM; |
younousleconquerant | 64:4cd8fed3f437 | 836 | |
younousleconquerant | 64:4cd8fed3f437 | 837 | // Sanity check |
younousleconquerant | 64:4cd8fed3f437 | 838 | if ((buffer == NULL) || (*bufferSize < 18)) |
younousleconquerant | 64:4cd8fed3f437 | 839 | { |
younousleconquerant | 64:4cd8fed3f437 | 840 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 841 | } |
younousleconquerant | 64:4cd8fed3f437 | 842 | |
younousleconquerant | 64:4cd8fed3f437 | 843 | // Build command buffer |
younousleconquerant | 64:4cd8fed3f437 | 844 | buffer[0] = PICC_CMD_MF_READ; |
younousleconquerant | 64:4cd8fed3f437 | 845 | buffer[1] = blockAddr; |
younousleconquerant | 64:4cd8fed3f437 | 846 | |
younousleconquerant | 64:4cd8fed3f437 | 847 | // Calculate CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 848 | result = PCD_CalculateCRC(buffer, 2, &buffer[2]); |
younousleconquerant | 64:4cd8fed3f437 | 849 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 850 | { |
younousleconquerant | 64:4cd8fed3f437 | 851 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 852 | } |
younousleconquerant | 64:4cd8fed3f437 | 853 | |
younousleconquerant | 64:4cd8fed3f437 | 854 | // Transmit the buffer and receive the response, validate CRC_A. |
younousleconquerant | 64:4cd8fed3f437 | 855 | return PCD_TransceiveData(buffer, 4, buffer, bufferSize, NULL, 0, true); |
younousleconquerant | 64:4cd8fed3f437 | 856 | } // End MIFARE_Read() |
younousleconquerant | 64:4cd8fed3f437 | 857 | |
younousleconquerant | 64:4cd8fed3f437 | 858 | /* |
younousleconquerant | 64:4cd8fed3f437 | 859 | * Writes 16 bytes to the active PICC. |
younousleconquerant | 64:4cd8fed3f437 | 860 | */ |
younousleconquerant | 64:4cd8fed3f437 | 861 | uint8_t MFRC522::MIFARE_Write(uint8_t blockAddr, uint8_t *buffer, uint8_t bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 862 | { |
younousleconquerant | 64:4cd8fed3f437 | 863 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 864 | |
younousleconquerant | 64:4cd8fed3f437 | 865 | // Sanity check |
younousleconquerant | 64:4cd8fed3f437 | 866 | if (buffer == NULL || bufferSize < 16) |
younousleconquerant | 64:4cd8fed3f437 | 867 | { |
younousleconquerant | 64:4cd8fed3f437 | 868 | return STATUS_INVALID; |
younousleconquerant | 64:4cd8fed3f437 | 869 | } |
younousleconquerant | 64:4cd8fed3f437 | 870 | |
younousleconquerant | 64:4cd8fed3f437 | 871 | // Mifare Classic protocol requires two communications to perform a write. |
younousleconquerant | 64:4cd8fed3f437 | 872 | // Step 1: Tell the PICC we want to write to block blockAddr. |
younousleconquerant | 64:4cd8fed3f437 | 873 | uint8_t cmdBuffer[2]; |
younousleconquerant | 64:4cd8fed3f437 | 874 | cmdBuffer[0] = PICC_CMD_MF_WRITE; |
younousleconquerant | 64:4cd8fed3f437 | 875 | cmdBuffer[1] = blockAddr; |
younousleconquerant | 64:4cd8fed3f437 | 876 | // Adds CRC_A and checks that the response is MF_ACK. |
younousleconquerant | 64:4cd8fed3f437 | 877 | result = PCD_MIFARE_Transceive(cmdBuffer, 2); |
younousleconquerant | 64:4cd8fed3f437 | 878 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 879 | { |
younousleconquerant | 64:4cd8fed3f437 | 880 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 881 | } |
younousleconquerant | 64:4cd8fed3f437 | 882 | |
younousleconquerant | 64:4cd8fed3f437 | 883 | // Step 2: Transfer the data |
younousleconquerant | 64:4cd8fed3f437 | 884 | // Adds CRC_A and checks that the response is MF_ACK. |
younousleconquerant | 64:4cd8fed3f437 | 885 | result = PCD_MIFARE_Transceive(buffer, bufferSize); |
younousleconquerant | 64:4cd8fed3f437 | 886 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 887 | { |
younousleconquerant | 64:4cd8fed3f437 | 888 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 889 | } |
younousleconquerant | 64:4cd8fed3f437 | 890 | |
younousleconquerant | 64:4cd8fed3f437 | 891 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 892 | } // End MIFARE_Write() |
younousleconquerant | 64:4cd8fed3f437 | 893 | |
younousleconquerant | 64:4cd8fed3f437 | 894 | /* |
younousleconquerant | 64:4cd8fed3f437 | 895 | * Writes a 4 byte page to the active MIFARE Ultralight PICC. |
younousleconquerant | 64:4cd8fed3f437 | 896 | */ |
younousleconquerant | 64:4cd8fed3f437 | 897 | uint8_t MFRC522::MIFARE_UltralightWrite(uint8_t page, uint8_t *buffer, uint8_t bufferSize) |
younousleconquerant | 64:4cd8fed3f437 | 898 | { |
younousleconquerant | 64:4cd8fed3f437 | 899 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 900 | |
younousleconquerant | 64:4cd8fed3f437 | 901 | // Sanity check |
younousleconquerant | 64:4cd8fed3f437 | 902 | if (buffer == NULL || bufferSize < 4) |
younousleconquerant | 64:4cd8fed3f437 | 903 | { |
younousleconquerant | 64:4cd8fed3f437 | 904 | return STATUS_INVALID; |
younousleconquerant | 64:4cd8fed3f437 | 905 | } |
younousleconquerant | 64:4cd8fed3f437 | 906 | |
younousleconquerant | 64:4cd8fed3f437 | 907 | // Build commmand buffer |
younousleconquerant | 64:4cd8fed3f437 | 908 | uint8_t cmdBuffer[6]; |
younousleconquerant | 64:4cd8fed3f437 | 909 | cmdBuffer[0] = PICC_CMD_UL_WRITE; |
younousleconquerant | 64:4cd8fed3f437 | 910 | cmdBuffer[1] = page; |
younousleconquerant | 64:4cd8fed3f437 | 911 | memcpy(&cmdBuffer[2], buffer, 4); |
younousleconquerant | 64:4cd8fed3f437 | 912 | |
younousleconquerant | 64:4cd8fed3f437 | 913 | // Perform the write |
younousleconquerant | 64:4cd8fed3f437 | 914 | result = PCD_MIFARE_Transceive(cmdBuffer, 6); // Adds CRC_A and checks that the response is MF_ACK. |
younousleconquerant | 64:4cd8fed3f437 | 915 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 916 | { |
younousleconquerant | 64:4cd8fed3f437 | 917 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 918 | } |
younousleconquerant | 64:4cd8fed3f437 | 919 | |
younousleconquerant | 64:4cd8fed3f437 | 920 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 921 | } // End MIFARE_Ultralight_Write() |
younousleconquerant | 64:4cd8fed3f437 | 922 | |
younousleconquerant | 64:4cd8fed3f437 | 923 | /* |
younousleconquerant | 64:4cd8fed3f437 | 924 | * MIFARE Decrement subtracts the delta from the value of the addressed block, and stores the result in a volatile memory. |
younousleconquerant | 64:4cd8fed3f437 | 925 | */ |
younousleconquerant | 64:4cd8fed3f437 | 926 | uint8_t MFRC522::MIFARE_Decrement(uint8_t blockAddr, uint32_t delta) |
younousleconquerant | 64:4cd8fed3f437 | 927 | { |
younousleconquerant | 64:4cd8fed3f437 | 928 | return MIFARE_TwoStepHelper(PICC_CMD_MF_DECREMENT, blockAddr, delta); |
younousleconquerant | 64:4cd8fed3f437 | 929 | } // End MIFARE_Decrement() |
younousleconquerant | 64:4cd8fed3f437 | 930 | |
younousleconquerant | 64:4cd8fed3f437 | 931 | /* |
younousleconquerant | 64:4cd8fed3f437 | 932 | * MIFARE Increment adds the delta to the value of the addressed block, and stores the result in a volatile memory. |
younousleconquerant | 64:4cd8fed3f437 | 933 | */ |
younousleconquerant | 64:4cd8fed3f437 | 934 | uint8_t MFRC522::MIFARE_Increment(uint8_t blockAddr, uint32_t delta) |
younousleconquerant | 64:4cd8fed3f437 | 935 | { |
younousleconquerant | 64:4cd8fed3f437 | 936 | return MIFARE_TwoStepHelper(PICC_CMD_MF_INCREMENT, blockAddr, delta); |
younousleconquerant | 64:4cd8fed3f437 | 937 | } // End MIFARE_Increment() |
younousleconquerant | 64:4cd8fed3f437 | 938 | |
younousleconquerant | 64:4cd8fed3f437 | 939 | /** |
younousleconquerant | 64:4cd8fed3f437 | 940 | * MIFARE Restore copies the value of the addressed block into a volatile memory. |
younousleconquerant | 64:4cd8fed3f437 | 941 | */ |
younousleconquerant | 64:4cd8fed3f437 | 942 | uint8_t MFRC522::MIFARE_Restore(uint8_t blockAddr) |
younousleconquerant | 64:4cd8fed3f437 | 943 | { |
younousleconquerant | 64:4cd8fed3f437 | 944 | // The datasheet describes Restore as a two step operation, but does not explain what data to transfer in step 2. |
younousleconquerant | 64:4cd8fed3f437 | 945 | // Doing only a single step does not work, so I chose to transfer 0L in step two. |
younousleconquerant | 64:4cd8fed3f437 | 946 | return MIFARE_TwoStepHelper(PICC_CMD_MF_RESTORE, blockAddr, 0L); |
younousleconquerant | 64:4cd8fed3f437 | 947 | } // End MIFARE_Restore() |
younousleconquerant | 64:4cd8fed3f437 | 948 | |
younousleconquerant | 64:4cd8fed3f437 | 949 | /* |
younousleconquerant | 64:4cd8fed3f437 | 950 | * Helper function for the two-step MIFARE Classic protocol operations Decrement, Increment and Restore. |
younousleconquerant | 64:4cd8fed3f437 | 951 | */ |
younousleconquerant | 64:4cd8fed3f437 | 952 | uint8_t MFRC522::MIFARE_TwoStepHelper(uint8_t command, uint8_t blockAddr, uint32_t data) |
younousleconquerant | 64:4cd8fed3f437 | 953 | { |
younousleconquerant | 64:4cd8fed3f437 | 954 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 955 | uint8_t cmdBuffer[2]; // We only need room for 2 bytes. |
younousleconquerant | 64:4cd8fed3f437 | 956 | |
younousleconquerant | 64:4cd8fed3f437 | 957 | // Step 1: Tell the PICC the command and block address |
younousleconquerant | 64:4cd8fed3f437 | 958 | cmdBuffer[0] = command; |
younousleconquerant | 64:4cd8fed3f437 | 959 | cmdBuffer[1] = blockAddr; |
younousleconquerant | 64:4cd8fed3f437 | 960 | |
younousleconquerant | 64:4cd8fed3f437 | 961 | // Adds CRC_A and checks that the response is MF_ACK. |
younousleconquerant | 64:4cd8fed3f437 | 962 | result = PCD_MIFARE_Transceive(cmdBuffer, 2); |
younousleconquerant | 64:4cd8fed3f437 | 963 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 964 | { |
younousleconquerant | 64:4cd8fed3f437 | 965 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 966 | } |
younousleconquerant | 64:4cd8fed3f437 | 967 | |
younousleconquerant | 64:4cd8fed3f437 | 968 | // Step 2: Transfer the data |
younousleconquerant | 64:4cd8fed3f437 | 969 | // Adds CRC_A and accept timeout as success. |
younousleconquerant | 64:4cd8fed3f437 | 970 | result = PCD_MIFARE_Transceive((uint8_t *) &data, 4, true); |
younousleconquerant | 64:4cd8fed3f437 | 971 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 972 | { |
younousleconquerant | 64:4cd8fed3f437 | 973 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 974 | } |
younousleconquerant | 64:4cd8fed3f437 | 975 | |
younousleconquerant | 64:4cd8fed3f437 | 976 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 977 | } // End MIFARE_TwoStepHelper() |
younousleconquerant | 64:4cd8fed3f437 | 978 | |
younousleconquerant | 64:4cd8fed3f437 | 979 | /* |
younousleconquerant | 64:4cd8fed3f437 | 980 | * MIFARE Transfer writes the value stored in the volatile memory into one MIFARE Classic block. |
younousleconquerant | 64:4cd8fed3f437 | 981 | */ |
younousleconquerant | 64:4cd8fed3f437 | 982 | uint8_t MFRC522::MIFARE_Transfer(uint8_t blockAddr) |
younousleconquerant | 64:4cd8fed3f437 | 983 | { |
younousleconquerant | 64:4cd8fed3f437 | 984 | uint8_t cmdBuffer[2]; // We only need room for 2 bytes. |
younousleconquerant | 64:4cd8fed3f437 | 985 | |
younousleconquerant | 64:4cd8fed3f437 | 986 | // Tell the PICC we want to transfer the result into block blockAddr. |
younousleconquerant | 64:4cd8fed3f437 | 987 | cmdBuffer[0] = PICC_CMD_MF_TRANSFER; |
younousleconquerant | 64:4cd8fed3f437 | 988 | cmdBuffer[1] = blockAddr; |
younousleconquerant | 64:4cd8fed3f437 | 989 | |
younousleconquerant | 64:4cd8fed3f437 | 990 | // Adds CRC_A and checks that the response is MF_ACK. |
younousleconquerant | 64:4cd8fed3f437 | 991 | return PCD_MIFARE_Transceive(cmdBuffer, 2); |
younousleconquerant | 64:4cd8fed3f437 | 992 | } // End MIFARE_Transfer() |
younousleconquerant | 64:4cd8fed3f437 | 993 | |
younousleconquerant | 64:4cd8fed3f437 | 994 | |
younousleconquerant | 64:4cd8fed3f437 | 995 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 996 | // Support functions |
younousleconquerant | 64:4cd8fed3f437 | 997 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 998 | |
younousleconquerant | 64:4cd8fed3f437 | 999 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1000 | * Wrapper for MIFARE protocol communication. |
younousleconquerant | 64:4cd8fed3f437 | 1001 | * Adds CRC_A, executes the Transceive command and checks that the response is MF_ACK or a timeout. |
younousleconquerant | 64:4cd8fed3f437 | 1002 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1003 | uint8_t MFRC522::PCD_MIFARE_Transceive(uint8_t *sendData, uint8_t sendLen, bool acceptTimeout) |
younousleconquerant | 64:4cd8fed3f437 | 1004 | { |
younousleconquerant | 64:4cd8fed3f437 | 1005 | uint8_t result; |
younousleconquerant | 64:4cd8fed3f437 | 1006 | uint8_t cmdBuffer[18]; // We need room for 16 bytes data and 2 bytes CRC_A. |
younousleconquerant | 64:4cd8fed3f437 | 1007 | |
younousleconquerant | 64:4cd8fed3f437 | 1008 | // Sanity check |
younousleconquerant | 64:4cd8fed3f437 | 1009 | if (sendData == NULL || sendLen > 16) |
younousleconquerant | 64:4cd8fed3f437 | 1010 | { |
younousleconquerant | 64:4cd8fed3f437 | 1011 | return STATUS_INVALID; |
younousleconquerant | 64:4cd8fed3f437 | 1012 | } |
younousleconquerant | 64:4cd8fed3f437 | 1013 | |
younousleconquerant | 64:4cd8fed3f437 | 1014 | // Copy sendData[] to cmdBuffer[] and add CRC_A |
younousleconquerant | 64:4cd8fed3f437 | 1015 | memcpy(cmdBuffer, sendData, sendLen); |
younousleconquerant | 64:4cd8fed3f437 | 1016 | result = PCD_CalculateCRC(cmdBuffer, sendLen, &cmdBuffer[sendLen]); |
younousleconquerant | 64:4cd8fed3f437 | 1017 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 1018 | { |
younousleconquerant | 64:4cd8fed3f437 | 1019 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 1020 | } |
younousleconquerant | 64:4cd8fed3f437 | 1021 | |
younousleconquerant | 64:4cd8fed3f437 | 1022 | sendLen += 2; |
younousleconquerant | 64:4cd8fed3f437 | 1023 | |
younousleconquerant | 64:4cd8fed3f437 | 1024 | // Transceive the data, store the reply in cmdBuffer[] |
younousleconquerant | 64:4cd8fed3f437 | 1025 | uint8_t waitIRq = 0x30; // RxIRq and IdleIRq |
younousleconquerant | 64:4cd8fed3f437 | 1026 | uint8_t cmdBufferSize = sizeof(cmdBuffer); |
younousleconquerant | 64:4cd8fed3f437 | 1027 | uint8_t validBits = 0; |
younousleconquerant | 64:4cd8fed3f437 | 1028 | result = PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, cmdBuffer, sendLen, cmdBuffer, &cmdBufferSize, &validBits); |
younousleconquerant | 64:4cd8fed3f437 | 1029 | if (acceptTimeout && result == STATUS_TIMEOUT) |
younousleconquerant | 64:4cd8fed3f437 | 1030 | { |
younousleconquerant | 64:4cd8fed3f437 | 1031 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 1032 | } |
younousleconquerant | 64:4cd8fed3f437 | 1033 | |
younousleconquerant | 64:4cd8fed3f437 | 1034 | if (result != STATUS_OK) |
younousleconquerant | 64:4cd8fed3f437 | 1035 | { |
younousleconquerant | 64:4cd8fed3f437 | 1036 | return result; |
younousleconquerant | 64:4cd8fed3f437 | 1037 | } |
younousleconquerant | 64:4cd8fed3f437 | 1038 | |
younousleconquerant | 64:4cd8fed3f437 | 1039 | // The PICC must reply with a 4 bit ACK |
younousleconquerant | 64:4cd8fed3f437 | 1040 | if (cmdBufferSize != 1 || validBits != 4) |
younousleconquerant | 64:4cd8fed3f437 | 1041 | { |
younousleconquerant | 64:4cd8fed3f437 | 1042 | return STATUS_ERROR; |
younousleconquerant | 64:4cd8fed3f437 | 1043 | } |
younousleconquerant | 64:4cd8fed3f437 | 1044 | |
younousleconquerant | 64:4cd8fed3f437 | 1045 | if (cmdBuffer[0] != MF_ACK) |
younousleconquerant | 64:4cd8fed3f437 | 1046 | { |
younousleconquerant | 64:4cd8fed3f437 | 1047 | return STATUS_MIFARE_NACK; |
younousleconquerant | 64:4cd8fed3f437 | 1048 | } |
younousleconquerant | 64:4cd8fed3f437 | 1049 | |
younousleconquerant | 64:4cd8fed3f437 | 1050 | return STATUS_OK; |
younousleconquerant | 64:4cd8fed3f437 | 1051 | } // End PCD_MIFARE_Transceive() |
younousleconquerant | 64:4cd8fed3f437 | 1052 | |
younousleconquerant | 64:4cd8fed3f437 | 1053 | |
younousleconquerant | 64:4cd8fed3f437 | 1054 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1055 | * Translates the SAK (Select Acknowledge) to a PICC type. |
younousleconquerant | 64:4cd8fed3f437 | 1056 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1057 | uint8_t MFRC522::PICC_GetType(uint8_t sak) |
younousleconquerant | 64:4cd8fed3f437 | 1058 | { |
younousleconquerant | 64:4cd8fed3f437 | 1059 | uint8_t retType = PICC_TYPE_UNKNOWN; |
younousleconquerant | 64:4cd8fed3f437 | 1060 | |
younousleconquerant | 64:4cd8fed3f437 | 1061 | if (sak & 0x04) |
younousleconquerant | 64:4cd8fed3f437 | 1062 | { // UID not complete |
younousleconquerant | 64:4cd8fed3f437 | 1063 | retType = PICC_TYPE_NOT_COMPLETE; |
younousleconquerant | 64:4cd8fed3f437 | 1064 | } |
younousleconquerant | 64:4cd8fed3f437 | 1065 | else |
younousleconquerant | 64:4cd8fed3f437 | 1066 | { |
younousleconquerant | 64:4cd8fed3f437 | 1067 | switch (sak) |
younousleconquerant | 64:4cd8fed3f437 | 1068 | { |
younousleconquerant | 64:4cd8fed3f437 | 1069 | case 0x09: retType = PICC_TYPE_MIFARE_MINI; break; |
younousleconquerant | 64:4cd8fed3f437 | 1070 | case 0x08: retType = PICC_TYPE_MIFARE_1K; break; |
younousleconquerant | 64:4cd8fed3f437 | 1071 | case 0x18: retType = PICC_TYPE_MIFARE_4K; break; |
younousleconquerant | 64:4cd8fed3f437 | 1072 | case 0x00: retType = PICC_TYPE_MIFARE_UL; break; |
younousleconquerant | 64:4cd8fed3f437 | 1073 | case 0x10: |
younousleconquerant | 64:4cd8fed3f437 | 1074 | case 0x11: retType = PICC_TYPE_MIFARE_PLUS; break; |
younousleconquerant | 64:4cd8fed3f437 | 1075 | case 0x01: retType = PICC_TYPE_TNP3XXX; break; |
younousleconquerant | 64:4cd8fed3f437 | 1076 | default: |
younousleconquerant | 64:4cd8fed3f437 | 1077 | if (sak & 0x20) |
younousleconquerant | 64:4cd8fed3f437 | 1078 | { |
younousleconquerant | 64:4cd8fed3f437 | 1079 | retType = PICC_TYPE_ISO_14443_4; |
younousleconquerant | 64:4cd8fed3f437 | 1080 | } |
younousleconquerant | 64:4cd8fed3f437 | 1081 | else if (sak & 0x40) |
younousleconquerant | 64:4cd8fed3f437 | 1082 | { |
younousleconquerant | 64:4cd8fed3f437 | 1083 | retType = PICC_TYPE_ISO_18092; |
younousleconquerant | 64:4cd8fed3f437 | 1084 | } |
younousleconquerant | 64:4cd8fed3f437 | 1085 | break; |
younousleconquerant | 64:4cd8fed3f437 | 1086 | } |
younousleconquerant | 64:4cd8fed3f437 | 1087 | } |
younousleconquerant | 64:4cd8fed3f437 | 1088 | |
younousleconquerant | 64:4cd8fed3f437 | 1089 | return (retType); |
younousleconquerant | 64:4cd8fed3f437 | 1090 | } // End PICC_GetType() |
younousleconquerant | 64:4cd8fed3f437 | 1091 | |
younousleconquerant | 64:4cd8fed3f437 | 1092 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1093 | * Returns a string pointer to the PICC type name. |
younousleconquerant | 64:4cd8fed3f437 | 1094 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1095 | char* MFRC522::PICC_GetTypeName(uint8_t piccType) |
younousleconquerant | 64:4cd8fed3f437 | 1096 | { |
younousleconquerant | 64:4cd8fed3f437 | 1097 | if(piccType == PICC_TYPE_NOT_COMPLETE) |
younousleconquerant | 64:4cd8fed3f437 | 1098 | { |
younousleconquerant | 64:4cd8fed3f437 | 1099 | piccType = MFRC522_MaxPICCs - 1; |
younousleconquerant | 64:4cd8fed3f437 | 1100 | } |
younousleconquerant | 64:4cd8fed3f437 | 1101 | |
younousleconquerant | 64:4cd8fed3f437 | 1102 | return((char *) _TypeNamePICC[piccType]); |
younousleconquerant | 64:4cd8fed3f437 | 1103 | } // End PICC_GetTypeName() |
younousleconquerant | 64:4cd8fed3f437 | 1104 | |
younousleconquerant | 64:4cd8fed3f437 | 1105 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1106 | * Returns a string pointer to a status code name. |
younousleconquerant | 64:4cd8fed3f437 | 1107 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1108 | char* MFRC522::GetStatusCodeName(uint8_t code) |
younousleconquerant | 64:4cd8fed3f437 | 1109 | { |
younousleconquerant | 64:4cd8fed3f437 | 1110 | return((char *) _ErrorMessage[code]); |
younousleconquerant | 64:4cd8fed3f437 | 1111 | } // End GetStatusCodeName() |
younousleconquerant | 64:4cd8fed3f437 | 1112 | |
younousleconquerant | 64:4cd8fed3f437 | 1113 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1114 | * Calculates the bit pattern needed for the specified access bits. In the [C1 C2 C3] tupples C1 is MSB (=4) and C3 is LSB (=1). |
younousleconquerant | 64:4cd8fed3f437 | 1115 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1116 | void MFRC522::MIFARE_SetAccessBits(uint8_t *accessBitBuffer, |
younousleconquerant | 64:4cd8fed3f437 | 1117 | uint8_t g0, |
younousleconquerant | 64:4cd8fed3f437 | 1118 | uint8_t g1, |
younousleconquerant | 64:4cd8fed3f437 | 1119 | uint8_t g2, |
younousleconquerant | 64:4cd8fed3f437 | 1120 | uint8_t g3) |
younousleconquerant | 64:4cd8fed3f437 | 1121 | { |
younousleconquerant | 64:4cd8fed3f437 | 1122 | uint8_t c1 = ((g3 & 4) << 1) | ((g2 & 4) << 0) | ((g1 & 4) >> 1) | ((g0 & 4) >> 2); |
younousleconquerant | 64:4cd8fed3f437 | 1123 | uint8_t c2 = ((g3 & 2) << 2) | ((g2 & 2) << 1) | ((g1 & 2) << 0) | ((g0 & 2) >> 1); |
younousleconquerant | 64:4cd8fed3f437 | 1124 | uint8_t c3 = ((g3 & 1) << 3) | ((g2 & 1) << 2) | ((g1 & 1) << 1) | ((g0 & 1) << 0); |
younousleconquerant | 64:4cd8fed3f437 | 1125 | |
younousleconquerant | 64:4cd8fed3f437 | 1126 | accessBitBuffer[0] = (~c2 & 0xF) << 4 | (~c1 & 0xF); |
younousleconquerant | 64:4cd8fed3f437 | 1127 | accessBitBuffer[1] = c1 << 4 | (~c3 & 0xF); |
younousleconquerant | 64:4cd8fed3f437 | 1128 | accessBitBuffer[2] = c3 << 4 | c2; |
younousleconquerant | 64:4cd8fed3f437 | 1129 | } // End MIFARE_SetAccessBits() |
younousleconquerant | 64:4cd8fed3f437 | 1130 | |
younousleconquerant | 64:4cd8fed3f437 | 1131 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 1132 | // Convenience functions - does not add extra functionality |
younousleconquerant | 64:4cd8fed3f437 | 1133 | ///////////////////////////////////////////////////////////////////////////////////// |
younousleconquerant | 64:4cd8fed3f437 | 1134 | |
younousleconquerant | 64:4cd8fed3f437 | 1135 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1136 | * Returns true if a PICC responds to PICC_CMD_REQA. |
younousleconquerant | 64:4cd8fed3f437 | 1137 | * Only "new" cards in state IDLE are invited. Sleeping cards in state HALT are ignored. |
younousleconquerant | 64:4cd8fed3f437 | 1138 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1139 | bool MFRC522::PICC_IsNewCardPresent(void) |
younousleconquerant | 64:4cd8fed3f437 | 1140 | { |
younousleconquerant | 64:4cd8fed3f437 | 1141 | uint8_t bufferATQA[2]; |
younousleconquerant | 64:4cd8fed3f437 | 1142 | uint8_t bufferSize = sizeof(bufferATQA); |
younousleconquerant | 64:4cd8fed3f437 | 1143 | uint8_t result = PICC_RequestA(bufferATQA, &bufferSize); |
younousleconquerant | 64:4cd8fed3f437 | 1144 | return ((result == STATUS_OK) || (result == STATUS_COLLISION)); |
younousleconquerant | 64:4cd8fed3f437 | 1145 | } // End PICC_IsNewCardPresent() |
younousleconquerant | 64:4cd8fed3f437 | 1146 | |
younousleconquerant | 64:4cd8fed3f437 | 1147 | /* |
younousleconquerant | 64:4cd8fed3f437 | 1148 | * Simple wrapper around PICC_Select. |
younousleconquerant | 64:4cd8fed3f437 | 1149 | */ |
younousleconquerant | 64:4cd8fed3f437 | 1150 | bool MFRC522::PICC_ReadCardSerial(void) |
younousleconquerant | 64:4cd8fed3f437 | 1151 | { |
younousleconquerant | 64:4cd8fed3f437 | 1152 | uint8_t result = PICC_Select(&uid); |
younousleconquerant | 64:4cd8fed3f437 | 1153 | return (result == STATUS_OK); |
younousleconquerant | 64:4cd8fed3f437 | 1154 | } // End PICC_ReadCardSerial() |