Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 //Acknowledgement(rfid): Donatien Garnier (http://mbed.org/users/donatien/code/RSEDP_DPDemo/file/fd63457452f4/main.cpp) 00002 00003 #include "mbed.h" 00004 //self included 00005 #include "RWDMifare.h" 00006 00007 //defines 00008 #define MAX_UID_LEN 10 //Mifare max UID length is 10 bytes (triple-uid) 00009 #define CMD_LENGTH 128 00010 00011 uint8_t CMD[CMD_LENGTH]; 00012 00013 //setup hardware 00014 DigitalOut myled(LED1); 00015 RWDMifare reader(p9, p10, p30); 00016 Serial pc (USBTX, USBRX); 00017 DigitalIn CardDetect (p29); 00018 00019 00020 bool CMD_Received; 00021 uint8_t CMD_Ptr; 00022 uint8_t *ptr; 00023 DigitalOut led1(LED1); 00024 DigitalOut led2(LED2); 00025 00026 //! Method to read from UART and control if a command has been received 00027 void callback() { 00028 if(!CMD_Received) //previous cmd executed 00029 { 00030 CMD[CMD_Ptr] = (uint8_t)pc.getc(); 00031 pc.putc(CMD[CMD_Ptr]); 00032 if (CMD[CMD_Ptr] == 13){ 00033 CMD_Received = true; 00034 CMD_Ptr = 0; 00035 } 00036 else{ 00037 CMD_Ptr++; 00038 } 00039 } 00040 } 00041 00042 uint8_t CharToInt(char c) 00043 { 00044 if((c >= '0') && (c <= '9')) 00045 return c - '0'; 00046 if((c >= 'A') && (c <= 'F')) 00047 return c - 'A' + 10; 00048 if((c >= 'a') && (c <= 'f')) 00049 return c - 'a' + 10; 00050 return -1; 00051 } 00052 int main() 00053 { 00054 //Reading Mifare 00055 uint8_t uid[MAX_UID_LEN]; //< Buffer for UID 00056 CMD_Received = false; 00057 CMD_Ptr = 0; 00058 size_t uidLen = 4; 00059 uint8_t Data[16]; 00060 00061 pc.attach(&callback); 00062 pc.printf("\xCMifare Card Reader\n\r"); 00063 00064 //Init Mifare Reader 00065 RWDMifare::RWDMifareErr readerErr = reader.init(); 00066 if(readerErr) { 00067 pc.printf("Could not init reader (error %d)\r\n", readerErr); 00068 return -1; 00069 } 00070 00071 while(1) { 00072 if(CMD_Received) 00073 { 00074 ptr = CMD; //set the pointer 00075 switch(*ptr++) 00076 { 00077 case 's': /*!<get status [no args] */ 00078 uint8_t *status; 00079 readerErr = reader.getStatus(status); 00080 reader.printStatus(*status); //print it out. 00081 break; 00082 case 'u': //get a UID of the card [no args] 00083 pc.printf("UID=0x"); 00084 if(!reader.getUID(uid, &uidLen)) { //Got an UID successfully 00085 uidLen = (uidLen < 4) ? uidLen : 4; //Check length 00086 for(int i = 0; i < uidLen; i++) 00087 pc.printf("%02x", uid[i]); 00088 pc.printf("\n\r"); 00089 } 00090 else{ 00091 pc.printf("None"); 00092 } 00093 break; 00094 case 'r'://read a card block 00095 // CMD of form = 'r' <key_num> [A|B] <block addr> <num of blocks> 00096 while(*ptr == ' ') 00097 ptr++; //skip white space 00098 uint8_t key = atoi((char*)ptr); 00099 key &= 0x1F; //need to mask it 00100 while(*ptr != ' ') 00101 ptr++; //find next white space 00102 while(*ptr == ' ') 00103 ptr++; // then skip white space 00104 if((*ptr == 'A') | (*ptr == 'a'))//type A 00105 key &= ~0x80; //turn off MSb 00106 else if((*ptr == 'B') | (*ptr == 'b')) 00107 key |= 0x80; //turn on MSb 00108 else { 00109 pc.printf("\n\rKey type error"); 00110 break; 00111 } 00112 while(*ptr != ' ') 00113 ptr++; //find next white space 00114 while(*ptr == ' ') 00115 ptr++; // then skip white space 00116 00117 00118 uint8_t Block = atoi((char*)ptr); 00119 int8_t length = 1; 00120 while(*ptr != ' ') 00121 ptr++; //find next white space 00122 while(*ptr == ' ') 00123 ptr++; // then skip white space 00124 00125 if(*ptr) 00126 length = atoi((char*)ptr); 00127 if(length == 1) 00128 pc.printf("\n\rKey = %d, type: %c: Block: %d", key & 0x1F, key&0x80? 'B':'A', Block); 00129 else 00130 pc.printf("\n\rKey = %d, type: %c: Blocks: %d, Number of blocks: %d", key & 0x1F, key&0x80? 'B':'A', Block, length); 00131 00132 for(int j = 0; j < length; j++){ 00133 readerErr = reader.ReadBlock(Block+j, key, Data); 00134 if(!readerErr) 00135 { 00136 pc.printf("\n\r"); 00137 for(int i = 0; i < 16; i ++) 00138 pc.printf("%02x", Data[i]); 00139 } 00140 else 00141 { 00142 pc.printf("Fail"); 00143 length = -1;//escape 00144 } 00145 } 00146 break; 00147 00148 case 'k': // k <number> [6 byte key in hex, no spaces] 00149 while(*(ptr++) == ' ') ; 00150 //get number id of key to write 00151 uint8_t num = atoi((char*)ptr); 00152 while(*ptr != ' ') 00153 ptr++; //find next white space 00154 while(*ptr == ' ') 00155 ptr++; // then skip white space 00156 uint8_t key_ar[6] = {0}; 00157 00158 for (int i = 0; i < 6; i++) 00159 { 00160 key_ar[i] |= CharToInt(*ptr) << 4; 00161 ptr++; 00162 key_ar[i] |= CharToInt(*ptr); 00163 ptr++; 00164 } 00165 pc.printf("Key received: {"); 00166 for(int i = 0; i < 6; i++) 00167 { 00168 pc.printf("%02x", key_ar[i]); 00169 } 00170 pc.printf("}\n\r"); 00171 readerErr = reader.StoreKey(num, key_ar); 00172 if(readerErr){ 00173 pc.printf("Write Key Failed\n\r"); 00174 } 00175 else{ 00176 pc.printf("Write Key successfull\n\r"); 00177 } 00178 00179 break; 00180 case 'P': //program EEPROM: P <addr> <val> (hex) 00181 while(*ptr == ' ') 00182 ptr++; 00183 uint8_t addr = CharToInt(*ptr); 00184 ptr++; 00185 if(*ptr != ' ') 00186 { 00187 addr <<= 4; 00188 addr |= CharToInt(*ptr); 00189 ptr++; 00190 } 00191 while(*ptr != ' ') 00192 ptr++; 00193 while(*ptr == ' ') 00194 ptr++; 00195 uint8_t val = CharToInt(*ptr); 00196 ptr++; 00197 if(*ptr != ' ') 00198 { 00199 val <<= 4; 00200 val |= CharToInt(*ptr); 00201 ptr++; 00202 } 00203 00204 pc.printf("\n\rAddr = 0x%02x, Data = 0x%02x", addr, val); 00205 readerErr = reader.Prog_EEPROM(addr, val); 00206 if(readerErr) 00207 pc.printf("\n\rWrite Error"); 00208 else 00209 pc.printf("\n\rSuccess"); 00210 break; 00211 case '?': 00212 pc.printf("\n\rMifare Card Reader / Writer software."); 00213 pc.printf("\n\rCommands: "); 00214 pc.printf("\n\r\t'?' - Shows this prompt"); 00215 pc.printf("\n\r\t's' - Gets and prints Device status"); 00216 pc.printf("\n\r\t'u' - Gets and prints the UID of a card if in detect"); 00217 pc.printf("\n\r\t'P' - Programs the EEPROM - use \"P [addr] [value]\". addr and value should be in hex"); 00218 pc.printf("\n\r\t'k' - Stores a key to EEPROM - use \"k [key_id] [key - 6 bytes in hex]\" e.g. k 0 a0a1a2a3a4a5"); 00219 pc.printf("\n\r\t'r' - Reads data from the card if in detect - use \"r [key_id] [type='A'|'B'] [block addr] (num of blocks)\" e.g. 'r 0 A 0 4'"); 00220 pc.printf("[_] indicates a mandatory field. (_) indicates an optional field."); 00221 break; 00222 default: 00223 pc.printf("Command %c not recognised\n\r? - help prompt", *(ptr-1)); 00224 break; 00225 } 00226 CMD_Received=false;//dealt with command 00227 pc.printf("\r\n"); 00228 } 00229 00230 } 00231 } 00232 00233 /* 00234 case 'w': 00235 uint8_t WrData[16] = {0xc3,0x1e,0x71,0xda,0x77,0x98,0x02,0x00,0xe1,0x50,0x00,0x00,0x00,0x00,0xFF,0x11}; 00236 00237 readerErr=reader.WriteBlock(Addr,Key,Type,WrData); 00238 if(readerErr){ 00239 pc.printf("Write Block Failed\n\r"); 00240 } 00241 else{ 00242 pc.printf("Write Block successfull\n\r"); 00243 } 00244 break; 00245 */
Generated on Wed Jul 13 2022 03:38:17 by
1.7.2