Test example

Dependencies:   mbed

Fork of readMifare by Interactive Device Design

Committer:
mjovanov1
Date:
Thu Mar 17 09:58:57 2016 +0000
Revision:
6:9109b95c3e97
Parent:
5:4b238fd5f347
Child:
7:016813c4a92a
Cleaned

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nebgnahz 0:54bf4b21c7fa 1 #include "mbed.h"
nebgnahz 0:54bf4b21c7fa 2 #include "Adafruit_PN532.h"
nebgnahz 0:54bf4b21c7fa 3
mjovanov1 4:70acff42d8b4 4 #define MOSI p11
mjovanov1 4:70acff42d8b4 5 #define MISO p12
mjovanov1 4:70acff42d8b4 6 #define SCK p13
mjovanov1 4:70acff42d8b4 7 #define SS p20
nebgnahz 0:54bf4b21c7fa 8
nebgnahz 0:54bf4b21c7fa 9 Serial pc(USBTX, USBRX);
nebgnahz 0:54bf4b21c7fa 10 Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);
nebgnahz 0:54bf4b21c7fa 11
nebgnahz 0:54bf4b21c7fa 12 void loop(void);
mjovanov1 5:4b238fd5f347 13 int sectorToBlock(int sectorIndex);
nebgnahz 0:54bf4b21c7fa 14
mjovanov1 6:9109b95c3e97 15 int main()
mjovanov1 6:9109b95c3e97 16 {
mjovanov1 6:9109b95c3e97 17 pc.printf("Hello!\r\n");
mjovanov1 6:9109b95c3e97 18 nfc.begin();
nebgnahz 0:54bf4b21c7fa 19
mjovanov1 6:9109b95c3e97 20 uint32_t versiondata = nfc.getFirmwareVersion();
mjovanov1 6:9109b95c3e97 21 if (! versiondata) {
mjovanov1 6:9109b95c3e97 22 pc.printf("Didn't find PN53x board");
mjovanov1 6:9109b95c3e97 23 while (1);
mjovanov1 6:9109b95c3e97 24 }
mjovanov1 6:9109b95c3e97 25
mjovanov1 6:9109b95c3e97 26 pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\r\n", versiondata >> 24, (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
mjovanov1 6:9109b95c3e97 27 nfc.SAMConfig();
mjovanov1 6:9109b95c3e97 28 pc.printf("Waiting for an ISO14443A Card ...\r\n");
mjovanov1 6:9109b95c3e97 29
mjovanov1 6:9109b95c3e97 30 while(1) {
mjovanov1 6:9109b95c3e97 31 loop();
mjovanov1 6:9109b95c3e97 32 }
nebgnahz 0:54bf4b21c7fa 33 }
nebgnahz 0:54bf4b21c7fa 34
nebgnahz 0:54bf4b21c7fa 35
mjovanov1 6:9109b95c3e97 36 void loop(void)
mjovanov1 6:9109b95c3e97 37 {
mjovanov1 6:9109b95c3e97 38 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
mjovanov1 6:9109b95c3e97 39 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
mjovanov1 6:9109b95c3e97 40 int sector = 32;
mjovanov1 6:9109b95c3e97 41
mjovanov1 6:9109b95c3e97 42 if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {
mjovanov1 6:9109b95c3e97 43 // Display some basic information about the card
mjovanov1 6:9109b95c3e97 44 pc.printf("\r\n\r\nFound an ISO14443A card\r\n");
mjovanov1 6:9109b95c3e97 45 pc.printf(" UID Length: %d bytes", uidLength);
mjovanov1 6:9109b95c3e97 46 pc.printf(" UID Value: ");
mjovanov1 6:9109b95c3e97 47 nfc.PrintHex(uid, uidLength);
mjovanov1 6:9109b95c3e97 48 pc.printf("\r\n");
mjovanov1 6:9109b95c3e97 49
mjovanov1 6:9109b95c3e97 50 if (uidLength == 4) {
mjovanov1 6:9109b95c3e97 51 pc.printf("Seems to be a Mifare Classic card (4 byte UID)\r\n");
mjovanov1 6:9109b95c3e97 52 pc.printf("Trying to authenticate sector %d with default KEYA value\r\n", sector);
mjovanov1 6:9109b95c3e97 53 uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
mjovanov1 6:9109b95c3e97 54 int blockNumber = sectorToBlock(sector);
nebgnahz 0:54bf4b21c7fa 55
mjovanov1 6:9109b95c3e97 56 if (nfc.mifareclassic_AuthenticateBlock(uid, uidLength, blockNumber, 0, keya)) {
mjovanov1 6:9109b95c3e97 57 uint8_t data;
mjovanov1 6:9109b95c3e97 58
mjovanov1 6:9109b95c3e97 59 if (nfc.mifareclassic_ReadDataBlock(blockNumber, &data)) {
mjovanov1 6:9109b95c3e97 60 // Data seems to have been read ... spit it out
mjovanov1 6:9109b95c3e97 61 pc.printf("Reading Block %d:\r\n\t", 128);
mjovanov1 6:9109b95c3e97 62 nfc.PrintHexChar(&data, 16);
mjovanov1 6:9109b95c3e97 63 pc.printf("\r\n");
mjovanov1 6:9109b95c3e97 64
mjovanov1 6:9109b95c3e97 65 // Wait a bit before reading the card again
mjovanov1 6:9109b95c3e97 66 wait(1);
mjovanov1 6:9109b95c3e97 67 } else {
mjovanov1 6:9109b95c3e97 68 pc.printf("Ooops ... unable to read the requested block. Try another key?\r\n");
mjovanov1 6:9109b95c3e97 69 }
mjovanov1 6:9109b95c3e97 70 } else {
mjovanov1 6:9109b95c3e97 71 pc.printf("Ooops ... authentication failed: Try another key?\r\n");
mjovanov1 3:08251c301e26 72 }
mjovanov1 6:9109b95c3e97 73 }
nebgnahz 0:54bf4b21c7fa 74 }
mjovanov1 5:4b238fd5f347 75 }
mjovanov1 6:9109b95c3e97 76
mjovanov1 6:9109b95c3e97 77 int sectorToBlock(int sectorIndex)
mjovanov1 6:9109b95c3e97 78 {
mjovanov1 5:4b238fd5f347 79 if (sectorIndex < 32) {
mjovanov1 5:4b238fd5f347 80 return sectorIndex * 4;
mjovanov1 5:4b238fd5f347 81 } else {
mjovanov1 5:4b238fd5f347 82 return 32 * 4 + (sectorIndex - 32) * 16;
mjovanov1 5:4b238fd5f347 83 }
nebgnahz 0:54bf4b21c7fa 84 }