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