This program is for reading and writing from /to MIFARE Classic 1k ,MIFARE Ultralight MF0ICU1,MIFARE Ultralight NTAG213,MIFARE Ultralight NTAG203

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*--------------------------------------------------------Reading Writing MIFARE Tags---------------------------------------------------------------
00002 
00003 1.This program is for reading and writing from /to MIFARE Classic 1k ,MIFARE Ultralight MF0ICU1,MIFARE Ultralight NTAG213,MIFARE Ultralight NTAG203
00004 2.Generally we are using MBED model WIZnet WIZWiki W7500 to interfacng with MFRC522 Rader and writer device using SPI protocol
00005 3.This program is designed such a way that it will take input 1,2 for reading or writing from serial monitor
00006 4.Once user can entering input based on input it will call read and write function respectively...
00007 
00008 ----------------------------------------------------------------------------------------------------------------------------------------------------*/
00009 
00010 /*Test of cheap 13.56Mhz RFID-RC522 module
00011 Connect as follows:
00012 RFID pins        ->  WIZWiki-W7500 header CN5 (Arduino-compatible header)
00013 --------------------------------------------------------------------------
00014 1.RFID IRQ    ->   Not used. Leave open
00015 2.RFID MISO   ->   WIZWiki-W7500 SPI_MISO     =D12
00016 3.RFID MOSI   ->   WIZWiki-W7500 SPI_MOSI     =D11
00017 4.RFID SCK    ->   WIZWiki-W7500 SPI_SCK      =D13
00018 5.RFID SDA    ->   WIZWiki-W7500 SPI_CS       =D10
00019 6.RFID RST    ->   WIZWiki-W7500              =D9
00020 3.3V and Gnd to the respective pins
00021 -------------------------------------------------------------------------*/
00022 
00023 
00024 
00025 
00026 //Adding Library for Mbed
00027 #include "mbed.h"
00028 //Adding Library for MFRC522
00029 #include "MFRC522.h"
00030 //Adding Library for SPI protocol
00031 #include "SPI.h"
00032 
00033 //Define RFID version and date
00034 #define VERSION "RFID_2017_03_20"
00035 //Define board
00036 #define BOARD "WIZWIKI_W7500"
00037 //Adding Library for RFID tags read and Write
00038 #include "RFIDRW.h"
00039 
00040 
00041 // ARMmbed WIZwiki W7500 Pin for MFRC522 SPI Communication
00042 #define SPI_MOSI    D11
00043 #define SPI_MISO    D12
00044 #define SPI_SCLK    D13
00045 #define SPI_CS      D10
00046 
00047 // WIZWiki-W7500 Pin for MFRC522 reset(pick another D pin if you need D8)
00048 #define MF_RESET    D9
00049 
00050 //Define led for identification
00051 DigitalOut LedGreen(D7);
00052 
00053 //Serial connection to cp for output
00054 Serial cp(USBTX, USBRX);
00055 
00056 //Creating MFRC522 class abject  and passing pin as a argument
00057 MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
00058 
00059 /**
00060  * Dumps debug info about the selected PICC to Serial.
00061  * On success the PICC is halted after dumping the data.
00062  * For MIFARE Classic the factory default key of 0xFFFFFFFFFFFF is tried.
00063  */
00064 //Declaring MIFARE Classic 1k key
00065 MFRC522::MIFARE_Key key;
00066 
00067 void DumpToSerial(MFRC522::Uid *uid)
00068 {
00069 
00070 
00071     // Print Card UID
00072     printf("Card UID: ");
00073     for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00074         printf(" %X", RfChip.uid.uidByte[i]);
00075     }
00076     printf("\n\r");
00077 
00078     // Print Card type
00079     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00080     printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00081     wait_ms(50);
00082 
00083     // Dump contents
00084     switch (piccType) {
00085         case MFRC522::PICC_TYPE_MIFARE_MINI:
00086         case MFRC522::PICC_TYPE_MIFARE_1K:
00087         case MFRC522::PICC_TYPE_MIFARE_4K:
00088             // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
00089             for (uint8_t i = 0; i < 6; i++) {
00090                 key.keyByte[i] = 0xFF;
00091             }
00092             //Calling MIFARE Classic 1k memory block and sector
00093             DumpMifareClassicToSerial(uid, piccType, &key);
00094             break;
00095 
00096         case MFRC522::PICC_TYPE_MIFARE_UL:
00097             //Calling MIFARE Ultralight memory block,block data and sector,sector data
00098             DumpMifareUltralightToSerial();
00099             break;
00100 
00101         default:
00102             break; // No memory dump here
00103     }
00104 
00105     printf("\n\r");
00106 
00107     RfChip.PICC_HaltA(); // Instructs a PICC in state ACTIVE(*) to go to state HALT.
00108 } // End PICC_DumpToSerial()
00109 
00110 
00111 /**
00112  * Dumps memory contents of a MIFARE Ultralight PICC.
00113  */
00114 
00115 //Create function for writting data
00116 void WriteToRfidTag(MFRC522::Uid *uid)
00117 {
00118 
00119     // Print Card UID
00120     printf("Card UID: ");
00121     for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00122         printf(" %X", RfChip.uid.uidByte[i]);
00123     }
00124     printf("\n\r");
00125 
00126     // Print Card type
00127     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00128     printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00129     wait_ms(50);
00130 
00131     // Dump contents
00132     switch (piccType) {
00133         case MFRC522::PICC_TYPE_MIFARE_MINI:
00134         case MFRC522::PICC_TYPE_MIFARE_1K:
00135         case MFRC522::PICC_TYPE_MIFARE_4K:
00136             // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
00137             for (uint8_t i = 0; i < 6; i++) {
00138                 key.keyByte[i] = 0xFF;
00139             }
00140             writeDataClassic1k(uid);  // WriteToRfidTag(&(RfChip.uid))
00141             break;
00142 
00143         case MFRC522::PICC_TYPE_MIFARE_UL:
00144             writeDataUL(uid);
00145             break;
00146         default:
00147             break; // No memory dump here
00148     }
00149     printf("\n\r");
00150 }
00151 
00152 int main()
00153 {
00154     /* Set debug UART speed */
00155     printf("\n\rUART 9600 baud\n\r");
00156     cp.baud(9600);
00157     printf("\n\r%s %s\n\r",VERSION,BOARD);
00158 
00159     /* Init. RC522 Chip */
00160     RfChip.PCD_Init();
00161 
00162     /* Read RC522 version */
00163     uint8_t temp = RfChip.PCD_ReadRegister(MFRC522::VersionReg);
00164     printf("MFRC522 version: %d\n\r", temp);
00165     printf("\n\r");
00166     cp.printf("Enter 1 Reading \n");
00167     cp.printf("Enter 2 Writing \n");
00168 
00169 
00170     while(true) {
00171 
00172         LedGreen = 1;
00173 
00174         // Look for new cards
00175         if ( ! RfChip.PICC_IsNewCardPresent()) {
00176             wait_ms(50);
00177             continue;
00178         }
00179         LedGreen   = 0;
00180         wait_ms(50);
00181         // Select one of the cards
00182         if ( ! RfChip.PICC_ReadCardSerial()) {
00183             wait_ms(50);
00184             continue;
00185         }
00186         LedGreen = 1;
00187         wait_ms(50);
00188         char ch=cp.getc();
00189         if(ch=='1') {
00190             cp.printf("reading..........\n");
00191             // Dump debug info about the card. PICC_HaltA() is automatically called.
00192             DumpToSerial(&(RfChip.uid));
00193             wait_ms(20);
00194         } else if(ch=='2') {
00195             cp.printf("Writing..... \n");
00196             //Calling WriteToRfidTag()
00197             WriteToRfidTag(&(RfChip.uid));
00198             wait_ms(50);
00199 
00200         }
00201 
00202     }
00203 }