-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnfc.cpp
1168 lines (1054 loc) · 29.4 KB
/
nfc.cpp
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
/*****************************************************************************/
/*!
@file nfc.h
@author www.elechouse.com
@brief NFC Module I2C library source file.
This is a library for the Elechoues NFC_Module
----> LINKS HERE!!!
NOTE:
IRQ pin is unused.
@section HISTORY
V1.1 Add fuction about Peer to Peer communication
u8 P2PInitiatorInit();
u8 P2PTargetInit();
u8 P2PInitiatorTxRx(u8 *t_buf, u8 t_len, u8 *r_buf, u8 *r_len);
u8 P2PTargetTxRx(u8 *t_buf, u8 t_len, u8 *r_buf, u8 *r_len);
Change wait_ready(void) to wait_ready(u8 ms=NFC_WAIT_TIME);
Attach Wire library with NFC_MODULE, modify I2C buffer length to 64
and change i2c speed to 400KHz
V1.0 Initial version.
Copyright (c) 2012 www.elechouse.com All right reserved.
*/
/*****************************************************************************/
#include "nfc.h"
u8 hextab[17]="0123456789ABCDEF";
u8 ack[6]={
0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00
};
u8 nfc_version[6]={
0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03
};
/** data buffer */
u8 nfc_buf[NFC_CMD_BUF_LEN];
/*****************************************************************************/
/*!
@brief
@param
*/
/*****************************************************************************/
NFC_Module::NFC_Module(void)
{
}
/*****************************************************************************/
/*!
@brief initial function.
@param NONE.
@return NONE.
*/
/*****************************************************************************/
void NFC_Module::begin(void)
{
#ifdef PN532DEBUG
u8 i;
#endif
Wire.begin();
#ifdef PN532DEBUG
for(i=0; i<16; i++){
Serial.write((u8)hextab[i]);
}
for(i=0; i<6; i++){
Serial.write((u8)ack[i]);
}
for(i=0; i<6; i++){
Serial.write((u8)nfc_version[i]);
}
#endif
}
/*****************************************************************************/
/*!
@brief Get version of PN532
@param NONE
@return version number.
*/
/*****************************************************************************/
u32 NFC_Module::get_version(void)
{
u32 version;
nfc_buf[0] = PN532_COMMAND_GETFIRMWAREVERSION;
if(!write_cmd_check_ack(nfc_buf, 1)){
return 0;
}
wait_ready();
read_dt(nfc_buf, 12);
if(nfc_buf[5] != 0xD5){
return 0;
}
// check some basic stuff
if (0 != strncmp((char *)nfc_buf, (char *)nfc_version, 6)) {
#ifdef PN532DEBUG
Serial.println("Firmware doesn't match!");
#endif
return 0;
}
version = nfc_buf[7];
version <<= 8;
version |= nfc_buf[8];
version <<= 8;
version |= nfc_buf[9];
version <<= 8;
version |= nfc_buf[10];
return version;
}
/*****************************************************************************/
/*!
@brief Configures the SAM (Secure Access Module)
@param mode - set mode, normal mode default
@param timeout - Details in NXP's PN532UM.pdf
@param irq - 0 unused (default), 1 used
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::SAMConfiguration(u8 mode, u8 timeout, u8 irq)
{
#ifdef PN532DEBUG
Serial.print("SAMConfiguration\n");
#endif
nfc_buf[0] = PN532_COMMAND_SAMCONFIGURATION;
nfc_buf[1] = mode; // normal mode;
nfc_buf[2] = timeout; // timeout 50ms * 20 = 1 second
nfc_buf[3] = irq; // use IRQ pin!
if(!write_cmd_check_ack(nfc_buf, 4)){
return 0;
}
// read data packet
read_dt(nfc_buf, 8);
return (nfc_buf[6] == PN532_COMMAND_SAMCONFIGURATION);
}
/*****************************************************************************/
/*!
@brief card inventory.
@param buf - buf[0] UUID length; buf[1], buf[2], buf[3] buf[4] UUID
@param brty - optional parameter, braud rate: PN532_BRTY_ISO14443A,
PN532_BRTY_ISO14443B,
PN532_BRTY_212KBPS,
PN532_BRTY_424KBPS,
PN532_BRTY_JEWEL,
@param maxtg - optional parameter, maximum tag numbers to read once,
maximum 2, recommend 1.
@param idata - assistant parameter, unused.
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::InListPassiveTarget(u8 *buf, u8 brty,
u8 len, u8 *idata, u8 maxtg)
{
nfc_buf[0] = PN532_COMMAND_INLISTPASSIVETARGET;
nfc_buf[1] = maxtg;
nfc_buf[2] = brty;
if(len){
memcpy(nfc_buf+3, idata, len);
}
if(!write_cmd_check_ack(nfc_buf, 3+len)){
return 0;
}
#ifdef PN532DEBUG
puthex(nfc_buf, 3+len);
Serial.println();
#endif
// puthex(nfc_buf, 3+len);
// Serial.println();
/** "Waiting for IRQ (indicates card presence)" */
wait_ready();
wait_ready();
wait_ready();
#ifdef PN532DEBUG
Serial.print(" Found Card.\n");
#endif
read_dt(nfc_buf,40);
// puthex(nfc_buf, nfc_buf[3]+6);
// Serial.println();
if(nfc_buf[NFC_FRAME_ID_INDEX-1] != 0xD5){
return 0;
}
puthex(nfc_buf, nfc_buf[3]+6);
Serial.println();
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INLISTPASSIVETARGET+1)){
return 0;
}
// if(nfc_buf[NFC_FRAME_ID_INDEX+1]!=1){
//#ifdef PN532DEBUG
// Serial.println(nfc_buf[NFC_FRAME_ID_INDEX+1],DEC);
//#endif
// return 0;
// }
if(brty == PN532_BRTY_ISO14443A){
/** UUID length */
buf[0] = nfc_buf[12];
for(u8 i=1; i<5; i++){
buf[i] = nfc_buf[12+i];
}
}else{
buf[0] = nfc_buf[3];
memcpy(buf, nfc_buf+5, nfc_buf[3]);
}
return 1;
}
u8 NFC_Module::FelicaPoll(u8 *buf, u8 len, u8 *idata)
{
static u8 sta=0;
if(!sta){
nfc_buf[0] = PN532_COMMAND_INLISTPASSIVETARGET;
nfc_buf[1] = 0x02;
nfc_buf[2] = 0x02;
if(len){
memcpy(nfc_buf+3, idata, len);
}
if(!write_cmd_check_ack(nfc_buf, 3+len)){
return 0;
}
#ifdef PN532DEBUG
puthex(nfc_buf, 3+len);
Serial.println();
#endif
// puthex(nfc_buf, 3+len);
Serial.println("Send command");
}
sta = 1;
/** "Waiting for IRQ (indicates card presence)" */
wait_ready();
wait_ready();
wait_ready();
#ifdef PN532DEBUG
Serial.print(" Found Card.\n");
#endif
read_dt(nfc_buf,40);
puthex(nfc_buf, nfc_buf[3]+6);
Serial.println();
if(nfc_buf[NFC_FRAME_ID_INDEX-1] != 0xD5){
return 0;
}
sta = 0;
puthex(nfc_buf, nfc_buf[3]+6);
Serial.println();
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INLISTPASSIVETARGET+1)){
return 0;
}
// if(nfc_buf[NFC_FRAME_ID_INDEX+1]!=1){
//#ifdef PN532DEBUG
// Serial.println(nfc_buf[NFC_FRAME_ID_INDEX+1],DEC);
//#endif
// return 0;
// }
buf[0] = nfc_buf[3];
memcpy(buf, nfc_buf+5, nfc_buf[3]);
return 1;
}
/*****************************************************************************/
/*!
@brief Mifare funciton. Authentication a block for more operation.
@param type - key type. 0-KEYA, 1-KEYB
@param block - block to Authentication
@param uuid - pointer to selected card's UUID
@param uuid_len - UUID length
@param key - pointer to key buffer.
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::MifareAuthentication(u8 type, u8 block,
u8 *uuid, u8 uuid_len, u8 *key)
{
u8 i;
nfc_buf[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_buf[1] = 1; // logical number of the relevant target
nfc_buf[2] = MIFARE_CMD_AUTH_A+type;
nfc_buf[3] = block;
for(i=0; i<6; i++){
nfc_buf[4+i] = key[i];
}
for(i=0; i<uuid_len; i++){
nfc_buf[10+i] = uuid[i];
}
if(!write_cmd_check_ack( nfc_buf, (10+uuid_len) )){
return 0;
}
wait_ready();
read_dt(nfc_buf, 8);
#if 0
if(nfc_buf[5] == 0xD5){
Serial.print("Authentication receive:");
puthex(nfc_buf, nfc_buf[3]+6);
Serial.println();
}
#endif
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INDATAEXCHANGE+1)){
#ifdef PN532DEBUG
puthex(nfc_buf, 20);
Serial.println("Authentication fail.");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
return 0;
}
return 1;
}
/*****************************************************************************/
/*!
@brief Mifare funciton. Read a block.
@param block - block to read
@param buf - pointer to data buffer.
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::MifareReadBlock(u8 block, u8 *buf)
{
nfc_buf[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_buf[1] = 1; // logical number of the relevant target
nfc_buf[2] = MIFARE_CMD_READ;
nfc_buf[3] = block;
if(!write_cmd_check_ack(nfc_buf, 4)){
return 0;
}
wait_ready();
read_dt(nfc_buf, 26);
/**
if(nfc_buf[5] == 0xD5){
Serial.print("Block receive:");
puthex(nfc_buf, nfc_buf[3]+6);
Serial.println();
}
*/
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INDATAEXCHANGE+1)){
#ifdef PN532DEBUG
puthex(nfc_buf, 20);
Serial.println("Authentication fail.");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
return 0;
}
memcpy(buf, nfc_buf+8, 16);
return 1;
}
/*****************************************************************************/
/*!
@brief Mifare funciton. Write to a block.
@param block - block to write
@param buf - pointer to data buffer.
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::MifareWriteBlock(u8 block, u8 *buf)
{
nfc_buf[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_buf[1] = 1; // logical number of the relevant target
nfc_buf[2] = MIFARE_CMD_WRITE;
nfc_buf[3] = block;
memcpy(nfc_buf+4, buf, 16);
if(!write_cmd_check_ack(nfc_buf, 20)){
return 0;
}
wait_ready();
read_dt(nfc_buf, 26);
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INDATAEXCHANGE+1)){
#ifdef PN532DEBUG
puthex(nfc_buf, 20);
Serial.println("Authentication fail.");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
return 0;
}
return 1;
}
/*****************************************************************************/
/*!
@brief Configure PN532 as Initiator
@param NONE
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::P2PInitiatorInit()
{
/** avoid resend command */
static u8 send_flag=1;
nfc_buf[0] = PN532_COMMAND_INJUMPFORDEP;
nfc_buf[1] = 0x01; // avtive mode
nfc_buf[2] = 0x02; // 201Kbps
nfc_buf[3] = 0x01;
nfc_buf[4] = 0x00;
nfc_buf[5] = 0xFF;
nfc_buf[6] = 0xFF;
nfc_buf[7] = 0x00;
nfc_buf[8] = 0x00;
if(send_flag){
send_flag = 0;
if(!write_cmd_check_ack(nfc_buf, 9)){
#ifdef PN532_P2P_DEBUG
Serial.println("InJumpForDEP sent fialed\n");
#endif
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("InJumpForDEP sent ******\n");
#endif
}
wait_ready(10);
read_dt(nfc_buf, 25);
if(nfc_buf[5] != 0xD5){
// Serial.println("InJumpForDEP sent read failed");
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INJUMPFORDEP+1)){
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println("Initiator init failed");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("InJumpForDEP read success");
#endif
send_flag = 1;
return 1;
}
/*****************************************************************************/
/*!
@brief Configure PN532 as Target.
@param NONE.
@return 0 - failed
1 - successfully
*/
/*****************************************************************************/
u8 NFC_Module::P2PTargetInit()
{
/** avoid resend command */
static u8 send_flag=1;
nfc_buf[0] = PN532_COMMAND_TGINITASTARGET;
/** 14443-4A Card only */
nfc_buf[1] = 0x00;
/** SENS_RES */
nfc_buf[2] = 0x04;
nfc_buf[3] = 0x00;
/** NFCID1 */
nfc_buf[4] = 0x12;
nfc_buf[5] = 0x34;
nfc_buf[6] = 0x56;
/** SEL_RES */
nfc_buf[7] = 0x40; // DEP only mode
/**Parameters to build POL_RES (18 bytes including system code) */
nfc_buf[8] = 0x01;
nfc_buf[9] = 0xFE;
nfc_buf[10] = 0xA2;
nfc_buf[11] = 0xA3;
nfc_buf[12] = 0xA4;
nfc_buf[13] = 0xA5;
nfc_buf[14] = 0xA6;
nfc_buf[15] = 0xA7;
nfc_buf[16] = 0xC0;
nfc_buf[17] = 0xC1;
nfc_buf[18] = 0xC2;
nfc_buf[19] = 0xC3;
nfc_buf[20] = 0xC4;
nfc_buf[21] = 0xC5;
nfc_buf[22] = 0xC6;
nfc_buf[23] = 0xC7;
nfc_buf[24] = 0xFF;
nfc_buf[25] = 0xFF;
/** NFCID3t */
nfc_buf[26] = 0xAA;
nfc_buf[27] = 0x99;
nfc_buf[28] = 0x88;
nfc_buf[29] = 0x77;
nfc_buf[30] = 0x66;
nfc_buf[31] = 0x55;
nfc_buf[32] = 0x44;
nfc_buf[33] = 0x33;
nfc_buf[34] = 0x22;
nfc_buf[35] = 0x11;
/** Length of general bytes */
nfc_buf[36] = 0x00;
/** Length of historical bytes */
nfc_buf[37] = 0x00;
if(send_flag){
send_flag = 0;
if(!write_cmd_check_ack(nfc_buf, 38)){
send_flag = 1;
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("Target init sent.");
#endif
}
wait_ready(10);
read_dt(nfc_buf, 24);
if(nfc_buf[5] != 0xD5){
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_TGINITASTARGET+1)){
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println("Target init fail.");
#endif
return 0;
}
send_flag = 1;
#ifdef PN532_P2P_DEBUG
Serial.println("TgInitAsTarget read success");
#endif
return 1;
}
/*****************************************************************************/
/*!
@brief Initiator send and reciev data.
@param tx_buf --- data send buffer, user sets
tx_len --- data send legth, user sets.
rx_buf --- data recieve buffer, returned by P2PInitiatorTxRx
rx_len --- data receive length, returned by P2PInitiatorTxRx
@return 0 - send failed
1 - send successfully
*/
/*****************************************************************************/
u8 NFC_Module::P2PInitiatorTxRx(u8 *t_buf, u8 t_len, u8 *r_buf, u8 *r_len)
{
// wait_ready();
// wait_ready();
wait_ready(15);
nfc_buf[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_buf[1] = 0x01; // logical number of the relevant target
memcpy(nfc_buf+2, t_buf, t_len);
if(!write_cmd_check_ack(nfc_buf, t_len+2)){
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("Initiator DataExchange sent.");
#endif
wait_ready(200);
read_dt(nfc_buf, 60);
if(nfc_buf[5] != 0xD5){
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("Initiator DataExchange Get.");
#endif
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_INDATAEXCHANGE+1)){
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println("Send data failed");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
#ifdef PN532_P2P_DEBUG
Serial.print("InExchangeData Error:");
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println();
#endif
return 0;
}
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println();
#endif
/** return read data */
*r_len = nfc_buf[3]-3;
memcpy(r_buf, nfc_buf+8, *r_len);
return 1;
}
/*****************************************************************************/
/*!
@brief Target sends and recievs data.
@param tx_buf --- data send buffer, user sets
tx_len --- data send legth, user sets.
rx_buf --- data recieve buffer, returned by P2PInitiatorTxRx
rx_len --- data receive length, returned by P2PInitiatorTxRx
@return 0 - send failed
1 - send successfully
*/
/*****************************************************************************/
u8 NFC_Module::P2PTargetTxRx(u8 *t_buf, u8 t_len, u8 *r_buf, u8 *r_len)
{
nfc_buf[0] = PN532_COMMAND_TGGETDATA;
if(!write_cmd_check_ack(nfc_buf, 1)){
return 0;
}
wait_ready(100);
read_dt(nfc_buf, 60);
if(nfc_buf[5] != 0xD5){
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_TGGETDATA+1)){
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, 20);
Serial.println("Target GetData failed");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
#ifdef PN532_P2P_DEBUG
Serial.print("TgGetData Error:");
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println();
#endif
return 0;
}
#ifdef PN532_P2P_DEBUG
Serial.println("TgGetData:");
puthex(nfc_buf, nfc_buf[3]+7);
Serial.println();
#endif
/** return read data */
*r_len = nfc_buf[3]-3;
memcpy(r_buf, nfc_buf+8, *r_len);
nfc_buf[0] = PN532_COMMAND_TGSETDATA;
memcpy(nfc_buf+1, t_buf, t_len);
if(!write_cmd_check_ack(nfc_buf, 1+t_len)){
return 0;
}
wait_ready(100);
read_dt(nfc_buf, 26);
if(nfc_buf[5] != 0xD5){
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_TGSETDATA+1)){
#ifdef PN532_P2P_DEBUG
puthex(nfc_buf, 20);
Serial.println("Send data failed");
#endif
return 0;
}
if(nfc_buf[NFC_FRAME_ID_INDEX+1]){
return 0;
}
return 1;
}
/*****************************************************************************/
/*!
@brief Unfinished
@param
@return 0 - send failed
1 - send successfully
*/
/*****************************************************************************/
u8 NFC_Module::TgInitAsTarget()
{
nfc_buf[0] = PN532_COMMAND_TGINITASTARGET;
/** 14443-4A Card only */
nfc_buf[1] = 0x04;
/** SENS_RES */
nfc_buf[2] = 0x04;
nfc_buf[3] = 0x00;
/** NFCID1 */
nfc_buf[4] = 0x12;
nfc_buf[5] = 0x34;
nfc_buf[6] = 0x56;
/** SEL_RES */
nfc_buf[7] = 0x60;
/**Parameters to build POL_RES (18 bytes including system code) */
nfc_buf[8] = 0x01;
nfc_buf[9] = 0xFE;
nfc_buf[10] = 0xA2;
nfc_buf[11] = 0xA3;
nfc_buf[12] = 0xA4;
nfc_buf[13] = 0xA5;
nfc_buf[14] = 0xA6;
nfc_buf[15] = 0xA7;
nfc_buf[16] = 0xC0;
nfc_buf[17] = 0xC1;
nfc_buf[18] = 0xC2;
nfc_buf[19] = 0xC3;
nfc_buf[20] = 0xC4;
nfc_buf[21] = 0xC5;
nfc_buf[22] = 0xC6;
nfc_buf[23] = 0xC7;
nfc_buf[24] = 0xFF;
nfc_buf[25] = 0xFF;
/** NFCID3t */
nfc_buf[26] = 0xAA;
nfc_buf[27] = 0x99;
nfc_buf[28] = 0x88;
nfc_buf[29] = 0x77;
nfc_buf[30] = 0x66;
nfc_buf[31] = 0x55;
nfc_buf[32] = 0x44;
nfc_buf[33] = 0x33;
nfc_buf[34] = 0x22;
nfc_buf[35] = 0x11;
/** Length of general bytes */
nfc_buf[36] = 0x00;
/** Length of historical bytes */
nfc_buf[37] = 0x00;
if(!write_cmd_check_ack(nfc_buf, 38)){
return 0;
}
return 1;
}
/*****************************************************************************/
/*!
@brief Unfinished
@param
@return
*/
/*****************************************************************************/
u8 NFC_Module::TargetPolling()
{
static poll_sta_type sta;
static u8 count=0;
wait_ready();
read_dt(nfc_buf, 30);
#ifdef PN532DEBUG
delay(300);
puthex(nfc_buf, 9);
Serial.println();
#endif
switch(sta){
case NFC_STA_TAG:
break;
case NFC_STA_GETDATA:
count++;
if(count > 10){
Serial.println("*************Reinit Target************");
/** Target release */
TgInitAsTarget();
Serial.println("*************Reinit Target************");
sta = NFC_STA_TAG;
}
break;
case NFC_STA_SETDATA:
break;
}
if(nfc_buf[5] == 0xD5){
puthex(nfc_buf, nfc_buf[3]+6);
switch(nfc_buf[NFC_FRAME_ID_INDEX]){
case PN532_COMMAND_TGINITASTARGET+1:
Serial.println("TgInitTatget");
nfc_buf[0] = PN532_COMMAND_TGGETDATA;
sta = NFC_STA_GETDATA;
count=0;
if(!write_cmd_check_ack(nfc_buf, 1)){
return 0;
}
break;
case PN532_COMMAND_TGRESPONSETOINITIATOR+1:
Serial.println("TgResponseToInitiator");
break;
case PN532_COMMAND_TGGETINITIATORCOMMAND+1:
Serial.println("TgGetInitiatorCommand");
break;
case PN532_COMMAND_TGGETDATA+1:
Serial.println("TgGetData");
if(!nfc_buf[NFC_FRAME_ID_INDEX+1]){
Serial.println("TgGetData Success.");
switch(nfc_buf[NFC_FRAME_ID_INDEX+2]){
case 0x60:
nfc_buf[0] = PN532_COMMAND_TGSETDATA;
nfc_buf[1] = 0x00;
nfc_buf[2] = 0xFF;
if(!write_cmd_check_ack(nfc_buf, 3)){
return 0;
}
break;
case 0x30:
nfc_buf[0] = PN532_COMMAND_TGSETDATA;
for(u8 i=1; i<17; i++){
nfc_buf[i] = i;
}
if(!write_cmd_check_ack(nfc_buf, 17)){
return 0;
}
break;
default:
Serial.println("TgGetData Error.");
wait_ready();
/** Target release */
TgInitAsTarget();
break;
}
}else{
/** Target release */
TgInitAsTarget();
}
break;
case PN532_COMMAND_TGSETDATA+1:
Serial.println("TgSetData");
if(!nfc_buf[NFC_FRAME_ID_INDEX+1]){
Serial.println("TgSetData Success.");
nfc_buf[0] = PN532_COMMAND_TGGETDATA;
sta = NFC_STA_GETDATA;
count=0;
if(!write_cmd_check_ack(nfc_buf, 1)){
/** */
Serial.println("SetData check error.");
return 0;
}
}
break;
default:
Serial.println("Undifined command");
break;
}
}else{
Serial.println(".");
}
return 1;
}
/*****************************************************************************/
/*!
@brief PN532 SetParameters command. Details in NXP's PN532UM.pdf
@param para - parameter to set
@return 0 - send failed
1 - send successfully
*/
/*****************************************************************************/
u8 NFC_Module::SetParameters(u8 para)
{
nfc_buf[0] = PN532_COMMAND_SETPARAMETERS;
nfc_buf[1] = para;
if(!write_cmd_check_ack(nfc_buf, 2)){
return 0;
}
read_dt(nfc_buf, 8);
if(nfc_buf[NFC_FRAME_ID_INDEX] != (PN532_COMMAND_SETPARAMETERS+1)){
return 0;
}
return 1;
}
/*****************************************************************************/
/*!
@brief send frame to PN532 and wait for ack
@param cmd - pointer to frame buffer
@param len - frame length
@return 0 - send failed
1 - send successfully
*/
/*****************************************************************************/
u8 NFC_Module::write_cmd_check_ack(u8 *cmd, u8 len)
{
write_cmd(cmd, len);
wait_ready();
#ifdef PN532DEBUG
Serial.println("IRQ received");
#endif
// read acknowledgement
if (!read_ack()) {
#ifdef PN532DEBUG
Serial.println("No ACK frame received!");
#endif
return false;
}
return true; // ack'd command
}
/*****************************************************************************/
/*!
@brief send a byte data via I2C interface
@param data - The byte to send.
@return 0 - send failed
1 - successful
*/
/*****************************************************************************/
inline u8 NFC_Module::send(u8 data)
{
#if ARDUINO >= 100
return Wire.write((u8)data);
#else
return Wire.send((u8)data);
#endif
}
/*****************************************************************************/
/*!
@brief receive a byte from I2C interface
@param NONE
@return received data
*/
/*****************************************************************************/
inline u8 NFC_Module::receive(void)
{
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
/*****************************************************************************/
/*!
@brief Send a byte by hex format
@param data - the byte
@return NONE
*/
/*****************************************************************************/
void NFC_Module::puthex(u8 data)
{
Serial.write(hextab[(data>>4)&0x0F]);
Serial.write(hextab[data&0x0F]);
Serial.write(' ');
}
/*****************************************************************************/
/*!
@brief Send hexadecimal data through Serial with specified length.
@param buf - pointer of data buffer.
@param len - length need to send.
@return NONE
*/
/*****************************************************************************/
void NFC_Module::puthex(u8 *buf, u32 len)
{
u32 i;
for(i=0; i<len; i++)
{