Use Seeeduino Arch (or Arch Pro) + NFC Shield to read a Mifare Classic tag
Fork of PN532_P2P by
Diff: main.cpp
- Revision:
- 2:b238c6772bc4
- Parent:
- 1:a9df716f8a64
--- a/main.cpp Thu Oct 17 07:47:22 2013 +0000 +++ b/main.cpp Thu Nov 21 07:11:15 2013 +0000 @@ -1,126 +1,35 @@ #include "mbed.h" -#include "PN532.h" +#include "PN532_SPI.h" +#include "NfcAdapter.h" -#if 0 // Output debug message or not +#if 0 // Using Seeeduino Arch #define LOG(args...) -#else -#include "USBSerial.h" +SPI spi(P1_22, P1_21, P1_20); // SPI(mosi, miso, clk) +PN532_SPI pn532spi(spi, P0_2); + +#else // Using Arch Pro #define LOG(args...) pc.printf(args) - -USBSerial pc; // Use USB to Serial -//Serial pc(USBTX, USBRX); +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); - -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(); +NfcAdapter nfc(pn532spi); +DigitalOut led(LED1); - while (1) { - uint8_t 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, &uidLength, 0); - - if (success) { - // Display some basic information about the card - LOG("Found an ISO14443A card\r\n"); - LOG("UID length: %d\r\nUID Value: ", uidLength); - for (uint8_t i = 0; i < uidLength; i++) { - LOG("0x%X ", uid[i]); - } - - if (uidLength == 4) { - // We probably have a Mifare Classic card ... - LOG("Seems to be a Mifare Classic card (4 byte UID)\r\n"); - - // Now we need to try to authenticate it for read/write access - // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF - LOG("Trying to authenticate block 4 with default KEYA value\r\n"); - uint8_t keya[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }; - // Start with block 4 (the first block of sector 1) since sector 0 - // contains the manufacturer data and it's probably better just - // to leave it alone unless you know what you're doing - success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); - - if (success) { - LOG("Sector 1 (Blocks 4..7) has been authenticated\r\n"); - uint8_t data[16]; - - // If you want to write something to block 4 to test with, uncomment - // the following line and this text should be read back in a minute - // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0}; - // success = nfc.mifareclassic_WriteDataBlock (4, data); - - // Try to read the contents of block 4 - success = nfc.mifareclassic_ReadDataBlock(4, data); - - if (success) { - // Data seems to have been read ... spit it out - LOG("Reading Block 4:\r\n"); - for (int i = 0; i < 16; i++) { - LOG("%2X ", data[i]); - } - LOG(" "); - for (int i = 0; i < 16; i++) { - LOG("%c", (char)data[i]); - } - LOG("\r\n"); - } else { - LOG("Ooops ... unable to read the requested block. Try another key?\r\n"); - } - } else { - LOG("Ooops ... authentication failed: Try another key?\r\n"); - } - } - - if (uidLength == 7) { - // We probably have a Mifare Ultralight card ... - LOG("Seems to be a Mifare Ultralight tag (7 byte UID)\r\n"); - - // Try to read the first general-purpose user page (#4) - LOG("Reading page 4\r\n"); - uint8_t data[32]; - success = nfc.mifareultralight_ReadPage (4, data); - if (success) { - // Data seems to have been read ... spit it out - for (int i = 0; i < 4; i++) { - LOG("%2X ", data[i]); - } - LOG(" "); - for (int i = 0; i < 4; i++) { - LOG("%c", (char)data[i]); - } - LOG("\r\n"); - } else { - LOG("Ooops ... unable to read the requested page!?\r\n"); - } +int main() { + LOG("NDEF Reader\n"); + nfc.begin(); + while (1) { + LOG("\nScan a NFC tag\n"); + if (nfc.tagPresent()) { + NfcTag tag = nfc.read(); // tag.print() doesn't work yet + if (tag.hasNdefMessage()) { + LOG("Get a NDEF message."); + NdefMessage message = tag.getNdefMessage(); } } - - wait(3); + wait(5); } } -