This program is for reading and writing from /to MIFARE Classic 1k ,MIFARE Ultralight MF0ICU1,MIFARE Ultralight NTAG213,MIFARE Ultralight NTAG203
main.cpp
- Committer:
- sheralikhan
- Date:
- 2017-05-26
- Revision:
- 3:f9ed412458d5
- Parent:
- 2:e27413743e1b
- Child:
- 4:9d923c6e6c14
File content as of revision 3:f9ed412458d5:
/*--------------------------------------------------------Reading Writing MIFARE Tags---------------------------------------------------------------
Note:
---------
1.This program is for reading and writing from /to MIFARE Classic 1k ,MIFARE Ultralight MF0ICU1,MIFARE Ultralight NTAG213,MIFARE Ultralight NTAG203
2.Generally we are using MBED model WIZnet WIZWiki W7500 to interfacng with MFRC522 Rader and writer device using SPI protocol
3.This program is designed such a way that it will take input 1,2 for reading or writing from/to tags
4.Once user can entering input based on input it will call read and write function respectively...
----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*Test of cheap 13.56Mhz RFID-RC522 module
Connect as follows:
RFID pins -> WIZWiki-W7500 header CN5 (Arduino-compatible header)
--------------------------------------------------------------------------
1.RFID IRQ -> Not used. Leave open
2.RFID MISO -> WIZWiki-W7500 SPI_MISO =D12
3.RFID MOSI -> WIZWiki-W7500 SPI_MOSI =D11
4.RFID SCK -> WIZWiki-W7500 SPI_SCK =D13
5.RFID SDA -> WIZWiki-W7500 SPI_CS =D10
6.RFID RST -> WIZWiki-W7500 =D9
3.3V and Gnd to the respective pins*/
//Adding Library for Mbed
#include "mbed.h"
//Adding Library for MFRC522
#include "MFRC522.h"
//Adding Library for SPI protocol
#include "SPI.h"
//Define RFID version and date
#define VERSION "RFID_2017_03_20"
//Define board
#define CIBLE "WIZwiki-W7500"
// ARMmbed WIZwiki W7500 Pin for MFRC522 SPI Communication
#define SPI_MOSI D11
#define SPI_MISO D12
#define SPI_SCLK D13
#define SPI_CS D10
// WIZWiki-W7500 Pin for MFRC522 reset(pick another D pin if you need D8)
#define MF_RESET D9
//Define led for identification
DigitalOut LedGreen(D7);
//Serial connection to PC for output
Serial pc(USBTX, USBRX);
//Define or create MFRC522 class and passing pins as arguments
MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
//* Local functions */
void DumpMifareClassicToSerial (MFRC522::Uid *uid, uint8_t piccType, MFRC522::MIFARE_Key *key); //It is reading MIFARE Classic 1k memory block and sector
void DumpMifareClassicSectorToSerial(MFRC522::Uid *uid, MFRC522::MIFARE_Key *key, uint8_t sector); //It is reading MIFARE Classic 1k memory block data and sector data
void DumpMifareUltralightToSerial (void); //It is reading MIFARE Ultralight memory block,block data and sector,sector data
void writeDataUL(void); //It is Writing function to MIFARE Ultralight
void writeDataClassic1k(void); //It is Writing function to MIFARE Classic 1k
uint8_t page;
/**
* Dumps debug info about the selected PICC to Serial.
* On success the PICC is halted after dumping the data.
* For MIFARE Classic the factory default key of 0xFFFFFFFFFFFF is tried.
*/
void DumpToSerial(MFRC522::Uid *uid)
{
//Declaring MIFARE Classic 1k key
MFRC522::MIFARE_Key key;
// Print Card UID
printf("Card UID: ");
for (uint8_t i = 0; i < RfChip.uid.size; i++) {
printf(" %X", RfChip.uid.uidByte[i]);
}
printf("\n\r");
// Print Card type
uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
wait_ms(50);
// Dump contents
switch (piccType) {
case MFRC522::PICC_TYPE_MIFARE_MINI:
case MFRC522::PICC_TYPE_MIFARE_1K:
case MFRC522::PICC_TYPE_MIFARE_4K:
// All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (uint8_t i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
//Calling MIFARE Classic 1k memory block and sector
DumpMifareClassicToSerial(uid, piccType, &key);
break;
case MFRC522::PICC_TYPE_MIFARE_UL:
//Calling MIFARE Ultralight memory block,block data and sector,sector data
DumpMifareUltralightToSerial();
break;
default:
break; // No memory dump here
}
printf("\n\r");
RfChip.PICC_HaltA(); // Instructs a PICC in state ACTIVE(*) to go to state HALT.
} // End PICC_DumpToSerial()
/**
* Dumps memory contents of a MIFARE Classic PICC.
* On success the PICC is halted after dumping the data.
*/
void DumpMifareClassicToSerial(MFRC522::Uid *uid, uint8_t piccType, MFRC522::MIFARE_Key *key)
{
uint8_t no_of_sectors = 0;
switch (piccType) {
case MFRC522::PICC_TYPE_MIFARE_MINI:
// Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
no_of_sectors = 5;
break;
case MFRC522::PICC_TYPE_MIFARE_1K:
// Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
no_of_sectors = 16;
break;
case MFRC522::PICC_TYPE_MIFARE_4K:
// Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
no_of_sectors = 40;
break;
default:
// Should not happen. Ignore.
break;
}
// Dump sectors, highest address first.
if (no_of_sectors) {
printf("Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits \n\r");
printf("----------------------------------------------------------------------------------------- \n\r");
for (int8_t i = no_of_sectors-1 ; i>= 0; i--) {
DumpMifareClassicSectorToSerial(uid, key, i);
}
}
RfChip.PICC_HaltA(); // Halt the PICC before stopping the encrypted session.
RfChip.PCD_StopCrypto1();
} // End PICC_DumpMifareClassicToSerial()
/**
* Dumps memory contents of a sector of a MIFARE Classic PICC.
* Uses PCD_Authenticate(), MIFARE_Read() and PCD_StopCrypto1.
* Always uses PICC_CMD_MF_AUTH_KEY_A because only Key A can always read the sector trailer access bits.
*/
void DumpMifareClassicSectorToSerial(MFRC522::Uid *uid, MFRC522::MIFARE_Key *key, uint8_t sector)
{
uint8_t status; // Set what type of error is found
uint8_t firstBlock; // Address of lowest address to dump actually last block dumped)
uint8_t no_of_blocks; // Number of blocks in sector
bool isSectorTrailer; // Set to true while handling the "last" (ie highest address) in the sector.
// The access bits are stored in a peculiar fashion.
// There are four groups:
// g[3] Access bits for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39)
// g[2] Access bits for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39)
// g[1] Access bits for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39)
// g[0] Access bits for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39)
// Each group has access bits [C1 C2 C3]. In this code C1 is MSB and C3 is LSB.
// The four CX bits are stored together in a nible cx and an inverted nible cx_.
uint8_t c1, c2, c3; // Nibbles
uint8_t c1_, c2_, c3_; // Inverted nibbles
bool invertedError = false; // True if one of the inverted nibbles did not match
uint8_t g[4]; // Access bits for each of the four groups.
uint8_t group; // 0-3 - active group for access bits
bool firstInGroup; // True for the first block dumped in the group
// Determine position and size of sector.
if (sector < 32) {
// Sectors 0..31 has 4 blocks each
no_of_blocks = 4;
firstBlock = sector * no_of_blocks;
} else if (sector < 40) {
// Sectors 32-39 has 16 blocks each
no_of_blocks = 16;
firstBlock = 128 + (sector - 32) * no_of_blocks;
} else {
// Illegal input, no MIFARE Classic PICC has more than 40 sectors.
return;
}
// Dump blocks, highest address first.
uint8_t byteCount; //No of reading data
uint8_t buffer[18]; //Reading data of buffer
uint8_t blockAddr; // No of Sector
isSectorTrailer = true;
for (int8_t blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--) {
blockAddr = firstBlock + blockOffset;
// Sector number - only on first line
if (isSectorTrailer) {
printf(" %2d ", sector);
} else {
printf(" ");
}
// Block number
printf(" %3d ", blockAddr);
// Establish encrypted communications before reading the first block
/*While MFRC522 reader/writer is not uthenticate MIFARE card then it will through PCD_Authenticate() failed: Timeout in communication */
if (isSectorTrailer) {
status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, firstBlock, key, uid);
if (status != MFRC522::STATUS_OK) {
printf("PCD_Authenticate() failed: %s \r\n", RfChip.GetStatusCodeName(status));
return;
}
}
// Read block
/*If MFRC522 reader/writer is uthenticate MIFARE card but it not uthenticate key then it will through MIFARE_Read() failed: Timeout in communication */
byteCount = sizeof(buffer);
status = RfChip.MIFARE_Read(blockAddr, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
printf("MIFARE_Read() failed: %s \r\n", RfChip.GetStatusCodeName(status));
continue;
}
// Dump data
for (uint8_t index = 0; index < 16; index++) {
printf(" %2X", buffer[index]);
}
//This block is printing access bit......................................................................................
// Parse sector trailer data
if (isSectorTrailer) {
c1 = buffer[7] >> 4;
c2 = buffer[8] & 0xF;
c3 = buffer[8] >> 4;
c1_ = buffer[6] & 0xF;
c2_ = buffer[6] >> 4;
c3_ = buffer[7] & 0xF;
invertedError = (c1 != (~c1_ & 0xF)) || (c2 != (~c2_ & 0xF)) || (c3 != (~c3_ & 0xF));
g[0] = ((c1 & 1) << 2) | ((c2 & 1) << 1) | ((c3 & 1) << 0);
g[1] = ((c1 & 2) << 1) | ((c2 & 2) << 0) | ((c3 & 2) >> 1);
g[2] = ((c1 & 4) << 0) | ((c2 & 4) >> 1) | ((c3 & 4) >> 2);
g[3] = ((c1 & 8) >> 1) | ((c2 & 8) >> 2) | ((c3 & 8) >> 3);
isSectorTrailer = false;
}
// Which access group is this block in?
if (no_of_blocks == 4) {
group = blockOffset;
firstInGroup = true;
} else {
group = blockOffset / 5;
firstInGroup = (group == 3) || (group != (blockOffset + 1) / 5);
}
if (firstInGroup) {
// Print access bits
printf(" [ %d %d %d ] ", (g[group] >> 2) & 1, (g[group] >> 1) & 1, (g[group] >> 0) & 1);
if (invertedError) {
printf(" Inverted access bits did not match! ");
}
}
if (group != 3 && (g[group] == 1 || g[group] == 6)) {
// Not a sector trailer, a value block
printf(" Addr = 0x%02X, Value = 0x%02X%02X%02X%02X", buffer[12],
buffer[3],
buffer[2],
buffer[1],
buffer[0]);
}
//END....................................................................................................................
printf("\n\r");
}
return;
} // End PICC_DumpMifareClassicSectorToSerial()
/**
* Dumps memory contents of a MIFARE Ultralight PICC.
*/
void DumpMifareUltralightToSerial(void)
{
uint8_t status; // Set what type of error is found
uint8_t byteCount; //No of reading data
uint8_t buffer[18]; //Reading data from buffer
uint8_t i;
printf("Page 0 1 2 3 \n");
// Try the mpages of the original Ultralight. Ultralight C has more pages.
for ( page = 0; page < 16; page +=4) {
// Read pages
/*If MFRC522 reader/writer is uthenticate MIFARE card but it not uthenticate key then it will through MIFARE_Read() failed: Timeout in communication */
byteCount = sizeof(buffer);
status = RfChip.MIFARE_Read(page, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
printf("MIFARE_Read() failed: %s \n\r", RfChip.GetStatusCodeName(status));
break;
}
// Dump data
for (uint8_t offset = 0; offset < 4; offset++) {
i = page + offset;
printf(" %2d ", i); // Pad with spaces
for (uint8_t index = 0; index < 4; index++) {
i = 4 * offset + index;
printf(" %02X ", buffer[i]);
}
printf("\n\r");
}
}
} // End PICC_DumpMifareUltralightToSerial()
//Create function for writting data
void WriteToRfidTag(MFRC522::Uid *uid)
{
MFRC522::MIFARE_Key key;
// Print Card UID
printf("Card UID: ");
for (uint8_t i = 0; i < RfChip.uid.size; i++) {
printf(" %X", RfChip.uid.uidByte[i]);
}
printf("\n\r");
// Print Card type
uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
wait_ms(50);
// Dump contents
switch (piccType) {
case MFRC522::PICC_TYPE_MIFARE_MINI:
case MFRC522::PICC_TYPE_MIFARE_1K:
case MFRC522::PICC_TYPE_MIFARE_4K:
// All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (uint8_t i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
writeDataClassic1k(); // WriteToRfidTag(&(RfChip.uid))
break;
case MFRC522::PICC_TYPE_MIFARE_UL:
writeDataUL();
break;
default:
break; // No memory dump here
}
printf("\n\r");
}
void writeDataClassic1k()
{
//Declaration of MFRC522 key
MFRC522::MIFARE_Key key;
//Declaration of MFRC522 status
MFRC522::StatusCode status;
uint8_t buffer[16];
uint8_t block;
while(true) {
// All keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (uint8_t i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
pc.printf("Enter 1 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 1;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to block 1: ");
}
printf("\n\r");
pc.printf("Enter 2 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 2;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen = 1;
pc.printf("Succesfully Written to block 2: ");
}
printf("\n\r");
pc.printf("Enter 4 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 4;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen = 1;
pc.printf("Succesfully Written to block 4: ");
}
printf("\n\r");
pc.printf("Enter 5 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 5;
/*While MFRC522 reader/writer is not uthenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen = 1;
pc.printf("Succesfully Written to block 5: ");
}
printf("\n\r");
pc.printf("Enter 6 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 6;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to block 6: ");
}
printf("\n\r");
printf("\n\r");
pc.printf("Enter 8 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 8;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 10 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 10;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 12 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 12;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 13 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 13;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 14 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 14;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 16 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 16;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 17 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 17;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 18 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 18;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 20 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 20;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 21 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 21;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 22 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 22;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 24 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 24;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 25 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 25;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 26 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 26;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 28 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 28;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 29 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 29;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Enter 30 block: "); // Enter Data
for(uint8_t i = 0; i < 16; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
block = 30;
/*While MFRC522 reader/writer is not Authenticate MIFARE card key A then it will through PCD_Authenticate() failed: Timeout in communication */
pc.printf("Authenticating using key A...");
status = (MFRC522::StatusCode)RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
if (status != MFRC522::STATUS_OK) {
pc.printf("PCD_Authenticate() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
}
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
pc.printf("Succesfully Written to tag: ");
}
printf("\n\r");
pc.printf("Written to Classic card successfully....\n\r");
RfChip.PICC_HaltA();
RfChip.PCD_StopCrypto1();
break;
// switchcase();
}
} // end of void writeDataClassic1k()
void writeDataUL()
{
//Declaration of MFRC522 status
MFRC522::StatusCode status;
uint8_t buffer[16]; //Define buffer
uint8_t page; //Define page
while(true) {
pc.printf("Enter 4 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 4;
// Write block
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 4: ");
}
printf("\n\r");
pc.printf("Enter 5 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 5;
// Write block
//While MFRC522 reader/writer is not Authenticate MIFARE card it will through MIFARE_Write() failed: Error in communication
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 5: ");
}
printf("\n\r");
pc.printf("Enter 6 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 6;
// Write block
//While MFRC522 reader/writer is not Authenticate MIFARE card it will through MIFARE_Write() failed: Error in communication
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 6: ");
}
printf("\n\r");
pc.printf("Enter 7 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 7;
// Write block
//While MFRC522 reader/writer is not Authenticate MIFARE card it will through MIFARE_Write() failed: Error in communication
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 7: ");
}
printf("\n\r");
pc.printf("Enter 8 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 8;
// Write block
//While MFRC522 reader/writer is not Authenticate MIFARE card it will through MIFARE_Write() failed: Error in communication
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 8: ");
}
printf("\n\r");
pc.printf("Enter 9 page: "); // Enter Data
for(uint8_t i = 0; i < 4; i++) {
buffer[i] = pc.putc(pc.getc());
}
printf("\n\r");
page = 9;
// Write block
//While MFRC522 reader/writer is not Authenticate MIFARE card it will through MIFARE_Write() failed: Error in communication
status = (MFRC522::StatusCode)RfChip.MIFARE_UltralightWrite(page, buffer, 4);
if (status != MFRC522::STATUS_OK) {
pc.printf("MIFARE_Write() failed: ");
pc.printf(RfChip.GetStatusCodeName(status));
continue;
} else {
LedGreen=1;
pc.printf("Succesfully Written to page 9: ");
}
printf("Written Succefully to ultralight ......\n\r");
RfChip.PICC_HaltA();
break;
}
} //End writeDataUL()
int main()
{
/* Set debug UART speed */
printf("\n\rUART 9600 baud\n\r");
pc.baud(9600);
printf("\n\r%s %s\n\r",VERSION,CIBLE);
/* Init. RC522 Chip */
RfChip.PCD_Init();
/* Read RC522 version */
uint8_t temp = RfChip.PCD_ReadRegister(MFRC522::VersionReg);
printf("MFRC522 version: %d\n\r", temp & 0x07);
printf("\n\r");
pc.printf("Enter 1 Reading \n");
pc.printf("Enter 2 Writing \n");
while(true) {
LedGreen = 1;
// Look for new cards
if ( ! RfChip.PICC_IsNewCardPresent()) {
wait_ms(50);
continue;
}
LedGreen = 0;
wait_ms(50);
// Select one of the cards
if ( ! RfChip.PICC_ReadCardSerial()) {
wait_ms(50);
continue;
}
LedGreen = 1;
wait_ms(50);
char ch=pc.getc();
if(ch=='1') {
pc.printf("reading..........\n");
// Dump debug info about the card. PICC_HaltA() is automatically called.
DumpToSerial(&(RfChip.uid));
wait_ms(20);
} else if(ch=='2') {
pc.printf("Writing..... \n");
//Calling WriteToRfidTag()
WriteToRfidTag(&(RfChip.uid));
wait_ms(50);
}
}
}