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