MFRC522 Writing and reading RFID tag

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Test of cheap 13.56Mhz RFID-RC522 module
00002 //Connect as follows:
00003 //RFID pins        ->  WIZWiki-W7500 header CN5 (Arduino-compatible header)
00004 //----------------------------------------
00005 //1.RFID IRQ    ->   Not used. Leave open
00006 //2.RFID MISO   ->   WIZWiki-W7500 SPI_MISO     =D12
00007 //3.RFID MOSI   ->   WIZWiki-W7500 SPI_MOSI     =D11
00008 //4.RFID SCK    ->   WIZWiki-W7500 SPI_SCK      =D13
00009 //5.RFID SDA    ->   WIZWiki-W7500 SPI_CS       =D10
00010 //6.RFID RST    ->   WIZWiki-W7500              =D9
00011 //3.3V and Gnd to the respective pins
00012 
00013 
00014 //Adding Library for Mbed
00015 #include "mbed.h"
00016 //Adding Library for MFRC522
00017 #include "MFRC522.h"
00018 //Adding Library for SPI protocol
00019 #include "SPI.h"
00020 #define VERSION "RFID_2017_04_03"
00021 #define CIBLE "WIZwiki-W7500"
00022 
00023 
00024 // ARMmbed WIZwiki W7500 Pin for MFRC522 SPI Communication
00025 #define SPI_MOSI    D11
00026 #define SPI_MISO    D12
00027 #define SPI_SCLK    D13
00028 #define SPI_CS      D10
00029 
00030 // WIZWiki-W7500 Pin for MFRC522 reset(pick another D pin if you need D8)
00031 #define MF_RESET    D9
00032 
00033 
00034 //Declaration of LED
00035 DigitalOut LedGreen(D7);
00036 DigitalOut LedRed(D6);
00037 DigitalOut LedYellow(D5);
00038 
00039 //Serial connection to PC for output
00040 Serial pc(USBTX, USBRX);
00041 
00042 
00043 //Declaration MFRC522 class
00044 MFRC522    RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
00045 
00046 //* Local functions */
00047 void DumpToSerial(MFRC522::Uid *uid);
00048 void DumpMifareClassicToSerial      (MFRC522::Uid *uid, uint8_t piccType, MFRC522::MIFARE_Key *key);
00049 void DumpMifareClassicSectorToSerial(MFRC522::Uid *uid, MFRC522::MIFARE_Key *key, uint8_t sector);
00050 void DumpMifareUltralightToSerial   (void);
00051 
00052 
00053 //Create function for writting data
00054 void WriteToRfidTag(MFRC522::Uid *uid)
00055 {
00056 
00057     //Declaration of MFRC522 key
00058     MFRC522::MIFARE_Key key;
00059     //Declaration of MFRC522 status
00060     MFRC522::StatusCode status;
00061     uint8_t buffer[16];
00062     uint8_t block;
00063     uint8_t len;
00064     while(true) {
00065         LedGreen=1;
00066         for (uint8_t i = 0; i < 6; i++) {
00067             key.keyByte[i] = 0xFF;
00068         }
00069         // Look for new cards
00070         if ( ! RfChip.PICC_IsNewCardPresent()) {
00071             continue;
00072         }
00073 
00074         // Select one of the cards
00075         if ( ! RfChip.PICC_ReadCardSerial()) {
00076             continue;
00077         }
00078 
00079         printf("\n\r");
00080         pc.printf("Card UID: ");   //Dump UID
00081         for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00082             pc.printf(RfChip.uid.uidByte[i] < 0x10 ? " 0" : " ");
00083             printf(" %X", RfChip.uid.uidByte[i]);
00084         }
00085         printf("\n\r");
00086         pc.printf("PICC type: ");   // Dump PICC type
00087         uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00088         pc.printf( RfChip.PICC_GetTypeName(piccType));
00089         LedGreen=0;
00090 
00091         printf("\n\r");
00092         pc.printf("Enter 1 block: "); // Enter Data
00093         len=sizeof(buffer);
00094         for(uint8_t i = 0; i < 16; i++) {
00095             buffer[i] = pc.putc(pc.getc());
00096         }
00097         printf("\n\r");
00098         block = 1;
00099         pc.printf("Authenticating using key A...");
00100         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00101         if (status != MFRC522::STATUS_OK) {
00102             pc.printf("PCD_Authenticate() failed: ");
00103             pc.printf(RfChip.GetStatusCodeName(status));
00104             LedRed=1;
00105             continue;
00106         }
00107         // Write block
00108         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00109         if (status != MFRC522::STATUS_OK) {
00110             pc.printf("MIFARE_Write() failed: ");
00111             pc.printf(RfChip.GetStatusCodeName(status));
00112             LedRed=1;
00113             continue;
00114         } else {
00115             LedYellow=1;
00116             pc.printf("Succesfully Written to tag: ");
00117         }
00118         printf("\n\r");
00119         pc.printf("Enter 2 block: "); // Enter Data
00120         len=sizeof(buffer);
00121         for(uint8_t i = 0; i < 16; i++) {
00122             buffer[i] = pc.putc(pc.getc());
00123         }
00124         printf("\n\r");
00125         block = 2;
00126         pc.printf("Authenticating using key A...");
00127         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00128         if (status != MFRC522::STATUS_OK) {
00129             pc.printf("PCD_Authenticate() failed: ");
00130             pc.printf(RfChip.GetStatusCodeName(status));
00131             LedRed=1;
00132             continue;
00133         }
00134 
00135         // Write block
00136         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00137         if (status != MFRC522::STATUS_OK) {
00138             pc.printf("MIFARE_Write() failed: ");
00139             pc.printf(RfChip.GetStatusCodeName(status));
00140             LedRed=1;
00141             continue;
00142         } else {
00143             LedYellow=1;
00144             pc.printf("Succesfully Written to tag: ");
00145         }
00146         printf("\n\r");
00147         pc.printf("Enter 4 block: "); // Enter Data
00148         len=sizeof(buffer);
00149         for(uint8_t i = 0; i < 16; i++) {
00150             buffer[i] = pc.putc(pc.getc());
00151         }
00152         printf("\n\r");
00153         block = 4;
00154         pc.printf("Authenticating using key A...");
00155         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00156         if (status != MFRC522::STATUS_OK) {
00157             pc.printf("PCD_Authenticate() failed: ");
00158             pc.printf(RfChip.GetStatusCodeName(status));
00159             LedRed=1;
00160             continue;
00161         }
00162         // Write block
00163         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00164         if (status != MFRC522::STATUS_OK) {
00165             pc.printf("MIFARE_Write() failed: ");
00166             pc.printf(RfChip.GetStatusCodeName(status));
00167             LedRed=1;
00168             continue;
00169         } else {
00170             LedYellow=1;
00171             pc.printf("Succesfully Written to tag: ");
00172         }
00173         printf("\n\r");
00174         pc.printf("Enter 5 block: "); // Enter Data
00175         len=sizeof(buffer);
00176         for(uint8_t i = 0; i < 16; i++) {
00177             buffer[i] = pc.putc(pc.getc());
00178         }
00179         printf("\n\r");
00180         block = 5;
00181         pc.printf("Authenticating using key A...");
00182         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00183         if (status != MFRC522::STATUS_OK) {
00184             pc.printf("PCD_Authenticate() failed: ");
00185             pc.printf(RfChip.GetStatusCodeName(status));
00186             LedRed=1;
00187             continue;
00188         }
00189 
00190         // Write block
00191         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00192         if (status != MFRC522::STATUS_OK) {
00193             pc.printf("MIFARE_Write() failed: ");
00194             pc.printf(RfChip.GetStatusCodeName(status));
00195             LedRed=1;
00196             continue;
00197         } else {
00198             LedYellow=1;
00199             pc.printf("Succesfully Written to tag: ");
00200         }
00201         printf("\n\r");
00202         pc.printf("Enter 6 block: "); // Enter Data
00203         len=sizeof(buffer);
00204         for(uint8_t i = 0; i < 16; i++) {
00205             buffer[i] = pc.putc(pc.getc());
00206         }
00207         printf("\n\r");
00208         block = 6;
00209         pc.printf("Authenticating using key A...");
00210         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00211         if (status != MFRC522::STATUS_OK) {
00212             pc.printf("PCD_Authenticate() failed: ");
00213             pc.printf(RfChip.GetStatusCodeName(status));
00214             LedRed=1;
00215             continue;
00216         }
00217         // Write block
00218         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00219         if (status != MFRC522::STATUS_OK) {
00220             pc.printf("MIFARE_Write() failed: ");
00221             pc.printf(RfChip.GetStatusCodeName(status));
00222             LedRed=1;
00223             continue;
00224         } else {
00225             LedYellow=1;
00226             pc.printf("Succesfully Written to tag: ");
00227         }
00228         printf("\n\r");
00229         pc.printf("Enter 8 block: "); // Enter Data
00230         len=sizeof(buffer);
00231         for(uint8_t i = 0; i < 16; i++) {
00232             buffer[i] = pc.putc(pc.getc());
00233         }
00234         printf("\n\r");
00235         block = 8;
00236         pc.printf("Authenticating using key A...");
00237         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00238         if (status != MFRC522::STATUS_OK) {
00239             pc.printf("PCD_Authenticate() failed: ");
00240             pc.printf(RfChip.GetStatusCodeName(status));
00241             LedRed=1;
00242             continue;
00243         }
00244 
00245         // Write block
00246         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00247         if (status != MFRC522::STATUS_OK) {
00248             pc.printf("MIFARE_Write() failed: ");
00249             pc.printf(RfChip.GetStatusCodeName(status));
00250             LedRed=1;
00251             continue;
00252         } else {
00253             LedYellow=1;
00254             pc.printf("Succesfully Written to tag: ");
00255         }
00256         printf("\n\r");
00257         pc.printf("Enter 9 block: "); // Enter Data
00258         len=sizeof(buffer);
00259         for(uint8_t i = 0; i < 16; i++) {
00260             buffer[i] = pc.putc(pc.getc());
00261         }
00262         printf("\n\r");
00263         block = 9;
00264         pc.printf("Authenticating using key A...");
00265         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00266         if (status != MFRC522::STATUS_OK) {
00267             pc.printf("PCD_Authenticate() failed: ");
00268             pc.printf(RfChip.GetStatusCodeName(status));
00269             LedRed=1;
00270             continue;
00271         }
00272 
00273         // Write block
00274         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00275         if (status != MFRC522::STATUS_OK) {
00276             pc.printf("MIFARE_Write() failed: ");
00277             pc.printf(RfChip.GetStatusCodeName(status));
00278             LedRed=1;
00279             continue;
00280         } else {
00281             LedYellow=1;
00282             pc.printf("Succesfully Written to tag: ");
00283         }
00284         printf("\n\r");
00285         pc.printf("Enter 10 block: "); // Enter Data
00286         len=sizeof(buffer);
00287         for(uint8_t i = 0; i < 16; i++) {
00288             buffer[i] = pc.putc(pc.getc());
00289         }
00290         printf("\n\r");
00291         block = 10;
00292         pc.printf("Authenticating using key A...");
00293         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00294         if (status != MFRC522::STATUS_OK) {
00295             pc.printf("PCD_Authenticate() failed: ");
00296             pc.printf(RfChip.GetStatusCodeName(status));
00297             LedRed=1;
00298             continue;
00299         }
00300 
00301         // Write block
00302         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00303         if (status != MFRC522::STATUS_OK) {
00304             pc.printf("MIFARE_Write() failed: ");
00305             pc.printf(RfChip.GetStatusCodeName(status));
00306             LedRed=1;
00307             continue;
00308         } else {
00309             LedYellow=1;
00310             pc.printf("Succesfully Written to tag: ");
00311         }
00312         printf("\n\r");
00313         pc.printf("Enter 12 block: "); // Enter Data
00314         len=sizeof(buffer);
00315         for(uint8_t i = 0; i < 16; i++) {
00316             buffer[i] = pc.putc(pc.getc());
00317         }
00318         printf("\n\r");
00319         block = 12;
00320         pc.printf("Authenticating using key A...");
00321         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00322         if (status != MFRC522::STATUS_OK) {
00323             pc.printf("PCD_Authenticate() failed: ");
00324             pc.printf(RfChip.GetStatusCodeName(status));
00325             LedRed=1;
00326             continue;
00327         }
00328 
00329         // Write block
00330         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00331         if (status != MFRC522::STATUS_OK) {
00332             pc.printf("MIFARE_Write() failed: ");
00333             pc.printf(RfChip.GetStatusCodeName(status));
00334             LedRed=1;
00335             continue;
00336         } else {
00337             LedYellow=1;
00338             pc.printf("Succesfully Written to tag: ");
00339         }
00340         printf("\n\r");
00341         pc.printf("Enter 13 block: "); // Enter Data
00342         len=sizeof(buffer);
00343         for(uint8_t i = 0; i < 16; i++) {
00344             buffer[i] = pc.putc(pc.getc());
00345         }
00346         printf("\n\r");
00347         block = 13;
00348         pc.printf("Authenticating using key A...");
00349         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00350         if (status != MFRC522::STATUS_OK) {
00351             pc.printf("PCD_Authenticate() failed: ");
00352             pc.printf(RfChip.GetStatusCodeName(status));
00353             LedRed=1;
00354             continue;
00355         }
00356 
00357         // Write block
00358         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00359         if (status != MFRC522::STATUS_OK) {
00360             pc.printf("MIFARE_Write() failed: ");
00361             pc.printf(RfChip.GetStatusCodeName(status));
00362             LedRed=1;
00363             continue;
00364         } else {
00365             LedYellow=1;
00366             pc.printf("Succesfully Written to tag: ");
00367         }
00368         printf("\n\r");
00369         pc.printf("Enter 14 block: "); // Enter Data
00370         len=sizeof(buffer);
00371         for(uint8_t i = 0; i < 16; i++) {
00372             buffer[i] = pc.putc(pc.getc());
00373         }
00374         printf("\n\r");
00375         block = 14;
00376         pc.printf("Authenticating using key A...");
00377         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00378         if (status != MFRC522::STATUS_OK) {
00379             pc.printf("PCD_Authenticate() failed: ");
00380             pc.printf(RfChip.GetStatusCodeName(status));
00381             LedRed=1;
00382             continue;
00383         }
00384 
00385         // Write block
00386         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00387         if (status != MFRC522::STATUS_OK) {
00388             pc.printf("MIFARE_Write() failed: ");
00389             pc.printf(RfChip.GetStatusCodeName(status));
00390             LedRed=1;
00391             continue;
00392         } else {
00393             LedYellow=1;
00394             pc.printf("Succesfully Written to tag: ");
00395         }
00396 
00397 
00398         printf("\n\r");
00399         pc.printf("Enter 16 block: "); // Enter Data
00400         len=sizeof(buffer);
00401         for(uint8_t i = 0; i < 16; i++) {
00402             buffer[i] = pc.putc(pc.getc());
00403         }
00404         printf("\n\r");
00405         block = 16;
00406         pc.printf("Authenticating using key A...");
00407         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00408         if (status != MFRC522::STATUS_OK) {
00409             pc.printf("PCD_Authenticate() failed: ");
00410             pc.printf(RfChip.GetStatusCodeName(status));
00411             LedRed=1;
00412             continue;
00413         }
00414 
00415         // Write block
00416         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00417         if (status != MFRC522::STATUS_OK) {
00418             pc.printf("MIFARE_Write() failed: ");
00419             pc.printf(RfChip.GetStatusCodeName(status));
00420             LedRed=1;
00421             continue;
00422         } else {
00423             LedYellow=1;
00424             pc.printf("Succesfully Written to tag: ");
00425         }
00426         printf("\n\r");
00427         pc.printf("Enter 17 block: "); // Enter Data
00428         len=sizeof(buffer);
00429         for(uint8_t i = 0; i < 16; i++) {
00430             buffer[i] = pc.putc(pc.getc());
00431         }
00432         printf("\n\r");
00433         block = 17;
00434         pc.printf("Authenticating using key A...");
00435         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00436         if (status != MFRC522::STATUS_OK) {
00437             pc.printf("PCD_Authenticate() failed: ");
00438             pc.printf(RfChip.GetStatusCodeName(status));
00439             LedRed=1;
00440             continue;
00441         }
00442 
00443         // Write block
00444         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00445         if (status != MFRC522::STATUS_OK) {
00446             pc.printf("MIFARE_Write() failed: ");
00447             pc.printf(RfChip.GetStatusCodeName(status));
00448             LedRed=1;
00449             continue;
00450         } else {
00451             LedYellow=1;
00452             pc.printf("Succesfully Written to tag: ");
00453         }
00454         printf("\n\r");
00455         pc.printf("Enter 18 block: "); // Enter Data
00456         len=sizeof(buffer);
00457         for(uint8_t i = 0; i < 16; i++) {
00458             buffer[i] = pc.putc(pc.getc());
00459         }
00460         printf("\n\r");
00461         block = 18;
00462         pc.printf("Authenticating using key A...");
00463         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00464         if (status != MFRC522::STATUS_OK) {
00465             pc.printf("PCD_Authenticate() failed: ");
00466             pc.printf(RfChip.GetStatusCodeName(status));
00467             LedRed=1;
00468             continue;
00469         }
00470 
00471         // Write block
00472         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00473         if (status != MFRC522::STATUS_OK) {
00474             pc.printf("MIFARE_Write() failed: ");
00475             pc.printf(RfChip.GetStatusCodeName(status));
00476             LedRed=1;
00477             continue;
00478         } else {
00479             LedYellow=1;
00480             pc.printf("Succesfully Written to tag: ");
00481         }
00482 
00483         printf("\n\r");
00484         pc.printf("Enter 20 block: "); // Enter Data
00485         len=sizeof(buffer);
00486         for(uint8_t i = 0; i < 16; i++) {
00487             buffer[i] = pc.putc(pc.getc());
00488         }
00489         printf("\n\r");
00490         block = 20;
00491         pc.printf("Authenticating using key A...");
00492         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00493         if (status != MFRC522::STATUS_OK) {
00494             pc.printf("PCD_Authenticate() failed: ");
00495             pc.printf(RfChip.GetStatusCodeName(status));
00496             LedRed=1;
00497             continue;
00498         }
00499 
00500         // Write block
00501         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00502         if (status != MFRC522::STATUS_OK) {
00503             pc.printf("MIFARE_Write() failed: ");
00504             pc.printf(RfChip.GetStatusCodeName(status));
00505             LedRed=1;
00506             continue;
00507         } else {
00508             LedYellow=1;
00509             pc.printf("Succesfully Written to tag: ");
00510         }
00511         printf("\n\r");
00512         pc.printf("Enter 21 block: "); // Enter Data
00513         len=sizeof(buffer);
00514         for(uint8_t i = 0; i < 16; i++) {
00515             buffer[i] = pc.putc(pc.getc());
00516         }
00517         printf("\n\r");
00518         block = 21;
00519         pc.printf("Authenticating using key A...");
00520         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00521         if (status != MFRC522::STATUS_OK) {
00522             pc.printf("PCD_Authenticate() failed: ");
00523             pc.printf(RfChip.GetStatusCodeName(status));
00524             LedRed=1;
00525             continue;
00526         }
00527 
00528         // Write block
00529         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00530         if (status != MFRC522::STATUS_OK) {
00531             pc.printf("MIFARE_Write() failed: ");
00532             pc.printf(RfChip.GetStatusCodeName(status));
00533             LedRed=1;
00534             continue;
00535         } else {
00536             LedYellow=1;
00537             pc.printf("Succesfully Written to tag: ");
00538         }
00539         printf("\n\r");
00540         pc.printf("Enter 22 block: "); // Enter Data
00541         len=sizeof(buffer);
00542         for(uint8_t i = 0; i < 16; i++) {
00543             buffer[i] = pc.putc(pc.getc());
00544         }
00545         printf("\n\r");
00546         block = 22;
00547         pc.printf("Authenticating using key A...");
00548         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00549         if (status != MFRC522::STATUS_OK) {
00550             pc.printf("PCD_Authenticate() failed: ");
00551             pc.printf(RfChip.GetStatusCodeName(status));
00552             LedRed=1;
00553             continue;
00554         }
00555 
00556         // Write block
00557         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00558         if (status != MFRC522::STATUS_OK) {
00559             pc.printf("MIFARE_Write() failed: ");
00560             pc.printf(RfChip.GetStatusCodeName(status));
00561             LedRed=1;
00562             continue;
00563         } else {
00564             LedYellow=1;
00565             pc.printf("Succesfully Written to tag: ");
00566         }
00567         printf("\n\r");
00568         pc.printf("Enter 24 block: "); // Enter Data
00569         len=sizeof(buffer);
00570         for(uint8_t i = 0; i < 16; i++) {
00571             buffer[i] = pc.putc(pc.getc());
00572         }
00573         printf("\n\r");
00574         block = 24;
00575         pc.printf("Authenticating using key A...");
00576         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00577         if (status != MFRC522::STATUS_OK) {
00578             pc.printf("PCD_Authenticate() failed: ");
00579             pc.printf(RfChip.GetStatusCodeName(status));
00580             LedRed=1;
00581             continue;
00582         }
00583 
00584         // Write block
00585         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00586         if (status != MFRC522::STATUS_OK) {
00587             pc.printf("MIFARE_Write() failed: ");
00588             pc.printf(RfChip.GetStatusCodeName(status));
00589             LedRed=1;
00590             continue;
00591         } else {
00592             LedYellow=1;
00593             pc.printf("Succesfully Written to tag: ");
00594         }
00595         printf("\n\r");
00596         pc.printf("Enter 25 block: "); // Enter Data
00597         len=sizeof(buffer);
00598         for(uint8_t i = 0; i < 16; i++) {
00599             buffer[i] = pc.putc(pc.getc());
00600         }
00601         printf("\n\r");
00602         block = 25;
00603         pc.printf("Authenticating using key A...");
00604         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00605         if (status != MFRC522::STATUS_OK) {
00606             pc.printf("PCD_Authenticate() failed: ");
00607             pc.printf(RfChip.GetStatusCodeName(status));
00608             LedRed=1;
00609             continue;
00610         }
00611 
00612         // Write block
00613         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00614         if (status != MFRC522::STATUS_OK) {
00615             pc.printf("MIFARE_Write() failed: ");
00616             pc.printf(RfChip.GetStatusCodeName(status));
00617             LedRed=1;
00618             continue;
00619         } else {
00620             LedYellow=1;
00621             pc.printf("Succesfully Written to tag: ");
00622         }
00623         printf("\n\r");
00624         pc.printf("Enter 26 block: "); // Enter Data
00625         len=sizeof(buffer);
00626         for(uint8_t i = 0; i < 16; i++) {
00627             buffer[i] = pc.putc(pc.getc());
00628         }
00629         printf("\n\r");
00630         block = 26;
00631         pc.printf("Authenticating using key A...");
00632         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00633         if (status != MFRC522::STATUS_OK) {
00634             pc.printf("PCD_Authenticate() failed: ");
00635             pc.printf(RfChip.GetStatusCodeName(status));
00636             LedRed=1;
00637             continue;
00638         }
00639 
00640         // Write block
00641         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00642         if (status != MFRC522::STATUS_OK) {
00643             pc.printf("MIFARE_Write() failed: ");
00644             pc.printf(RfChip.GetStatusCodeName(status));
00645             LedRed=1;
00646             continue;
00647         } else {
00648             LedYellow=1;
00649             pc.printf("Succesfully Written to tag: ");
00650         }
00651         printf("\n\r");
00652         pc.printf("Enter 28 block: "); // Enter Data
00653         len=sizeof(buffer);
00654         for(uint8_t i = 0; i < 16; i++) {
00655             buffer[i] = pc.putc(pc.getc());
00656         }
00657         printf("\n\r");
00658         block = 28;
00659         pc.printf("Authenticating using key A...");
00660         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00661         if (status != MFRC522::STATUS_OK) {
00662             pc.printf("PCD_Authenticate() failed: ");
00663             pc.printf(RfChip.GetStatusCodeName(status));
00664             LedRed=1;
00665             continue;
00666         }
00667 
00668         // Write block
00669         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00670         if (status != MFRC522::STATUS_OK) {
00671             pc.printf("MIFARE_Write() failed: ");
00672             pc.printf(RfChip.GetStatusCodeName(status));
00673             LedRed=1;
00674             continue;
00675         } else {
00676             LedYellow=1;
00677             pc.printf("Succesfully Written to tag: ");
00678         }
00679         printf("\n\r");
00680         pc.printf("Enter 29 block: "); // Enter Data
00681         len=sizeof(buffer);
00682         for(uint8_t i = 0; i < 16; i++) {
00683             buffer[i] = pc.putc(pc.getc());
00684         }
00685         printf("\n\r");
00686         block = 29;
00687         pc.printf("Authenticating using key A...");
00688         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00689         if (status != MFRC522::STATUS_OK) {
00690             pc.printf("PCD_Authenticate() failed: ");
00691             pc.printf(RfChip.GetStatusCodeName(status));
00692             LedRed=1;
00693             continue;
00694         }
00695 
00696         // Write block
00697         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00698         if (status != MFRC522::STATUS_OK) {
00699             pc.printf("MIFARE_Write() failed: ");
00700             pc.printf(RfChip.GetStatusCodeName(status));
00701             LedRed=1;
00702             continue;
00703         } else {
00704             LedYellow=1;
00705             pc.printf("Succesfully Written to tag: ");
00706         }
00707         printf("\n\r");
00708         pc.printf("Enter 30 block: "); // Enter Data
00709         len=sizeof(buffer);
00710         for(uint8_t i = 0; i < 16; i++) {
00711             buffer[i] = pc.putc(pc.getc());
00712         }
00713         printf("\n\r");
00714         block = 30;
00715         pc.printf("Authenticating using key A...");
00716         status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
00717         if (status != MFRC522::STATUS_OK) {
00718             pc.printf("PCD_Authenticate() failed: ");
00719             pc.printf(RfChip.GetStatusCodeName(status));
00720             LedRed=1;
00721             continue;
00722         }
00723 
00724         // Write block
00725         status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
00726         if (status != MFRC522::STATUS_OK) {
00727             pc.printf("MIFARE_Write() failed: ");
00728             pc.printf(RfChip.GetStatusCodeName(status));
00729             LedRed=1;
00730             continue;
00731         } else {
00732             LedYellow=1;
00733             pc.printf("Succesfully Written to tag: ");
00734         }
00735 
00736         /* Here I am writing 3 Sector ie 1,2,4,5,6,7,8,9,10,12,13,14 blocks
00737         If we you want to write more block so you should to define that block
00738         .........................
00739         ..........block..........
00740         .........................
00741         ..........block..........
00742         .........................
00743         ..........block..........
00744         .........................
00745         ..........block..........
00746                     61
00747                     62
00748         */
00749         DumpToSerial(&(RfChip.uid));
00750         wait_ms(200);
00751     }
00752 
00753 } //End WriteToRfidTag()
00754 
00755 //Create function for reading data
00756 void DumpToSerial(MFRC522::Uid *uid)
00757 {
00758     MFRC522::MIFARE_Key key;
00759 
00760     // Print Card UID
00761     printf("Card UID: ");
00762     for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00763         printf(" %X", RfChip.uid.uidByte[i]);
00764     }
00765     printf("\n\r");
00766 
00767     // Print Card type
00768     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00769     printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00770     wait_ms(1000);
00771 
00772     // Dump contents
00773     switch (piccType) {
00774         case MFRC522::PICC_TYPE_MIFARE_MINI:
00775         case MFRC522::PICC_TYPE_MIFARE_1K:
00776         case MFRC522::PICC_TYPE_MIFARE_4K:
00777             // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
00778             for (uint8_t i = 0; i < 6; i++) {
00779                 key.keyByte[i] = 0xFF;
00780             }
00781             DumpMifareClassicToSerial(uid, piccType, &key);
00782             break;
00783 
00784         case MFRC522::PICC_TYPE_MIFARE_UL:
00785             DumpMifareUltralightToSerial();
00786             break;
00787         case MFRC522::PICC_TYPE_TNP3XXX:
00788             printf("Dumping memory contents not implemented for that PICC type. \n\r");
00789             break;
00790         case MFRC522::PICC_TYPE_ISO_14443_4:
00791         case MFRC522::PICC_TYPE_ISO_18092:
00792         case MFRC522::PICC_TYPE_MIFARE_PLUS:
00793             printf("Dumping memory contents not implemented for that PICC type. \n\r");
00794             break;
00795 
00796         case MFRC522::PICC_TYPE_UNKNOWN:
00797         case MFRC522::PICC_TYPE_NOT_COMPLETE:
00798         default:
00799             break; // No memory dump here
00800     }
00801 
00802     printf("\n\r");
00803 
00804     RfChip.PICC_HaltA();
00805 } // End PICC_DumpToSerial()
00806 
00807 /**
00808  * Dumps memory contents of a MIFARE Classic PICC.
00809  * On success the PICC is halted after dumping the data.
00810  */
00811 void DumpMifareClassicToSerial(MFRC522::Uid *uid, uint8_t piccType, MFRC522::MIFARE_Key *key)
00812 {
00813     uint8_t no_of_sectors = 0;
00814     switch (piccType) {
00815         case MFRC522::PICC_TYPE_MIFARE_MINI:
00816             // Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
00817             no_of_sectors = 5;
00818             break;
00819 
00820         case MFRC522::PICC_TYPE_MIFARE_1K:
00821             // Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
00822             no_of_sectors = 16;
00823             break;
00824 
00825         case MFRC522::PICC_TYPE_MIFARE_4K:
00826             // Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
00827             no_of_sectors = 40;
00828             break;
00829 
00830         default:
00831             // Should not happen. Ignore.
00832             break;
00833     }
00834 
00835     // Dump sectors, highest address first.
00836     if (no_of_sectors) {
00837         printf("Sector  Block   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  AccessBits \n\r");
00838         printf("----------------------------------------------------------------------------------------- \n\r");
00839         for (int8_t i = no_of_sectors-1 ; i>= 0; i--) {
00840 
00841             DumpMifareClassicSectorToSerial(uid, key, i);
00842 
00843 
00844         }
00845     }
00846 
00847     RfChip.PICC_HaltA(); // Halt the PICC before stopping the encrypted session.
00848     RfChip.PCD_StopCrypto1();
00849 } // End PICC_DumpMifareClassicToSerial()
00850 
00851 /**
00852  * Dumps memory contents of a sector of a MIFARE Classic PICC.
00853  * Uses PCD_Authenticate(), MIFARE_Read() and PCD_StopCrypto1.
00854  * Always uses PICC_CMD_MF_AUTH_KEY_A because only Key A can always read the sector trailer access bits.
00855  */
00856 void DumpMifareClassicSectorToSerial(MFRC522::Uid *uid, MFRC522::MIFARE_Key *key, uint8_t sector)
00857 {
00858     uint8_t status;
00859     uint8_t firstBlock;    // Address of lowest address to dump actually last block dumped)
00860     uint8_t no_of_blocks;    // Number of blocks in sector
00861     bool    isSectorTrailer; // Set to true while handling the "last" (ie highest address) in the sector.
00862 
00863     // The access bits are stored in a peculiar fashion.
00864     // There are four groups:
00865     //    g[3]  Access bits for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39)
00866     //    g[2]  Access bits for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39)
00867     //    g[1]  Access bits for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39)
00868     //    g[0]  Access bits for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39)
00869     // Each group has access bits [C1 C2 C3]. In this code C1 is MSB and C3 is LSB.
00870     // The four CX bits are stored together in a nible cx and an inverted nible cx_.
00871     uint8_t c1, c2, c3;      // Nibbles
00872     uint8_t c1_, c2_, c3_;   // Inverted nibbles
00873     bool    invertedError = false;   // True if one of the inverted nibbles did not match
00874     uint8_t g[4];            // Access bits for each of the four groups.
00875     uint8_t group;           // 0-3 - active group for access bits
00876     bool    firstInGroup;    // True for the first block dumped in the group
00877 
00878     // Determine position and size of sector.
00879     if (sector < 32) {
00880         // Sectors 0..31 has 4 blocks each
00881         no_of_blocks = 4;
00882         firstBlock = sector * no_of_blocks;
00883     } else if (sector < 40) {
00884         // Sectors 32-39 has 16 blocks each
00885         no_of_blocks = 16;
00886         firstBlock = 128 + (sector - 32) * no_of_blocks;
00887     } else {
00888         // Illegal input, no MIFARE Classic PICC has more than 40 sectors.
00889         return;
00890     }
00891 
00892     // Dump blocks, highest address first.
00893     uint8_t byteCount;
00894     uint8_t buffer[18];
00895     uint8_t blockAddr;
00896     isSectorTrailer = true;
00897     for (int8_t blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--) {
00898         blockAddr = firstBlock + blockOffset;
00899 
00900         // Sector number - only on first line
00901         if (isSectorTrailer) {
00902             printf("  %2d   ", sector);
00903         } else {
00904             printf("       ");
00905         }
00906 
00907         // Block number
00908         printf(" %3d  ", blockAddr);
00909 
00910         // Establish encrypted communications before reading the first block
00911         if (isSectorTrailer) {
00912             status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, firstBlock, key, uid);
00913             if (status != MFRC522::STATUS_OK) {
00914                 printf("PCD_Authenticate() failed: %s \r\n", RfChip.GetStatusCodeName(status));
00915                 return;
00916             }
00917         }
00918 
00919         // Read block
00920         byteCount = sizeof(buffer);
00921         status = RfChip.MIFARE_Read(blockAddr, buffer, &byteCount);
00922         if (status != MFRC522::STATUS_OK) {
00923             printf("MIFARE_Read() failed: %s \r\n", RfChip.GetStatusCodeName(status));
00924             continue;
00925         }
00926 
00927         // Dump data
00928         for (uint8_t index = 0; index < 16; index++) {
00929             printf(" %3d", buffer[index]);
00930 //      if ((index % 4) == 3)
00931 //      {
00932 //        printf(" ");
00933 //      }
00934         }
00935 
00936         // Parse sector trailer data
00937         if (isSectorTrailer) {
00938             c1  = buffer[7] >> 4;
00939             c2  = buffer[8] & 0xF;
00940             c3  = buffer[8] >> 4;
00941             c1_ = buffer[6] & 0xF;
00942             c2_ = buffer[6] >> 4;
00943             c3_ = buffer[7] & 0xF;
00944             invertedError = (c1 != (~c1_ & 0xF)) || (c2 != (~c2_ & 0xF)) || (c3 != (~c3_ & 0xF));
00945 
00946             g[0] = ((c1 & 1) << 2) | ((c2 & 1) << 1) | ((c3 & 1) << 0);
00947             g[1] = ((c1 & 2) << 1) | ((c2 & 2) << 0) | ((c3 & 2) >> 1);
00948             g[2] = ((c1 & 4) << 0) | ((c2 & 4) >> 1) | ((c3 & 4) >> 2);
00949             g[3] = ((c1 & 8) >> 1) | ((c2 & 8) >> 2) | ((c3 & 8) >> 3);
00950             isSectorTrailer = false;
00951         }
00952 
00953         // Which access group is this block in?
00954         if (no_of_blocks == 4) {
00955             group = blockOffset;
00956             firstInGroup = true;
00957         } else {
00958             group = blockOffset / 5;
00959             firstInGroup = (group == 3) || (group != (blockOffset + 1) / 5);
00960         }
00961 
00962         if (firstInGroup) {
00963             // Print access bits
00964             printf("   [ %d %d %d ] ", (g[group] >> 2) & 1, (g[group] >> 1) & 1, (g[group] >> 0) & 1);
00965             if (invertedError) {
00966                 printf(" Inverted access bits did not match! ");
00967             }
00968         }
00969 
00970         if (group != 3 && (g[group] == 1 || g[group] == 6)) {
00971             // Not a sector trailer, a value block
00972             printf(" Addr = 0x%02X, Value = 0x%02X%02X%02X%02X", buffer[12],
00973                    buffer[3],
00974                    buffer[2],
00975                    buffer[1],
00976                    buffer[0]);
00977         }
00978 
00979         printf("\n\r");
00980     }
00981 
00982     return;
00983 } // End PICC_DumpMifareClassicSectorToSerial()
00984 
00985 /**
00986  * Dumps memory contents of a MIFARE Ultralight PICC.
00987  */
00988 void DumpMifareUltralightToSerial(void)
00989 {
00990     uint8_t status;
00991     uint8_t byteCount;
00992     uint8_t buffer[18];
00993     uint8_t i;
00994 
00995     printf("Page   0  1  2  3");
00996     // Try the mpages of the original Ultralight. Ultralight C has more pages.
00997     for (uint8_t page = 0; page < 16; page +=4) {
00998         // Read pages
00999         byteCount = sizeof(buffer);
01000         status = RfChip.MIFARE_Read(page, buffer, &byteCount);
01001         if (status != MFRC522::STATUS_OK) {
01002             printf("MIFARE_Read() failed: %s \n\r", RfChip.GetStatusCodeName(status));
01003             break;
01004         }
01005 
01006         // Dump data
01007         for (uint8_t offset = 0; offset < 4; offset++) {
01008             i = page + offset;
01009             printf(" %2d  ", i); // Pad with spaces
01010             for (uint8_t index = 0; index < 4; index++) {
01011                 i = 4 * offset + index;
01012                 printf(" %02X ", buffer[i]);
01013             }
01014 
01015             printf("\n\r");
01016         }
01017     }
01018 } // End PICC_DumpMifareUltralightToSerial()
01019 
01020 int main()
01021 {
01022     /* Set debug UART speed */
01023     printf("\n\rUART 9600 baud\n\r");
01024     pc.baud(9600);
01025     printf("\n\r%s %s\n\r",VERSION,CIBLE);
01026 
01027     /* Init. RC522 Chip */
01028     RfChip.PCD_Init();
01029 
01030     /* Read RC522 version */
01031     uint8_t temp = RfChip.PCD_ReadRegister(MFRC522::VersionReg);
01032     printf("MFRC522 version: %d\n\r", temp & 0x07);
01033     printf("\n\r");
01034 
01035     while(true) {
01036 
01037         LedGreen = 1;
01038 
01039         // Look for new cards
01040         if ( ! RfChip.PICC_IsNewCardPresent()) {
01041             wait_ms(500);
01042             continue;
01043         }
01044 
01045         LedRed   = 0;
01046 
01047         // Select one of the cards
01048         if ( ! RfChip.PICC_ReadCardSerial()) {
01049             wait_ms(500);
01050             continue;
01051         }
01052 
01053         LedGreen = 0;
01054 
01055         //Calling WriteToRfidTag()
01056         WriteToRfidTag(&(RfChip.uid));
01057         wait_ms(200);
01058 
01059     }
01060 }