sds

Dependencies:   NOKIA_5110 mbed

Committer:
mustwillza
Date:
Sun Nov 15 19:38:36 2015 +0000
Revision:
0:8b94afcb61eb
dd

Who changed what in which revision?

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