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_formatndef/mifareclassic_formatndef.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 format a clean Mifare Classic 1K card as |
dotnfc | 0:db8030e71f55 | 4 | an NFC Forum tag (to store NDEF messages that can be read by any |
dotnfc | 0:db8030e71f55 | 5 | NFC enabled Android phone, etc.) |
dotnfc | 0:db8030e71f55 | 6 | |
dotnfc | 0:db8030e71f55 | 7 | Note that you need the baud rate to be 115200 because we need to print |
dotnfc | 0:db8030e71f55 | 8 | out the data and read from the card at the same time! |
dotnfc | 0:db8030e71f55 | 9 | |
dotnfc | 0:db8030e71f55 | 10 | To enable debug message, define DEBUG in PN532/PN532_debug.h |
dotnfc | 0:db8030e71f55 | 11 | */ |
dotnfc | 0:db8030e71f55 | 12 | /**************************************************************************/ |
dotnfc | 0:db8030e71f55 | 13 | |
dotnfc | 0:db8030e71f55 | 14 | #include <SPI.h> |
dotnfc | 0:db8030e71f55 | 15 | #include <PN532_SPI.h> |
dotnfc | 0:db8030e71f55 | 16 | #include "PN532.h" |
dotnfc | 0:db8030e71f55 | 17 | |
dotnfc | 0:db8030e71f55 | 18 | PN532_SPI pn532spi(SPI, 10); |
dotnfc | 0:db8030e71f55 | 19 | PN532 nfc(pn532spi); |
dotnfc | 0:db8030e71f55 | 20 | |
dotnfc | 0:db8030e71f55 | 21 | /* |
dotnfc | 0:db8030e71f55 | 22 | We can encode many different kinds of pointers to the card, |
dotnfc | 0:db8030e71f55 | 23 | from a URL, to an Email address, to a phone number, and many more |
dotnfc | 0:db8030e71f55 | 24 | check the library header .h file to see the large # of supported |
dotnfc | 0:db8030e71f55 | 25 | prefixes! |
dotnfc | 0:db8030e71f55 | 26 | */ |
dotnfc | 0:db8030e71f55 | 27 | // For a http://www. url: |
dotnfc | 0:db8030e71f55 | 28 | const char * url = "seeedstudio.com"; |
dotnfc | 0:db8030e71f55 | 29 | uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT; |
dotnfc | 0:db8030e71f55 | 30 | |
dotnfc | 0:db8030e71f55 | 31 | // for an email address |
dotnfc | 0:db8030e71f55 | 32 | //const char * url = "mail@example.com"; |
dotnfc | 0:db8030e71f55 | 33 | //uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO; |
dotnfc | 0:db8030e71f55 | 34 | |
dotnfc | 0:db8030e71f55 | 35 | // for a phone number |
dotnfc | 0:db8030e71f55 | 36 | //const char * url = "+1 212 555 1212"; |
dotnfc | 0:db8030e71f55 | 37 | //uint8_t ndefprefix = NDEF_URIPREFIX_TEL; |
dotnfc | 0:db8030e71f55 | 38 | |
dotnfc | 0:db8030e71f55 | 39 | |
dotnfc | 0:db8030e71f55 | 40 | void setup(void) { |
dotnfc | 0:db8030e71f55 | 41 | Serial.begin(115200); |
dotnfc | 0:db8030e71f55 | 42 | Serial.println("Looking for PN532..."); |
dotnfc | 0:db8030e71f55 | 43 | |
dotnfc | 0:db8030e71f55 | 44 | nfc.begin(); |
dotnfc | 0:db8030e71f55 | 45 | |
dotnfc | 0:db8030e71f55 | 46 | uint32_t versiondata = nfc.getFirmwareVersion(); |
dotnfc | 0:db8030e71f55 | 47 | if (! versiondata) { |
dotnfc | 0:db8030e71f55 | 48 | Serial.print("Didn't find PN53x board"); |
dotnfc | 0:db8030e71f55 | 49 | while (1); // halt |
dotnfc | 0:db8030e71f55 | 50 | } |
dotnfc | 0:db8030e71f55 | 51 | |
dotnfc | 0:db8030e71f55 | 52 | // Got ok data, print it out! |
dotnfc | 0:db8030e71f55 | 53 | Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); |
dotnfc | 0:db8030e71f55 | 54 | Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); |
dotnfc | 0:db8030e71f55 | 55 | Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); |
dotnfc | 0:db8030e71f55 | 56 | |
dotnfc | 0:db8030e71f55 | 57 | // configure board to read RFID tags |
dotnfc | 0:db8030e71f55 | 58 | nfc.SAMConfig(); |
dotnfc | 0:db8030e71f55 | 59 | } |
dotnfc | 0:db8030e71f55 | 60 | |
dotnfc | 0:db8030e71f55 | 61 | void loop(void) { |
dotnfc | 0:db8030e71f55 | 62 | uint8_t success; // Flag to check if there was an error with the PN532 |
dotnfc | 0:db8030e71f55 | 63 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
dotnfc | 0:db8030e71f55 | 64 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
dotnfc | 0:db8030e71f55 | 65 | bool authenticated = false; // Flag to indicate if the sector is authenticated |
dotnfc | 0:db8030e71f55 | 66 | |
dotnfc | 0:db8030e71f55 | 67 | // Use the default key |
dotnfc | 0:db8030e71f55 | 68 | uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
dotnfc | 0:db8030e71f55 | 69 | |
dotnfc | 0:db8030e71f55 | 70 | Serial.println(""); |
dotnfc | 0:db8030e71f55 | 71 | Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the"); |
dotnfc | 0:db8030e71f55 | 72 | Serial.println("authentication keys. To reformat your NDEF tag as a clean Mifare"); |
dotnfc | 0:db8030e71f55 | 73 | Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!"); |
dotnfc | 0:db8030e71f55 | 74 | Serial.println(""); |
dotnfc | 0:db8030e71f55 | 75 | Serial.println("Place your Mifare Classic card on the reader to format with NDEF"); |
dotnfc | 0:db8030e71f55 | 76 | Serial.println("and press any key to continue ..."); |
dotnfc | 0:db8030e71f55 | 77 | // Wait for user input before proceeding |
dotnfc | 0:db8030e71f55 | 78 | while (!Serial.available()); |
dotnfc | 0:db8030e71f55 | 79 | // a key was pressed1 |
dotnfc | 0:db8030e71f55 | 80 | while (Serial.available()) Serial.read(); |
dotnfc | 0:db8030e71f55 | 81 | |
dotnfc | 0:db8030e71f55 | 82 | // Wait for an ISO14443A type card (Mifare, etc.). When one is found |
dotnfc | 0:db8030e71f55 | 83 | // 'uid' will be populated with the UID, and uidLength will indicate |
dotnfc | 0:db8030e71f55 | 84 | // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) |
dotnfc | 0:db8030e71f55 | 85 | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); |
dotnfc | 0:db8030e71f55 | 86 | |
dotnfc | 0:db8030e71f55 | 87 | if (success) |
dotnfc | 0:db8030e71f55 | 88 | { |
dotnfc | 0:db8030e71f55 | 89 | // Display some basic information about the card |
dotnfc | 0:db8030e71f55 | 90 | Serial.println("Found an ISO14443A card"); |
dotnfc | 0:db8030e71f55 | 91 | Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); |
dotnfc | 0:db8030e71f55 | 92 | Serial.print(" UID Value: "); |
dotnfc | 0:db8030e71f55 | 93 | nfc.PrintHex(uid, uidLength); |
dotnfc | 0:db8030e71f55 | 94 | for (uint8_t i = 0; i < uidLength; i++) { |
dotnfc | 0:db8030e71f55 | 95 | Serial.print(uid[i], HEX); |
dotnfc | 0:db8030e71f55 | 96 | Serial.print(' '); |
dotnfc | 0:db8030e71f55 | 97 | } |
dotnfc | 0:db8030e71f55 | 98 | Serial.println(""); |
dotnfc | 0:db8030e71f55 | 99 | |
dotnfc | 0:db8030e71f55 | 100 | // Make sure this is a Mifare Classic card |
dotnfc | 0:db8030e71f55 | 101 | if (uidLength != 4) |
dotnfc | 0:db8030e71f55 | 102 | { |
dotnfc | 0:db8030e71f55 | 103 | Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); |
dotnfc | 0:db8030e71f55 | 104 | return; |
dotnfc | 0:db8030e71f55 | 105 | } |
dotnfc | 0:db8030e71f55 | 106 | |
dotnfc | 0:db8030e71f55 | 107 | // We probably have a Mifare Classic card ... |
dotnfc | 0:db8030e71f55 | 108 | Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); |
dotnfc | 0:db8030e71f55 | 109 | |
dotnfc | 0:db8030e71f55 | 110 | // Try to format the card for NDEF data |
dotnfc | 0:db8030e71f55 | 111 | success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya); |
dotnfc | 0:db8030e71f55 | 112 | if (!success) |
dotnfc | 0:db8030e71f55 | 113 | { |
dotnfc | 0:db8030e71f55 | 114 | Serial.println("Unable to authenticate block 0 to enable card formatting!"); |
dotnfc | 0:db8030e71f55 | 115 | return; |
dotnfc | 0:db8030e71f55 | 116 | } |
dotnfc | 0:db8030e71f55 | 117 | success = nfc.mifareclassic_FormatNDEF(); |
dotnfc | 0:db8030e71f55 | 118 | if (!success) |
dotnfc | 0:db8030e71f55 | 119 | { |
dotnfc | 0:db8030e71f55 | 120 | Serial.println("Unable to format the card for NDEF"); |
dotnfc | 0:db8030e71f55 | 121 | return; |
dotnfc | 0:db8030e71f55 | 122 | } |
dotnfc | 0:db8030e71f55 | 123 | |
dotnfc | 0:db8030e71f55 | 124 | Serial.println("Card has been formatted for NDEF data using MAD1"); |
dotnfc | 0:db8030e71f55 | 125 | |
dotnfc | 0:db8030e71f55 | 126 | // Try to authenticate block 4 (first block of sector 1) using our key |
dotnfc | 0:db8030e71f55 | 127 | success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya); |
dotnfc | 0:db8030e71f55 | 128 | |
dotnfc | 0:db8030e71f55 | 129 | // Make sure the authentification process didn't fail |
dotnfc | 0:db8030e71f55 | 130 | if (!success) |
dotnfc | 0:db8030e71f55 | 131 | { |
dotnfc | 0:db8030e71f55 | 132 | Serial.println("Authentication failed."); |
dotnfc | 0:db8030e71f55 | 133 | return; |
dotnfc | 0:db8030e71f55 | 134 | } |
dotnfc | 0:db8030e71f55 | 135 | |
dotnfc | 0:db8030e71f55 | 136 | // Try to write a URL |
dotnfc | 0:db8030e71f55 | 137 | Serial.println("Writing URI to sector 1 as an NDEF Message"); |
dotnfc | 0:db8030e71f55 | 138 | |
dotnfc | 0:db8030e71f55 | 139 | // Authenticated seems to have worked |
dotnfc | 0:db8030e71f55 | 140 | // Try to write an NDEF record to sector 1 |
dotnfc | 0:db8030e71f55 | 141 | // Use 0x01 for the URI Identifier Code to prepend "http://www." |
dotnfc | 0:db8030e71f55 | 142 | // to the url (and save some space). For information on URI ID Codes |
dotnfc | 0:db8030e71f55 | 143 | // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef |
dotnfc | 0:db8030e71f55 | 144 | if (strlen(url) > 38) |
dotnfc | 0:db8030e71f55 | 145 | { |
dotnfc | 0:db8030e71f55 | 146 | // The length is also checked in the WriteNDEFURI function, but lets |
dotnfc | 0:db8030e71f55 | 147 | // warn users here just in case they change the value and it's bigger |
dotnfc | 0:db8030e71f55 | 148 | // than it should be |
dotnfc | 0:db8030e71f55 | 149 | Serial.println("URI is too long ... must be less than 38 characters long"); |
dotnfc | 0:db8030e71f55 | 150 | return; |
dotnfc | 0:db8030e71f55 | 151 | } |
dotnfc | 0:db8030e71f55 | 152 | |
dotnfc | 0:db8030e71f55 | 153 | // URI is within size limits ... write it to the card and report success/failure |
dotnfc | 0:db8030e71f55 | 154 | success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url); |
dotnfc | 0:db8030e71f55 | 155 | if (success) |
dotnfc | 0:db8030e71f55 | 156 | { |
dotnfc | 0:db8030e71f55 | 157 | Serial.println("NDEF URI Record written to sector 1"); |
dotnfc | 0:db8030e71f55 | 158 | } |
dotnfc | 0:db8030e71f55 | 159 | else |
dotnfc | 0:db8030e71f55 | 160 | { |
dotnfc | 0:db8030e71f55 | 161 | Serial.println("NDEF Record creation failed! :("); |
dotnfc | 0:db8030e71f55 | 162 | } |
dotnfc | 0:db8030e71f55 | 163 | } |
dotnfc | 0:db8030e71f55 | 164 | |
dotnfc | 0:db8030e71f55 | 165 | // Wait a bit before trying again |
dotnfc | 0:db8030e71f55 | 166 | Serial.println("\n\nDone!"); |
dotnfc | 0:db8030e71f55 | 167 | delay(1000); |
dotnfc | 0:db8030e71f55 | 168 | Serial.flush(); |
dotnfc | 0:db8030e71f55 | 169 | while(Serial.available()) Serial.read(); |
dotnfc | 0:db8030e71f55 | 170 | } |