root / trunk / cardbox / cardreader.c @ 267
History | View | Annotate | Download (4.34 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 uint8_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 byte_idx = 0; // Which bit to store in byte |
54 |
static uint8_t byte = 0; // The current character being read |
55 |
static uint8_t parity = 0; // Current calculated parity |
56 |
static uint8_t parity_error = 0; // If we saw a parity error this read |
57 |
static uint8_t start_sentinal = 0; // Saw a start sentinal |
58 |
static uint8_t stop_sentinal = 0; // Saw a stop sentinal |
59 |
static uint8_t reading = 0; // Currently reading |
60 |
|
61 |
uint8_t rval; // Current bit read in
|
62 |
|
63 |
|
64 |
// Only read card if we are expecting it
|
65 |
if (!read_card) {
|
66 |
return;
|
67 |
} |
68 |
|
69 |
// Check if the card is inserted or not
|
70 |
if (!(PIND & CR_CL) && (reading == 0)) { |
71 |
byte_idx = 0;
|
72 |
byte = 0;
|
73 |
parity_error = 0;
|
74 |
|
75 |
cr_flag = CR_NONE; |
76 |
|
77 |
cr_buf_idx = 0;
|
78 |
|
79 |
start_sentinal = 0;
|
80 |
stop_sentinal = 0;
|
81 |
reading = 1;
|
82 |
} else if (PIND & CR_CL) { |
83 |
toggle_led(LED_RED, ON); |
84 |
reading = 0;
|
85 |
|
86 |
if ((stop_sentinal == 1) && (parity_error == 0)) { |
87 |
cr_flag = CR_GOOD; |
88 |
} else {
|
89 |
cr_flag = CR_BAD; |
90 |
} |
91 |
return;
|
92 |
} |
93 |
|
94 |
// Read data in on the downtick of the clock
|
95 |
if (!(PIND & (CR_CLK))) {
|
96 |
// Read data from card reader and flip since ours inverts
|
97 |
rval = (PIND & CR_DATA) ? 0 : 0x40; |
98 |
cr_buf[cr_buf_idx] = (rval == 0) ? '0' : '1'; |
99 |
cr_buf_idx++; |
100 |
} |
101 |
/*
|
102 |
if (start_sentinal == 0) {
|
103 |
byte = (((byte >> 1) & 0x3F) | (rval));
|
104 |
|
105 |
if (cr_dict[byte] == CR_SS) {
|
106 |
//cr_buf[cr_buf_idx] = CR_SS;
|
107 |
//cr_buf_idx++;
|
108 |
|
109 |
toggle_led(LED_GREEN, ON);
|
110 |
start_sentinal = 1;
|
111 |
|
112 |
byte_idx = CR_MAX_IDX;
|
113 |
//byte = 0;
|
114 |
//parity = 1;
|
115 |
}
|
116 |
// This is the parity bit
|
117 |
} else if (byte_idx == CR_MAX_IDX) {
|
118 |
// This failed the parity check
|
119 |
//if (rval != parity) {
|
120 |
// parity_error = 1;
|
121 |
// Passes the parity check
|
122 |
//} else {
|
123 |
// Only commit if we found the start and we haven't hit the end
|
124 |
//if ((start_sentinal == 1) && (stop_sentinal == 0)) {
|
125 |
cr_buf[cr_buf_idx] = cr_dict[byte];
|
126 |
|
127 |
|
128 |
// Detect an end sentinal
|
129 |
|
130 |
//if (cr_dict[byte] == CR_ES) {
|
131 |
// stop_sentinal = 1;
|
132 |
//}
|
133 |
|
134 |
cr_buf_idx++;
|
135 |
//cr_buf[cr_buf_idx] = '|';
|
136 |
//cr_buf_idx++;
|
137 |
//}
|
138 |
|
139 |
byte_idx = 0;
|
140 |
byte = 0;
|
141 |
parity = 1;
|
142 |
// Normal bits
|
143 |
} else {
|
144 |
byte = (((byte >> 1) & 0x3F) | (rval));
|
145 |
parity ^= rval;
|
146 |
byte_idx++;
|
147 |
}*/
|
148 |
} |
149 |
//}
|