Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 /**************************************************************************/ 00002 /*! 00003 00004 @file main.cpp 00005 @author Adafruit Industries 00006 @modified_by Ben Zhang <benzh@eecs.berkeley.edu> 00007 @license BSD (see license.txt) 00008 00009 This example will wait for any ISO14443A card or tag, and 00010 depending on the size of the UID will attempt to read from it. 00011 00012 If the card has a 4-byte UID it is probably a Mifare 00013 Classic card, and the following steps are taken: 00014 00015 - Authenticate block 4 (the first block of Sector 1) using 00016 the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF 00017 - If authentication succeeds, we can then read any of the 00018 4 blocks in that sector (though only block 4 is read here) 00019 00020 If the card has a 7-byte UID it is probably a Mifare 00021 Ultralight card, and the 4 byte pages can be read directly. 00022 Page 4 is read by default since this is the first 'general- 00023 purpose' page on the tags. 00024 00025 00026 This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards 00027 This library works with the Adafruit NFC breakout 00028 ----> https://www.adafruit.com/products/364 00029 00030 Check out the links above for our tutorials and wiring diagrams 00031 These chips use SPI to communicate, 4 required to interface 00032 00033 Adafruit invests time and resources providing this open source code, 00034 please support Adafruit and open-source hardware by purchasing 00035 products from Adafruit! 00036 00037 */ 00038 /**************************************************************************/ 00039 00040 #include "mbed.h" 00041 #include "Adafruit_PN532.h " 00042 00043 #define SS PTD0 00044 // PTD1 is also LED_BLUE, it will blink during SPI communication. 00045 #define SCK PTD1 00046 #define MOSI PTD2 00047 #define MISO PTD3 00048 00049 DigitalOut red(LED_RED); 00050 DigitalOut green(LED_GREEN); 00051 DigitalOut blue(LED_BLUE); 00052 00053 Serial pc(USBTX, USBRX); 00054 Adafruit_PN532 nfc(SCK, MISO, MOSI, SS); 00055 00056 void loop(void); 00057 00058 int main() { 00059 pc.printf("Hello!\n"); 00060 // By default, no color 00061 green = 1; red = 1, blue = 1; 00062 00063 nfc.begin(); 00064 00065 uint32_t versiondata = nfc.getFirmwareVersion(); 00066 if (! versiondata) { 00067 pc.printf("Didn't find PN53x board"); 00068 while (1); // halt 00069 } 00070 // Got ok data, print it out! 00071 pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\n", 00072 versiondata >> 24, 00073 (versiondata >> 16) & 0xFF, 00074 (versiondata >> 8) & 0xFF); 00075 00076 // configure board to read RFID tags 00077 nfc.SAMConfig(); 00078 00079 pc.printf("Waiting for an ISO14443A Card ...\n"); 00080 00081 while(1) { loop(); } 00082 } 00083 00084 00085 void loop(void) { 00086 // Turn back to no color 00087 green = 1; red = 1, blue = 1; 00088 00089 uint8_t success; 00090 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 00091 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) 00092 00093 // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 00094 // 'uid' will be populated with the UID, and uidLength will indicate 00095 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 00096 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); 00097 00098 if (success) { 00099 // Turn off to indicate Card swipe 00100 green = 1; red = 1, blue = 1; 00101 // Display some basic information about the card 00102 pc.printf("\n\nFound an ISO14443A card\n"); 00103 pc.printf(" UID Length: %d bytes", uidLength); 00104 pc.printf(" UID Value: "); 00105 nfc.PrintHex(uid, uidLength); 00106 pc.printf("\n"); 00107 00108 if (uidLength == 4) 00109 { 00110 // We probably have a Mifare Classic card ... 00111 pc.printf("Seems to be a Mifare Classic card (4 byte UID)\n"); 00112 00113 // Now we need to try to authenticate it for read/write access 00114 // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 00115 pc.printf("Trying to authenticate block 4 with default KEYA value\n"); 00116 uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 00117 00118 // Start with block 4 (the first block of sector 1) since sector 0 00119 // contains the manufacturer data and it's probably better just 00120 // to leave it alone unless you know what you're doing 00121 success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); 00122 00123 if (success) 00124 { 00125 // Turn colors to indicate which tag 00126 //green = 0; red = 1; 00127 uint8_t tag_1 = 250; 00128 uint8_t tag_2 = 186; 00129 uint8_t tag_3 = 106; 00130 uint8_t tag_4 = 10; 00131 if (uid[0] == tag_1) 00132 green = 0; 00133 else if (uid[0] == tag_2) 00134 red = 0; 00135 else if (uid[0] == tag_3) { 00136 green = 0, red = 0; 00137 } else if (uid[0] == tag_4) { 00138 red = 0, blue = 0; 00139 } 00140 00141 pc.printf("Sector 1 (Blocks 4..7) has been authenticated\n"); 00142 uint8_t data[16]; 00143 00144 // If you want to write something to block 4 to test with, remove 00145 // the definition of data above, and uncomment the following lines 00146 /************************************************************************ 00147 uint8_t data[16] = { 'b', 'e', 'n', ' ', 'z', 'h', 'a', 'n', 'g' }; 00148 success = nfc.mifareclassic_WriteDataBlock (4, data); 00149 if (success) 00150 { 00151 // Data seems to have been read ... spit it out 00152 pc.printf("Data Written to Block 4:\n\t"); 00153 nfc.PrintHexChar(data, 16); 00154 pc.printf("\n"); 00155 00156 // Wait a bit before reading the card again 00157 wait(1); 00158 } 00159 else 00160 { 00161 pc.printf("Ooops ... unable to write the requested block.\n"); 00162 } 00163 return; 00164 ************************************************************************/ 00165 00166 // Try to read the contents of block 4 00167 /*success = nfc.mifareclassic_ReadDataBlock(4, data); 00168 00169 if (success) 00170 { 00171 // Data seems to have been read ... spit it out 00172 pc.printf("Reading Block 4:\n\t"); 00173 nfc.PrintHexChar(data, 16); 00174 pc.printf("\n"); 00175 00176 // Wait a bit before reading the card again 00177 wait(1); 00178 } 00179 else 00180 { 00181 pc.printf("Ooops ... unable to read the requested block. Try another key?\n"); 00182 }*/ 00183 } 00184 else 00185 { 00186 pc.printf("Ooops ... authentication failed: Try another key?\n"); 00187 } 00188 } 00189 00190 if (uidLength == 7) 00191 { 00192 // We probably have a Mifare Ultralight card ... 00193 pc.printf("Seems to be a Mifare Ultralight tag (7 byte UID)\n"); 00194 00195 // Try to read the first general-purpose user page (#4) 00196 pc.printf("Reading page 4\n"); 00197 uint8_t data[32]; 00198 success = nfc.mifareultralight_ReadPage (4, data); 00199 if (success) 00200 { 00201 // Data seems to have been read ... spit it out 00202 nfc.PrintHexChar(data, 4); 00203 pc.printf("\n"); 00204 00205 // Wait a bit before reading the card again 00206 wait(1); 00207 } 00208 else 00209 { 00210 pc.printf("Ooops ... unable to read the requested page!?\n"); 00211 } 00212 } 00213 } 00214 }
Generated on Wed Jul 20 2022 14:01:01 by
