![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test example
Fork of readMifare by
Diff: main.cpp
- Revision:
- 3:08251c301e26
- Parent:
- 2:96f420fa4a6e
- Child:
- 4:70acff42d8b4
diff -r 96f420fa4a6e -r 08251c301e26 main.cpp --- a/main.cpp Mon Nov 03 05:48:29 2014 +0000 +++ b/main.cpp Wed Mar 16 09:52:22 2016 +0000 @@ -40,15 +40,10 @@ #include "mbed.h" #include "Adafruit_PN532.h" -#define SS PTD0 -// PTD1 is also LED_BLUE, it will blink during SPI communication. -#define SCK PTD1 -#define MOSI PTD2 -#define MISO PTD3 - -DigitalOut red(LED_RED); -DigitalOut green(LED_GREEN); -DigitalOut blue(LED_BLUE); +#define MOSI p5 +#define MISO p6 +#define SCK p7 +#define SS p8 Serial pc(USBTX, USBRX); Adafruit_PN532 nfc(SCK, MISO, MOSI, SS); @@ -56,9 +51,8 @@ void loop(void); int main() { - pc.printf("Hello!\n"); + pc.printf("Hello!\r\n"); // By default, no color - green = 1; red = 1, blue = 1; nfc.begin(); @@ -68,7 +62,7 @@ while (1); // halt } // Got ok data, print it out! - pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\n", + pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\r\n", versiondata >> 24, (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); @@ -76,7 +70,7 @@ // configure board to read RFID tags nfc.SAMConfig(); - pc.printf("Waiting for an ISO14443A Card ...\n"); + pc.printf("Waiting for an ISO14443A Card ...\r\n"); while(1) { loop(); } } @@ -84,7 +78,6 @@ void loop(void) { // Turn back to no color - green = 1; red = 1, blue = 1; uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID @@ -96,118 +89,79 @@ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { - // Turn off to indicate Card swipe - green = 1; red = 1, blue = 1; // Display some basic information about the card - pc.printf("\n\nFound an ISO14443A card\n"); + pc.printf("\r\n\r\nFound an ISO14443A card\r\n"); pc.printf(" UID Length: %d bytes", uidLength); pc.printf(" UID Value: "); nfc.PrintHex(uid, uidLength); - pc.printf("\n"); + pc.printf("\r\n"); if (uidLength == 4) { // We probably have a Mifare Classic card ... - pc.printf("Seems to be a Mifare Classic card (4 byte UID)\n"); + pc.printf("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 - pc.printf("Trying to authenticate block 4 with default KEYA value\n"); - uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + pc.printf("Trying to authenticate block 4 with default KEYA value\r\n"); + uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; + int block = 128; // 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) - { - // Turn colors to indicate which tag - //green = 0; red = 1; - uint8_t tag_1 = 250; - uint8_t tag_2 = 186; - uint8_t tag_3 = 106; - uint8_t tag_4 = 10; - if (uid[0] == tag_1) - green = 0; - else if (uid[0] == tag_2) - red = 0; - else if (uid[0] == tag_3) { - green = 0, red = 0; - } else if (uid[0] == tag_4) { - red = 0, blue = 0; - } - - pc.printf("Sector 1 (Blocks 4..7) has been authenticated\n"); - uint8_t data[16]; + success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, block, 0, keya); - // If you want to write something to block 4 to test with, remove - // the definition of data above, and uncomment the following lines - /************************************************************************ - uint8_t data[16] = { 'b', 'e', 'n', ' ', 'z', 'h', 'a', 'n', 'g' }; - success = nfc.mifareclassic_WriteDataBlock (4, data); - if (success) - { - // Data seems to have been read ... spit it out - pc.printf("Data Written to Block 4:\n\t"); - nfc.PrintHexChar(data, 16); - pc.printf("\n"); - - // Wait a bit before reading the card again - wait(1); - } - else - { - pc.printf("Ooops ... unable to write the requested block.\n"); - } - return; - ************************************************************************/ - + if (success) + { // 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 - pc.printf("Reading Block 4:\n\t"); - nfc.PrintHexChar(data, 16); - pc.printf("\n"); - - // Wait a bit before reading the card again - wait(1); - } - else - { - pc.printf("Ooops ... unable to read the requested block. Try another key?\n"); - }*/ + int respSize = 16; + uint8_t data[respSize]; + + success = nfc.mifareclassic_ReadDataBlock(block, data); + + if (success) + { + // Data seems to have been read ... spit it out + pc.printf("Reading Block %d:\r\n\t", block); + nfc.PrintHexChar(data, respSize); + pc.printf("\r\n"); + + // Wait a bit before reading the card again + wait(1); + } + else + { + pc.printf("Ooops ... unable to read the requested block. Try another key?\r\n"); + } } else { - pc.printf("Ooops ... authentication failed: Try another key?\n"); + pc.printf("Ooops ... authentication failed: Try another key?\r\n"); } } if (uidLength == 7) { // We probably have a Mifare Ultralight card ... - pc.printf("Seems to be a Mifare Ultralight tag (7 byte UID)\n"); + pc.printf("Seems to be a Mifare Ultralight tag (7 byte UID)\r\n"); // Try to read the first general-purpose user page (#4) - pc.printf("Reading page 4\n"); + pc.printf("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 nfc.PrintHexChar(data, 4); - pc.printf("\n"); + pc.printf("\r\n"); // Wait a bit before reading the card again wait(1); } else { - pc.printf("Ooops ... unable to read the requested page!?\n"); + pc.printf("Ooops ... unable to read the requested page!?\r\n"); } } }