![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test example
Fork of readMifare by
Diff: main.cpp
- Revision:
- 6:9109b95c3e97
- Parent:
- 5:4b238fd5f347
- Child:
- 7:016813c4a92a
--- a/main.cpp Thu Mar 17 09:49:16 2016 +0000 +++ b/main.cpp Thu Mar 17 09:58:57 2016 +0000 @@ -1,42 +1,3 @@ -/**************************************************************************/ -/*! - - @file main.cpp - @author Adafruit Industries - @modified_by Ben Zhang <benzh@eecs.berkeley.edu> - @license BSD (see license.txt) - - This example will wait for any ISO14443A card or tag, and - depending on the size of the UID will attempt to read from it. - - If the card has a 4-byte UID it is probably a Mifare - Classic card, and the following steps are taken: - - - Authenticate block 4 (the first block of Sector 1) using - the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF - - If authentication succeeds, we can then read any of the - 4 blocks in that sector (though only block 4 is read here) - - If the card has a 7-byte UID it is probably a Mifare - Ultralight card, and the 4 byte pages can be read directly. - Page 4 is read by default since this is the first 'general- - purpose' page on the tags. - - -This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards -This library works with the Adafruit NFC breakout - ----> https://www.adafruit.com/products/364 - -Check out the links above for our tutorials and wiring diagrams -These chips use SPI to communicate, 4 required to interface - -Adafruit invests time and resources providing this open source code, -please support Adafruit and open-source hardware by purchasing -products from Adafruit! - -*/ -/**************************************************************************/ - #include "mbed.h" #include "Adafruit_PN532.h" @@ -51,127 +12,70 @@ void loop(void); int sectorToBlock(int sectorIndex); -int main() { - pc.printf("Hello!\r\n"); - // By default, no color - - nfc.begin(); +int main() +{ + pc.printf("Hello!\r\n"); + nfc.begin(); - uint32_t versiondata = nfc.getFirmwareVersion(); - if (! versiondata) { - pc.printf("Didn't find PN53x board"); - while (1); // halt - } - // Got ok data, print it out! - pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\r\n", - versiondata >> 24, - (versiondata >> 16) & 0xFF, - (versiondata >> 8) & 0xFF); - - // configure board to read RFID tags - nfc.SAMConfig(); - - pc.printf("Waiting for an ISO14443A Card ...\r\n"); - - while(1) { loop(); } + uint32_t versiondata = nfc.getFirmwareVersion(); + if (! versiondata) { + pc.printf("Didn't find PN53x board"); + while (1); + } + + pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\r\n", versiondata >> 24, (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); + nfc.SAMConfig(); + pc.printf("Waiting for an ISO14443A Card ...\r\n"); + + while(1) { + loop(); + } } -void loop(void) { - // Turn back to no color +void loop(void) +{ + 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) + int sector = 32; + + if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) { + // Display some basic information about the card + 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("\r\n"); + + if (uidLength == 4) { + pc.printf("Seems to be a Mifare Classic card (4 byte UID)\r\n"); + pc.printf("Trying to authenticate sector %d with default KEYA value\r\n", sector); + uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; + int blockNumber = sectorToBlock(sector); - 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) - int sector = 32; - - // 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); - - if (success) { - // Display some basic information about the card - 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("\r\n"); - - if (uidLength == 4) - { - // We probably have a Mifare Classic card ... - 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 sector %d with default KEYA value\r\n", sector); - uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; - int blockNumber = sectorToBlock(sector); - - // 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, blockNumber, 0, keya); - - if (success) - { - // Try to read the contents of block 4 - int respSize = 16; - uint8_t data[respSize]; - - success = nfc.mifareclassic_ReadDataBlock(blockNumber, data); - - if (success) - { - // Data seems to have been read ... spit it out - pc.printf("Reading Block %d:\r\n\t", 128); - nfc.PrintHexChar(data, respSize); - pc.printf("\r\n"); - - - - // Wait a bit before reading the card again - wait(1); + if (nfc.mifareclassic_AuthenticateBlock(uid, uidLength, blockNumber, 0, keya)) { + uint8_t data; + + if (nfc.mifareclassic_ReadDataBlock(blockNumber, &data)) { + // Data seems to have been read ... spit it out + pc.printf("Reading Block %d:\r\n\t", 128); + nfc.PrintHexChar(&data, 16); + 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?\r\n"); } - else - { - pc.printf("Ooops ... unable to read the requested block. Try another key?\r\n"); - } - } - else - { - 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)\r\n"); - - // Try to read the first general-purpose user page (#4) - 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("\r\n"); - - // Wait a bit before reading the card again - wait(1); - } - else - { - pc.printf("Ooops ... unable to read the requested page!?\r\n"); - } - } - } } - -int sectorToBlock(int sectorIndex) { + +int sectorToBlock(int sectorIndex) +{ if (sectorIndex < 32) { return sectorIndex * 4; } else {