CCC

Dependents:   Integrate_All

Committer:
Tezcorp
Date:
Sun Dec 16 19:40:03 2018 +0000
Revision:
0:879cf546d390
BBB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tezcorp 0:879cf546d390 1 /**
Tezcorp 0:879cf546d390 2 * MFRC522.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
Tezcorp 0:879cf546d390 3 * Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
Tezcorp 0:879cf546d390 4 * Created by Miguel Balboa (circuitito.com), Jan, 2012.
Tezcorp 0:879cf546d390 5 * Rewritten by Soren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
Tezcorp 0:879cf546d390 6 * Ported to mbed by Martin Olejar, Dec, 2013
Tezcorp 0:879cf546d390 7 *
Tezcorp 0:879cf546d390 8 * Please read this file for an overview and then MFRC522.cpp for comments on the specific functions.
Tezcorp 0:879cf546d390 9 * Search for "mf-rc522" on ebay.com to purchase the MF-RC522 board.
Tezcorp 0:879cf546d390 10 *
Tezcorp 0:879cf546d390 11 * There are three hardware components involved:
Tezcorp 0:879cf546d390 12 * 1) The micro controller: An Arduino
Tezcorp 0:879cf546d390 13 * 2) The PCD (short for Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
Tezcorp 0:879cf546d390 14 * 3) The PICC (short for Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
Tezcorp 0:879cf546d390 15 *
Tezcorp 0:879cf546d390 16 * The microcontroller and card reader uses SPI for communication.
Tezcorp 0:879cf546d390 17 * The protocol is described in the MFRC522 datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf
Tezcorp 0:879cf546d390 18 *
Tezcorp 0:879cf546d390 19 * The card reader and the tags communicate using a 13.56MHz electromagnetic field.
Tezcorp 0:879cf546d390 20 * The protocol is defined in ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision".
Tezcorp 0:879cf546d390 21 * A free version of the final draft can be found at http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf
Tezcorp 0:879cf546d390 22 * Details are found in chapter 6, Type A: Initialization and anticollision.
Tezcorp 0:879cf546d390 23 *
Tezcorp 0:879cf546d390 24 * If only the PICC UID is wanted, the above documents has all the needed information.
Tezcorp 0:879cf546d390 25 * To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected.
Tezcorp 0:879cf546d390 26 * The MIFARE Classic chips and protocol is described in the datasheets:
Tezcorp 0:879cf546d390 27 * 1K: http://www.nxp.com/documents/data_sheet/MF1S503x.pdf
Tezcorp 0:879cf546d390 28 * 4K: http://www.nxp.com/documents/data_sheet/MF1S703x.pdf
Tezcorp 0:879cf546d390 29 * Mini: http://www.idcardmarket.com/download/mifare_S20_datasheet.pdf
Tezcorp 0:879cf546d390 30 * The MIFARE Ultralight chip and protocol is described in the datasheets:
Tezcorp 0:879cf546d390 31 * Ultralight: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf
Tezcorp 0:879cf546d390 32 * Ultralight C: http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf
Tezcorp 0:879cf546d390 33 *
Tezcorp 0:879cf546d390 34 * MIFARE Classic 1K (MF1S503x):
Tezcorp 0:879cf546d390 35 * Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
Tezcorp 0:879cf546d390 36 * The blocks are numbered 0-63.
Tezcorp 0:879cf546d390 37 * Block 3 in each sector is the Sector Trailer. See http://www.nxp.com/documents/data_sheet/MF1S503x.pdf sections 8.6 and 8.7:
Tezcorp 0:879cf546d390 38 * Bytes 0-5: Key A
Tezcorp 0:879cf546d390 39 * Bytes 6-8: Access Bits
Tezcorp 0:879cf546d390 40 * Bytes 9: User data
Tezcorp 0:879cf546d390 41 * Bytes 10-15: Key B (or user data)
Tezcorp 0:879cf546d390 42 * Block 0 is read only manufacturer data.
Tezcorp 0:879cf546d390 43 * To access a block, an authentication using a key from the block's sector must be performed first.
Tezcorp 0:879cf546d390 44 * Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11).
Tezcorp 0:879cf546d390 45 * All keys are set to FFFFFFFFFFFFh at chip delivery.
Tezcorp 0:879cf546d390 46 * Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked.
Tezcorp 0:879cf546d390 47 * To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use PICC_SetAccessBits() to calculate the bit patterns.
Tezcorp 0:879cf546d390 48 * MIFARE Classic 4K (MF1S703x):
Tezcorp 0:879cf546d390 49 * Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
Tezcorp 0:879cf546d390 50 * The blocks are numbered 0-255.
Tezcorp 0:879cf546d390 51 * The last block in each sector is the Sector Trailer like above.
Tezcorp 0:879cf546d390 52 * MIFARE Classic Mini (MF1 IC S20):
Tezcorp 0:879cf546d390 53 * Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
Tezcorp 0:879cf546d390 54 * The blocks are numbered 0-19.
Tezcorp 0:879cf546d390 55 * The last block in each sector is the Sector Trailer like above.
Tezcorp 0:879cf546d390 56 *
Tezcorp 0:879cf546d390 57 * MIFARE Ultralight (MF0ICU1):
Tezcorp 0:879cf546d390 58 * Has 16 pages of 4 bytes = 64 bytes.
Tezcorp 0:879cf546d390 59 * Pages 0 + 1 is used for the 7-byte UID.
Tezcorp 0:879cf546d390 60 * Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
Tezcorp 0:879cf546d390 61 * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
Tezcorp 0:879cf546d390 62 * Pages 4-15 are read/write unless blocked by the lock bytes in page 2.
Tezcorp 0:879cf546d390 63 * MIFARE Ultralight C (MF0ICU2):
Tezcorp 0:879cf546d390 64 * Has 48 pages of 4 bytes = 64 bytes.
Tezcorp 0:879cf546d390 65 * Pages 0 + 1 is used for the 7-byte UID.
Tezcorp 0:879cf546d390 66 * Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
Tezcorp 0:879cf546d390 67 * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
Tezcorp 0:879cf546d390 68 * Pages 4-39 are read/write unless blocked by the lock bytes in page 2.
Tezcorp 0:879cf546d390 69 * Page 40 Lock bytes
Tezcorp 0:879cf546d390 70 * Page 41 16 bit one way counter
Tezcorp 0:879cf546d390 71 * Pages 42-43 Authentication configuration
Tezcorp 0:879cf546d390 72 * Pages 44-47 Authentication key
Tezcorp 0:879cf546d390 73 */
Tezcorp 0:879cf546d390 74 #ifndef MFRC522_h
Tezcorp 0:879cf546d390 75 #define MFRC522_h
Tezcorp 0:879cf546d390 76
Tezcorp 0:879cf546d390 77 #include "mbed.h"
Tezcorp 0:879cf546d390 78
Tezcorp 0:879cf546d390 79 /**
Tezcorp 0:879cf546d390 80 * MFRC522 example
Tezcorp 0:879cf546d390 81 *
Tezcorp 0:879cf546d390 82 * @code
Tezcorp 0:879cf546d390 83 * #include "mbed.h"
Tezcorp 0:879cf546d390 84 * #include "MFRC522.h"
Tezcorp 0:879cf546d390 85 *
Tezcorp 0:879cf546d390 86 * //KL25Z Pins for MFRC522 SPI interface
Tezcorp 0:879cf546d390 87 * #define SPI_MOSI PTC6
Tezcorp 0:879cf546d390 88 * #define SPI_MISO PTC7
Tezcorp 0:879cf546d390 89 * #define SPI_SCLK PTC5
Tezcorp 0:879cf546d390 90 * #define SPI_CS PTC4
Tezcorp 0:879cf546d390 91 * // KL25Z Pin for MFRC522 reset
Tezcorp 0:879cf546d390 92 * #define MF_RESET PTC3
Tezcorp 0:879cf546d390 93 * // KL25Z Pins for Debug UART port
Tezcorp 0:879cf546d390 94 * #define UART_RX PTA1
Tezcorp 0:879cf546d390 95 * #define UART_TX PTA2
Tezcorp 0:879cf546d390 96 *
Tezcorp 0:879cf546d390 97 * DigitalOut LedRed (LED_RED);
Tezcorp 0:879cf546d390 98 * DigitalOut LedGreen (LED_GREEN);
Tezcorp 0:879cf546d390 99 *
Tezcorp 0:879cf546d390 100 * Serial DebugUART(UART_TX, UART_RX);
Tezcorp 0:879cf546d390 101 * MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
Tezcorp 0:879cf546d390 102 *
Tezcorp 0:879cf546d390 103 * int main(void) {
Tezcorp 0:879cf546d390 104 * // Set debug UART speed
Tezcorp 0:879cf546d390 105 * DebugUART.baud(115200);
Tezcorp 0:879cf546d390 106 *
Tezcorp 0:879cf546d390 107 * // Init. RC522 Chip
Tezcorp 0:879cf546d390 108 * RfChip.PCD_Init();
Tezcorp 0:879cf546d390 109 *
Tezcorp 0:879cf546d390 110 * while (true) {
Tezcorp 0:879cf546d390 111 * LedRed = 1;
Tezcorp 0:879cf546d390 112 * LedGreen = 1;
Tezcorp 0:879cf546d390 113 *
Tezcorp 0:879cf546d390 114 * // Look for new cards
Tezcorp 0:879cf546d390 115 * if ( ! RfChip.PICC_IsNewCardPresent())
Tezcorp 0:879cf546d390 116 * {
Tezcorp 0:879cf546d390 117 * wait_ms(500);
Tezcorp 0:879cf546d390 118 * continue;
Tezcorp 0:879cf546d390 119 * }
Tezcorp 0:879cf546d390 120 *
Tezcorp 0:879cf546d390 121 * LedRed = 0;
Tezcorp 0:879cf546d390 122 *
Tezcorp 0:879cf546d390 123 * // Select one of the cards
Tezcorp 0:879cf546d390 124 * if ( ! RfChip.PICC_ReadCardSerial())
Tezcorp 0:879cf546d390 125 * {
Tezcorp 0:879cf546d390 126 * wait_ms(500);
Tezcorp 0:879cf546d390 127 * continue;
Tezcorp 0:879cf546d390 128 * }
Tezcorp 0:879cf546d390 129 *
Tezcorp 0:879cf546d390 130 * LedRed = 1;
Tezcorp 0:879cf546d390 131 * LedGreen = 0;
Tezcorp 0:879cf546d390 132 *
Tezcorp 0:879cf546d390 133 * // Print Card UID
Tezcorp 0:879cf546d390 134 * printf("Card UID: ");
Tezcorp 0:879cf546d390 135 * for (uint8_t i = 0; i < RfChip.uid.size; i++)
Tezcorp 0:879cf546d390 136 * {
Tezcorp 0:879cf546d390 137 * printf(" %X02", RfChip.uid.uidByte[i]);
Tezcorp 0:879cf546d390 138 * }
Tezcorp 0:879cf546d390 139 * printf("\n\r");
Tezcorp 0:879cf546d390 140 *
Tezcorp 0:879cf546d390 141 * // Print Card type
Tezcorp 0:879cf546d390 142 * uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Tezcorp 0:879cf546d390 143 * printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Tezcorp 0:879cf546d390 144 * wait_ms(1000);
Tezcorp 0:879cf546d390 145 * }
Tezcorp 0:879cf546d390 146 * }
Tezcorp 0:879cf546d390 147 * @endcode
Tezcorp 0:879cf546d390 148 */
Tezcorp 0:879cf546d390 149
Tezcorp 0:879cf546d390 150 class MFRC522 {
Tezcorp 0:879cf546d390 151 public:
Tezcorp 0:879cf546d390 152
Tezcorp 0:879cf546d390 153 /**
Tezcorp 0:879cf546d390 154 * MFRC522 registers (described in chapter 9 of the datasheet).
Tezcorp 0:879cf546d390 155 * When using SPI all addresses are shifted one bit left in the "SPI address byte" (section 8.1.2.3)
Tezcorp 0:879cf546d390 156 */
Tezcorp 0:879cf546d390 157 enum PCD_Register {
Tezcorp 0:879cf546d390 158 // Page 0: Command and status
Tezcorp 0:879cf546d390 159 // 0x00 // reserved for future use
Tezcorp 0:879cf546d390 160 CommandReg = 0x01 << 1, // starts and stops command execution
Tezcorp 0:879cf546d390 161 ComIEnReg = 0x02 << 1, // enable and disable interrupt request control bits
Tezcorp 0:879cf546d390 162 DivIEnReg = 0x03 << 1, // enable and disable interrupt request control bits
Tezcorp 0:879cf546d390 163 ComIrqReg = 0x04 << 1, // interrupt request bits
Tezcorp 0:879cf546d390 164 DivIrqReg = 0x05 << 1, // interrupt request bits
Tezcorp 0:879cf546d390 165 ErrorReg = 0x06 << 1, // error bits showing the error status of the last command executed
Tezcorp 0:879cf546d390 166 Status1Reg = 0x07 << 1, // communication status bits
Tezcorp 0:879cf546d390 167 Status2Reg = 0x08 << 1, // receiver and transmitter status bits
Tezcorp 0:879cf546d390 168 FIFODataReg = 0x09 << 1, // input and output of 64 byte FIFO buffer
Tezcorp 0:879cf546d390 169 FIFOLevelReg = 0x0A << 1, // number of bytes stored in the FIFO buffer
Tezcorp 0:879cf546d390 170 WaterLevelReg = 0x0B << 1, // level for FIFO underflow and overflow warning
Tezcorp 0:879cf546d390 171 ControlReg = 0x0C << 1, // miscellaneous control registers
Tezcorp 0:879cf546d390 172 BitFramingReg = 0x0D << 1, // adjustments for bit-oriented frames
Tezcorp 0:879cf546d390 173 CollReg = 0x0E << 1, // bit position of the first bit-collision detected on the RF interface
Tezcorp 0:879cf546d390 174 // 0x0F // reserved for future use
Tezcorp 0:879cf546d390 175
Tezcorp 0:879cf546d390 176 // Page 1:Command
Tezcorp 0:879cf546d390 177 // 0x10 // reserved for future use
Tezcorp 0:879cf546d390 178 ModeReg = 0x11 << 1, // defines general modes for transmitting and receiving
Tezcorp 0:879cf546d390 179 TxModeReg = 0x12 << 1, // defines transmission data rate and framing
Tezcorp 0:879cf546d390 180 RxModeReg = 0x13 << 1, // defines reception data rate and framing
Tezcorp 0:879cf546d390 181 TxControlReg = 0x14 << 1, // controls the logical behavior of the antenna driver pins TX1 and TX2
Tezcorp 0:879cf546d390 182 TxASKReg = 0x15 << 1, // controls the setting of the transmission modulation
Tezcorp 0:879cf546d390 183 TxSelReg = 0x16 << 1, // selects the internal sources for the antenna driver
Tezcorp 0:879cf546d390 184 RxSelReg = 0x17 << 1, // selects internal receiver settings
Tezcorp 0:879cf546d390 185 RxThresholdReg = 0x18 << 1, // selects thresholds for the bit decoder
Tezcorp 0:879cf546d390 186 DemodReg = 0x19 << 1, // defines demodulator settings
Tezcorp 0:879cf546d390 187 // 0x1A // reserved for future use
Tezcorp 0:879cf546d390 188 // 0x1B // reserved for future use
Tezcorp 0:879cf546d390 189 MfTxReg = 0x1C << 1, // controls some MIFARE communication transmit parameters
Tezcorp 0:879cf546d390 190 MfRxReg = 0x1D << 1, // controls some MIFARE communication receive parameters
Tezcorp 0:879cf546d390 191 // 0x1E // reserved for future use
Tezcorp 0:879cf546d390 192 SerialSpeedReg = 0x1F << 1, // selects the speed of the serial UART interface
Tezcorp 0:879cf546d390 193
Tezcorp 0:879cf546d390 194 // Page 2: Configuration
Tezcorp 0:879cf546d390 195 // 0x20 // reserved for future use
Tezcorp 0:879cf546d390 196 CRCResultRegH = 0x21 << 1, // shows the MSB and LSB values of the CRC calculation
Tezcorp 0:879cf546d390 197 CRCResultRegL = 0x22 << 1,
Tezcorp 0:879cf546d390 198 // 0x23 // reserved for future use
Tezcorp 0:879cf546d390 199 ModWidthReg = 0x24 << 1, // controls the ModWidth setting?
Tezcorp 0:879cf546d390 200 // 0x25 // reserved for future use
Tezcorp 0:879cf546d390 201 RFCfgReg = 0x26 << 1, // configures the receiver gain
Tezcorp 0:879cf546d390 202 GsNReg = 0x27 << 1, // selects the conductance of the antenna driver pins TX1 and TX2 for modulation
Tezcorp 0:879cf546d390 203 CWGsPReg = 0x28 << 1, // defines the conductance of the p-driver output during periods of no modulation
Tezcorp 0:879cf546d390 204 ModGsPReg = 0x29 << 1, // defines the conductance of the p-driver output during periods of modulation
Tezcorp 0:879cf546d390 205 TModeReg = 0x2A << 1, // defines settings for the internal timer
Tezcorp 0:879cf546d390 206 TPrescalerReg = 0x2B << 1, // the lower 8 bits of the TPrescaler value. The 4 high bits are in TModeReg.
Tezcorp 0:879cf546d390 207 TReloadRegH = 0x2C << 1, // defines the 16-bit timer reload value
Tezcorp 0:879cf546d390 208 TReloadRegL = 0x2D << 1,
Tezcorp 0:879cf546d390 209 TCntValueRegH = 0x2E << 1, // shows the 16-bit timer value
Tezcorp 0:879cf546d390 210 TCntValueRegL = 0x2F << 1,
Tezcorp 0:879cf546d390 211
Tezcorp 0:879cf546d390 212 // Page 3:Test Registers
Tezcorp 0:879cf546d390 213 // 0x30 // reserved for future use
Tezcorp 0:879cf546d390 214 TestSel1Reg = 0x31 << 1, // general test signal configuration
Tezcorp 0:879cf546d390 215 TestSel2Reg = 0x32 << 1, // general test signal configuration
Tezcorp 0:879cf546d390 216 TestPinEnReg = 0x33 << 1, // enables pin output driver on pins D1 to D7
Tezcorp 0:879cf546d390 217 TestPinValueReg = 0x34 << 1, // defines the values for D1 to D7 when it is used as an I/O bus
Tezcorp 0:879cf546d390 218 TestBusReg = 0x35 << 1, // shows the status of the internal test bus
Tezcorp 0:879cf546d390 219 AutoTestReg = 0x36 << 1, // controls the digital self test
Tezcorp 0:879cf546d390 220 VersionReg = 0x37 << 1, // shows the software version
Tezcorp 0:879cf546d390 221 AnalogTestReg = 0x38 << 1, // controls the pins AUX1 and AUX2
Tezcorp 0:879cf546d390 222 TestDAC1Reg = 0x39 << 1, // defines the test value for TestDAC1
Tezcorp 0:879cf546d390 223 TestDAC2Reg = 0x3A << 1, // defines the test value for TestDAC2
Tezcorp 0:879cf546d390 224 TestADCReg = 0x3B << 1 // shows the value of ADC I and Q channels
Tezcorp 0:879cf546d390 225 // 0x3C // reserved for production tests
Tezcorp 0:879cf546d390 226 // 0x3D // reserved for production tests
Tezcorp 0:879cf546d390 227 // 0x3E // reserved for production tests
Tezcorp 0:879cf546d390 228 // 0x3F // reserved for production tests
Tezcorp 0:879cf546d390 229 };
Tezcorp 0:879cf546d390 230
Tezcorp 0:879cf546d390 231 // MFRC522 commands Described in chapter 10 of the datasheet.
Tezcorp 0:879cf546d390 232 enum PCD_Command {
Tezcorp 0:879cf546d390 233 PCD_Idle = 0x00, // no action, cancels current command execution
Tezcorp 0:879cf546d390 234 PCD_Mem = 0x01, // stores 25 bytes into the internal buffer
Tezcorp 0:879cf546d390 235 PCD_GenerateRandomID = 0x02, // generates a 10-byte random ID number
Tezcorp 0:879cf546d390 236 PCD_CalcCRC = 0x03, // activates the CRC coprocessor or performs a self test
Tezcorp 0:879cf546d390 237 PCD_Transmit = 0x04, // transmits data from the FIFO buffer
Tezcorp 0:879cf546d390 238 PCD_NoCmdChange = 0x07, // no command change, can be used to modify the CommandReg register bits without affecting the command, for example, the PowerDown bit
Tezcorp 0:879cf546d390 239 PCD_Receive = 0x08, // activates the receiver circuits
Tezcorp 0:879cf546d390 240 PCD_Transceive = 0x0C, // transmits data from FIFO buffer to antenna and automatically activates the receiver after transmission
Tezcorp 0:879cf546d390 241 PCD_MFAuthent = 0x0E, // performs the MIFARE standard authentication as a reader
Tezcorp 0:879cf546d390 242 PCD_SoftReset = 0x0F // resets the MFRC522
Tezcorp 0:879cf546d390 243 };
Tezcorp 0:879cf546d390 244
Tezcorp 0:879cf546d390 245 // Commands sent to the PICC.
Tezcorp 0:879cf546d390 246 enum PICC_Command {
Tezcorp 0:879cf546d390 247 // The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4)
Tezcorp 0:879cf546d390 248 PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame.
Tezcorp 0:879cf546d390 249 PICC_CMD_WUPA = 0x52, // 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.
Tezcorp 0:879cf546d390 250 PICC_CMD_CT = 0x88, // Cascade Tag. Not really a command, but used during anti collision.
Tezcorp 0:879cf546d390 251 PICC_CMD_SEL_CL1 = 0x93, // Anti collision/Select, Cascade Level 1
Tezcorp 0:879cf546d390 252 PICC_CMD_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 1
Tezcorp 0:879cf546d390 253 PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 1
Tezcorp 0:879cf546d390 254 PICC_CMD_HLTA = 0x50, // HaLT command, Type A. Instructs an ACTIVE PICC to go to state HALT.
Tezcorp 0:879cf546d390 255
Tezcorp 0:879cf546d390 256 // The commands used for MIFARE Classic (from http://www.nxp.com/documents/data_sheet/MF1S503x.pdf, Section 9)
Tezcorp 0:879cf546d390 257 // Use PCD_MFAuthent to authenticate access to a sector, then use these commands to read/write/modify the blocks on the sector.
Tezcorp 0:879cf546d390 258 // The read/write commands can also be used for MIFARE Ultralight.
Tezcorp 0:879cf546d390 259 PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A
Tezcorp 0:879cf546d390 260 PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B
Tezcorp 0:879cf546d390 261 PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight.
Tezcorp 0:879cf546d390 262 PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight.
Tezcorp 0:879cf546d390 263 PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register.
Tezcorp 0:879cf546d390 264 PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register.
Tezcorp 0:879cf546d390 265 PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register.
Tezcorp 0:879cf546d390 266 PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block.
Tezcorp 0:879cf546d390 267
Tezcorp 0:879cf546d390 268 // The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6)
Tezcorp 0:879cf546d390 269 // The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
Tezcorp 0:879cf546d390 270 PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC.
Tezcorp 0:879cf546d390 271 };
Tezcorp 0:879cf546d390 272
Tezcorp 0:879cf546d390 273 // MIFARE constants that does not fit anywhere else
Tezcorp 0:879cf546d390 274 enum MIFARE_Misc {
Tezcorp 0:879cf546d390 275 MF_ACK = 0xA, // The MIFARE Classic uses a 4 bit ACK/NAK. Any other value than 0xA is NAK.
Tezcorp 0:879cf546d390 276 MF_KEY_SIZE = 6 // A Mifare Crypto1 key is 6 bytes.
Tezcorp 0:879cf546d390 277 };
Tezcorp 0:879cf546d390 278
Tezcorp 0:879cf546d390 279 // PICC types we can detect. Remember to update PICC_GetTypeName() if you add more.
Tezcorp 0:879cf546d390 280 enum PICC_Type {
Tezcorp 0:879cf546d390 281 PICC_TYPE_UNKNOWN = 0,
Tezcorp 0:879cf546d390 282 PICC_TYPE_ISO_14443_4 = 1, // PICC compliant with ISO/IEC 14443-4
Tezcorp 0:879cf546d390 283 PICC_TYPE_ISO_18092 = 2, // PICC compliant with ISO/IEC 18092 (NFC)
Tezcorp 0:879cf546d390 284 PICC_TYPE_MIFARE_MINI = 3, // MIFARE Classic protocol, 320 bytes
Tezcorp 0:879cf546d390 285 PICC_TYPE_MIFARE_1K = 4, // MIFARE Classic protocol, 1KB
Tezcorp 0:879cf546d390 286 PICC_TYPE_MIFARE_4K = 5, // MIFARE Classic protocol, 4KB
Tezcorp 0:879cf546d390 287 PICC_TYPE_MIFARE_UL = 6, // MIFARE Ultralight or Ultralight C
Tezcorp 0:879cf546d390 288 PICC_TYPE_MIFARE_PLUS = 7, // MIFARE Plus
Tezcorp 0:879cf546d390 289 PICC_TYPE_TNP3XXX = 8, // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure
Tezcorp 0:879cf546d390 290 PICC_TYPE_NOT_COMPLETE = 255 // SAK indicates UID is not complete.
Tezcorp 0:879cf546d390 291 };
Tezcorp 0:879cf546d390 292
Tezcorp 0:879cf546d390 293 // Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more.
Tezcorp 0:879cf546d390 294 enum StatusCode {
Tezcorp 0:879cf546d390 295 STATUS_OK = 1, // Success
Tezcorp 0:879cf546d390 296 STATUS_ERROR = 2, // Error in communication
Tezcorp 0:879cf546d390 297 STATUS_COLLISION = 3, // Collision detected
Tezcorp 0:879cf546d390 298 STATUS_TIMEOUT = 4, // Timeout in communication.
Tezcorp 0:879cf546d390 299 STATUS_NO_ROOM = 5, // A buffer is not big enough.
Tezcorp 0:879cf546d390 300 STATUS_INTERNAL_ERROR = 6, // Internal error in the code. Should not happen ;-)
Tezcorp 0:879cf546d390 301 STATUS_INVALID = 7, // Invalid argument.
Tezcorp 0:879cf546d390 302 STATUS_CRC_WRONG = 8, // The CRC_A does not match
Tezcorp 0:879cf546d390 303 STATUS_MIFARE_NACK = 9 // A MIFARE PICC responded with NAK.
Tezcorp 0:879cf546d390 304 };
Tezcorp 0:879cf546d390 305
Tezcorp 0:879cf546d390 306 // A struct used for passing the UID of a PICC.
Tezcorp 0:879cf546d390 307 typedef struct {
Tezcorp 0:879cf546d390 308 uint8_t size; // Number of bytes in the UID. 4, 7 or 10.
Tezcorp 0:879cf546d390 309 uint8_t uidByte[10];
Tezcorp 0:879cf546d390 310 uint8_t sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
Tezcorp 0:879cf546d390 311 } Uid;
Tezcorp 0:879cf546d390 312
Tezcorp 0:879cf546d390 313 // A struct used for passing a MIFARE Crypto1 key
Tezcorp 0:879cf546d390 314 typedef struct {
Tezcorp 0:879cf546d390 315 uint8_t keyByte[MF_KEY_SIZE];
Tezcorp 0:879cf546d390 316 } MIFARE_Key;
Tezcorp 0:879cf546d390 317
Tezcorp 0:879cf546d390 318 // Member variables
Tezcorp 0:879cf546d390 319 Uid uid; // Used by PICC_ReadCardSerial().
Tezcorp 0:879cf546d390 320
Tezcorp 0:879cf546d390 321 // Size of the MFRC522 FIFO
Tezcorp 0:879cf546d390 322 static const uint8_t FIFO_SIZE = 64; // The FIFO is 64 bytes.
Tezcorp 0:879cf546d390 323
Tezcorp 0:879cf546d390 324 /**
Tezcorp 0:879cf546d390 325 * MFRC522 constructor
Tezcorp 0:879cf546d390 326 *
Tezcorp 0:879cf546d390 327 * @param mosi SPI MOSI pin
Tezcorp 0:879cf546d390 328 * @param miso SPI MISO pin
Tezcorp 0:879cf546d390 329 * @param sclk SPI SCLK pin
Tezcorp 0:879cf546d390 330 * @param cs SPI CS pin
Tezcorp 0:879cf546d390 331 * @param reset Reset pin
Tezcorp 0:879cf546d390 332 */
Tezcorp 0:879cf546d390 333 MFRC522(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
Tezcorp 0:879cf546d390 334
Tezcorp 0:879cf546d390 335 /**
Tezcorp 0:879cf546d390 336 * MFRC522 destructor
Tezcorp 0:879cf546d390 337 */
Tezcorp 0:879cf546d390 338 ~MFRC522();
Tezcorp 0:879cf546d390 339
Tezcorp 0:879cf546d390 340
Tezcorp 0:879cf546d390 341 // ************************************************************************************
Tezcorp 0:879cf546d390 342 //! @name Functions for manipulating the MFRC522
Tezcorp 0:879cf546d390 343 // ************************************************************************************
Tezcorp 0:879cf546d390 344 //@{
Tezcorp 0:879cf546d390 345
Tezcorp 0:879cf546d390 346 /**
Tezcorp 0:879cf546d390 347 * Initializes the MFRC522 chip.
Tezcorp 0:879cf546d390 348 */
Tezcorp 0:879cf546d390 349 void PCD_Init (void);
Tezcorp 0:879cf546d390 350
Tezcorp 0:879cf546d390 351 /**
Tezcorp 0:879cf546d390 352 * Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
Tezcorp 0:879cf546d390 353 */
Tezcorp 0:879cf546d390 354 void PCD_Reset (void);
Tezcorp 0:879cf546d390 355
Tezcorp 0:879cf546d390 356 /**
Tezcorp 0:879cf546d390 357 * Turns the antenna on by enabling pins TX1 and TX2.
Tezcorp 0:879cf546d390 358 * After a reset these pins disabled.
Tezcorp 0:879cf546d390 359 */
Tezcorp 0:879cf546d390 360 void PCD_AntennaOn (void);
Tezcorp 0:879cf546d390 361
Tezcorp 0:879cf546d390 362 /**
Tezcorp 0:879cf546d390 363 * Writes a byte to the specified register in the MFRC522 chip.
Tezcorp 0:879cf546d390 364 * The interface is described in the datasheet section 8.1.2.
Tezcorp 0:879cf546d390 365 *
Tezcorp 0:879cf546d390 366 * @param reg The register to write to. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 367 * @param value The value to write.
Tezcorp 0:879cf546d390 368 */
Tezcorp 0:879cf546d390 369 void PCD_WriteRegister (uint8_t reg, uint8_t value);
Tezcorp 0:879cf546d390 370
Tezcorp 0:879cf546d390 371 /**
Tezcorp 0:879cf546d390 372 * Writes a number of bytes to the specified register in the MFRC522 chip.
Tezcorp 0:879cf546d390 373 * The interface is described in the datasheet section 8.1.2.
Tezcorp 0:879cf546d390 374 *
Tezcorp 0:879cf546d390 375 * @param reg The register to write to. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 376 * @param count The number of bytes to write to the register
Tezcorp 0:879cf546d390 377 * @param values The values to write. Byte array.
Tezcorp 0:879cf546d390 378 */
Tezcorp 0:879cf546d390 379 void PCD_WriteRegister (uint8_t reg, uint8_t count, uint8_t *values);
Tezcorp 0:879cf546d390 380
Tezcorp 0:879cf546d390 381 /**
Tezcorp 0:879cf546d390 382 * Reads a byte from the specified register in the MFRC522 chip.
Tezcorp 0:879cf546d390 383 * The interface is described in the datasheet section 8.1.2.
Tezcorp 0:879cf546d390 384 *
Tezcorp 0:879cf546d390 385 * @param reg The register to read from. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 386 * @returns Register value
Tezcorp 0:879cf546d390 387 */
Tezcorp 0:879cf546d390 388 uint8_t PCD_ReadRegister (uint8_t reg);
Tezcorp 0:879cf546d390 389
Tezcorp 0:879cf546d390 390 /**
Tezcorp 0:879cf546d390 391 * Reads a number of bytes from the specified register in the MFRC522 chip.
Tezcorp 0:879cf546d390 392 * The interface is described in the datasheet section 8.1.2.
Tezcorp 0:879cf546d390 393 *
Tezcorp 0:879cf546d390 394 * @param reg The register to read from. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 395 * @param count The number of bytes to read.
Tezcorp 0:879cf546d390 396 * @param values Byte array to store the values in.
Tezcorp 0:879cf546d390 397 * @param rxAlign Only bit positions rxAlign..7 in values[0] are updated.
Tezcorp 0:879cf546d390 398 */
Tezcorp 0:879cf546d390 399 void PCD_ReadRegister (uint8_t reg, uint8_t count, uint8_t *values, uint8_t rxAlign = 0);
Tezcorp 0:879cf546d390 400
Tezcorp 0:879cf546d390 401 /**
Tezcorp 0:879cf546d390 402 * Sets the bits given in mask in register reg.
Tezcorp 0:879cf546d390 403 *
Tezcorp 0:879cf546d390 404 * @param reg The register to update. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 405 * @param mask The bits to set.
Tezcorp 0:879cf546d390 406 */
Tezcorp 0:879cf546d390 407 void PCD_SetRegisterBits(uint8_t reg, uint8_t mask);
Tezcorp 0:879cf546d390 408
Tezcorp 0:879cf546d390 409 /**
Tezcorp 0:879cf546d390 410 * Clears the bits given in mask from register reg.
Tezcorp 0:879cf546d390 411 *
Tezcorp 0:879cf546d390 412 * @param reg The register to update. One of the PCD_Register enums.
Tezcorp 0:879cf546d390 413 * @param mask The bits to clear.
Tezcorp 0:879cf546d390 414 */
Tezcorp 0:879cf546d390 415 void PCD_ClrRegisterBits(uint8_t reg, uint8_t mask);
Tezcorp 0:879cf546d390 416
Tezcorp 0:879cf546d390 417 /**
Tezcorp 0:879cf546d390 418 * Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
Tezcorp 0:879cf546d390 419 *
Tezcorp 0:879cf546d390 420 * @param data Pointer to the data to transfer to the FIFO for CRC calculation.
Tezcorp 0:879cf546d390 421 * @param length The number of bytes to transfer.
Tezcorp 0:879cf546d390 422 * @param result Pointer to result buffer. Result is written to result[0..1], low byte first.
Tezcorp 0:879cf546d390 423 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 424 */
Tezcorp 0:879cf546d390 425 uint8_t PCD_CalculateCRC (uint8_t *data, uint8_t length, uint8_t *result);
Tezcorp 0:879cf546d390 426
Tezcorp 0:879cf546d390 427 /**
Tezcorp 0:879cf546d390 428 * Executes the Transceive command.
Tezcorp 0:879cf546d390 429 * CRC validation can only be done if backData and backLen are specified.
Tezcorp 0:879cf546d390 430 *
Tezcorp 0:879cf546d390 431 * @param sendData Pointer to the data to transfer to the FIFO.
Tezcorp 0:879cf546d390 432 * @param sendLen Number of bytes to transfer to the FIFO.
Tezcorp 0:879cf546d390 433 * @param backData NULL or pointer to buffer if data should be read back after executing the command.
Tezcorp 0:879cf546d390 434 * @param backLen Max number of bytes to write to *backData. Out: The number of bytes returned.
Tezcorp 0:879cf546d390 435 * @param validBits The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL.
Tezcorp 0:879cf546d390 436 * @param rxAlign Defines the bit position in backData[0] for the first bit received. Default 0.
Tezcorp 0:879cf546d390 437 * @param checkCRC True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
Tezcorp 0:879cf546d390 438 *
Tezcorp 0:879cf546d390 439 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 440 */
Tezcorp 0:879cf546d390 441 uint8_t PCD_TransceiveData (uint8_t *sendData,
Tezcorp 0:879cf546d390 442 uint8_t sendLen,
Tezcorp 0:879cf546d390 443 uint8_t *backData,
Tezcorp 0:879cf546d390 444 uint8_t *backLen,
Tezcorp 0:879cf546d390 445 uint8_t *validBits = NULL,
Tezcorp 0:879cf546d390 446 uint8_t rxAlign = 0,
Tezcorp 0:879cf546d390 447 bool checkCRC = false);
Tezcorp 0:879cf546d390 448
Tezcorp 0:879cf546d390 449
Tezcorp 0:879cf546d390 450 /**
Tezcorp 0:879cf546d390 451 * Transfers data to the MFRC522 FIFO, executes a commend, waits for completion and transfers data back from the FIFO.
Tezcorp 0:879cf546d390 452 * CRC validation can only be done if backData and backLen are specified.
Tezcorp 0:879cf546d390 453 *
Tezcorp 0:879cf546d390 454 * @param command The command to execute. One of the PCD_Command enums.
Tezcorp 0:879cf546d390 455 * @param waitIRq The bits in the ComIrqReg register that signals successful completion of the command.
Tezcorp 0:879cf546d390 456 * @param sendData Pointer to the data to transfer to the FIFO.
Tezcorp 0:879cf546d390 457 * @param sendLen Number of bytes to transfer to the FIFO.
Tezcorp 0:879cf546d390 458 * @param backData NULL or pointer to buffer if data should be read back after executing the command.
Tezcorp 0:879cf546d390 459 * @param backLen In: Max number of bytes to write to *backData. Out: The number of bytes returned.
Tezcorp 0:879cf546d390 460 * @param validBits In/Out: The number of valid bits in the last byte. 0 for 8 valid bits.
Tezcorp 0:879cf546d390 461 * @param rxAlign In: Defines the bit position in backData[0] for the first bit received. Default 0.
Tezcorp 0:879cf546d390 462 * @param checkCRC In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
Tezcorp 0:879cf546d390 463 *
Tezcorp 0:879cf546d390 464 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 465 */
Tezcorp 0:879cf546d390 466 uint8_t PCD_CommunicateWithPICC(uint8_t command,
Tezcorp 0:879cf546d390 467 uint8_t waitIRq,
Tezcorp 0:879cf546d390 468 uint8_t *sendData,
Tezcorp 0:879cf546d390 469 uint8_t sendLen,
Tezcorp 0:879cf546d390 470 uint8_t *backData = NULL,
Tezcorp 0:879cf546d390 471 uint8_t *backLen = NULL,
Tezcorp 0:879cf546d390 472 uint8_t *validBits = NULL,
Tezcorp 0:879cf546d390 473 uint8_t rxAlign = 0,
Tezcorp 0:879cf546d390 474 bool checkCRC = false);
Tezcorp 0:879cf546d390 475
Tezcorp 0:879cf546d390 476 /**
Tezcorp 0:879cf546d390 477 * Transmits a REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame.
Tezcorp 0:879cf546d390 478 * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Tezcorp 0:879cf546d390 479 *
Tezcorp 0:879cf546d390 480 * @param bufferATQA The buffer to store the ATQA (Answer to request) in
Tezcorp 0:879cf546d390 481 * @param bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Tezcorp 0:879cf546d390 482 *
Tezcorp 0:879cf546d390 483 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 484 */
Tezcorp 0:879cf546d390 485 uint8_t PICC_RequestA (uint8_t *bufferATQA, uint8_t *bufferSize);
Tezcorp 0:879cf546d390 486
Tezcorp 0:879cf546d390 487 /**
Tezcorp 0:879cf546d390 488 * 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.
Tezcorp 0:879cf546d390 489 * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Tezcorp 0:879cf546d390 490 *
Tezcorp 0:879cf546d390 491 * @param bufferATQA The buffer to store the ATQA (Answer to request) in
Tezcorp 0:879cf546d390 492 * @param bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Tezcorp 0:879cf546d390 493 *
Tezcorp 0:879cf546d390 494 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 495 */
Tezcorp 0:879cf546d390 496 uint8_t PICC_WakeupA (uint8_t *bufferATQA, uint8_t *bufferSize);
Tezcorp 0:879cf546d390 497
Tezcorp 0:879cf546d390 498 /**
Tezcorp 0:879cf546d390 499 * Transmits REQA or WUPA commands.
Tezcorp 0:879cf546d390 500 * Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Tezcorp 0:879cf546d390 501 *
Tezcorp 0:879cf546d390 502 * @param command The command to send - PICC_CMD_REQA or PICC_CMD_WUPA
Tezcorp 0:879cf546d390 503 * @param bufferATQA The buffer to store the ATQA (Answer to request) in
Tezcorp 0:879cf546d390 504 * @param bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Tezcorp 0:879cf546d390 505 *
Tezcorp 0:879cf546d390 506 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 507 */
Tezcorp 0:879cf546d390 508 uint8_t PICC_REQA_or_WUPA (uint8_t command, uint8_t *bufferATQA, uint8_t *bufferSize);
Tezcorp 0:879cf546d390 509
Tezcorp 0:879cf546d390 510 /**
Tezcorp 0:879cf546d390 511 * Transmits SELECT/ANTICOLLISION commands to select a single PICC.
Tezcorp 0:879cf546d390 512 * Before calling this function the PICCs must be placed in the READY(*) state by calling PICC_RequestA() or PICC_WakeupA().
Tezcorp 0:879cf546d390 513 * On success:
Tezcorp 0:879cf546d390 514 * - The chosen PICC is in state ACTIVE(*) and all other PICCs have returned to state IDLE/HALT. (Figure 7 of the ISO/IEC 14443-3 draft.)
Tezcorp 0:879cf546d390 515 * - The UID size and value of the chosen PICC is returned in *uid along with the SAK.
Tezcorp 0:879cf546d390 516 *
Tezcorp 0:879cf546d390 517 * A PICC UID consists of 4, 7 or 10 bytes.
Tezcorp 0:879cf546d390 518 * Only 4 bytes can be specified in a SELECT command, so for the longer UIDs two or three iterations are used:
Tezcorp 0:879cf546d390 519 *
Tezcorp 0:879cf546d390 520 * UID size Number of UID bytes Cascade levels Example of PICC
Tezcorp 0:879cf546d390 521 * ======== =================== ============== ===============
Tezcorp 0:879cf546d390 522 * single 4 1 MIFARE Classic
Tezcorp 0:879cf546d390 523 * double 7 2 MIFARE Ultralight
Tezcorp 0:879cf546d390 524 * triple 10 3 Not currently in use?
Tezcorp 0:879cf546d390 525 *
Tezcorp 0:879cf546d390 526 *
Tezcorp 0:879cf546d390 527 * @param uid Pointer to Uid struct. Normally output, but can also be used to supply a known UID.
Tezcorp 0:879cf546d390 528 * @param validBits The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size.
Tezcorp 0:879cf546d390 529 *
Tezcorp 0:879cf546d390 530 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 531 */
Tezcorp 0:879cf546d390 532 uint8_t PICC_Select (Uid *uid, uint8_t validBits = 0);
Tezcorp 0:879cf546d390 533
Tezcorp 0:879cf546d390 534 /**
Tezcorp 0:879cf546d390 535 * Instructs a PICC in state ACTIVE(*) to go to state HALT.
Tezcorp 0:879cf546d390 536 *
Tezcorp 0:879cf546d390 537 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 538 */
Tezcorp 0:879cf546d390 539 uint8_t PICC_HaltA (void);
Tezcorp 0:879cf546d390 540
Tezcorp 0:879cf546d390 541 // ************************************************************************************
Tezcorp 0:879cf546d390 542 //@}
Tezcorp 0:879cf546d390 543
Tezcorp 0:879cf546d390 544
Tezcorp 0:879cf546d390 545 // ************************************************************************************
Tezcorp 0:879cf546d390 546 //! @name Functions for communicating with MIFARE PICCs
Tezcorp 0:879cf546d390 547 // ************************************************************************************
Tezcorp 0:879cf546d390 548 //@{
Tezcorp 0:879cf546d390 549
Tezcorp 0:879cf546d390 550 /**
Tezcorp 0:879cf546d390 551 * Executes the MFRC522 MFAuthent command.
Tezcorp 0:879cf546d390 552 * This command manages MIFARE authentication to enable a secure communication to any MIFARE Mini, MIFARE 1K and MIFARE 4K card.
Tezcorp 0:879cf546d390 553 * The authentication is described in the MFRC522 datasheet section 10.3.1.9 and http://www.nxp.com/documents/data_sheet/MF1S503x.pdf section 10.1.
Tezcorp 0:879cf546d390 554 * For use with MIFARE Classic PICCs.
Tezcorp 0:879cf546d390 555 * The PICC must be selected - ie in state ACTIVE(*) - before calling this function.
Tezcorp 0:879cf546d390 556 * Remember to call PCD_StopCrypto1() after communicating with the authenticated PICC - otherwise no new communications can start.
Tezcorp 0:879cf546d390 557 *
Tezcorp 0:879cf546d390 558 * All keys are set to FFFFFFFFFFFFh at chip delivery.
Tezcorp 0:879cf546d390 559 *
Tezcorp 0:879cf546d390 560 * @param command PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B
Tezcorp 0:879cf546d390 561 * @param blockAddr The block number. See numbering in the comments in the .h file.
Tezcorp 0:879cf546d390 562 * @param key Pointer to the Crypteo1 key to use (6 bytes)
Tezcorp 0:879cf546d390 563 * @param uid Pointer to Uid struct. The first 4 bytes of the UID is used.
Tezcorp 0:879cf546d390 564 *
Tezcorp 0:879cf546d390 565 * @return STATUS_OK on success, STATUS_??? otherwise. Probably STATUS_TIMEOUT if you supply the wrong key.
Tezcorp 0:879cf546d390 566 */
Tezcorp 0:879cf546d390 567 uint8_t PCD_Authenticate (uint8_t command, uint8_t blockAddr, MIFARE_Key *key, Uid *uid);
Tezcorp 0:879cf546d390 568
Tezcorp 0:879cf546d390 569 /**
Tezcorp 0:879cf546d390 570 * Used to exit the PCD from its authenticated state.
Tezcorp 0:879cf546d390 571 * Remember to call this function after communicating with an authenticated PICC - otherwise no new communications can start.
Tezcorp 0:879cf546d390 572 */
Tezcorp 0:879cf546d390 573 void PCD_StopCrypto1 (void);
Tezcorp 0:879cf546d390 574
Tezcorp 0:879cf546d390 575 /**
Tezcorp 0:879cf546d390 576 * Reads 16 bytes (+ 2 bytes CRC_A) from the active PICC.
Tezcorp 0:879cf546d390 577 *
Tezcorp 0:879cf546d390 578 * For MIFARE Classic the sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 579 *
Tezcorp 0:879cf546d390 580 * For MIFARE Ultralight only addresses 00h to 0Fh are decoded.
Tezcorp 0:879cf546d390 581 * The MF0ICU1 returns a NAK for higher addresses.
Tezcorp 0:879cf546d390 582 * The MF0ICU1 responds to the READ command by sending 16 bytes starting from the page address defined by the command argument.
Tezcorp 0:879cf546d390 583 * For example; if blockAddr is 03h then pages 03h, 04h, 05h, 06h are returned.
Tezcorp 0:879cf546d390 584 * A roll-back is implemented: If blockAddr is 0Eh, then the contents of pages 0Eh, 0Fh, 00h and 01h are returned.
Tezcorp 0:879cf546d390 585 *
Tezcorp 0:879cf546d390 586 * The buffer must be at least 18 bytes because a CRC_A is also returned.
Tezcorp 0:879cf546d390 587 * Checks the CRC_A before returning STATUS_OK.
Tezcorp 0:879cf546d390 588 *
Tezcorp 0:879cf546d390 589 * @param blockAddr MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.
Tezcorp 0:879cf546d390 590 * @param buffer The buffer to store the data in
Tezcorp 0:879cf546d390 591 * @param bufferSize Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK.
Tezcorp 0:879cf546d390 592 *
Tezcorp 0:879cf546d390 593 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 594 */
Tezcorp 0:879cf546d390 595 uint8_t MIFARE_Read (uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize);
Tezcorp 0:879cf546d390 596
Tezcorp 0:879cf546d390 597 /**
Tezcorp 0:879cf546d390 598 * Writes 16 bytes to the active PICC.
Tezcorp 0:879cf546d390 599 *
Tezcorp 0:879cf546d390 600 * For MIFARE Classic the sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 601 *
Tezcorp 0:879cf546d390 602 * For MIFARE Ultralight the opretaion is called "COMPATIBILITY WRITE".
Tezcorp 0:879cf546d390 603 * Even though 16 bytes are transferred to the Ultralight PICC, only the least significant 4 bytes (bytes 0 to 3)
Tezcorp 0:879cf546d390 604 * are written to the specified address. It is recommended to set the remaining bytes 04h to 0Fh to all logic 0.
Tezcorp 0:879cf546d390 605 *
Tezcorp 0:879cf546d390 606 * @param blockAddr MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to.
Tezcorp 0:879cf546d390 607 * @param buffer The 16 bytes to write to the PICC
Tezcorp 0:879cf546d390 608 * @param bufferSize Buffer size, must be at least 16 bytes. Exactly 16 bytes are written.
Tezcorp 0:879cf546d390 609 *
Tezcorp 0:879cf546d390 610 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 611 */
Tezcorp 0:879cf546d390 612 uint8_t MIFARE_Write (uint8_t blockAddr, uint8_t *buffer, uint8_t bufferSize);
Tezcorp 0:879cf546d390 613
Tezcorp 0:879cf546d390 614 /**
Tezcorp 0:879cf546d390 615 * Writes a 4 byte page to the active MIFARE Ultralight PICC.
Tezcorp 0:879cf546d390 616 *
Tezcorp 0:879cf546d390 617 * @param page The page (2-15) to write to.
Tezcorp 0:879cf546d390 618 * @param buffer The 4 bytes to write to the PICC
Tezcorp 0:879cf546d390 619 * @param bufferSize Buffer size, must be at least 4 bytes. Exactly 4 bytes are written.
Tezcorp 0:879cf546d390 620 *
Tezcorp 0:879cf546d390 621 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 622 */
Tezcorp 0:879cf546d390 623 uint8_t MIFARE_UltralightWrite(uint8_t page, uint8_t *buffer, uint8_t bufferSize);
Tezcorp 0:879cf546d390 624
Tezcorp 0:879cf546d390 625 /**
Tezcorp 0:879cf546d390 626 * MIFARE Decrement subtracts the delta from the value of the addressed block, and stores the result in a volatile memory.
Tezcorp 0:879cf546d390 627 * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 628 * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001].
Tezcorp 0:879cf546d390 629 * Use MIFARE_Transfer() to store the result in a block.
Tezcorp 0:879cf546d390 630 *
Tezcorp 0:879cf546d390 631 * @param blockAddr The block (0-0xff) number.
Tezcorp 0:879cf546d390 632 * @param delta This number is subtracted from the value of block blockAddr.
Tezcorp 0:879cf546d390 633 *
Tezcorp 0:879cf546d390 634 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 635 */
Tezcorp 0:879cf546d390 636 uint8_t MIFARE_Decrement (uint8_t blockAddr, uint32_t delta);
Tezcorp 0:879cf546d390 637
Tezcorp 0:879cf546d390 638 /**
Tezcorp 0:879cf546d390 639 * MIFARE Increment adds the delta to the value of the addressed block, and stores the result in a volatile memory.
Tezcorp 0:879cf546d390 640 * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 641 * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001].
Tezcorp 0:879cf546d390 642 * Use MIFARE_Transfer() to store the result in a block.
Tezcorp 0:879cf546d390 643 *
Tezcorp 0:879cf546d390 644 * @param blockAddr The block (0-0xff) number.
Tezcorp 0:879cf546d390 645 * @param delta This number is added to the value of block blockAddr.
Tezcorp 0:879cf546d390 646 *
Tezcorp 0:879cf546d390 647 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 648 */
Tezcorp 0:879cf546d390 649 uint8_t MIFARE_Increment (uint8_t blockAddr, uint32_t delta);
Tezcorp 0:879cf546d390 650
Tezcorp 0:879cf546d390 651 /**
Tezcorp 0:879cf546d390 652 * MIFARE Restore copies the value of the addressed block into a volatile memory.
Tezcorp 0:879cf546d390 653 * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 654 * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001].
Tezcorp 0:879cf546d390 655 * Use MIFARE_Transfer() to store the result in a block.
Tezcorp 0:879cf546d390 656 *
Tezcorp 0:879cf546d390 657 * @param blockAddr The block (0-0xff) number.
Tezcorp 0:879cf546d390 658 *
Tezcorp 0:879cf546d390 659 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 660 */
Tezcorp 0:879cf546d390 661 uint8_t MIFARE_Restore (uint8_t blockAddr);
Tezcorp 0:879cf546d390 662
Tezcorp 0:879cf546d390 663 /**
Tezcorp 0:879cf546d390 664 * MIFARE Transfer writes the value stored in the volatile memory into one MIFARE Classic block.
Tezcorp 0:879cf546d390 665 * For MIFARE Classic only. The sector containing the block must be authenticated before calling this function.
Tezcorp 0:879cf546d390 666 * Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001].
Tezcorp 0:879cf546d390 667 *
Tezcorp 0:879cf546d390 668 * @param blockAddr The block (0-0xff) number.
Tezcorp 0:879cf546d390 669 *
Tezcorp 0:879cf546d390 670 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 671 */
Tezcorp 0:879cf546d390 672 uint8_t MIFARE_Transfer (uint8_t blockAddr);
Tezcorp 0:879cf546d390 673
Tezcorp 0:879cf546d390 674 // ************************************************************************************
Tezcorp 0:879cf546d390 675 //@}
Tezcorp 0:879cf546d390 676
Tezcorp 0:879cf546d390 677
Tezcorp 0:879cf546d390 678 // ************************************************************************************
Tezcorp 0:879cf546d390 679 //! @name Support functions
Tezcorp 0:879cf546d390 680 // ************************************************************************************
Tezcorp 0:879cf546d390 681 //@{
Tezcorp 0:879cf546d390 682
Tezcorp 0:879cf546d390 683 /**
Tezcorp 0:879cf546d390 684 * Wrapper for MIFARE protocol communication.
Tezcorp 0:879cf546d390 685 * Adds CRC_A, executes the Transceive command and checks that the response is MF_ACK or a timeout.
Tezcorp 0:879cf546d390 686 *
Tezcorp 0:879cf546d390 687 * @param sendData Pointer to the data to transfer to the FIFO. Do NOT include the CRC_A.
Tezcorp 0:879cf546d390 688 * @param sendLen Number of bytes in sendData.
Tezcorp 0:879cf546d390 689 * @param acceptTimeout True => A timeout is also success
Tezcorp 0:879cf546d390 690 *
Tezcorp 0:879cf546d390 691 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 692 */
Tezcorp 0:879cf546d390 693 uint8_t PCD_MIFARE_Transceive(uint8_t *sendData, uint8_t sendLen, bool acceptTimeout = false);
Tezcorp 0:879cf546d390 694
Tezcorp 0:879cf546d390 695 /**
Tezcorp 0:879cf546d390 696 * Translates the SAK (Select Acknowledge) to a PICC type.
Tezcorp 0:879cf546d390 697 *
Tezcorp 0:879cf546d390 698 * @param sak The SAK byte returned from PICC_Select().
Tezcorp 0:879cf546d390 699 *
Tezcorp 0:879cf546d390 700 * @return PICC_Type
Tezcorp 0:879cf546d390 701 */
Tezcorp 0:879cf546d390 702 uint8_t PICC_GetType (uint8_t sak);
Tezcorp 0:879cf546d390 703
Tezcorp 0:879cf546d390 704 /**
Tezcorp 0:879cf546d390 705 * Returns a string pointer to the PICC type name.
Tezcorp 0:879cf546d390 706 *
Tezcorp 0:879cf546d390 707 * @param type One of the PICC_Type enums.
Tezcorp 0:879cf546d390 708 *
Tezcorp 0:879cf546d390 709 * @return A string pointer to the PICC type name.
Tezcorp 0:879cf546d390 710 */
Tezcorp 0:879cf546d390 711 char* PICC_GetTypeName (uint8_t type);
Tezcorp 0:879cf546d390 712
Tezcorp 0:879cf546d390 713 /**
Tezcorp 0:879cf546d390 714 * Returns a string pointer to a status code name.
Tezcorp 0:879cf546d390 715 *
Tezcorp 0:879cf546d390 716 * @param code One of the StatusCode enums.
Tezcorp 0:879cf546d390 717 *
Tezcorp 0:879cf546d390 718 * @return A string pointer to a status code name.
Tezcorp 0:879cf546d390 719 */
Tezcorp 0:879cf546d390 720 char* GetStatusCodeName (uint8_t code);
Tezcorp 0:879cf546d390 721
Tezcorp 0:879cf546d390 722 /**
Tezcorp 0:879cf546d390 723 * 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).
Tezcorp 0:879cf546d390 724 *
Tezcorp 0:879cf546d390 725 * @param accessBitBuffer Pointer to byte 6, 7 and 8 in the sector trailer. Bytes [0..2] will be set.
Tezcorp 0:879cf546d390 726 * @param g0 Access bits [C1 C2 C3] for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39)
Tezcorp 0:879cf546d390 727 * @param g1 Access bits [C1 C2 C3] for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39)
Tezcorp 0:879cf546d390 728 * @param g2 Access bits [C1 C2 C3] for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39)
Tezcorp 0:879cf546d390 729 * @param g3 Access bits [C1 C2 C3] for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39)
Tezcorp 0:879cf546d390 730 */
Tezcorp 0:879cf546d390 731 void MIFARE_SetAccessBits (uint8_t *accessBitBuffer,
Tezcorp 0:879cf546d390 732 uint8_t g0,
Tezcorp 0:879cf546d390 733 uint8_t g1,
Tezcorp 0:879cf546d390 734 uint8_t g2,
Tezcorp 0:879cf546d390 735 uint8_t g3);
Tezcorp 0:879cf546d390 736
Tezcorp 0:879cf546d390 737 // ************************************************************************************
Tezcorp 0:879cf546d390 738 //@}
Tezcorp 0:879cf546d390 739
Tezcorp 0:879cf546d390 740
Tezcorp 0:879cf546d390 741 // ************************************************************************************
Tezcorp 0:879cf546d390 742 //! @name Convenience functions - does not add extra functionality
Tezcorp 0:879cf546d390 743 // ************************************************************************************
Tezcorp 0:879cf546d390 744 //@{
Tezcorp 0:879cf546d390 745
Tezcorp 0:879cf546d390 746 /**
Tezcorp 0:879cf546d390 747 * Returns true if a PICC responds to PICC_CMD_REQA.
Tezcorp 0:879cf546d390 748 * Only "new" cards in state IDLE are invited. Sleeping cards in state HALT are ignored.
Tezcorp 0:879cf546d390 749 *
Tezcorp 0:879cf546d390 750 * @return bool
Tezcorp 0:879cf546d390 751 */
Tezcorp 0:879cf546d390 752 bool PICC_IsNewCardPresent(void);
Tezcorp 0:879cf546d390 753
Tezcorp 0:879cf546d390 754 /**
Tezcorp 0:879cf546d390 755 * Simple wrapper around PICC_Select.
Tezcorp 0:879cf546d390 756 * Returns true if a UID could be read.
Tezcorp 0:879cf546d390 757 * Remember to call PICC_IsNewCardPresent(), PICC_RequestA() or PICC_WakeupA() first.
Tezcorp 0:879cf546d390 758 * The read UID is available in the class variable uid.
Tezcorp 0:879cf546d390 759 *
Tezcorp 0:879cf546d390 760 * @return bool
Tezcorp 0:879cf546d390 761 */
Tezcorp 0:879cf546d390 762 bool PICC_ReadCardSerial (void);
Tezcorp 0:879cf546d390 763
Tezcorp 0:879cf546d390 764 // ************************************************************************************
Tezcorp 0:879cf546d390 765 //@}
Tezcorp 0:879cf546d390 766
Tezcorp 0:879cf546d390 767
Tezcorp 0:879cf546d390 768 private:
Tezcorp 0:879cf546d390 769 SPI m_SPI;
Tezcorp 0:879cf546d390 770 DigitalOut m_CS;
Tezcorp 0:879cf546d390 771 DigitalOut m_RESET;
Tezcorp 0:879cf546d390 772
Tezcorp 0:879cf546d390 773 /**
Tezcorp 0:879cf546d390 774 * Helper function for the two-step MIFARE Classic protocol operations Decrement, Increment and Restore.
Tezcorp 0:879cf546d390 775 *
Tezcorp 0:879cf546d390 776 * @param command The command to use
Tezcorp 0:879cf546d390 777 * @param blockAddr The block (0-0xff) number.
Tezcorp 0:879cf546d390 778 * @param data The data to transfer in step 2
Tezcorp 0:879cf546d390 779 *
Tezcorp 0:879cf546d390 780 * @return STATUS_OK on success, STATUS_??? otherwise.
Tezcorp 0:879cf546d390 781 */
Tezcorp 0:879cf546d390 782 uint8_t MIFARE_TwoStepHelper(uint8_t command, uint8_t blockAddr, uint32_t data);
Tezcorp 0:879cf546d390 783 };
Tezcorp 0:879cf546d390 784
Tezcorp 0:879cf546d390 785 #endif