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 attempt to connect to an ISO14443A
dotnfc 0:db8030e71f55 4 card or tag and retrieve some basic information about it
dotnfc 0:db8030e71f55 5 that can be used to determine what type of card it is.
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
dotnfc 0:db8030e71f55 15 #include <SPI.h>
dotnfc 0:db8030e71f55 16 #include <PN532_SPI.h>
dotnfc 0:db8030e71f55 17 #include "PN532.h"
dotnfc 0:db8030e71f55 18
dotnfc 0:db8030e71f55 19 PN532_SPI pn532spi(SPI, 10);
dotnfc 0:db8030e71f55 20 PN532 nfc(pn532spi);
dotnfc 0:db8030e71f55 21
dotnfc 0:db8030e71f55 22 void setup(void) {
dotnfc 0:db8030e71f55 23 Serial.begin(115200);
dotnfc 0:db8030e71f55 24 Serial.println("Hello!");
dotnfc 0:db8030e71f55 25
dotnfc 0:db8030e71f55 26 nfc.begin();
dotnfc 0:db8030e71f55 27
dotnfc 0:db8030e71f55 28 uint32_t versiondata = nfc.getFirmwareVersion();
dotnfc 0:db8030e71f55 29 if (! versiondata) {
dotnfc 0:db8030e71f55 30 Serial.print("Didn't find PN53x board");
dotnfc 0:db8030e71f55 31 while (1); // halt
dotnfc 0:db8030e71f55 32 }
dotnfc 0:db8030e71f55 33
dotnfc 0:db8030e71f55 34 // Got ok data, print it out!
dotnfc 0:db8030e71f55 35 Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
dotnfc 0:db8030e71f55 36 Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
dotnfc 0:db8030e71f55 37 Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
dotnfc 0:db8030e71f55 38
dotnfc 0:db8030e71f55 39 // Set the max number of retry attempts to read from a card
dotnfc 0:db8030e71f55 40 // This prevents us from waiting forever for a card, which is
dotnfc 0:db8030e71f55 41 // the default behaviour of the PN532.
dotnfc 0:db8030e71f55 42 nfc.setPassiveActivationRetries(0xFF);
dotnfc 0:db8030e71f55 43
dotnfc 0:db8030e71f55 44 // configure board to read RFID tags
dotnfc 0:db8030e71f55 45 nfc.SAMConfig();
dotnfc 0:db8030e71f55 46
dotnfc 0:db8030e71f55 47 Serial.println("Waiting for an ISO14443A card");
dotnfc 0:db8030e71f55 48 }
dotnfc 0:db8030e71f55 49
dotnfc 0:db8030e71f55 50 void loop(void) {
dotnfc 0:db8030e71f55 51 boolean success;
dotnfc 0:db8030e71f55 52 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
dotnfc 0:db8030e71f55 53 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
dotnfc 0:db8030e71f55 54
dotnfc 0:db8030e71f55 55 // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
dotnfc 0:db8030e71f55 56 // 'uid' will be populated with the UID, and uidLength will indicate
dotnfc 0:db8030e71f55 57 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
dotnfc 0:db8030e71f55 58 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
dotnfc 0:db8030e71f55 59
dotnfc 0:db8030e71f55 60 if (success) {
dotnfc 0:db8030e71f55 61 Serial.println("Found a card!");
dotnfc 0:db8030e71f55 62 Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
dotnfc 0:db8030e71f55 63 Serial.print("UID Value: ");
dotnfc 0:db8030e71f55 64 for (uint8_t i=0; i < uidLength; i++)
dotnfc 0:db8030e71f55 65 {
dotnfc 0:db8030e71f55 66 Serial.print(" 0x");Serial.print(uid[i], HEX);
dotnfc 0:db8030e71f55 67 }
dotnfc 0:db8030e71f55 68 Serial.println("");
dotnfc 0:db8030e71f55 69
dotnfc 0:db8030e71f55 70 // wait until the card is taken away
dotnfc 0:db8030e71f55 71 while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength)) {}
dotnfc 0:db8030e71f55 72 }
dotnfc 0:db8030e71f55 73 else
dotnfc 0:db8030e71f55 74 {
dotnfc 0:db8030e71f55 75 // PN532 probably timed out waiting for a card
dotnfc 0:db8030e71f55 76 Serial.println("Timed out waiting for a card");
dotnfc 0:db8030e71f55 77 }
dotnfc 0:db8030e71f55 78 }