forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mfrc522.cs
994 lines (880 loc) · 35.8 KB
/
Mfrc522.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers.Binary;
using System.Device.Gpio;
using System.Device.I2c;
using System.Device.Spi;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Threading;
using Iot.Device.Card;
using Iot.Device.Rfid;
using Iot.Device.Card.Mifare;
using Iot.Device.Card.Ultralight;
#if DEBUG
using Microsoft.Extensions.Logging;
using nanoFramework.Logging;
#endif
namespace Iot.Device.Mfrc522
{
/// <summary>
/// MfRc522 module
/// </summary>
public class MfRc522 : CardTransceiver, IDisposable
{
/// <summary>
/// The maximum speed for SPI transfer speed
/// </summary>
public const int MaximumSpiClockFrequency = 10_000_000;
/// <summary>
/// Only SPI Mode supported is Mode0
/// </summary>
public const SpiMode DefaultSpiMode = SpiMode.Mode0;
private readonly int _pinReset;
#if DEBUG
private readonly ILogger _logger;
#endif
private readonly SerialPort _serialPort;
private SpiDevice _spiDevice;
private I2cDevice _i2CDevice;
private GpioController? _controller;
private bool _shouldDispose;
#region Constructors
/// <summary>
/// Constructor for MFRC5222 with SPI interface.
/// </summary>
/// <param name="spiDevice">A SPI device</param>
/// <param name="pinReset">A reset pin for the hardware reset.</param>
/// <param name="gpioController">A GpioController for the hardware reset.</param>
/// <param name="shouldDispose">True to dispose the GpioController.</param>
public MfRc522(SpiDevice spiDevice, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
{
_spiDevice = spiDevice;
_pinReset = pinReset;
#if DEBUG
_logger = this.GetCurrentClassLogger();
#endif
HardReset(gpioController, shouldDispose);
SetDefaultValues();
}
/// <summary>
/// Constructor for MFRC5222 with I2C interface.
/// </summary>
/// <param name="i2cDevice">An I2C device, note that there is no default address for this device, it can be programmed with pins.</param>
/// <param name="pinReset">A reset pin for the hardware reset.</param>
/// <param name="gpioController">A GpioController for the hardware reset.</param>
/// <param name="shouldDispose">True to dispose the GpioController.</param>
public MfRc522(I2cDevice i2cDevice, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
{
_i2CDevice = i2cDevice;
_pinReset = pinReset;
#if DEBUG
_logger = this.GetCurrentClassLogger();
#endif
HardReset(gpioController, shouldDispose);
SetDefaultValues();
}
/// <summary>
/// Constructor for MFRC5222 with Serial Port interface.
/// </summary>
/// <param name="serialPort">A Serial Port name, will construct a SerialPort with default speed of 9600 baud, no parity, 1 bit stop.</param>
/// <param name="pinReset">A reset pin for the hardware reset.</param>
/// <param name="gpioController">A GpioController for the hardware reset.</param>
/// <param name="shouldDispose">True to dispose the GpioController.</param>
public MfRc522(string serialPort, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
: this(new SerialPort(serialPort, 9600, Parity.None, 8, StopBits.One), pinReset, gpioController, shouldDispose)
{
}
/// <summary>
/// Constructor for MFRC5222 with Serial Port interface.
/// </summary>
/// <param name="serialPort">A Serial Port, default speed is 9600 baud, no parity, 1 bit stop.</param>
/// <param name="pinReset">A reset pin for the hardware reset.</param>
/// <param name="gpioController">A GpioController for the hardware reset.</param>
/// <param name="shouldDispose">True to dispose the GpioController.</param>
public MfRc522(SerialPort serialPort, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
{
#if DEBUG
_logger = this.GetCurrentClassLogger();
#endif
_serialPort = serialPort;
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
if (!_serialPort.IsOpen)
{
_serialPort.Open();
}
_pinReset = pinReset;
HardReset(gpioController, shouldDispose);
SetDefaultValues();
}
private void SetDefaultValues()
{
// Switch off the crypto for Mifare card in case it's on
ClearRegisterBit(Register.Status2, (byte)Status2.MFCrypto1On);
// Set Timer for Timeout, see documentation on those registers to understand the values
WriteRegister(Register.TMode, (byte)TMode.TAutoRestart);
WriteRegister(Register.TPrescaler, 0xA9);
WriteRegister(Register.TReloadHigh, 0x06);
WriteRegister(Register.TReloadLow, 0xE8);
// forces a 100 % ASK modulation independent of the ModGsPReg register setting
WriteRegister(Register.TxAsk, 0x40);
// Set CRC to 0x6363 (iso 14443-3 6.1.6)
WriteRegister(Register.Mode, (byte)(Mode.TxWaitRF | Mode.PolMFinHigh) | (byte)ModeCrc.Preset6363h);
// Switching on the antenna
Enabled = true;
}
private void HardReset(GpioController? gpioController, bool shouldDispose)
{
if (_pinReset >= 0)
{
_shouldDispose = shouldDispose || gpioController == null;
_controller = gpioController ?? new GpioController();
_controller.OpenPin(_pinReset, PinMode.Output);
_controller.Write(_pinReset, PinValue.Low);
// 100 nano seconds at least, take some margin
Thread.Sleep(1);
_controller.Write(_pinReset, PinValue.High);
// 37.7 milliseconds according to documentation
Thread.Sleep(38);
}
}
#endregion
#region Properties and functions
/// <summary>
/// Get or Set the gain.
/// </summary>
public Gain Gain
{
get => (Gain)(ReadRegister(Register.RFCfg) & (byte)Gain.G48dB);
set => WriteRegister(Register.RFCfg, (byte)value);
}
/// <summary>
/// Get the Version.
/// </summary>
/// <remarks>Only versions 1.0 and 2.0 are valid for authentic MFRC522.
/// Some copies may not have a proper version but would just work.</remarks>
public Version Version
{
get
{
Version version;
var rev = ReadRegister(Register.Version);
// See documentation page 66, chapter 9.3.4.8
if (rev == 0x91)
{
version = new Version(1, 0);
}
else if (rev == 0x92)
{
version = new Version(2, 0);
}
else
{
version = new Version(0, 0);
}
return version;
}
}
/// <summary>
/// Switch on or off the antenna.
/// </summary>
public bool Enabled
{
get => (ReadRegister(Register.TxControl) & (byte)(TxControl.Tx2RFEn | TxControl.Tx1RFEn)) == (byte)(TxControl.Tx2RFEn | TxControl.Tx1RFEn);
set
{
if (value)
{
SetRegisterBit(Register.TxControl, (byte)(TxControl.Tx2RFEn | TxControl.Tx1RFEn));
}
else
{
ClearRegisterBit(Register.TxControl, (byte)(TxControl.Tx2RFEn | TxControl.Tx1RFEn));
}
}
}
/// <summary>
/// Set or Get the baud rate for the serial port communication.
/// Default is 9600 baud.
/// </summary>
public SerialSpeed SerialSpeed
{
get => (SerialSpeed)ReadRegister(Register.SerialSpeed);
set => WriteRegister(Register.SerialSpeed, (byte)value);
}
/// <summary>
/// Perform a soft reset. The configuration data of the internal buffer
/// remains unchanged.All registers are set to the reset values.This command automatically
/// terminates when finished.
/// </summary>
/// <remarks>The SerialSpeedReg register is reset and therefore the serial data rate is set to 9600 baud.</remarks>
public void SoftReset()
{
WriteRegister(Register.Command, (byte)MfrcCommand.ResetPhase);
// 37.7 milliseconds
Thread.Sleep(38);
}
/// <summary>
/// Listen to any 14443 Type A card.
/// </summary>
/// <param name="card">A card once detected.</param>
/// <param name="timeout">A timeout for pulling the card.</param>
/// <returns>True if success.</returns>
public bool ListenToCardIso14443TypeA(out Data106kbpsTypeA card, TimeSpan timeout)
{
card = new Data106kbpsTypeA(0, 0, 0, new byte[0], null);
byte[] atqa = new byte[2];
DateTime dtTimeout = DateTime.UtcNow.Add(timeout);
// Switch off the cryptography for Mifare card in case it's on
ClearRegisterBit(Register.Status2, (byte)Status2.MFCrypto1On);
do
{
Status sc = PiccRequestA(atqa);
if (sc == Status.Collision || sc == Status.Ok)
{
break;
}
if (dtTimeout > DateTime.UtcNow)
{
return false;
}
// Give a bit of time for the card and reader
Thread.Sleep(10);
}
while (true);
card.Atqa = BinaryPrimitives.ReadUInt16LittleEndian(atqa);
var status = Select(out byte[]? nfcId, out byte sak);
if (status != Status.Ok)
{
return false;
}
if (nfcId is object)
{
card.NfcId = new byte[nfcId.Length];
nfcId.CopyTo(card.NfcId, 0);
}
card.Sak = sak;
return true;
}
/// <summary>
/// Check if a new card is present.
/// </summary>
/// <param name="atqa">ATQA buffer must be 2 bytes length and will contain the ATQA answer if there is a card.</param>
/// <returns>true if there is a card, else false.</returns>
public bool IsCardPresent(byte[] atqa)
{
if (atqa is not object or { Length: not 2 })
{
throw new ArgumentException($"{nameof(atqa)} must be initialized and its size must be 2.");
}
// Switch off the cryptography for Mifare card in case it's on
ClearRegisterBit(Register.Status2, (byte)Status2.MFCrypto1On);
Status sc = PiccRequestA(atqa);
if (sc == Status.Collision || sc == Status.Ok)
{
return true;
}
return false;
}
private Status Select(out byte[]? uid, out byte sak)
{
sak = 0;
uid = null;
bool selectDone = false;
int sizeUid = 0;
int bitKnown = 0;
byte[] uidKnown = new byte[4];
byte[] tempUid = new byte[10];
// all received bits will be cleared after a collision
// only used during bitwise anticollision at 106 kBd,
// otherwise it is set to logic 1
ClearRegisterBit(Register.Coll, 0x80);
int selectCascadeLevel = 1;
// There are 3 SL maximum. For looping and adjusting the SL
// SL1 = 0x93, SL2 = 0x95, SL3 = 0x97
while (!selectDone)
{
var bufferLength = bitKnown == 0 ? 2 : 9;
byte[] dataToCard = new byte[bufferLength];
byte[] dataFromCard = bitKnown == 0 ? new byte[5] : new byte[3];
byte numValidBits = (byte)(bitKnown == 0 ? 0x20 : 0x70);
int destinationIndex;
switch (selectCascadeLevel)
{
case 1:
dataToCard[0] = (byte)CardCommand.SelectCascadeLevel1;
sizeUid = 4;
destinationIndex = 0;
break;
case 2:
dataToCard[0] = (byte)CardCommand.SelectCascadeLevel2;
sizeUid = 7;
destinationIndex = 3;
break;
case 3:
dataToCard[0] = (byte)CardCommand.SelectCascadeLevel3;
sizeUid = 10;
destinationIndex = 6;
break;
default:
return Status.Error;
}
dataToCard[1] = numValidBits;
if (bitKnown != 0)
{
for (int i = 0; i < 4; i++)
{
dataToCard[i + 2] = uidKnown[i];
}
// Standard CRC byte calculation for this specific action
dataToCard[6] = (byte)(dataToCard[2] ^ dataToCard[3] ^ dataToCard[4] ^ dataToCard[5]);
var crcStatus = CalculateCrc(new SpanByte(dataToCard, 0, 7), new SpanByte(dataToCard, 7, dataToCard.Length - 7));
if (crcStatus != Status.Ok)
{
return crcStatus;
}
}
// Reset Bit Framing
WriteRegister(Register.BitFraming, 0x00);
Status sc = SendAndReceiveData(MfrcCommand.Transceive, dataToCard, dataFromCard);
if (sc != Status.Ok)
{
return sc;
}
// Once all the bits will be known
if (bitKnown >= 32)
{
if (dataToCard[2] == 0x88)
{
// check that there is no cascade
if ((dataFromCard[0] & 0x04) != 0x04)
{
return Status.Error;
}
Array.Copy(dataToCard, 3, tempUid, destinationIndex, 3);
selectCascadeLevel++;
// ready for next CascadeLevel
bitKnown = 0;
}
else
{
selectDone = true;
Array.Copy(dataToCard, 2, tempUid, destinationIndex, 4);
sak = dataFromCard[0];
// check that there is no cascade
if ((dataFromCard[0] & 0x04) == 0x04)
{
return Status.Error;
}
}
}
else
{
// All bit are known, redo loop to do select the card
bitKnown = 32;
new SpanByte(dataFromCard, 0, 4).CopyTo(uidKnown);
}
}
// Finally create the uid buffer
uid = new byte[sizeUid];
Array.Copy(tempUid, uid, uid.Length);
return Status.Ok;
}
private Status PiccRequestA(byte[] bufferAtqa)
{
// all received bits will be cleared after a collision
// only used during bitwise anticollision at 106 kBd,
// otherwise it is set to logic 1
ClearRegisterBit(Register.Coll, 0x80);
// Only 7 bits are valid in th ReqA request
byte validBits = 0x07;
Status sc = SendAndReceiveData(MfrcCommand.Transceive, new[] { (byte)CardCommand.ReqA }, bufferAtqa, validBits);
if (sc != Status.Ok)
{
return sc;
}
// Valid bits coded on 3 bits
validBits = (byte)(ReadRegister(Register.Control) & 0x07);
if (validBits != 0)
{
return Status.Error;
}
return Status.Ok;
}
/// <summary>
/// Send and Receive Data.
/// </summary>
/// <param name="command">The MFRC522 command.</param>
/// <param name="sendData">The data to send.</param>
/// <param name="receiveData">The data to receive. Note that you need to have at least the size of data you expect to receive.</param>
/// <param name="numberValidBitsLastByte">The number of bits valid in the last byte, 8 is the default.</param>
/// <returns>True if the operation is successful.</returns>
public Status SendAndReceiveData(MfrcCommand command, SpanByte sendData, SpanByte receiveData, byte numberValidBitsLastByte = 8)
{
byte bitFraming = (byte)(numberValidBitsLastByte == 8 ? 0 : numberValidBitsLastByte & (byte)BitFraming.TxLastBitsMask);
byte waitIrq = command == MfrcCommand.MifareAuthenticate ? (byte)(ComIr.IdleIRq) : (byte)(ComIr.IdleIRq | ComIr.RxIRq);
byte irqEn = command == MfrcCommand.MifareAuthenticate ? (byte)(ComIr.IdleIRq | ComIr.ErrIRq | ComIr.SetIrq) : (byte)(ComIr.ErrIRq | ComIr.IdleIRq | ComIr.LoAlertIRq | ComIr.RxIRq | ComIr.SetIrq | ComIr.TimerIRq | ComIr.TxIRq);
// Set to idle, prepare FIFO and bit framing
WriteRegister(Register.Command, (byte)MfrcCommand.Idle);
WriteRegister(Register.ComIEn, irqEn);
ClearRegisterBit(Register.ComIrq, (byte)ComIr.SetIrq);
SetRegisterBit(Register.FifoLevel, 0x80);
WriteRegister(Register.FifoData, sendData);
WriteRegister(Register.BitFraming, bitFraming);
// Set the real command
WriteRegister(Register.Command, (byte)command);
if (command == MfrcCommand.Transceive)
{
SetRegisterBit(Register.BitFraming, 0x80);
}
Status status = WaitForCommandToComplete(waitIrq);
if (status == Status.Timeout)
{
return status;
}
// Check all is cleared
Error error = (Error)ReadRegister(Register.Error);
if (error.HasFlag(Error.BufferOvfl) || error.HasFlag(Error.ParityErr) || error.HasFlag(Error.ProtocolErr))
{
return Status.Error;
}
// Read if there is something to read
if (receiveData.Length > 0)
{
var bytesRead = ReadRegister(Register.FifoLevel);
if (bytesRead == 0 && receiveData.Length > 0)
{
return Status.Error;
}
// We still read the data even if there are more available
if (bytesRead > receiveData.Length)
{
return Status.Error;
}
ReadRegister(Register.FifoData, receiveData);
}
// Check collision
if (error.HasFlag(Error.CollErr))
{
return Status.Collision;
}
return Status.Ok;
}
/// <summary>
/// Stop to communicate with a card.
/// </summary>
/// <returns>True if success.</returns>
/// <remarks>It's not because you don't get a positive result the card is not halt.</remarks>
public bool Halt()
{
byte[] buffer = new byte[4];
buffer[0] = (byte)CardCommand.HaltA;
buffer[1] = 0;
var status = CalculateCrc(new SpanByte(buffer, 0, 2), new SpanByte(buffer, 2, buffer.Length - 2));
if (status != Status.Ok)
{
return false;
}
status = SendAndReceiveData(MfrcCommand.Transceive, buffer, null);
if (status == Status.Timeout)
{
return true;
}
return status != Status.Error;
}
/// <summary>
/// Prepare for sleep, make sure cryptography is off and switch off the antenna.
/// </summary>
public void PrepareForSleep()
{
Enabled = false;
ClearRegisterBit(Register.Status2, (byte)Status2.MFCrypto1On);
}
/// <summary>
/// Specific function to authenticate Mifare cards
/// </summary>
/// <param name="key">A 6 bytes key</param>
/// <param name="mifareCommand">MifareCardCommand.AuthenticationA or MifareCardCommand.AuthenticationB</param>
/// <param name="blockAddress">The block address to authenticate.</param>
/// <param name="cardUid">The 4 bytes UUID of the card.</param>
/// <returns>True if success.</returns>
public Status MifareAuthenticate(SpanByte key, MifareCardCommand mifareCommand, byte blockAddress, SpanByte cardUid)
{
if (mifareCommand != MifareCardCommand.AuthenticationA && mifareCommand != MifareCardCommand.AuthenticationB)
{
throw new ArgumentException("Must be AuthenticateA or AuthenticateB only");
}
if (key.Length != 6)
{
throw new ArgumentException("Key must have a length of 6.", nameof(key));
}
byte[] buffer = new byte[12];
buffer[0] = (byte)mifareCommand;
buffer[1] = blockAddress;
key.CopyTo(new SpanByte(buffer, 2, 6));
cardUid.CopyTo(new SpanByte(buffer, 8, 4));
return SendAndReceiveData(MfrcCommand.MifareAuthenticate, buffer, null);
}
private Status CalculateCrc(SpanByte buffer, SpanByte crc)
{
// Timeout for the CRC calculation
const long Timeout = 89;
if (crc.Length < 2)
{
throw new ArgumentException($"CRC buffer must be at least 2 bytes");
}
WriteRegister(Register.Command, (byte)MfrcCommand.Idle);
WriteRegister(Register.DivIrq, (byte)DivIrq.CRCIRq);
WriteRegister(Register.FifoLevel, 0x80);
WriteRegister(Register.FifoData, buffer.ToArray());
WriteRegister(Register.Command, (byte)MfrcCommand.CalculateCrc);
Stopwatch stopwatch = Stopwatch.StartNew();
do
{
DivIrq irgStatus = (DivIrq)ReadRegister(Register.DivIrq);
if (irgStatus.HasFlag(DivIrq.CRCIRq))
{
WriteRegister(Register.Command, (byte)MfrcCommand.Idle);
crc[0] = ReadRegister(Register.CrcResultLow);
crc[1] = ReadRegister(Register.CrcResultHigh);
return Status.Ok;
}
}
while (stopwatch.ElapsedMilliseconds < Timeout);
return Status.Timeout;
}
private Status WaitForCommandToComplete(byte waitIrq)
{
// Timeout in ms for the CRC calculation, see documentation
const long Timeout = 36;
Stopwatch stopwatch = Stopwatch.StartNew();
do
{
byte irq = ReadRegister(Register.ComIrq);
if ((irq & waitIrq) != 0)
{
return Status.Ok;
}
if (((ComIr)irq).HasFlag(ComIr.TimerIRq))
{
return Status.Timeout;
}
}
while (stopwatch.ElapsedMilliseconds < Timeout);
return Status.Timeout;
}
#endregion
#region SPI, I2C and Serial Communication
private void WriteRegister(Register register, byte toCard)
{
if (_spiDevice is object)
{
SpiWriteRegister(register, toCard);
}
else if (_i2CDevice is object)
{
I2cWriteRegister(register, toCard);
}
else if (_serialPort is object)
{
SerialWriteRegister(register, toCard);
}
}
private void WriteRegister(Register register, SpanByte toCard)
{
if (_spiDevice is object)
{
SpiWriteRegister(register, toCard);
}
else if (_i2CDevice is object)
{
I2cWriteRegister(register, toCard);
}
else if (_serialPort is object)
{
SerialWriteRegister(register, toCard);
}
}
private byte ReadRegister(Register register)
{
if (_spiDevice is object)
{
return SpiReadRegister(register);
}
else if (_i2CDevice is object)
{
return I2cReadRegister(register);
}
else if (_serialPort is object)
{
return SerialReadRegister(register);
}
throw new IOException("No SPI, I2C or Serial port");
}
private void ReadRegister(Register register, SpanByte fromCard)
{
if (_spiDevice is object)
{
SpiReadRegister(register, fromCard);
}
else if (_i2CDevice is object)
{
I2cReadRegister(register, fromCard);
}
else if (_serialPort is object)
{
SerialReadRegister(register, fromCard);
}
}
private void SetRegisterBit(Register register, byte mask)
{
var tmp = ReadRegister(register);
WriteRegister(register, (byte)(tmp | mask));
}
private void ClearRegisterBit(Register register, byte mask)
{
var tmp = ReadRegister(register);
WriteRegister(register, (byte)(tmp & ~mask));
}
private void SpiWriteRegister(Register register, byte toCard)
{
SpanByte toWrite = new byte[2]
{
(byte)register,
toCard
};
_spiDevice!.TransferFullDuplex(toWrite, toWrite);
}
private void SpiWriteRegister(Register register, SpanByte toCard)
{
for (int i = 0; i < toCard.Length; i++)
{
SpiWriteRegister(register, toCard[i]);
}
}
private byte SpiReadRegister(Register register)
{
SpanByte buffer = new byte[2]
{
(byte)((byte)register | 0x80),
0x00
};
_spiDevice!.TransferFullDuplex(buffer, buffer);
return buffer[1];
}
private void SpiReadRegister(Register register, SpanByte fromCard)
{
if (fromCard is { Length: 0 })
{
return;
}
byte address = (byte)((byte)register | 0x80);
SpanByte buffer = new byte[fromCard.Length + 1];
for (int i = 0; i < fromCard.Length; i++)
{
buffer[i] = address;
}
_spiDevice!.TransferFullDuplex(buffer, buffer);
buffer.Slice(1).CopyTo(fromCard);
}
private void I2cWriteRegister(Register register, byte toCard)
{
SpanByte toWrite = new byte[2]
{
(byte)((byte)register >> 1),
toCard,
};
_i2CDevice!.Write(toWrite);
}
private void I2cWriteRegister(Register register, SpanByte toCard)
{
SpanByte toWrite = new byte[1 + toCard.Length];
toWrite[0] = (byte)((byte)register >> 1);
toCard.CopyTo(toWrite.Slice(1));
_i2CDevice!.Write(toWrite);
}
private byte I2cReadRegister(Register register)
{
_i2CDevice!.WriteByte((byte)(((byte)register >> 1) | 0x80));
return _i2CDevice!.ReadByte();
}
private void I2cReadRegister(Register register, SpanByte fromCard)
{
_i2CDevice!.WriteByte((byte)(((byte)register >> 1) | 0x80));
_i2CDevice!.Read(fromCard);
}
private void SerialReadRegister(Register register, SpanByte fromCard)
{
byte[] toSend = new byte[] { (byte)(((byte)register >> 1) | 0x80) };
for (int i = 0; i < fromCard.Length; i++)
{
_serialPort!.Write(toSend, 0, 1);
fromCard[i] = (byte)_serialPort.ReadByte();
}
}
private byte SerialReadRegister(Register register)
{
_serialPort!.Write(new byte[] { (byte)(((byte)register >> 1) | 0x80) }, 0, 1);
return (byte)_serialPort!.ReadByte();
}
private void SerialWriteRegister(Register register, byte toCard)
{
_serialPort!.Write(new byte[] { (byte)((byte)(register) >> 1), toCard }, 0, 2);
// We need to read 1 byte, it's the address as confirmation
// No need to check it
_serialPort!.ReadByte();
}
private void SerialWriteRegister(Register register, SpanByte toCard)
{
byte[] toSend = new byte[] { (byte)((byte)(register) >> 1), 0x00 };
for (int i = 0; i < toCard.Length; i++)
{
toSend[1] = toCard[i];
_serialPort!.Write(toSend, 0, 1);
// We need to read 1 byte, it's the address
_serialPort!.ReadByte();
_serialPort!.Write(toSend, 1, 1);
}
}
#endregion
/// <inheritdoc/>
public void Dispose()
{
if (_spiDevice is object)
{
PrepareForSleep();
_spiDevice.Dispose();
_spiDevice = null;
}
if (_i2CDevice is object)
{
PrepareForSleep();
_i2CDevice.Dispose();
_i2CDevice = null;
}
if (_serialPort is object)
{
_serialPort.Close();
}
if (_pinReset >= 0)
{
_controller?.ClosePin(_pinReset);
}
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null;
}
}
/// <inheritdoc/>
public override int Transceive(byte targetNumber, SpanByte dataToSend, SpanByte dataFromCard)
{
// targetNumber is not used here as only 1 card can be selected at a time so will be ignored
// The dataToSend buffer contains anyway the unique of the card
Status status;
// Use built in functions for authentication in case of classic Mifare cards
if ((dataToSend[0] == (byte)MifareCardCommand.AuthenticationA) || (dataToSend[0] == (byte)MifareCardCommand.AuthenticationB))
{
// UltralightCommand.GetVersion has the same command code as MifareCardCommand.AuthenticationA
// GetVersion returns data; AuthenticationA does not
if (dataFromCard.Length == 0)
{
status = SendAndReceiveData(MfrcCommand.MifareAuthenticate, dataToSend.ToArray(), null);
}
else
{
return SendWithCrc(dataToSend, dataFromCard);
}
return status == Status.Ok ? 0 : -1;
}
else if ((dataToSend[0] == (byte)MifareCardCommand.Incrementation) || (dataToSend[0] == (byte)MifareCardCommand.Decrementation)
|| (dataToSend[0] == (byte)MifareCardCommand.Restore) || (dataToSend[0] == (byte)MifareCardCommand.Write16Bytes))
{
return TwoStepsWrite16IncDecRestore(dataToSend);
}
else if (Helper.IsDefined((UltralightCommand)dataToSend[0]))
{
if ((dataToSend[0] == (byte)UltralightCommand.ReadFast) && (dataFromCard.Length > 62))
{
throw new ArgumentException($"Maximum number of pages to be able to read with MFRC522 is 7 as internal FIFO is limited to 64 including CRC.");
}
return SendWithCrc(dataToSend, dataFromCard);
}
status = SendAndReceiveData(MfrcCommand.Transceive, dataToSend.ToArray(), dataFromCard);
return status == Status.Ok ? dataFromCard.Length : -1;
}
private int SendWithCrc(SpanByte dataToSend, SpanByte dataFromCard)
{
Status status;
// 16 bytes + 2 from CRC
byte[] receivedBuffer = new byte[dataFromCard.Length + 2];
// Command + CRC
byte[] commandToSend = new byte[dataToSend.Length + 2];
dataToSend.CopyTo(commandToSend);
status = CalculateCrc(new SpanByte(commandToSend, 0, dataToSend.Length), new SpanByte(commandToSend, dataToSend.Length, commandToSend.Length - dataToSend.Length));
if (status != Status.Ok)
{
return -1;
}
status = SendAndReceiveData(MfrcCommand.Transceive, commandToSend, receivedBuffer);
if (status != Status.Ok)
{
#if DEBUG
_logger.LogDebug($"Status failed: {status}");
#endif
return -1;
}
if (dataFromCard.Length > 0)
{
// Check CRC
byte[] crc = new byte[2];
status = CalculateCrc(new SpanByte(receivedBuffer, 0, dataFromCard.Length), crc);
if (status != Status.Ok)
{
return -1;
}
if (receivedBuffer[dataFromCard.Length] == crc[0] && receivedBuffer[dataFromCard.Length + 1] == crc[1])
{
new SpanByte(receivedBuffer, 0, dataFromCard.Length).CopyTo(dataFromCard);
return dataFromCard.Length;
}
return -1;
}
return 0;
}
private int TwoStepsWrite16IncDecRestore(SpanByte dataToSend)
{
Status status;
SpanByte toSendFirst = new byte[4];
dataToSend.Slice(0, 2).CopyTo(toSendFirst);
CalculateCrc(toSendFirst.Slice(0, 2), toSendFirst.Slice(2, 2));
status = SendAndReceiveData(MfrcCommand.Transceive, toSendFirst.ToArray(), null);
if (status != Status.Ok)
{
#if DEBUG
_logger.LogWarning($"{nameof(TwoStepsWrite16IncDecRestore)} - Error {(MfrcCommand)dataToSend[0]}");
#endif
return -1;
}
SpanByte toSendSecond = new byte[dataToSend.Length];
int dataLength = toSendSecond.Length - 2;
dataToSend.Slice(2).CopyTo(toSendSecond);
CalculateCrc(toSendSecond.Slice(0, dataLength), toSendSecond.Slice(dataLength, 2));
status = SendAndReceiveData(MfrcCommand.Transceive, toSendSecond.ToArray(), SpanByte.Empty);
return status == Status.Ok ? 0 : -1;
}
/// <inheritdoc/>
public override bool ReselectTarget(byte targetNumber)
{
// We halt the card, and we don't care if it happens correctly
Halt();
// We reselect the card and ignore the target number as reader supports only 1 card
// And we assume here that the card hasn't been changed in the mean time
IsCardPresent(new byte[2]);
return Select(out byte[]? uuid, out byte sak) == Status.Ok;
}
}
}