Browse Source

working send atr

develop
Robin Thoni 7 years ago
parent
commit
000fef0bdb
3 changed files with 120 additions and 5 deletions
  1. 1
    0
      .gitignore
  2. 2
    2
      CMakeLists.txt
  3. 117
    3
      main.ino

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
1 1
 /.idea
2
+/cmake-build-debug

+ 2
- 2
CMakeLists.txt View File

@@ -1,10 +1,10 @@
1 1
 cmake_minimum_required(VERSION 2.8.4)
2 2
 set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
3
-set(PROJECT_NAME arduinoProject)
3
+set(PROJECT_NAME arduinoSmartcard)
4 4
 project(${PROJECT_NAME})
5 5
 
6 6
 set(${CMAKE_PROJECT_NAME}_BOARD nano328)
7
-set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyUSB0)
7
+set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyUSB1)
8 8
 
9 9
 #nano
10 10
 #set(${CMAKE_PROJECT_NAME}_BOARD nano328)

+ 117
- 3
main.ino View File

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

Loading…
Cancel
Save