root / trunk / cardbox / cardreader.c @ 272
History | View | Annotate | Download (3.52 KB)
1 |
#include "cardreader.h" |
---|---|
2 |
|
3 |
//#define SD
|
4 |
|
5 |
#define CR_CL (_BV(PIND5))
|
6 |
#define CR_CLK (_BV(PIND6))
|
7 |
#define CR_DATA (_BV(PIND7))
|
8 |
#ifdef SD
|
9 |
#define CR_MAX_IDX 4 |
10 |
#else
|
11 |
#define CR_MAX_IDX 6 |
12 |
#endif
|
13 |
#define CR_SS '%' // Start sentinal |
14 |
#define CR_ES '?' // End sentinal |
15 |
|
16 |
// This is only written in the PCINT2 intterupt or in the main loop
|
17 |
// but not concurrently. read_card acts as an implicit lock
|
18 |
volatile cr_flag_t cr_flag;
|
19 |
volatile uint8_t cr_buf[512]; |
20 |
volatile uint16_t cr_buf_idx;
|
21 |
volatile uint8_t read_card;
|
22 |
|
23 |
#ifdef SD
|
24 |
const char cr_dict[] = {'0', '1', '2', '3', '4', '5', '6', '7', |
25 |
'8', '9', ':', ';', '<', '=', '>', '?'}; |
26 |
#else
|
27 |
const char cr_dict[] = {' ', '!', '"', '#', '$', '%', '&', '\'', |
28 |
'(', ')', '*', '+', ',', '-', '.', '/', |
29 |
'0', '1', '2', '3', '4', '5', '6', '7', |
30 |
'8', '9', ':', ';', '<', '=', '>', '?', |
31 |
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
32 |
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
33 |
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
34 |
'X', 'Y', 'Z', '[', '\\',']', '^', '_'}; |
35 |
#endif
|
36 |
|
37 |
|
38 |
|
39 |
void card_reader_setup(void) { |
40 |
|
41 |
// Turn on pin interrupts for pins D7, D6, D5
|
42 |
PCICR = _BV(PCIE2); |
43 |
PCMSK2 = /*_BV(PCINT23) |*/ _BV(PCINT22) | _BV(PCINT21);
|
44 |
|
45 |
// Make pins D7, D6, D5 inputs
|
46 |
DDRD &= ~_BV(DDD7) & ~_BV(DDD6) & ~_BV(DDD5); |
47 |
cr_buf_idx = 0;
|
48 |
cr_flag = CR_NONE; |
49 |
read_card = 0;
|
50 |
} |
51 |
|
52 |
ISR(PCINT2_vect) { |
53 |
static uint8_t reading = 0; // Currently reading |
54 |
|
55 |
// Only read card if we are expecting it
|
56 |
if (!read_card) {
|
57 |
return;
|
58 |
} |
59 |
|
60 |
// Check if the card is inserted or not
|
61 |
if (!(PIND & CR_CL) && (reading == 0)) { |
62 |
cr_flag = CR_NONE; |
63 |
cr_buf_idx = 0;
|
64 |
reading = 1;
|
65 |
// check if the card has finished swiping
|
66 |
} else if (PIND & CR_CL) { |
67 |
toggle_led(LED_RED, ON); |
68 |
reading = 0;
|
69 |
cr_flag = CR_GOOD; |
70 |
return;
|
71 |
} |
72 |
|
73 |
// Read data in on the downtick of the clock
|
74 |
if (!(PIND & (CR_CLK))) {
|
75 |
// Read data from card reader and flip since ours inverts
|
76 |
cr_buf[cr_buf_idx] = (PIND & CR_DATA) ? 0 : 0x20; |
77 |
cr_buf_idx++; |
78 |
} |
79 |
} |
80 |
|
81 |
int8_t parse_card(void) {
|
82 |
int32_t i = cr_buf_idx - 1;
|
83 |
uint8_t start_sentinal = 0;
|
84 |
uint8_t byte_idx = 0;
|
85 |
uint8_t byte = 0;
|
86 |
uint8_t parity; |
87 |
|
88 |
while (start_sentinal == 0) { |
89 |
byte = (((byte >> 1) & 0x1F) | (cr_buf[i])); |
90 |
|
91 |
if (cr_dict[byte] == CR_SS) {
|
92 |
|
93 |
toggle_led(LED_GREEN, ON); |
94 |
start_sentinal = 1;
|
95 |
|
96 |
rs485_send_byte(cr_dict[byte]); |
97 |
|
98 |
// Skip parity bit
|
99 |
i--; |
100 |
} |
101 |
|
102 |
i--; |
103 |
|
104 |
if (i == 0) { |
105 |
return CR_ERR_NO_START;
|
106 |
} |
107 |
} |
108 |
|
109 |
byte = 0;
|
110 |
byte_idx = 0;
|
111 |
parity = 0;
|
112 |
|
113 |
while (i >= 0) { |
114 |
// Parity bit
|
115 |
if (byte_idx == CR_MAX_IDX) {
|
116 |
|
117 |
if (!(cr_buf[i]) != (parity % 2)) { |
118 |
return CR_ERR_BAD_PARITY;
|
119 |
} |
120 |
|
121 |
rs485_send_byte(cr_dict[byte]); |
122 |
|
123 |
// Stop at the stop sentinal
|
124 |
if (cr_dict[byte] == CR_ES) {
|
125 |
return 0; |
126 |
} |
127 |
|
128 |
byte_idx = 0;
|
129 |
byte = 0;
|
130 |
parity = 0;
|
131 |
|
132 |
// Data bits
|
133 |
} else {
|
134 |
byte = (((byte >> 1) & 0x1F) | (cr_buf[i])); |
135 |
byte_idx++; |
136 |
parity += (cr_buf[i]) ? 0 : 1; |
137 |
} |
138 |
|
139 |
// Goto next bit in cr_buf
|
140 |
i--; |
141 |
} |
142 |
|
143 |
return CR_ERR_NO_STOP;
|
144 |
} |