Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <Arduino.h>
  2. #include <pins_arduino.h>
  3. #include <SCLib.h>
  4. #include "SoftwareSerial8e1.h"
  5. #define STATUS_LED 13
  6. #define READER_VCC
  7. #define READER_RST 3
  8. #define READER_CLK
  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. int emulatorAtrBaudRate = -1;
  23. int emulatorCommandBaudRate = -1;
  24. int digitalPinToInterrupt(int pin)
  25. {
  26. if (pin == 2)
  27. {
  28. return INT0;
  29. }
  30. else if (pin == 3)
  31. {
  32. return INT1;
  33. }
  34. return -1;
  35. }
  36. void emulatorComputeBaudRate()
  37. {
  38. emulatorAtrBaudRate = 9600;
  39. emulatorCommandBaudRate = 13171;
  40. }
  41. void emulatorSend(const uint8_t* atr, size_t size, int baudRate)
  42. {
  43. SoftwareSerial8e1 emulatorSerial(5, READER_IO);
  44. emulatorSerial.begin(baudRate);
  45. emulatorSerial.write(atr, size);
  46. emulatorSerial.end();
  47. }
  48. bool emulatorRead(uint8_t* data, size_t* size, unsigned long timeout, int baudRate)
  49. {
  50. unsigned long end = micros() + timeout;
  51. SoftwareSerial8e1 emulatorSerial(READER_IO, 5);
  52. emulatorSerial.begin(baudRate);
  53. while (emulatorSerial.available() < 4 && (timeout == 0 || end >= micros()));
  54. if (timeout != 0 && end < micros())
  55. {
  56. return false;
  57. }
  58. int available = 0;
  59. do
  60. {
  61. available = emulatorSerial.available();
  62. delayMicroseconds(2 * 1000000 / baudRate);
  63. } while (available != emulatorSerial.available());
  64. *size = *size > available ? available : *size;
  65. emulatorSerial.readBytes((char*)data, *size);
  66. emulatorSerial.end();
  67. return true;
  68. }
  69. void readerResetAtr()
  70. {
  71. for (size_t i = 0; i < sizeof(readerAtr); ++i)
  72. {
  73. readerAtr[i] = 0;
  74. }
  75. readerAtrSize = 0;
  76. }
  77. void emulatorOnReset()
  78. {
  79. Serial.println("Reset");
  80. emulatorComputeBaudRate();
  81. emulatorSend(readerAtr, readerAtrSize, emulatorAtrBaudRate);
  82. }
  83. void setup() {
  84. Serial.begin(9600);
  85. Serial.println("Initializing...");
  86. pinMode(READER_RST, INPUT);
  87. pinMode(READER_IO, OUTPUT);
  88. digitalWrite(READER_IO, HIGH);
  89. pinMode(STATUS_LED, OUTPUT);
  90. digitalWrite(STATUS_LED, LOW);
  91. attachInterrupt(digitalPinToInterrupt(READER_RST), emulatorOnReset, RISING);
  92. Serial.println("Initialized");
  93. readerResetAtr();
  94. }
  95. void loop() {
  96. if (readerAtrSize == 0)
  97. {
  98. if (cardReader.cardInserted())
  99. {
  100. readerAtrSize = cardReader.activate(readerAtr, sizeof(readerAtr));
  101. cardReader.dumpHEX(readerAtr, readerAtrSize);
  102. cardReader.deactivate();
  103. digitalWrite(STATUS_LED, HIGH);
  104. }
  105. }
  106. else
  107. {
  108. if (!cardReader.cardInserted())
  109. {
  110. readerResetAtr();
  111. digitalWrite(STATUS_LED, LOW);
  112. }
  113. else
  114. {
  115. uint8_t data[42];
  116. size_t dataSize = sizeof(data) - 2;
  117. if (emulatorRead(data, &dataSize, 100000, emulatorCommandBaudRate))
  118. {
  119. delayMicroseconds(2300);
  120. // data[dataSize++] = 0x6d;
  121. // data[dataSize++] = 0x00;
  122. // emulatorSend(data, dataSize, emulatorCommandBaudRate);
  123. const uint8_t resp[] = {data[0], data[dataSize - 1]};
  124. emulatorSend(resp, sizeof(resp), emulatorCommandBaudRate);
  125. for (int i = 0; i < dataSize; ++i)
  126. {
  127. Serial.print((int) data[i]);
  128. Serial.print(' ');
  129. }
  130. Serial.println();
  131. }
  132. }
  133. }
  134. }