PN532 customized

Fork of PN532 by Components

Committer:
stanvn
Date:
Thu Feb 11 11:56:00 2016 +0000
Revision:
10:2fcf2448d199
Parent:
7:a26fa6ef10eb
Working code for reading type-a and type-b cards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 3:4189a10038e6 1 #include <NfcAdapter.h>
yihui 3:4189a10038e6 2 #include <PN532_debug.h>
yihui 3:4189a10038e6 3
yihui 3:4189a10038e6 4 NfcAdapter::NfcAdapter(PN532Interface &interface)
yihui 3:4189a10038e6 5 {
yihui 3:4189a10038e6 6 shield = new PN532(interface);
yihui 3:4189a10038e6 7 }
yihui 3:4189a10038e6 8
yihui 3:4189a10038e6 9 NfcAdapter::~NfcAdapter(void)
yihui 3:4189a10038e6 10 {
yihui 3:4189a10038e6 11 delete shield;
yihui 3:4189a10038e6 12 }
yihui 3:4189a10038e6 13
yihui 3:4189a10038e6 14 void NfcAdapter::begin()
yihui 3:4189a10038e6 15 {
yihui 3:4189a10038e6 16 shield->begin();
yihui 3:4189a10038e6 17
yihui 3:4189a10038e6 18 uint32_t versiondata = shield->getFirmwareVersion();
yihui 3:4189a10038e6 19 if (! versiondata) {
yihui 3:4189a10038e6 20 DMSG("Didn't find PN53x board");
yihui 3:4189a10038e6 21 while (1); // halt
yihui 3:4189a10038e6 22 }
yihui 3:4189a10038e6 23
yihui 3:4189a10038e6 24 DMSG("Found chip PN5%2X\r\n", versiondata >> 24);
yihui 3:4189a10038e6 25 DMSG("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
yihui 3:4189a10038e6 26
yihui 3:4189a10038e6 27 // configure board to read RFID tags
yihui 3:4189a10038e6 28 shield->SAMConfig();
yihui 3:4189a10038e6 29 }
yihui 3:4189a10038e6 30
yihui 3:4189a10038e6 31 bool NfcAdapter::tagPresent()
yihui 3:4189a10038e6 32 {
yihui 3:4189a10038e6 33 uint8_t success;
yihui 3:4189a10038e6 34 uidLength = 0;
yihui 3:4189a10038e6 35
yihui 3:4189a10038e6 36 // TODO is cast of uidLength OK?
stanvn 7:a26fa6ef10eb 37 success = shield->readPassiveTarget(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&uidLength, &sak);
yihui 3:4189a10038e6 38
yihui 3:4189a10038e6 39 // if (success)
yihui 3:4189a10038e6 40 // {
yihui 3:4189a10038e6 41 // DMSG("Found an ISO14443A card");
yihui 3:4189a10038e6 42 // DMSG(" UID Length: ");Serial.print(uidLength, DEC);DMSG(" uint8_ts");
yihui 3:4189a10038e6 43 // DMSG(" UID Value: ");
yihui 3:4189a10038e6 44 // shield->PrintHex(uid, uidLength);
yihui 3:4189a10038e6 45 // DMSG("");
yihui 3:4189a10038e6 46 // }
yihui 3:4189a10038e6 47
yihui 3:4189a10038e6 48 return success;
yihui 3:4189a10038e6 49 }
yihui 3:4189a10038e6 50
yihui 3:4189a10038e6 51 NfcTag NfcAdapter::read()
yihui 3:4189a10038e6 52 {
yihui 3:4189a10038e6 53
stanvn 7:a26fa6ef10eb 54 uint8_t type = getTagType();
yihui 3:4189a10038e6 55
yihui 3:4189a10038e6 56 // TODO need an abstraction of Driver
stanvn 7:a26fa6ef10eb 57 if (type == TAG_TYPE_MIFARE_UL)
stanvn 7:a26fa6ef10eb 58 {
stanvn 7:a26fa6ef10eb 59 #ifdef NDEF_DEBUG
stanvn 7:a26fa6ef10eb 60 printf("Reading Mifare Ultralight");
stanvn 7:a26fa6ef10eb 61 #endif
stanvn 7:a26fa6ef10eb 62 MifareUltralight ultralight = MifareUltralight(*shield);
stanvn 7:a26fa6ef10eb 63 return ultralight.read(uid, uidLength);
stanvn 7:a26fa6ef10eb 64 }
stanvn 7:a26fa6ef10eb 65 else if (type >= TAG_TYPE_MIFARE_MINI && type <= TAG_TYPE_MIFARE_PLUS_4K)
yihui 3:4189a10038e6 66 {
yihui 3:4189a10038e6 67 #ifdef NDEF_DEBUG
yihui 3:4189a10038e6 68 DMSG("Reading Mifare Classic");
yihui 3:4189a10038e6 69 #endif
stanvn 7:a26fa6ef10eb 70 MifareClassic mifareClassic = MifareClassic(*shield, type);
yihui 3:4189a10038e6 71 return mifareClassic.read(uid, uidLength);
yihui 3:4189a10038e6 72 }
stanvn 7:a26fa6ef10eb 73
yihui 3:4189a10038e6 74 else if (type == TAG_TYPE_UNKNOWN)
yihui 3:4189a10038e6 75 {
yihui 3:4189a10038e6 76 DMSG("Can not determine tag type");
yihui 3:4189a10038e6 77 //DMSG("Can not determine tag type for ATQA 0x");
yihui 3:4189a10038e6 78 //Serial.print(atqa, HEX);DMSG(" SAK 0x");DMSG(sak, HEX);
yihui 3:4189a10038e6 79 return NfcTag(uid, uidLength);
yihui 3:4189a10038e6 80 }
yihui 3:4189a10038e6 81 else
yihui 3:4189a10038e6 82 {
yihui 3:4189a10038e6 83 DMSG("No driver for card type ");
yihui 3:4189a10038e6 84 DMSG_INT(type);
yihui 3:4189a10038e6 85 // TODO should set type here
yihui 3:4189a10038e6 86 return NfcTag(uid, uidLength);
yihui 3:4189a10038e6 87 }
yihui 3:4189a10038e6 88
yihui 3:4189a10038e6 89 }
yihui 3:4189a10038e6 90
yihui 3:4189a10038e6 91 bool NfcAdapter::write(NdefMessage& ndefMessage)
yihui 3:4189a10038e6 92 {
yihui 3:4189a10038e6 93 bool success;
yihui 3:4189a10038e6 94
yihui 3:4189a10038e6 95 if (uidLength == 4)
yihui 3:4189a10038e6 96 {
stanvn 7:a26fa6ef10eb 97 MifareClassic mifareClassic = MifareClassic(*shield, 0);
yihui 3:4189a10038e6 98 success = mifareClassic.write(ndefMessage, uid, uidLength);
yihui 3:4189a10038e6 99 }
yihui 3:4189a10038e6 100 else
yihui 3:4189a10038e6 101 {
yihui 3:4189a10038e6 102 DMSG("Unsupported Tag");
yihui 3:4189a10038e6 103 success = false;
yihui 3:4189a10038e6 104 }
yihui 3:4189a10038e6 105 return success;
yihui 3:4189a10038e6 106 }
yihui 3:4189a10038e6 107
yihui 3:4189a10038e6 108 // TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown
yihui 3:4189a10038e6 109 // Guess Tag Type by looking at the ATQA and SAK values
yihui 3:4189a10038e6 110 // Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ???
yihui 3:4189a10038e6 111 unsigned int NfcAdapter::guessTagType()
yihui 3:4189a10038e6 112 {
yihui 3:4189a10038e6 113 // 4 uint8_t id - Mifare Classic
yihui 3:4189a10038e6 114 // - ATQA 0x4 && SAK 0x8
yihui 3:4189a10038e6 115 // 7 uint8_t id
yihui 3:4189a10038e6 116 // - ATQA 0x44 && SAK 0x8 - Mifare Classic
yihui 3:4189a10038e6 117 // - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2
yihui 3:4189a10038e6 118 // - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4
yihui 3:4189a10038e6 119
yihui 3:4189a10038e6 120 if (uidLength == 4)
yihui 3:4189a10038e6 121 {
stanvn 7:a26fa6ef10eb 122 return TAG_TYPE_MIFARE_1K;
yihui 3:4189a10038e6 123 }
yihui 3:4189a10038e6 124 else
yihui 3:4189a10038e6 125 {
yihui 3:4189a10038e6 126 return TAG_TYPE_2;
yihui 3:4189a10038e6 127 }
yihui 3:4189a10038e6 128 }
stanvn 7:a26fa6ef10eb 129 unsigned int NfcAdapter::getTagType(){
stanvn 7:a26fa6ef10eb 130 if((sak & 0b00011011) == 0b00001001){
stanvn 7:a26fa6ef10eb 131 return TAG_TYPE_MIFARE_MINI;
stanvn 7:a26fa6ef10eb 132 }
stanvn 7:a26fa6ef10eb 133 else if((sak & 0b00011011) == 0b00001000){
stanvn 7:a26fa6ef10eb 134 return TAG_TYPE_MIFARE_1K;
stanvn 7:a26fa6ef10eb 135 }
stanvn 7:a26fa6ef10eb 136 else if((sak & 0b00011010) == 0b00011000){
stanvn 7:a26fa6ef10eb 137 return TAG_TYPE_MIFARE_4K;
stanvn 7:a26fa6ef10eb 138 }
stanvn 7:a26fa6ef10eb 139 else if((sak & 0b00111010) == 0){
stanvn 7:a26fa6ef10eb 140 return TAG_TYPE_MIFARE_UL;
stanvn 7:a26fa6ef10eb 141 }
stanvn 7:a26fa6ef10eb 142 else if((sak & 0b00111010) == 0b00100000){
stanvn 7:a26fa6ef10eb 143 return TAG_TYPE_14443;
stanvn 7:a26fa6ef10eb 144 }
stanvn 7:a26fa6ef10eb 145 else if((sak & 0b00011011) == 0b00010000){
stanvn 7:a26fa6ef10eb 146 return TAG_TYPE_MIFARE_PLUS_2K;
stanvn 7:a26fa6ef10eb 147 }
stanvn 7:a26fa6ef10eb 148 else if((sak & 0b00011011) == 0b00010001){
stanvn 7:a26fa6ef10eb 149 return TAG_TYPE_MIFARE_4K;
stanvn 7:a26fa6ef10eb 150 }
stanvn 7:a26fa6ef10eb 151 }