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

Dependencies:   PN532 USBDevice mbed

main.cpp

Committer:
yihui
Date:
2013-10-08
Revision:
0:b81d113f3a83
Child:
1:c5b5be0d6c5d

File content as of revision 0:b81d113f3a83:

#include "mbed.h"
#include "PN532.h"
#include "USBSerial.h"

// PN532(mosi, miso, clk, cs)
PN532 nfc(P1_22, P1_21, P1_20, P0_2);

DigitalOut myled(LED1);

USBSerial pc;

int main() {

    nfc.begin();
    uint32_t versiondata = nfc.getFirmwareVersion();
    if (! versiondata) {
        pc.printf("Didn't find PN532\r\n");
        while (1) {
            myled = !myled;
            wait(0.1);
        }
    } 
    
    pc.printf("Found chip PN5%2X\r\n", versiondata >> 24);
    pc.printf("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
    
    nfc.SAMConfig();
    
    
    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) {
            myled = 1;
            pc.printf("Found a card\r\n");
            pc.printf("UID length: %d\r\nUID Value: ", uidLength);
            for (uint8_t i=0; i < uidLength; i++) {
              pc.printf("0x%X ", uid[i]); 
            }
            
            wait(1);
            myled = 0;
        } else {
            pc.printf("Time out\r\n");
            wait(1);
        }
    }
}