forked from stehgold/rc522
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1075 lines (925 loc) · 27.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* main.c
*/
/*
* Name RC522DumpMifare.ino
*
* 2013-07-xx started
* 2013-08-03 first commit
* 2013-08-08 anticol/select 7byte UID, RATS
*
* Based on code by www.electrodragon.com
* and modified/fixed by http://twitter.com/regnerischerTag
*
* TODO:
* - ISO/IEC 14443-4
* - Auth 7byte
*
*
*
* ported to Keil for Stellaris/Tiva Launchpads
* Stephan Goldenberg
* 2014-06-01 - started
* 2014-06-08 - 1st successful build
* 2014-06-15 - get back right version info - finally. Still not reading tags
*/
#include "tm4c123gh6pm.h" // TM4C123G Launchpad
#include "MFRC522.h" // symbols for RC522 by regnerischerTag
#include <stdbool.h> // true AND false
#include <string.h> // memcpy
// using SSI0 [PA_5 - MOSI, PA_4 - MISO, PA_3 - CS, PA_2 - SCK], PA_6 - CS, PA_7 - NRSTPD
// using UART0 PA_0 & PA_1 for serial communication
#define chipSelectPin 0x40
#define NRSTPD 0x80
#define uchar unsigned char
#define uint unsigned int
//data array maxium length
#define MAX_LEN 16
//unsigned char status;
unsigned char serNum[5];
unsigned char serNum7[8];
void Clock_Init(void);
void SPI_Init(void);
void UART_Init(void);
unsigned char SPI_transfer(unsigned char);
unsigned char sendAfterWaiting(unsigned char);
void Serial_print(unsigned char[]);
void Serial_println(unsigned char[]);
void Serial_print_hex(unsigned char);
void Serial_println_hex(unsigned char);
void UART_OutChar(unsigned char);
void SysTick_Init(void);
void SysTick_Wait(unsigned int);
void delay(unsigned int);
void MFRC522_Init(void);
bool selectCard(bool);
void Write_MFRC522(unsigned char, unsigned char);
unsigned char Read_MFRC522(unsigned char);
unsigned char MFRC522_Read(unsigned char, unsigned char *);
unsigned char MFRC522_Auth(unsigned char, unsigned char, unsigned char *, unsigned char *);
void dumpHex(unsigned char* buffer, int len);
unsigned char MFRC522_Request(unsigned char, unsigned char *);
unsigned char MFRC522_Anticoll(unsigned char *);
unsigned char MFRC522_Anticoll2(unsigned char *);
unsigned char MFRC522_SelectTag(unsigned char *, unsigned char *);
unsigned char MFRC522_SelectTag2(unsigned char *, unsigned char *);
unsigned char MFRC522_RATS(unsigned char *, unsigned int *);
unsigned char MFRC522_ToCard(unsigned char, unsigned char *, unsigned char, unsigned char *, unsigned int *);
unsigned char MFRC522_RATS(unsigned char *, unsigned int *);
void CalulateCRC(unsigned char *, unsigned char, unsigned char *);
void Clock_Init(){
SYSCTL_RCC2_R |= 0x80000000; // use RCC2
SYSCTL_RCC2_R |= 0x00000800; // bypass PLL during setup
SYSCTL_RCC_R = (SYSCTL_RCC_R& ~0x000007C0)+0x00000540; // 16 MHz XTAL
SYSCTL_RCC2_R &= ~0x00000070; // config for main oscillator source
SYSCTL_RCC2_R &= ~0x00002000; // activatte PLL by clearing PWRDN
SYSCTL_RCC2_R |= 0x40000000; // use 400 MHz PLL
SYSCTL_RCC2_R = (SYSCTL_RCC2_R& ~0x1FC00000)+(4<<22); // 80 MHz
while((SYSCTL_RIS_R&0x00000040) == 0){}; // wait for PLL lock
SYSCTL_RCC2_R &= ~0x00000800; // enable PLL by clearing BYPASS
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA; // activate clock for port A
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_SSI0; // activate clock for SSI 0
SYSCTL_RCGC1_R |= SYSCTL_SCGC1_UART0; // activate clock for UART 0
}
void SPI_Init(){
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFF0F00FF) + 0x00202200;
GPIO_PORTA_AMSEL_R = 0; // disable analog modules
GPIO_PORTA_AFSEL_R |= (0x34); // enable alt function on PA2,4,5
GPIO_PORTA_AFSEL_R &= ~(NRSTPD | chipSelectPin); // disable alt function on PA6 + 7
GPIO_PORTA_DIR_R |= (0x08); // disable PA3 (CS) - done using regular GPIO
GPIO_PORTA_DIR_R |= (NRSTPD | chipSelectPin); // PA6 + 7 GPIO Output
GPIO_PORTA_DEN_R |= (0x3C | NRSTPD | chipSelectPin); // enable digital IO on PA2,3,4,5,6,7
GPIO_PORTA_DATA_R |= 0x08; // pull PA3 high
SSI0_CR1_R &= ~SSI_CR1_SSE; // disable SSI0
SSI0_CR1_R &= ~SSI_CR1_MS; // master mode
SSI0_CPSR_R = (SSI0_CPSR_R&~SSI_CPSR_CPSDVSR_M)+80; // clock prescaler for 1 MHz SSIClk
SSI0_CR0_R &= ~(SSI_CR0_SCR_M | SSI_CR0_SPH | SSI_CR0_SPO);
SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_FRF_M)+SSI_CR0_FRF_MOTO; // Freescale mode
SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_DSS_M)+SSI_CR0_DSS_8; // 8-bit data
SSI0_CR1_R |= SSI_CR1_SSE; // enable SSI
}
void UART_Init(){
// SYSCTL_RCGC1_R |= 0x0001; // activate UART0 - done in
// SYSCTL_RCGC2_R |= 0x0001; // activate port A - Clock_Init() now
UART0_CTL_R &= ~0001; // disable UART0
UART0_IBRD_R = 43; // IBRD = (80M / (16 * 115k2)) = int(43.40278)
UART0_FBRD_R = 26; // FBRD = round(0.40278 * 64) = 26
UART0_LCRH_R = 0x0070; // 8-bit word length, enable FIFO
UART0_CTL_R = 0x0301; // enable RXE, TXE and UART
GPIO_PORTA_PCTL_R |= (GPIO_PORTA_PCTL_R&0xFFFFFF00) + 0x00000011;
GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog function
GPIO_PORTA_AFSEL_R |= 0x03; // enable alt functon on PA0-1
GPIO_PORTA_DEN_R |= 0x03; // enable digital IO on PA0-1
}
void SysTick_Init(){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = 0x00FFFFFF; // max reload value
NVIC_ST_CURRENT_R = 0; // any write to CURRENT clears it
NVIC_ST_CTRL_R = 0x05; // enable SysTick with core clock
}
void SysTick_Wait(unsigned int reload){ // wait for (reload-1) * 12.5ns
NVIC_ST_RELOAD_R = reload-1; // # of counts to wait
NVIC_ST_CURRENT_R = 0; // any write to CURRENT clears it
while((NVIC_ST_CTRL_R&0x000010000) == 0){}; // wait for count flag
}
void delay(unsigned int milis){ // delay milis ms
unsigned int i;
for(i=0; i<milis; i++){
SysTick_Wait(80000); // 80,000 * 12.5ns = 1ms
}
}
unsigned char SPI_transfer(unsigned char data){
unsigned char rec_data;
GPIO_PORTA_DATA_R &= ~chipSelectPin; // !CS = 0
rec_data = sendAfterWaiting(data); // send command
GPIO_PORTA_DATA_R |= chipSelectPin; // !CS = 1
return rec_data;
}
unsigned char sendAfterWaiting(unsigned char code){
while((SSI0_SR_R&SSI_SR_TFE) == 0){}; // wait until FIFO empty
SSI0_DR_R = code; // push data out
while((SSI0_SR_R&SSI_SR_RNE) == 0){}; // wait for response
return (unsigned char)SSI0_DR_R;
}
void UART_OutChar(unsigned char data){
while((UART0_FR_R&0x0020) != 0); // wait until TXFF is 0
UART0_DR_R = data;
}
void Serial_print(unsigned char text[]){
unsigned int i = 0;
while(text[i] != 0){
UART_OutChar(text[i]);
i++;
}
}
void Serial_println(unsigned char text[]){
Serial_print(text);
UART_OutChar('\r');
UART_OutChar('\n');
}
void Serial_print_hex(unsigned char hexnum){
unsigned char outchar;
outchar = hexnum / 16;
if (outchar > 9){
outchar += 0x40;
} else{
outchar += 0x30;
}
UART_OutChar(outchar);
outchar = hexnum % 16;
if (outchar > 9){
outchar += 0x40;
} else{
outchar += 0x30;
}
UART_OutChar(outchar);
}
void Serial_println_hex(unsigned char hexnum){
Serial_print_hex(hexnum);
UART_OutChar('\r');
UART_OutChar('\n');
}
int main(void){
unsigned char version = 0;
//unsigned char serNum[5]; // 4 bytes Serial number of card, the 5th byte is crc
//unsigned char serNum7[8]; // 7 bytes Serial number of card, the 8th byte is crc
//buffer
//uchar str[MAX_LEN];
//status = 0x00;
uchar defaultKeyA[MAX_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uchar madKeyA[MAX_LEN] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
uchar NDEFKeyA[MAX_LEN] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
Clock_Init();
SysTick_Init();
UART_Init();
SPI_Init();
GPIO_PORTA_DATA_R |= NRSTPD; // wake up reader
delay(2); // give it some time
GPIO_PORTA_DATA_R &= ~chipSelectPin; // Activate the RFID reader
MFRC522_Init();
//display version info
//9.3.4.8 VersionReg register : 0x91 / 0x92 -> 91: Version 1, 92: Version 2
version = Read_MFRC522(VersionReg);
Serial_print("MFRC522 Software Version: ");
Serial_println_hex(version&0x0F);
while(1){
uchar status;
uchar buffer[MAX_LEN];
if (selectCard(true)){
int block;
for(block=0; block < 64; block++)
{
status = MFRC522_Auth(PICC_AUTHENT1A, block, defaultKeyA, serNum); //auth with default key
if (status != MI_OK)
{
selectCard(false);
status = MFRC522_Auth(PICC_AUTHENT1A, block, madKeyA, serNum); //auth with MAD key
}
if (status != MI_OK)
{
selectCard(false);
status = MFRC522_Auth(PICC_AUTHENT1A, block, NDEFKeyA, serNum); //auth NDEF data key
}
if (status == MI_OK)
{
status = MFRC522_Read(block, buffer);
if (status == MI_OK)
{
if (block % 4 == 0)
{
Serial_print("Sector ");
Serial_print_hex(block / 4);
Serial_println(": ");
}
dumpHex((uchar*)buffer, MAX_LEN);
}
else
{
Serial_println("Read failed");
break;
}
}
else
{
Serial_println("Auth failed");
//TODO Mifare Ultra-Light
//MIFARE Ultralight C http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf
break;
}
}//for
delay(1000);
}
else
{
Serial_println("no card select");
}
//reset/init for next loop
MFRC522_Init();
delay(500);
}
}
bool selectCard(bool dumpInfo){
uchar status;
uchar buffer[MAX_LEN];
//Search card, return card types
status = MFRC522_Request(PICC_REQIDL, buffer);//ShortFrame: 0x26 REQA (Request Type A)
//status = MFRC522_Request(PICC_REQALL, buffer);//0x52 WUPA (Wake-Up)
if (status == MI_OK)
{
if (dumpInfo)
{
Serial_print("Card detected.\r\n ATQA:");
dumpHex((uchar*)buffer, 2);
Serial_println(" ");
}
//Prevent conflict, return the 4 bytes Serial number of the card
status = MFRC522_Anticoll(buffer);
if (status == MI_OK){
unsigned char sak = 0;
memcpy(serNum, buffer, 5);
status = MFRC522_SelectTag(serNum, &sak);
if (status == MI_OK && ((sak & 0x04) == 0x00))
{
if (dumpInfo)
{
Serial_print(" UID: ");
dumpHex((uchar*)serNum, 4);
Serial_println("");
}
if ((sak & 0x20) == 0x20)
{
//ISO/IEC FCD 14443-3: Table 9 Coding of SAK
//if (dumpInfo)
// Serial_println(" UID complete, PICC compliant with ISO/IEC 14443-4");
//send RATS (Request for Answer To Select)
uchar ats[MAX_LEN];
uint unLen = 0;
status = MFRC522_RATS(ats, &unLen);
if (status == MI_OK && dumpInfo)
{
Serial_println(" ATS: ");
dumpHex((uchar*)ats, ats[0]);
Serial_println("");
}
}
if (dumpInfo)
{
Serial_print(" SAK: ");
Serial_print_hex(sak);
Serial_println("");
}
return true;
}
else
{
//cascading level 2
memcpy(serNum7, &serNum[1], 3);//cascading L1
status = MFRC522_Anticoll2(buffer);
if (status == MI_OK)
{
memcpy(&serNum7[3], buffer, 4);
status = MFRC522_SelectTag2(&serNum7[3], &sak);
if (dumpInfo)
{
Serial_print(" UID: ");
dumpHex((uchar*)serNum7, 7);
Serial_println("");
Serial_print(" SAK: ");
Serial_print_hex(sak);
Serial_println("");
}
return true;
}
else
{
Serial_println("ANTICOLL error: cascading level 2");
}
}
}//Anticoll
else
{
Serial_print("ANTICOLL failed");
}
}
else
{
//Serial_print("-");
}
return false;
}//selectCard
/*
* Function:Write_MFRC5200
* Description:write a byte data into one register of MR RC522
* Input parameter:addr--register address;val--the value that need to write in
* Return:Null
*/
void Write_MFRC522(uchar addr, uchar val)
{
GPIO_PORTA_DATA_R &= ~chipSelectPin;
//address format:0XXXXXX0
SPI_transfer((addr<<1)&0x7E);
SPI_transfer(val);
GPIO_PORTA_DATA_R |= chipSelectPin;
}
/*
* Function:Read_MFRC522
* Description:read a byte data into one register of MR RC522
* Input parameter:addr--register address
* Return:return the read value
*/
uchar Read_MFRC522(uchar addr)
{
uchar val;
GPIO_PORTA_DATA_R &= ~chipSelectPin;
//address format:1XXXXXX0
SPI_transfer(((addr<<1)&0x7E) | 0x80);
val =SPI_transfer(0x00);
GPIO_PORTA_DATA_R |= chipSelectPin;
return val;
}
/*
* Function:SetBitMask
* Description:set RC522 register bit
* Input parameter:reg--register address;mask--value
* Return:null
*/
void SetBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg, tmp | mask); // set bit mask
}
/*
* Function:ClearBitMask
* Description:clear RC522 register bit
* Input parameter:reg--register address;mask--value
* Return:null
*/
void ClearBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg, tmp & (~mask)); // clear bit mask
}
/*
* Function:AntennaOn
* Description:Turn on antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
*/
void AntennaOn(void)
{
uchar temp;
temp = Read_MFRC522(TxControlReg);
if (!(temp & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
/*
* Function:AntennaOff
* Description:Turn off antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
*/
void AntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
/*
* Function:ResetMFRC522
* Description: reset RC522
* Input parameter:null
* Return:null
*/
void MFRC522_Reset(void)
{
Write_MFRC522(CommandReg, PCD_RESETPHASE);
}
/*
* Function:InitMFRC522
* Description:initilize RC522
* Input parameter:null
* Return:null
*/
void MFRC522_Init(void)
{
GPIO_PORTA_DATA_R |= NRSTPD;
MFRC522_Reset();
//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
Write_MFRC522(TReloadRegL, 30);
Write_MFRC522(TReloadRegH, 0);
Write_MFRC522(TxAutoReg, 0x40); //100%ASK
Write_MFRC522(ModeReg, 0x3D); //CRC initilizate value 0x6363 ???
//ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
//Write_MFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0]
//Write_MFRC522(RFCfgReg, 0x7F); //RxGain = 48dB
AntennaOn(); //turn on antenna
}
/*
* Function:MFRC522_Request
* Description:Searching card, read card type
* Input parameter:reqMode--search methods,
* TagType--return card types
* 0x4400 = Mifare_UltraLight
* 0x0400 = Mifare_One(S50)
* 0x0200 = Mifare_One(S70)
* 0x0800 = Mifare_Pro(X)
* 0x4403 = Mifare_DESFire
* return:return MI_OK if successed
*/
uchar MFRC522_Request(uchar reqMode, uchar *TagType)
{
uchar status;
uint backBits; //the data bits that received
Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
TagType[0] = reqMode;
status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
if ((status != MI_OK) || (backBits != 0x10))
{
status = MI_ERR;
//TODO
//comment out again
Serial_print("status: ");
Serial_print_hex(status);
Serial_print(" backBits: ");
Serial_print_hex(backBits);
Serial_println("");
}
return status;
}
/*
* Function:MFRC522_ToCard
* Description:communicate between RC522 and ISO14443
* Input parameter:command--MF522 command bits
* sendData--send data to card via rc522
* sendLen--send data length
* backData--the return data from card
* backLen--the length of return data
* return:return MI_OK if successed
*/
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
{
uchar status = MI_ERR;
uchar irqEn = 0x00;
uchar waitIRq = 0x00;
uchar lastBits;
uchar n;
uint i;
switch (command)
{
case PCD_AUTHENT: //verify card password
{
irqEn = 0x12;
waitIRq = 0x10;
break;
}
case PCD_TRANSCEIVE: //send data in the FIFO
{
irqEn = 0x77;
waitIRq = 0x30;
break;
}
default:
break;
}
Write_MFRC522(CommIEnReg, irqEn|0x80); //Allow interruption
ClearBitMask(CommIrqReg, 0x80); //Clear all the interrupt bits
SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO initilizate
Write_MFRC522(CommandReg, PCD_IDLE); //NO action;cancel current command ???
//write data into FIFO
for (i=0; i<sendLen; i++)
{
Write_MFRC522(FIFODataReg, sendData[i]);
}
//procceed it
Write_MFRC522(CommandReg, command);
if (command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
}
//waite receive data is finished
i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
do
{
//CommIrqReg[7..0]
//Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
n = Read_MFRC522(CommIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitIRq));
ClearBitMask(BitFramingReg, 0x80); //StartSend=0
if (i != 0)
{
if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
{
status = MI_OK;
if (n & irqEn & 0x01)
{
status = MI_NOTAGERR; //??
}
if (command == PCD_TRANSCEIVE)
{
n = Read_MFRC522(FIFOLevelReg);
lastBits = Read_MFRC522(ControlReg) & 0x07;
if (lastBits)
{
*backLen = (n-1)*8 + lastBits;
}
else
{
*backLen = n*8;
}
if (n == 0)
{
n = 1;
}
if (n > MAX_LEN)
{
n = MAX_LEN;
}
//read the data from FIFO
for (i=0; i<n; i++)
{
backData[i] = Read_MFRC522(FIFODataReg);
}
}
}
else
{
status = MI_ERR;
}
}
else
{
//Serial_print("i=0");
}
//SetBitMask(ControlReg,0x80); //timer stops
//Write_MFRC522(CommandReg, PCD_IDLE);
return status;
}
/*
* Function:MFRC522_Anticoll
* Description:Prevent conflict, read the card serial number
* Input parameter:serNum--return the 4 bytes card serial number, the 5th byte is recheck byte
* return:return MI_OK if successed
*/
uchar MFRC522_Anticoll(uchar *serNum)
{
uchar status;
uchar i;
uchar serNumCheck=0;
uint unLen;
//ClearBitMask(Status2Reg, 0x08); //TempSensclear
//ClearBitMask(CollReg,0x80); //ValuesAfterColl
Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
serNum[0] = PICC_ANTICOLL;
serNum[1] = 0x20;
status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
if (status == MI_OK)
{
//Verify card serial number
for (i=0; i<4; i++)
{
serNumCheck ^= serNum[i];
}
if (serNumCheck != serNum[i])
{
status = MI_ERR;
}
}
//SetBitMask(CollReg, 0x80); //ValuesAfterColl=1
return status;
}
//ANTICOLL cascading level 2
uchar MFRC522_Anticoll2(uchar *serNum)
{
uchar status;
uchar i;
uchar serNumCheck=0;
uint unLen;
//ClearBitMask(Status2Reg, 0x08); //TempSensclear
//ClearBitMask(CollReg,0x80); //ValuesAfterColl
Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
serNum[0] = PICC_ANTICOLL2;
serNum[1] = 0x20;
status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
if (status == MI_OK)
{
//Verify card serial number
for (i=0; i<4; i++)
{
serNumCheck ^= serNum[i];
}
if (serNumCheck != serNum[i])
{
status = MI_ERR;
}
}
//SetBitMask(CollReg, 0x80); //ValuesAfterColl=1
return status;
}
//send RATS and returns ATS
uchar MFRC522_RATS(uchar *recvData, uint *pLen)
{
uchar status;
uint unLen = 0;
recvData[0] = 0xE0; // RATS
recvData[1] = 0x50; // FSD=128, CID=0
CalulateCRC(recvData,2, &recvData[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
/*
Serial_print("unLen: ");
Serial_print_hex(unLen);
Serial_print(" status: ");
Serial_print_hex(status);
Serial_println("");
*/
//TODO
//if ((status != MI_OK) || (unLen != 0x90))
//{
// status = MI_ERR;
//}
return status;
}
/*
* Function:CalulateCRC
* Description:Use MF522 to caculate CRC
* Input parameter:pIndata--the CRC data need to be read,len--data length,pOutData-- the caculated result of CRC
* return:Null
*/
void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
{
uchar i, n;
ClearBitMask(DivIrqReg, 0x04); //CRCIrq = 0
SetBitMask(FIFOLevelReg, 0x80); //Clear FIFO pointer
//Write_MFRC522(CommandReg, PCD_IDLE);
//Write data into FIFO
for (i=0; i<len; i++)
{
Write_MFRC522(FIFODataReg, *(pIndata+i));
}
Write_MFRC522(CommandReg, PCD_CALCCRC);
//waite CRC caculation to finish
i = 0xFF;
do
{
n = Read_MFRC522(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04)); //CRCIrq = 1
//read CRC caculation result
pOutData[0] = Read_MFRC522(CRCResultRegL);
pOutData[1] = Read_MFRC522(CRCResultRegM);
}
/*
* Function:MFRC522_SelectTag
* Description:Select card, read card storage volume
* Input parameter:serNum--Send card serial number
* sak see ISO14443-3 Table 9 Coding of SAK
* return return MI_OK if successed
*/
uchar MFRC522_SelectTag(uchar *serNum, uchar *sak)
{
uchar i;
uchar status;
//uchar size;
uint recvBits;
uchar buffer[9];
//uchar buffCheck=0;
//ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
buffer[0] = PICC_SElECTTAG;
buffer[1] = 0x70;
for (i=0; i<5; i++)
{
buffer[i+2] = *(serNum+i);
}
CalulateCRC(buffer, 7, &buffer[7]); //??
status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
//TODO: the above call returns 2 instead of MI_OK -> why?
status = MI_OK;
//Serial_print("recvBits: ");
//Serial_print(recvBits, DEC);
/*
for (i=0; i<recBits / 8; i++)
{
buff[i] = *(buffer+i);
}*/
//dumpHex((char*)buffer, recvBits / 8);
*sak = buffer[0];
//Verify received buffer
/* TODO
for (i=0; i< recvBits/8; i++)
{
buffCheck ^= buffer[i];
}
if (buffCheck != buffer[i])
{
status = MI_ERR;
}*/
return status;
}
uchar MFRC522_SelectTag2(uchar *serNum, uchar *sak)
{
uchar i;
uchar status;
//uchar size;
uint recvBits;
uchar buffer[9];
//uchar buffCheck=0;
//ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
buffer[0] = PICC_ANTICOLL2;
buffer[1] = 0x70;
for (i=0; i<5; i++)
{
buffer[i+2] = *(serNum+i);
}
CalulateCRC(buffer, 7, &buffer[7]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
//TODO: the above call returns 2 instead of MI_OK -> why?
status = MI_OK;
//Serial_print("recvBits: ");
//Serial_print(recvBits, DEC);
/*
for (i=0; i<recBits / 8; i++)
{
buff[i] = *(buffer+i);
}*/
//dumpHex((char*)buffer, recvBits / 8);
*sak = buffer[0];
//Verify received buffer
/* TODO
for (i=0; i< recvBits/8; i++)
{
buffCheck ^= buffer[i];
}
if (buffCheck != buffer[i])
{
status = MI_ERR;
}*/
return status;
}
/*
* Function:MFRC522_Auth
* Description:verify card password
* Input parameters:authMode--password verify mode
0x60 = verify A passowrd key
0x61 = verify B passowrd key
BlockAddr--Block address
Sectorkey--Block password
serNum--Card serial number ,4 bytes
* return:return MI_OK if successed
*/
uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum)
{
uchar status;
uint recvBits;
uchar i;
uchar buff[12];
//Verify command + block address + buffer password + card SN
buff[0] = authMode;
buff[1] = BlockAddr;
for (i=0; i<6; i++)
{
buff[i+2] = *(Sectorkey+i);
}
for (i=0; i<4; i++)
{
buff[i+8] = *(serNum+i);
}
status = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits);
if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08)))
{
status = MI_ERR;
}
return status;
}
/*
* Function:MFRC522_Read
* Description:Read data
* Input parameters:blockAddr--block address;recvData--the block data which are read
* return:return MI_OK if successed
*/
uchar MFRC522_Read(uchar blockAddr, uchar *recvData)
{
uchar status;
uint unLen;
recvData[0] = PICC_READ;
recvData[1] = blockAddr;
CalulateCRC(recvData,2, &recvData[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
if ((status != MI_OK) || (unLen != 0x90))
{
status = MI_ERR;
}
return status;
}
/*
* Function:MFRC522_Write
* Description:write block data