PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.
Dependents: PN532_ReadUid Nfctest2
PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde@0:db8030e71f55, 2016-09-13 (annotated)
- Committer:
- dotnfc
- Date:
- Tue Sep 13 06:01:19 2016 +0000
- Revision:
- 0:db8030e71f55
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dotnfc | 0:db8030e71f55 | 1 | /**************************************************************************/ |
dotnfc | 0:db8030e71f55 | 2 | /*! |
dotnfc | 0:db8030e71f55 | 3 | This example attempts to dump the contents of a Mifare Classic 1K card |
dotnfc | 0:db8030e71f55 | 4 | |
dotnfc | 0:db8030e71f55 | 5 | Note that you need the baud rate to be 115200 because we need to print |
dotnfc | 0:db8030e71f55 | 6 | out the data and read from the card at the same time! |
dotnfc | 0:db8030e71f55 | 7 | |
dotnfc | 0:db8030e71f55 | 8 | To enable debug message, define DEBUG in PN532/PN532_debug.h |
dotnfc | 0:db8030e71f55 | 9 | */ |
dotnfc | 0:db8030e71f55 | 10 | /**************************************************************************/ |
dotnfc | 0:db8030e71f55 | 11 | |
dotnfc | 0:db8030e71f55 | 12 | #include <SPI.h> |
dotnfc | 0:db8030e71f55 | 13 | #include <PN532_SPI.h> |
dotnfc | 0:db8030e71f55 | 14 | #include "PN532.h" |
dotnfc | 0:db8030e71f55 | 15 | |
dotnfc | 0:db8030e71f55 | 16 | PN532_SPI pn532spi(SPI, 10); |
dotnfc | 0:db8030e71f55 | 17 | PN532 nfc(pn532spi); |
dotnfc | 0:db8030e71f55 | 18 | |
dotnfc | 0:db8030e71f55 | 19 | void setup(void) { |
dotnfc | 0:db8030e71f55 | 20 | // has to be fast to dump the entire memory contents! |
dotnfc | 0:db8030e71f55 | 21 | Serial.begin(115200); |
dotnfc | 0:db8030e71f55 | 22 | Serial.println("Looking for PN532..."); |
dotnfc | 0:db8030e71f55 | 23 | |
dotnfc | 0:db8030e71f55 | 24 | nfc.begin(); |
dotnfc | 0:db8030e71f55 | 25 | |
dotnfc | 0:db8030e71f55 | 26 | uint32_t versiondata = nfc.getFirmwareVersion(); |
dotnfc | 0:db8030e71f55 | 27 | if (! versiondata) { |
dotnfc | 0:db8030e71f55 | 28 | Serial.print("Didn't find PN53x board"); |
dotnfc | 0:db8030e71f55 | 29 | while (1); // halt |
dotnfc | 0:db8030e71f55 | 30 | } |
dotnfc | 0:db8030e71f55 | 31 | // Got ok data, print it out! |
dotnfc | 0:db8030e71f55 | 32 | Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); |
dotnfc | 0:db8030e71f55 | 33 | Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); |
dotnfc | 0:db8030e71f55 | 34 | Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); |
dotnfc | 0:db8030e71f55 | 35 | |
dotnfc | 0:db8030e71f55 | 36 | // configure board to read RFID tags |
dotnfc | 0:db8030e71f55 | 37 | nfc.SAMConfig(); |
dotnfc | 0:db8030e71f55 | 38 | |
dotnfc | 0:db8030e71f55 | 39 | Serial.println("Waiting for an ISO14443A Card ..."); |
dotnfc | 0:db8030e71f55 | 40 | } |
dotnfc | 0:db8030e71f55 | 41 | |
dotnfc | 0:db8030e71f55 | 42 | |
dotnfc | 0:db8030e71f55 | 43 | void loop(void) { |
dotnfc | 0:db8030e71f55 | 44 | uint8_t success; // Flag to check if there was an error with the PN532 |
dotnfc | 0:db8030e71f55 | 45 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
dotnfc | 0:db8030e71f55 | 46 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
dotnfc | 0:db8030e71f55 | 47 | uint8_t currentblock; // Counter to keep track of which block we're on |
dotnfc | 0:db8030e71f55 | 48 | bool authenticated = false; // Flag to indicate if the sector is authenticated |
dotnfc | 0:db8030e71f55 | 49 | uint8_t data[16]; // Array to store block data during reads |
dotnfc | 0:db8030e71f55 | 50 | |
dotnfc | 0:db8030e71f55 | 51 | // Keyb on NDEF and Mifare Classic should be the same |
dotnfc | 0:db8030e71f55 | 52 | uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
dotnfc | 0:db8030e71f55 | 53 | |
dotnfc | 0:db8030e71f55 | 54 | // Wait for an ISO14443A type cards (Mifare, etc.). When one is found |
dotnfc | 0:db8030e71f55 | 55 | // 'uid' will be populated with the UID, and uidLength will indicate |
dotnfc | 0:db8030e71f55 | 56 | // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) |
dotnfc | 0:db8030e71f55 | 57 | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); |
dotnfc | 0:db8030e71f55 | 58 | |
dotnfc | 0:db8030e71f55 | 59 | if (success) { |
dotnfc | 0:db8030e71f55 | 60 | // Display some basic information about the card |
dotnfc | 0:db8030e71f55 | 61 | Serial.println("Found an ISO14443A card"); |
dotnfc | 0:db8030e71f55 | 62 | Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); |
dotnfc | 0:db8030e71f55 | 63 | Serial.print(" UID Value: "); |
dotnfc | 0:db8030e71f55 | 64 | for (uint8_t i = 0; i < uidLength; i++) { |
dotnfc | 0:db8030e71f55 | 65 | Serial.print(uid[i], HEX); |
dotnfc | 0:db8030e71f55 | 66 | Serial.print(' '); |
dotnfc | 0:db8030e71f55 | 67 | } |
dotnfc | 0:db8030e71f55 | 68 | Serial.println(""); |
dotnfc | 0:db8030e71f55 | 69 | |
dotnfc | 0:db8030e71f55 | 70 | if (uidLength == 4) |
dotnfc | 0:db8030e71f55 | 71 | { |
dotnfc | 0:db8030e71f55 | 72 | // We probably have a Mifare Classic card ... |
dotnfc | 0:db8030e71f55 | 73 | Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); |
dotnfc | 0:db8030e71f55 | 74 | |
dotnfc | 0:db8030e71f55 | 75 | // Now we try to go through all 16 sectors (each having 4 blocks) |
dotnfc | 0:db8030e71f55 | 76 | // authenticating each sector, and then dumping the blocks |
dotnfc | 0:db8030e71f55 | 77 | for (currentblock = 0; currentblock < 64; currentblock++) |
dotnfc | 0:db8030e71f55 | 78 | { |
dotnfc | 0:db8030e71f55 | 79 | // Check if this is a new block so that we can reauthenticate |
dotnfc | 0:db8030e71f55 | 80 | if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false; |
dotnfc | 0:db8030e71f55 | 81 | |
dotnfc | 0:db8030e71f55 | 82 | // If the sector hasn't been authenticated, do so first |
dotnfc | 0:db8030e71f55 | 83 | if (!authenticated) |
dotnfc | 0:db8030e71f55 | 84 | { |
dotnfc | 0:db8030e71f55 | 85 | // Starting of a new sector ... try to to authenticate |
dotnfc | 0:db8030e71f55 | 86 | Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------"); |
dotnfc | 0:db8030e71f55 | 87 | if (currentblock == 0) |
dotnfc | 0:db8030e71f55 | 88 | { |
dotnfc | 0:db8030e71f55 | 89 | // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!) |
dotnfc | 0:db8030e71f55 | 90 | // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a, |
dotnfc | 0:db8030e71f55 | 91 | // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF) |
dotnfc | 0:db8030e71f55 | 92 | success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal); |
dotnfc | 0:db8030e71f55 | 93 | } |
dotnfc | 0:db8030e71f55 | 94 | else |
dotnfc | 0:db8030e71f55 | 95 | { |
dotnfc | 0:db8030e71f55 | 96 | // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!) |
dotnfc | 0:db8030e71f55 | 97 | // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a, |
dotnfc | 0:db8030e71f55 | 98 | // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF) |
dotnfc | 0:db8030e71f55 | 99 | success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal); |
dotnfc | 0:db8030e71f55 | 100 | } |
dotnfc | 0:db8030e71f55 | 101 | if (success) |
dotnfc | 0:db8030e71f55 | 102 | { |
dotnfc | 0:db8030e71f55 | 103 | authenticated = true; |
dotnfc | 0:db8030e71f55 | 104 | } |
dotnfc | 0:db8030e71f55 | 105 | else |
dotnfc | 0:db8030e71f55 | 106 | { |
dotnfc | 0:db8030e71f55 | 107 | Serial.println("Authentication error"); |
dotnfc | 0:db8030e71f55 | 108 | } |
dotnfc | 0:db8030e71f55 | 109 | } |
dotnfc | 0:db8030e71f55 | 110 | // If we're still not authenticated just skip the block |
dotnfc | 0:db8030e71f55 | 111 | if (!authenticated) |
dotnfc | 0:db8030e71f55 | 112 | { |
dotnfc | 0:db8030e71f55 | 113 | Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate"); |
dotnfc | 0:db8030e71f55 | 114 | } |
dotnfc | 0:db8030e71f55 | 115 | else |
dotnfc | 0:db8030e71f55 | 116 | { |
dotnfc | 0:db8030e71f55 | 117 | // Authenticated ... we should be able to read the block now |
dotnfc | 0:db8030e71f55 | 118 | // Dump the data into the 'data' array |
dotnfc | 0:db8030e71f55 | 119 | success = nfc.mifareclassic_ReadDataBlock(currentblock, data); |
dotnfc | 0:db8030e71f55 | 120 | if (success) |
dotnfc | 0:db8030e71f55 | 121 | { |
dotnfc | 0:db8030e71f55 | 122 | // Read successful |
dotnfc | 0:db8030e71f55 | 123 | Serial.print("Block ");Serial.print(currentblock, DEC); |
dotnfc | 0:db8030e71f55 | 124 | if (currentblock < 10) |
dotnfc | 0:db8030e71f55 | 125 | { |
dotnfc | 0:db8030e71f55 | 126 | Serial.print(" "); |
dotnfc | 0:db8030e71f55 | 127 | } |
dotnfc | 0:db8030e71f55 | 128 | else |
dotnfc | 0:db8030e71f55 | 129 | { |
dotnfc | 0:db8030e71f55 | 130 | Serial.print(" "); |
dotnfc | 0:db8030e71f55 | 131 | } |
dotnfc | 0:db8030e71f55 | 132 | // Dump the raw data |
dotnfc | 0:db8030e71f55 | 133 | nfc.PrintHexChar(data, 16); |
dotnfc | 0:db8030e71f55 | 134 | } |
dotnfc | 0:db8030e71f55 | 135 | else |
dotnfc | 0:db8030e71f55 | 136 | { |
dotnfc | 0:db8030e71f55 | 137 | // Oops ... something happened |
dotnfc | 0:db8030e71f55 | 138 | Serial.print("Block ");Serial.print(currentblock, DEC); |
dotnfc | 0:db8030e71f55 | 139 | Serial.println(" unable to read this block"); |
dotnfc | 0:db8030e71f55 | 140 | } |
dotnfc | 0:db8030e71f55 | 141 | } |
dotnfc | 0:db8030e71f55 | 142 | } |
dotnfc | 0:db8030e71f55 | 143 | } |
dotnfc | 0:db8030e71f55 | 144 | else |
dotnfc | 0:db8030e71f55 | 145 | { |
dotnfc | 0:db8030e71f55 | 146 | Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); |
dotnfc | 0:db8030e71f55 | 147 | } |
dotnfc | 0:db8030e71f55 | 148 | } |
dotnfc | 0:db8030e71f55 | 149 | // Wait a bit before trying again |
dotnfc | 0:db8030e71f55 | 150 | Serial.println("\n\nSend a character to run the mem dumper again!"); |
dotnfc | 0:db8030e71f55 | 151 | Serial.flush(); |
dotnfc | 0:db8030e71f55 | 152 | while (!Serial.available()); |
dotnfc | 0:db8030e71f55 | 153 | while (Serial.available()) { |
dotnfc | 0:db8030e71f55 | 154 | Serial.read(); |
dotnfc | 0:db8030e71f55 | 155 | } |
dotnfc | 0:db8030e71f55 | 156 | Serial.flush(); |
dotnfc | 0:db8030e71f55 | 157 | } |