📄 m500auc.lst
字号:
272 1 }
273
274 ///////////////////////////////////////////////////////////////////////
275 ///////////////////////////////////////////////////////////////////////
276 void FlushFIFO(void)
277 {
278 1 SetBitMask(RegControl,0x01);
279 1 }
280
281 ///////////////////////////////////////////////////////////////////////
282 // Value format operations for Mifare Standard card ICs
283 ///////////////////////////////////////////////////////////////////////
284 char M500PiccValue(unsigned char dd_mode,
285 unsigned char addr,
286 unsigned char *value,
287 unsigned char trans_addr)
288 {
289 1 char status = MI_OK;
290 1
291 1 M500PcdSetTmo(1);
292 1 ResetInfo(MInfo);
293 1 SerBuffer[0] = dd_mode;
294 1 SerBuffer[1] = addr;
295 1 MInfo.nBytesToSend = 2;
296 1 status = M500PcdCmd(PCD_TRANSCEIVE,
297 1 SerBuffer,
298 1 &MInfo);
299 1
300 1 if (status != MI_NOTAGERR)
301 1 {
302 2 if (MInfo.nBitsReceived != 4)
303 2 {
C51 COMPILER V7.06 M500AUC 03/16/2008 20:55:11 PAGE 6
304 3 status = MI_BITCOUNTERR;
305 3 }
306 2 else
307 2 {
308 3 SerBuffer[0] &= 0x0f;
309 3 switch(SerBuffer[0])
310 3 {
311 4 case 0x00:
312 4 status = MI_NOTAUTHERR;
313 4 break;
314 4 case 0x0a:
315 4 status = MI_OK;
316 4 break;
317 4 case 0x01:
318 4 status = MI_VALERR;
319 4 break;
320 4 default:
321 4 status = MI_CODEERR;
322 4 break;
323 4 }
324 3 }
325 2 }
326 1
327 1 if ( status == MI_OK)
328 1 {
329 2 M500PcdSetTmo(3);
330 2 ResetInfo(MInfo);
331 2 memcpy(SerBuffer,value,4);
332 2 MInfo.nBytesToSend = 4;
333 2 status = M500PcdCmd(PCD_TRANSCEIVE,
334 2 SerBuffer,
335 2 &MInfo);
336 2
337 2 if (status == MI_OK)
338 2 {
339 3 if (MInfo.nBitsReceived != 4)
340 3 {
341 4 status = MI_BITCOUNTERR;
342 4 }
343 3 else
344 3 {
345 4 SerBuffer[0] &= 0x0f;
346 4 switch(SerBuffer[0])
347 4 {
348 5 case 0x00:
349 5 status = MI_NOTAUTHERR;
350 5 break;
351 5 case 0x01:
352 5 status = MI_VALERR;
353 5 break;
354 5 default:
355 5 status = MI_CODEERR;
356 5 break;
357 5 }
358 4 }
359 3 }
360 2 else
361 2 {
362 3 if (status == MI_NOTAGERR )
363 3 status = MI_OK;
364 3 }
365 2 }
C51 COMPILER V7.06 M500AUC 03/16/2008 20:55:11 PAGE 7
366 1 if (status == MI_OK)
367 1 {
368 2 ResetInfo(MInfo);
369 2 SerBuffer[0] = PICC_TRANSFER;
370 2 SerBuffer[1] = trans_addr;
371 2 MInfo.nBytesToSend = 2;
372 2 status = M500PcdCmd(PCD_TRANSCEIVE,
373 2 SerBuffer,
374 2 &MInfo);
375 2 if (status != MI_NOTAGERR)
376 2 {
377 3 if (MInfo.nBitsReceived != 4)
378 3 {
379 4 status = MI_BITCOUNTERR;
380 4 }
381 3 else
382 3 {
383 4 SerBuffer[0] &= 0x0f;
384 4 switch(SerBuffer[0])
385 4 {
386 5 case 0x00:
387 5 status = MI_NOTAUTHERR;
388 5 break;
389 5 case 0x0a:
390 5 status = MI_OK;
391 5 break;
392 5 case 0x01:
393 5 status = MI_VALERR;
394 5 break;
395 5 default:
396 5 status = MI_CODEERR;
397 5 break;
398 5 }
399 4 }
400 3 }
401 2 }
402 1 return status;
403 1 }
404
405 ///////////////////////////////////////////////////////////////////////
406 // Set card in HALT-state
407 // 终止卡的操作
408 ///////////////////////////////////////////////////////////////////////
409 char M500PiccHalt(void)
410 {
411 1 char idata status = MI_CODEERR;
412 1
413 1 // ************* Cmd Sequence **********************************
414 1 ResetInfo(MInfo);
415 1 SerBuffer[0] = PICC_HALT ; // Halt command code
416 1 SerBuffer[1] = 0x00; // dummy address
417 1 MInfo.nBytesToSend = 2;
418 1 status = M500PcdCmd(PCD_TRANSCEIVE,
419 1 SerBuffer,
420 1 &MInfo);
421 1 if (status)
422 1 {
423 2 // timeout error ==> no NAK received ==> OK
424 2 if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
425 2 {
426 3 status = MI_OK;
427 3 }
C51 COMPILER V7.06 M500AUC 03/16/2008 20:55:11 PAGE 8
428 2 }
429 1 //reset command register - no response from tag
430 1 WriteIO(RegCommand,PCD_IDLE);
431 1 return status;
432 1 }
433
434 ///////////////////////////////////////////////////////////////////////
435 // Reset the MF RC500 重新设置MF RC500
436 ///////////////////////////////////////////////////////////////////////
437 char M500PcdReset(void)
438 {
439 1 char idata status = MI_OK;
440 1 unsigned int idata timecnt=0;
441 1
442 1 RC500RST = 0;
443 1 delay_1ms(25);
444 1 RC500RST = 1;
445 1 delay_50us(200);
446 1 RC500RST = 0;
447 1 delay_50us(50);
448 1 timecnt=1000;
449 1 while ((ReadIO(RegCommand) & 0x3F) && timecnt--);
450 1 if(!timecnt)
451 1 {
452 2 status = MI_RESETERR;
453 2 }
454 1 if (status == MI_OK)
455 1 {
456 2 //WriteIO(RegPage,0x80);
457 2 if (ReadIO(RegCommand) != 0x00)
458 2 {
459 3 status = MI_INTERFACEERR;
460 3 }
461 2 }
462 1 return status;
463 1 }
464
465 ///////////////////////////////////////////////////////////////////////
466 // Configures the MF RC500
467 //配置RC500内部寄存器函数
468 ///////////////////////////////////////////////////////////////////////
469 char M500PcdConfig(void)
470 {
471 1 char idata status;
472 1
473 1 if ((status = M500PcdReset()) == MI_OK)
474 1 {
475 2 WriteIO(RegClockQControl,0x00);
476 2 WriteIO(RegClockQControl,0x40);
477 2 delay_50us(2);
478 2 ClearBitMask(RegClockQControl,0x40);
479 2 WriteIO(RegBitPhase,0xAD);
480 2 WriteIO(RegRxThreshold,0xFF);
481 2 WriteIO(RegRxControl2,0x01);
482 2 WriteIO(RegFIFOLevel,0x1A);
483 2 WriteIO(RegTimerControl,0x02);
484 2 WriteIO(RegIRqPinConfig,0x03);
485 2 M500PcdRfReset(1);
486 2 }
487 1 return status;
488 1 }
489
C51 COMPILER V7.06 M500AUC 03/16/2008 20:55:11 PAGE 9
490 ///////////////////////////////////////////////////////////////
491 // Key loading into the MF RC500's EEPROM
492 //下载密码到MF RC500的 EEPROM
493 ///////////////////////////////////////////////////////////////
494 char M500PcdLoadKeyE2(unsigned char key_type,
495 unsigned char sector,
496 unsigned char *uncoded_keys)
497 {
498 1 signed char status = MI_OK;
499 1 unsigned int e2addr = 0x80 + sector * 0x18;
500 1 unsigned char coded_keys[12];
501 1
502 1 if (key_type == PICC_AUTHENT1B)
503 1 {
504 2 e2addr += 12; // key B offset
505 2 }
506 1 if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
507 1 {
508 2 status = PcdWriteE2(e2addr,12,coded_keys);
509 2 }
510 1 return status;
511 1 }
512
513 ///////////////////////////////////////////////////////////////////////
514 // Write the MF RC500's EEPROM
515 //写MF RC500的EEPROM
516 ///////////////////////////////////////////////////////////////////////
517 char PcdWriteE2(unsigned int startaddr,
518 unsigned char length,
519 unsigned char* _data)
520 {
521 1 char status = MI_OK;
522 1 ResetInfo(MInfo);
523 1 SerBuffer[0] = startaddr & 0xFF;
524 1 SerBuffer[1] = (startaddr >> 8) & 0xFF;
525 1 memcpy(SerBuffer + 2,_data,length);
526 1
527 1 MInfo.nBytesToSend = length + 2;
528 1
529 1 status = M500PcdCmd(PCD_WRITEE2,
530 1 SerBuffer,
531 1 &MInfo);
532 1 return status;
533 1 }
534
535 ///////////////////////////////////////////////////////////////////////
536 // Select Command defined in ISO14443(MIFARE)
537 //ISO14443(MIFARE)定义的选择命令
538 ///////////////////////////////////////////////////////////////////////
539 char M500PcdMfOutSelect(unsigned char type)
540 {
541 1 WriteIO(RegMfOutSelect,type&0x7);
542 1 return MI_OK;
543 1 }
544
545 ///////////////////////////////////////////////////////////////////////
546 // Request Command defined in ISO14443(MIFARE)
547 // Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)
548 // 寻卡,防冲突,选择卡 返回卡类型(2 bytes)+ 卡系列号(4 bytes)
549 ///////////////////////////////////////////////////////////////////////
550 char M500PiccCommonRequest(unsigned char req_code,unsigned char *atq)
551 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -