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

Committer:
dotnfc
Date:
Tue Sep 13 06:01:19 2016 +0000
Revision:
0:db8030e71f55
first commit

Who changed what in which revision?

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