Repository navigation
Expand file tree
/
Copy pathuart.c
More file actions
287 lines (212 loc) · 5.14 KB
/
Copy pathuart.c
File metadata and controls
287 lines (212 loc) · 5.14 KB
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <inttypes.h>
#include "config.h"
#include "uart.h"
#include "common.h"
volatile uint8_t etuFlag, startBitFlag;
//
// Interrupts
// ETU interrupt
ISR(TIMER0_COMPA_vect) {
etuFlag = 1;
}
// Smartcard io pin change interupt
ISR(PCINT1_vect) {
// start bit?
if (((PINB >> SMARTCARD_IO) & 0x01) == 0) {
startBitFlag = 1;
}
}
//
// Static functions
static void setEtuCycleBit(uint8_t value) {
if (value) {
SMARTCARD_HIGH_Z();
} else {
SMARTCARD_LOW();
}
}
static void waitForStartBit(void) {
startBitFlag = 0;
// enable pin change interupt on smartcard io
PCMSK1 |= (1 << PCINT14);
PCICR |= (1 << PCIE1);
// wait for the start bit
while (!startBitFlag);
// disable pin change interupt on smartcard io
PCICR &= ~(1 << PCIE1);
PCMSK1 &= ~(1 << PCINT14);
}
static void enableTimer(void) {
// enable output compare event A
TIMSK0 |= (1 << OCIE0A);
// enable counter 0 and set prescaler to 8 => 46 * 8 ~ 372
TCCR0B |= (1 << CS01);
}
static void resetTimer(void) {
// disable output compare event A
TIMSK0 &= ~(1 << OCIE0A);
// disable counter 0
TCCR0B &= !(1 << CS01);
// reset counter 0
TCNT0 = 0;
}
void init_uart(int *errnum) {
// idle smartcard io
setEtuCycleBit(1);
// reset timer 0
TCNT0 = 0;
// enable CTC mode
TCCR0A |= (1 << WGM01);
// set output compare register A to 1 ETU (46)
OCR0A = 0x2E;
// enable global interupt
sei();
*errnum = NO_ERROR;
}
void uart_write_byte(uint8_t *InputBufferPtr) {
enum uart_states_t state = START_BIT;
uint8_t etuCycle = 0;
uint8_t parityBit = 0;
uint8_t payloadCopy = 0;
#if DEBUG_UART == 1
printf("Sending payload 0x%X.\n", *InputBufferPtr);
#endif /* if DEBUG_UART == 1 */
// set etuFlag to 1 so that START_BIT is set immediately
etuFlag = 1;
enableTimer();
while (state != DONE) {
// evaluate once every etu cycle
if (etuFlag) {
etuFlag = 0;
switch (state) {
case START_BIT:
setEtuCycleBit(0);
etuCycle++;
payloadCopy = *InputBufferPtr;
state = DATA_BITS;
break;
case DATA_BITS:
setEtuCycleBit(payloadCopy & 0x01);
parityBit ^= (payloadCopy & 0x01);
payloadCopy >>= 1;
etuCycle++;
if (etuCycle == 9) {
state = PARITY_BIT;
}
break;
case PARITY_BIT:
setEtuCycleBit(parityBit);
etuCycle++;
state = STOP_BITS;
break;
case STOP_BITS:
setEtuCycleBit(1);
etuCycle++;
if (etuCycle == 12) {
// parity error?
if ((PINB >> SMARTCARD_IO) & 0x01) {
state = DONE;
} else {
state = PARITY_ERROR;
}
}
break;
case PARITY_ERROR:
setEtuCycleBit(1);
state = START_BIT;
etuCycle = 0;
parityBit = 0;
break;
case DONE:
break;
}
}
}
// wait for the last etu cycle to finish
while (!etuFlag);
// idle smartcard IO
SMARTCARD_HIGH_Z();
resetTimer();
}
void uart_write(uint8_t *InputBufferPtr, uint8_t NumBytes) {
uint8_t i;
for (i = 0; i < NumBytes; i++) {
uart_write_byte(&InputBufferPtr[i]);
}
}
void uart_read_byte(uint8_t *OutputBufferPtr) {
enum uart_states_t state = START_BIT;
uint8_t payload = 0;
uint8_t etuCycle = 0;
uint8_t etuCycleBit = 0;
uint8_t parityBit = 0;
etuFlag = 1;
waitForStartBit();
// The delay between the detection of the start bit and start of the timer is
// sufficient to ensure the bit on the smartcard IO is always read when it is
// stable
enableTimer();
while (state != DONE) {
if (etuFlag) {
etuFlag = 0;
switch (state) {
case START_BIT:
state = DATA_BITS;
break;
case DATA_BITS:
etuCycleBit = (PINB >> SMARTCARD_IO) & 0x01;
parityBit ^= etuCycleBit;
payload |= etuCycleBit << etuCycle;
etuCycle++;
if (etuCycle == 8) {
state = PARITY_BIT;
}
break;
case PARITY_BIT:
etuCycleBit = (PINB >> SMARTCARD_IO) & 0x01;
etuCycle++;
state = STOP_BITS;
break;
case STOP_BITS:
// parity error?
if (etuCycleBit == parityBit) {
etuCycle++;
if (etuCycle == 11) {
state = DONE;
}
} else {
setEtuCycleBit(0);
state = PARITY_ERROR;
}
break;
case PARITY_ERROR:
setEtuCycleBit(1);
resetTimer();
etuFlag = 1;
etuCycle = 0;
parityBit = 0;
payload = 0;
state = START_BIT;
waitForStartBit();
enableTimer();
break;
case DONE:
break;
}
}
}
resetTimer();
#if DEBUG_UART == 1
printf("Received payload 0x%X.\n", payload);
#endif /* if DEBUG_UART == 1 */
*OutputBufferPtr = payload;
}
void uart_read(uint8_t *OutputBufferPtr, uint8_t NumBytes) {
uint8_t i = 0;
for (i = 0; i < NumBytes; i++) {
uart_read_byte(&OutputBufferPtr[i]);
}
}