Milan Jovanovic
/
readMifareORG
Test example
Fork of readMifare by
main.cpp
- Committer:
- mjovanov1
- Date:
- 2016-03-17
- Revision:
- 9:be80c24599b5
- Parent:
- 7:016813c4a92a
- Child:
- 10:1773f3d5bdda
File content as of revision 9:be80c24599b5:
#include "mbed.h" #include "Adafruit_PN532.h" #define MOSI p11 #define MISO p12 #define SCK p13 #define SS p20 Serial pc(USBTX, USBRX); Adafruit_PN532 nfc(SCK, MISO, MOSI, SS); void loop(void); int sectorToBlock(int sectorIndex); int main() { pc.printf("Hello v1.0!\r\n"); nfc.begin(); 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) { 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); 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", blockNumber); 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"); } } } } int sectorToBlock(int sectorIndex) { if (sectorIndex < 32) { return sectorIndex * 4; } else { return 32 * 4 + (sectorIndex - 32) * 16; } }