#include #include #include #include #define ledPin 13 #define READER_VCC #define READER_RST 4 #define READER_CLK 3 #define READER_GND #define READER_VPP #define READER_IO 2 #define CARD_VCC 11 #define CARD_RST 7 #define CARD_CLK 9 #define CARD_GND #define CARD_VPP #define CARD_IO 10 #define CARD_PRE 12 SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, true); uint8_t atr[MAX_ATR_BYTES]; int atr_size; bool readerReset = false; int baudRate = -1; int etu = -1; #ifndef digitalPinToInterrupt int digitalPinToInterrupt(int pin) { if (pin == 2) { return INT0; } else if (pin == 3) { return INT1; } return -1; } #endif void onReaderReset() { readerReset = true; } void computeEtu() { baudRate = 9600; etu = 1000000 / baudRate; } void sendAtrByte(char byte) { unsigned long nextBitTime = micros() + (2 * etu); digitalWrite(READER_IO, HIGH); int bitNumber = 0; int parity = 0; while (bitNumber < 11) { if (nextBitTime <= micros()) { if (bitNumber == 0) { digitalWrite(READER_IO, LOW); } else if (bitNumber < 9) { int a = bitRead(byte, bitNumber - 1); parity += a; digitalWrite(READER_IO, a); } else if (bitNumber == 9) { digitalWrite(READER_IO, parity % 2); } else if (bitNumber == 10) { digitalWrite(READER_IO, HIGH); break; } nextBitTime += etu; ++bitNumber; } } } void sendAtr(const uint8_t* atr, size_t size) { pinMode(READER_IO, OUTPUT); for (int i = 0; i < size; ++i) { sendAtrByte(atr[i]); delayMicroseconds(4*etu); } } void setup() { Serial.begin(9600); Serial.println("Initializing..."); pinMode(READER_CLK, INPUT); pinMode(READER_RST, INPUT); pinMode(READER_IO, OUTPUT); digitalWrite(READER_IO, HIGH); pinMode(ledPin, OUTPUT); digitalWrite (ledPin, LOW); Serial.println("Initialized"); atr_size = cardReader.activate(atr, sizeof(atr)); cardReader.deactivate(); } void loop() { if (digitalRead(READER_RST)) { if (!readerReset) { // atr_size = cardReader.activate(atr, sizeof(atr)); // uint8_t atr[] = {0x3B, 0x65, 0x00, 0x00, 0x20, 0x63, 0xCB, 0x6C, 0x80}; // int atr_size = sizeof(atr); computeEtu(); sendAtr(atr, atr_size); readerReset = true; digitalWrite(ledPin, !digitalRead(ledPin)); // cardReader.deactivate(); } } else { if (readerReset) { readerReset = false; } } }