ADD TARGET_NUCLEO_F446ZE

Dependents:   FRDM_MFRC522

Fork of MFRC522 by Martin Olejar

Committer:
daniebrink
Date:
Fri Oct 21 13:31:31 2016 +0000
Revision:
2:1afff2a563ca
Parent:
1:63d729186747
SPI Frequency to 20Mbit when TARGET_NUCLEO_F446ZE

Who changed what in which revision?

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