Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.ino 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <Arduino.h>
  2. #include <pins_arduino.h>
  3. #include <SoftwareSerial.h>
  4. #include <SCLib.h>
  5. #define STATUS_LED 13
  6. #define READER_VCC
  7. #define READER_RST 4
  8. #define READER_CLK 3
  9. #define READER_GND
  10. #define READER_VPP
  11. #define READER_IO 2
  12. #define CARD_VCC 11
  13. #define CARD_RST 7
  14. #define CARD_CLK 9
  15. #define CARD_GND
  16. #define CARD_VPP
  17. #define CARD_IO 10
  18. #define CARD_PRE 12
  19. SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, false);
  20. uint8_t readerAtr[MAX_ATR_BYTES];
  21. int readerAtrSize = 0;
  22. bool readerReset = false;
  23. int baudRate = -1;
  24. int etu = -1;
  25. #ifndef digitalPinToInterrupt
  26. int digitalPinToInterrupt(int pin)
  27. {
  28. if (pin == 2)
  29. {
  30. return INT0;
  31. }
  32. else if (pin == 3)
  33. {
  34. return INT1;
  35. }
  36. return -1;
  37. }
  38. #endif
  39. void emulatorComputeEtu()
  40. {
  41. baudRate = 9600;
  42. etu = 1000000 / baudRate;
  43. }
  44. void emulatorSendAtrByte(char byte)
  45. {
  46. unsigned long nextBitTime = micros() + (2 * etu);
  47. digitalWrite(READER_IO, HIGH);
  48. int bitNumber = 0;
  49. int parity = 0;
  50. while (bitNumber < 11)
  51. {
  52. if (nextBitTime <= micros())
  53. {
  54. if (bitNumber == 0)
  55. {
  56. digitalWrite(READER_IO, LOW);
  57. }
  58. else if (bitNumber < 9)
  59. {
  60. int a = bitRead(byte, bitNumber - 1);
  61. parity += a;
  62. digitalWrite(READER_IO, a);
  63. }
  64. else if (bitNumber == 9)
  65. {
  66. digitalWrite(READER_IO, parity % 2);
  67. }
  68. else if (bitNumber == 10)
  69. {
  70. digitalWrite(READER_IO, HIGH);
  71. break;
  72. }
  73. nextBitTime += etu;
  74. ++bitNumber;
  75. }
  76. }
  77. }
  78. void emulatorSendAtr(const uint8_t* atr, size_t size)
  79. {
  80. pinMode(READER_IO, OUTPUT);
  81. for (int i = 0; i < size; ++i)
  82. {
  83. emulatorSendAtrByte(atr[i]);
  84. delayMicroseconds(4*etu);
  85. }
  86. }
  87. void readerResetAtr()
  88. {
  89. for (size_t i = 0; i < sizeof(readerAtr); ++i)
  90. {
  91. readerAtr[i] = 0;
  92. }
  93. readerAtrSize = 0;
  94. }
  95. void setup() {
  96. Serial.begin(9600);
  97. Serial.println("Initializing...");
  98. pinMode(READER_CLK, INPUT);
  99. pinMode(READER_RST, INPUT);
  100. pinMode(READER_IO, OUTPUT);
  101. digitalWrite(READER_IO, HIGH);
  102. pinMode(STATUS_LED, OUTPUT);
  103. digitalWrite(STATUS_LED, LOW);
  104. Serial.println("Initialized");
  105. readerResetAtr();
  106. }
  107. void loop() {
  108. if (readerAtrSize == 0)
  109. {
  110. if (cardReader.cardInserted())
  111. {
  112. readerAtrSize = cardReader.activate(readerAtr, sizeof(readerAtr));
  113. cardReader.dumpHEX(readerAtr, readerAtrSize);
  114. cardReader.deactivate();
  115. digitalWrite(STATUS_LED, HIGH);
  116. }
  117. }
  118. else
  119. {
  120. if (!cardReader.cardInserted())
  121. {
  122. readerResetAtr();
  123. digitalWrite(STATUS_LED, LOW);
  124. }
  125. else if (digitalRead(READER_RST))
  126. {
  127. if (!readerReset)
  128. {
  129. emulatorComputeEtu();
  130. emulatorSendAtr(readerAtr, readerAtrSize);
  131. readerReset = true;
  132. }
  133. }
  134. else
  135. {
  136. if (readerReset)
  137. {
  138. readerReset = false;
  139. }
  140. }
  141. }
  142. }