Commit 55af57c07393d669214c815c3b38ca9d4f371bb5
1 parent
bf0a878a
Customized version of the sample given by Arduino
Showing
1 changed file
with
129 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,129 @@ |
1 | +#include <SPI.h> | |
2 | +#include <PN532_SPI.h> | |
3 | +#include "PN532.h" | |
4 | + | |
5 | +PN532_SPI pn532spi(SPI, 10); | |
6 | +PN532 nfc(pn532spi); | |
7 | + | |
8 | +void setup(void) { | |
9 | + Serial.begin(115200); | |
10 | + Serial.println("Hello!"); | |
11 | + | |
12 | + nfc.begin(); | |
13 | + | |
14 | + uint32_t versiondata = nfc.getFirmwareVersion(); | |
15 | + if (! versiondata) | |
16 | + { | |
17 | + Serial.print("Didn't find PN53x board"); | |
18 | + while (1); | |
19 | + } | |
20 | + //Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); | |
21 | + Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); | |
22 | + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); | |
23 | + | |
24 | + // configure board to read RFID tags | |
25 | + nfc.SAMConfig(); | |
26 | + | |
27 | + Serial.println("Waiting for an ISO14443A Card ..."); | |
28 | +} | |
29 | + | |
30 | + | |
31 | +void loop(void) | |
32 | +{ | |
33 | + uint8_t success; | |
34 | + uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID | |
35 | + uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) | |
36 | + | |
37 | + // Wait for an ISO14443A type cards (Mifare, etc.). When one is found | |
38 | + // 'uid' will be populated with the UID, and uidLength will indicate | |
39 | + // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) | |
40 | + success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); | |
41 | + | |
42 | + if(success) | |
43 | + { | |
44 | + // Display some basic information about the card | |
45 | + Serial.println("Found an ISO14443A card"); | |
46 | + Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); | |
47 | + Serial.print(" UID Value: "); | |
48 | + nfc.PrintHex(uid, uidLength); | |
49 | + Serial.println(""); | |
50 | + | |
51 | + if(uidLength == 4) | |
52 | + { | |
53 | + // We probably have a Mifare Classic card ... | |
54 | + Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); | |
55 | + | |
56 | + // Now we need to try to authenticate it for read/write access | |
57 | + // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF | |
58 | + Serial.println("Trying to authenticate block 4 with default KEYA value"); | |
59 | + uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; | |
60 | + | |
61 | + // Start with block 4 (the first block of sector 1) since sector 0 | |
62 | + // contains the manufacturer data and it's probably better just | |
63 | + // to leave it alone unless you know what you're doing | |
64 | + success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 44, 0, keya); | |
65 | + | |
66 | + if(success) | |
67 | + { | |
68 | + Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); | |
69 | + uint8_t data[16]; | |
70 | + | |
71 | + // If you want to write something to block 4 to test with, uncomment | |
72 | + // the following line and this text should be read back in a minute | |
73 | + // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0}; | |
74 | + // success = nfc.mifareclassic_WriteDataBlock (4, data); | |
75 | + | |
76 | + // Try to read the contents of block 4 | |
77 | + success = nfc.mifareclassic_ReadDataBlock(44, data); | |
78 | + | |
79 | + if(success) | |
80 | + { | |
81 | + // Data seems to have been read ... spit it out | |
82 | + Serial.println("Reading Block 4:"); | |
83 | + nfc.PrintHexChar(data, 16); | |
84 | + nfc.mifareclassic_ReadDataBlock(45, data); | |
85 | + nfc.PrintHexChar(data, 16); | |
86 | + nfc.mifareclassic_ReadDataBlock(46, data); | |
87 | + nfc.PrintHexChar(data, 16); | |
88 | + Serial.println(""); | |
89 | + | |
90 | + // Wait a bit before reading the card again | |
91 | + delay(1000); | |
92 | + } | |
93 | + else | |
94 | + { | |
95 | + Serial.println("Ooops ... unable to read the requested block. Try another key?"); | |
96 | + } | |
97 | + } | |
98 | + else | |
99 | + { | |
100 | + Serial.println("Ooops ... authentication failed: Try another key?"); | |
101 | + } | |
102 | + } | |
103 | + | |
104 | + if(uidLength == 7) | |
105 | + { | |
106 | + // We probably have a Mifare Ultralight card ... | |
107 | + Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); | |
108 | + | |
109 | + // Try to read the first general-purpose user page (#4) | |
110 | + Serial.println("Reading page 4"); | |
111 | + uint8_t data[32]; | |
112 | + success = nfc.mifareultralight_ReadPage (4, data); | |
113 | + if(success) | |
114 | + { | |
115 | + // Data seems to have been read ... spit it out | |
116 | + nfc.PrintHexChar(data, 4); | |
117 | + Serial.println(""); | |
118 | + | |
119 | + // Wait a bit before reading the card again | |
120 | + delay(1000); | |
121 | + } | |
122 | + else | |
123 | + { | |
124 | + Serial.println("Ooops ... unable to read the requested page!?"); | |
125 | + } | |
126 | + } | |
127 | + } | |
128 | +} | |
129 | + | ... | ... |