HTM / PN532_MK20

Dependents:   NFC_HTM_READ EMULAR_TAGS Escribir_tag NFC_HTM_READ-WRITE

Files at this revision

API Documentation at this revision

Comitter:
mauroar211
Date:
Fri Apr 24 19:02:44 2015 +0000
Parent:
0:b805b487fbef
Commit message:
SE RESTAURARON LOS ARCHIVO DE LA LIBRER?A;

Changed in this revision

Ndef.cpp Show annotated file Show diff for this revision Revisions of this file
NfcAdapter.cpp Show annotated file Show diff for this revision Revisions of this file
NfcAdapter.h Show annotated file Show diff for this revision Revisions of this file
NfcTag.cpp Show annotated file Show diff for this revision Revisions of this file
PN532.cpp Show annotated file Show diff for this revision Revisions of this file
PN532_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
PN532_debug.h Show annotated file Show diff for this revision Revisions of this file
--- a/Ndef.cpp	Fri Apr 24 18:17:09 2015 +0000
+++ b/Ndef.cpp	Fri Apr 24 19:02:44 2015 +0000
@@ -10,7 +10,7 @@
     DMSG("0x");
     // Append leading 0 for small values
     if (data[szPos] <= 0xF)
-      DMSG("0");
+    DMSG("0");
     DMSG_HEX(data[szPos]&0xff);
     if ((numuint8_ts > 1) && (szPos != numuint8_ts - 1))
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NfcAdapter.cpp	Fri Apr 24 19:02:44 2015 +0000
@@ -0,0 +1,128 @@
+#include <NfcAdapter.h>
+#include <PN532_debug.h>
+
+NfcAdapter::NfcAdapter(PN532Interface &interface)
+{
+    shield = new PN532(interface);
+}
+
+NfcAdapter::~NfcAdapter(void)
+{
+    delete shield;
+}
+
+void NfcAdapter::begin()
+{
+    shield->begin();
+
+    uint32_t versiondata = shield->getFirmwareVersion();
+    if (! versiondata) {
+        printf("no se encuentra el chip PN532");
+        while (1); // halt
+    }
+    
+    printf("\r chip encontrado PN5%2X\r\n", versiondata >> 24);
+    printf("\r Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
+
+    // configure board to read RFID tags
+    shield->SAMConfig();
+}
+
+bool NfcAdapter::tagPresent()
+{
+    uint8_t success;
+    uidLength = 0;
+
+    // TODO is cast of uidLength OK?
+    success = shield->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&uidLength);
+
+    // if (success)
+    // {
+    //   DMSG("Found an ISO14443A card");
+    //   DMSG("  UID Length: ");Serial.print(uidLength, DEC);DMSG(" uint8_ts");
+    //   DMSG("  UID Value: ");
+    //  shield->PrintHex(uid, uidLength);
+    //   DMSG("");
+    // }
+
+    return success;
+}
+
+NfcTag NfcAdapter::read()
+{
+
+    uint8_t type = guessTagType();
+
+    // TODO need an abstraction of Driver
+    if (type == TAG_TYPE_MIFARE_CLASSIC)
+    {
+        
+        printf("\r Es un Tag Mifare Classic \n\r");
+       
+        MifareClassic mifareClassic = MifareClassic(*shield);
+        return mifareClassic.read(uid, uidLength);
+    }
+    else if (type == TAG_TYPE_2)
+    {
+        
+        printf("\r Es un Tag Mifare Ultralight \n\r");
+        
+        MifareUltralight ultralight = MifareUltralight(*shield);
+        return ultralight.read(uid, uidLength);
+    }
+    else if (type == TAG_TYPE_UNKNOWN)
+    {
+        printf("\r tipo de Tag no deteminado \n\r");
+        //DMSG("Can not determine tag type for ATQA 0x");
+        //Serial.print(atqa, HEX);DMSG(" SAK 0x");DMSG(sak, HEX);
+        return NfcTag(uid, uidLength);
+    }
+    else
+    {
+        DMSG("No driver for card type ");
+        DMSG_INT(type);
+        // TODO should set type here
+        return NfcTag(uid, uidLength);
+    }
+
+}
+
+bool NfcAdapter::write(NdefMessage& ndefMessage)
+{
+    bool success;
+
+    if (uidLength == 4)
+    {
+        MifareClassic mifareClassic = MifareClassic(*shield);
+        success = mifareClassic.write(ndefMessage, uid, uidLength);
+    }
+    else
+    {
+        DMSG("Unsupported Tag");
+        success = false;
+    }
+    return success;
+}
+
+// TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown
+// Guess Tag Type by looking at the ATQA and SAK values
+// Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ???
+unsigned int NfcAdapter::guessTagType()
+{
+
+    // 4 uint8_t id - Mifare Classic
+    //  - ATQA 0x4 && SAK 0x8
+    // 7 uint8_t id
+    //  - ATQA 0x44 && SAK 0x8 - Mifare Classic
+    //  - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2
+    //  - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4
+
+    if (uidLength == 4)
+    {
+        return TAG_TYPE_MIFARE_CLASSIC;
+    }
+    else
+    {
+        return TAG_TYPE_2;
+    }
+}
--- a/NfcAdapter.h	Fri Apr 24 18:17:09 2015 +0000
+++ b/NfcAdapter.h	Fri Apr 24 19:02:44 2015 +0000
@@ -33,11 +33,13 @@
         // FUTURE bool unshare();
         // FUTURE bool erase();
         // FUTURE bool format();
+        unsigned int guessTagType();
+        
     private:
         PN532* shield;
         uint8_t uid[7];    // Buffer to store the returned UID
         unsigned int uidLength; // Length of the UID (4 or 7 uint8_ts depending on ISO14443A card type)
-        unsigned int guessTagType();
+        
 };
 
 #endif
--- a/NfcTag.cpp	Fri Apr 24 18:17:09 2015 +0000
+++ b/NfcTag.cpp	Fri Apr 24 19:02:44 2015 +0000
@@ -103,6 +103,8 @@
                 }
         
     }
+    
+
     return uidString;
 }
 
--- a/PN532.cpp	Fri Apr 24 18:17:09 2015 +0000
+++ b/PN532.cpp	Fri Apr 24 19:02:44 2015 +0000
@@ -9,6 +9,7 @@
 #include "PN532.h"
 #include "PN532_debug.h"
 #include <string.h>
+#include "mbed.h"
 
 #ifndef ARDUINO
 #include <stdio.h>
--- a/PN532_SPI.cpp	Fri Apr 24 18:17:09 2015 +0000
+++ b/PN532_SPI.cpp	Fri Apr 24 19:02:44 2015 +0000
@@ -1,6 +1,7 @@
 
 #include "PN532_SPI.h"
 #include "PN532_debug.h"
+#include "mbed.h"
 
 #define STATUS_READ     2
 #define DATA_WRITE      1
@@ -14,8 +15,7 @@
     _spi->frequency(2000000);
 
     _ss  = 1;
-}
-
+}/*
 PN532_SPI::PN532_SPI(SPI *spi, PinName ss) : _ss(ss)
 {
     command = 0;
@@ -24,8 +24,7 @@
     _spi->frequency(2000000);
 
     _ss  = 1;
-}
-
+}*/
 void PN532_SPI::begin()
 {
 
--- a/PN532_debug.h	Fri Apr 24 18:17:09 2015 +0000
+++ b/PN532_debug.h	Fri Apr 24 19:02:44 2015 +0000
@@ -2,10 +2,11 @@
 #define __DEBUG_H__
 
 //#define DEBUG
+#include <stdio.h>
 
 #ifdef DEBUG
 
-#include <stdio.h>
+
 
 #define DMSG(args...)   printf(args)
 #define DMSG_STR(str)   printf("%s\n", str)