Use Seeeduino Arch (or Arch Pro) + NFC Shield to write a Mifare Classic tag

Dependencies:   PN532 USBDevice mbed

Revision:
2:56402cae2c3c
Parent:
1:c5b5be0d6c5d
Child:
3:59afaef9fb24
--- a/main.cpp	Thu Oct 17 07:10:06 2013 +0000
+++ b/main.cpp	Thu Nov 21 06:31:29 2013 +0000
@@ -1,66 +1,39 @@
 #include "mbed.h"
-#include "PN532.h"
-
-#if 0
-
-#define LOG(args...)
-
-#else
-#include "USBSerial.h"
+#include "PN532_SPI.h"
+#include "NfcAdapter.h"
 
 #define LOG(args...)        pc.printf(args)
 
+#if 0                           // Using Seeeduino Arch
+#include "USBSerial.h"
 USBSerial pc;
+SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
+PN532_SPI pn532spi(spi, P0_2)
+
+#else                           // Using Arch Pro
+Serial pc(USBTX, USBRX);
+SPI spi(P0_18, P0_17, P0_15);
+PN532_SPI pn532spi(spi, P0_6);
 #endif
 
-// PN532(mosi, miso, clk, cs)
-PN532 nfc(P1_22, P1_21, P1_20, P0_2);
-
-DigitalOut led1(LED1);
-
+NfcAdapter nfc(pn532spi);
+DigitalOut led(LED1);
 
 
 int main() {
-
     nfc.begin();
-    uint32_t versiondata = nfc.getFirmwareVersion();
-    if (! versiondata) {
-        LOG("Didn't find PN532\r\n");
-        while (1) {
-            led1 = !led1;
-            wait(0.1);
-        }
-    } 
-    
-    LOG("Found chip PN5%2X\r\n", versiondata >> 24);
-    LOG("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
-    
-    nfc.SAMConfig();
-    
+    while (1) {
+        if (nfc.tagPresent()) {
+            NdefMessage message = NdefMessage();
+            message.addUriRecord("http://seeedstudio.com");
     
-    while(1) {
-        bool success;
-        uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
-        uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
-
-        // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
-        // 'uid' will be populated with the UID, and uidLength will indicate
-        // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
-        success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
-        
-        if (success) {
-            led1 = 1;
-            LOG("Found a card\r\n");
-            LOG("UID length: %d\r\nUID Value: ", uidLength);
-            for (uint8_t i=0; i < uidLength; i++) {
-              LOG("0x%X ", uid[i]); 
+            bool success = nfc.write(message);
+            if (success) {
+                LOG("Success. Try reading this tag with your phone.");        
+            } else {
+                LOG("Write failed.");
             }
-            
-            wait(1);
-            led1 = 0;
-        } else {
-            LOG("Time out\r\n");
-            wait(1);
         }
+        wait(6);
     }
 }