Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit PN532examplesmifareclassic_memdumpmifareclassic_memdump.ino

190 lines · 7.2 KB

1/**************************************************************************/
2/*!
3 @file mifareclassic_memdump.pde
4 @author Adafruit Industries
5 @license BSD (see license.txt)
6
7 This example attempts to dump the contents of a Mifare Classic 1K card
8
9 Note that you need the baud rate to be 115200 because we need to print
10 out the data and read from the card at the same time!
11
12 This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
13 This library works with the Adafruit NFC breakout
14 ----> https://www.adafruit.com/products/364
15
16 Check out the links above for our tutorials and wiring diagrams
17 These chips use SPI or I2C to communicate
18
19 Adafruit invests time and resources providing this open source code,
20 please support Adafruit and open-source hardware by purchasing
21 products from Adafruit!
22
23*/
24/**************************************************************************/
25
26#include <Wire.h>
27#include <SPI.h>
28#include <Adafruit_PN532.h>
29
30// If using the breakout with SPI, define the pins for SPI communication.
31#define PN532_SCK (2)
32#define PN532_MOSI (3)
33#define PN532_SS (4)
34#define PN532_MISO (5)
35
36// If using the breakout or shield with I2C, define just the pins connected
37// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
38#define PN532_IRQ (2)
39#define PN532_RESET (3) // Not connected by default on the NFC Shield
40
41// Uncomment just _one_ line below depending on how your breakout or shield
42// is connected to the Arduino:
43
44// Use this line for a breakout with a SPI connection:
45Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
46
47// Use this line for a breakout with a hardware SPI connection. Note that
48// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
49// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
50// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
51//Adafruit_PN532 nfc(PN532_SS);
52
53// Or use this line for a breakout or shield with an I2C connection:
54//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
55
56void setup(void) {
57 // has to be fast to dump the entire memory contents!
58 Serial.begin(115200);
59 while (!Serial) delay(10); // for Leonardo/Micro/Zero
60
61 Serial.println("Looking for PN532...");
62
63 nfc.begin();
64
65 uint32_t versiondata = nfc.getFirmwareVersion();
66 if (! versiondata) {
67 Serial.print("Didn't find PN53x board");
68 while (1); // halt
69 }
70 // Got ok data, print it out!
71 Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
72 Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
73 Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
74
75 Serial.println("Waiting for an ISO14443A Card ...");
76}
77
78
79void loop(void) {
80 uint8_t success; // Flag to check if there was an error with the PN532
81 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
82 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
83 uint8_t currentblock; // Counter to keep track of which block we're on
84 bool authenticated = false; // Flag to indicate if the sector is authenticated
85 uint8_t data[16]; // Array to store block data during reads
86
87 // Keyb on NDEF and Mifare Classic should be the same
88 uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
89
90 // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
91 // 'uid' will be populated with the UID, and uidLength will indicate
92 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
93 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
94
95 if (success) {
96 // Display some basic information about the card
97 Serial.println("Found an ISO14443A card");
98 Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
99 Serial.print(" UID Value: ");
100 nfc.PrintHex(uid, uidLength);
101 Serial.println("");
102
103 if (uidLength == 4)
104 {
105 // We probably have a Mifare Classic card ...
106 Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
107
108 // Now we try to go through all 16 sectors (each having 4 blocks)
109 // authenticating each sector, and then dumping the blocks
110 for (currentblock = 0; currentblock < 64; currentblock++)
111 {
112 // Check if this is a new block so that we can reauthenticate
113 if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;
114
115 // If the sector hasn't been authenticated, do so first
116 if (!authenticated)
117 {
118 // Starting of a new sector ... try to to authenticate
119 Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------");
120 if (currentblock == 0)
121 {
122 // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
123 // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a,
124 // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
125 success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
126 }
127 else
128 {
129 // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
130 // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a,
131 // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
132 success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
133 }
134 if (success)
135 {
136 authenticated = true;
137 }
138 else
139 {
140 Serial.println("Authentication error");
141 }
142 }
143 // If we're still not authenticated just skip the block
144 if (!authenticated)
145 {
146 Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate");
147 }
148 else
149 {
150 // Authenticated ... we should be able to read the block now
151 // Dump the data into the 'data' array
152 success = nfc.mifareclassic_ReadDataBlock(currentblock, data);
153 if (success)
154 {
155 // Read successful
156 Serial.print("Block ");Serial.print(currentblock, DEC);
157 if (currentblock < 10)
158 {
159 Serial.print(" ");
160 }
161 else
162 {
163 Serial.print(" ");
164 }
165 // Dump the raw data
166 nfc.PrintHexChar(data, 16);
167 }
168 else
169 {
170 // Oops ... something happened
171 Serial.print("Block ");Serial.print(currentblock, DEC);
172 Serial.println(" unable to read this block");
173 }
174 }
175 }
176 }
177 else
178 {
179 Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
180 }
181 }
182 // Wait a bit before trying again
183 Serial.println("\n\nSend a character to run the mem dumper again!");
184 Serial.flush();
185 while (!Serial.available());
186 while (Serial.available()) {
187 Serial.read();
188 }
189 Serial.flush();
190}