| 17 |
17 |
// but not concurrently. read_card acts as an implicit lock
|
| 18 |
18 |
volatile cr_flag_t cr_flag;
|
| 19 |
19 |
volatile uint8_t cr_buf[512];
|
| 20 |
|
volatile uint8_t cr_buf_idx;
|
|
20 |
volatile uint16_t cr_buf_idx;
|
| 21 |
21 |
volatile uint8_t read_card;
|
| 22 |
22 |
|
| 23 |
23 |
#ifdef SD
|
| ... | ... | |
| 50 |
50 |
}
|
| 51 |
51 |
|
| 52 |
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
|
|
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 |
59 |
static uint8_t reading = 0; // Currently reading
|
| 60 |
60 |
|
| 61 |
|
uint8_t rval; // Current bit read in
|
|
61 |
//uint8_t rval; // Current bit read in
|
| 62 |
62 |
|
| 63 |
63 |
|
| 64 |
64 |
// Only read card if we are expecting it
|
| ... | ... | |
| 68 |
68 |
|
| 69 |
69 |
// Check if the card is inserted or not
|
| 70 |
70 |
if (!(PIND & CR_CL) && (reading == 0)) {
|
| 71 |
|
byte_idx = 0;
|
| 72 |
|
byte = 0;
|
| 73 |
|
parity_error = 0;
|
|
71 |
//byte_idx = 0;
|
|
72 |
//byte = 0;
|
|
73 |
//parity_error = 0;
|
| 74 |
74 |
|
| 75 |
75 |
cr_flag = CR_NONE;
|
| 76 |
|
|
| 77 |
76 |
cr_buf_idx = 0;
|
| 78 |
77 |
|
| 79 |
|
start_sentinal = 0;
|
| 80 |
|
stop_sentinal = 0;
|
|
78 |
//start_sentinal = 0;
|
|
79 |
//stop_sentinal = 0;
|
| 81 |
80 |
reading = 1;
|
| 82 |
81 |
} else if (PIND & CR_CL) {
|
| 83 |
82 |
toggle_led(LED_RED, ON);
|
| 84 |
83 |
reading = 0;
|
| 85 |
84 |
|
| 86 |
|
if ((stop_sentinal == 1) && (parity_error == 0)) {
|
|
85 |
//if ((stop_sentinal == 1) && (parity_error == 0)) {
|
| 87 |
86 |
cr_flag = CR_GOOD;
|
| 88 |
|
} else {
|
| 89 |
|
cr_flag = CR_BAD;
|
| 90 |
|
}
|
|
87 |
//} else {
|
|
88 |
// cr_flag = CR_BAD;
|
|
89 |
//}
|
| 91 |
90 |
return;
|
| 92 |
91 |
}
|
| 93 |
92 |
|
| 94 |
93 |
// Read data in on the downtick of the clock
|
| 95 |
94 |
if (!(PIND & (CR_CLK))) {
|
| 96 |
95 |
// 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';
|
|
96 |
//rval = (PIND & CR_DATA) ? 0 : 0x40;
|
|
97 |
//cr_buf[cr_buf_idx] = (rval == 0) ? '0' : '1';
|
|
98 |
cr_buf[cr_buf_idx] = (PIND & CR_DATA) ? 0 : 0x20;
|
| 99 |
99 |
cr_buf_idx++;
|
| 100 |
100 |
}
|
| 101 |
|
/*
|
| 102 |
|
if (start_sentinal == 0) {
|
| 103 |
|
byte = (((byte >> 1) & 0x3F) | (rval));
|
|
101 |
}
|
|
102 |
void parse_card(void) {
|
|
103 |
int32_t i = cr_buf_idx - 1;
|
|
104 |
uint8_t start_sentinal = 0;
|
|
105 |
uint8_t stop_sentinal = 0;
|
|
106 |
uint8_t byte_idx = 0;
|
|
107 |
uint8_t byte = 0;
|
|
108 |
uint8_t parity;
|
|
109 |
|
|
110 |
while (start_sentinal == 0) {
|
|
111 |
byte = (((byte >> 1) & 0x1F) | (cr_buf[i]));
|
|
112 |
|
|
113 |
if (cr_dict[byte] == CR_SS) {
|
|
114 |
|
|
115 |
toggle_led(LED_GREEN, ON);
|
|
116 |
start_sentinal = 1;
|
| 104 |
117 |
|
| 105 |
|
if (cr_dict[byte] == CR_SS) {
|
| 106 |
|
//cr_buf[cr_buf_idx] = CR_SS;
|
| 107 |
|
//cr_buf_idx++;
|
|
118 |
rs485_send_byte(cr_dict[byte]);
|
| 108 |
119 |
|
| 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];
|
|
120 |
// Skip parity bit
|
|
121 |
i--;
|
|
122 |
}
|
|
123 |
i--;
|
|
124 |
|
|
125 |
}
|
| 126 |
126 |
|
|
127 |
byte = 0;
|
|
128 |
byte_idx = 0;
|
| 127 |
129 |
|
| 128 |
|
// Detect an end sentinal
|
| 129 |
|
|
| 130 |
|
//if (cr_dict[byte] == CR_ES) {
|
| 131 |
|
// stop_sentinal = 1;
|
| 132 |
|
//}
|
|
130 |
while (i >= 0) {
|
|
131 |
// Parity bit
|
|
132 |
if (byte_idx == CR_MAX_IDX) {
|
| 133 |
133 |
|
| 134 |
|
cr_buf_idx++;
|
| 135 |
|
//cr_buf[cr_buf_idx] = '|';
|
| 136 |
|
//cr_buf_idx++;
|
| 137 |
|
//}
|
| 138 |
|
|
|
134 |
rs485_send_byte(cr_dict[byte]);
|
|
135 |
|
|
136 |
// Stop at the stop sentinal
|
|
137 |
if (cr_dict[byte] == CR_ES) {
|
|
138 |
return;
|
|
139 |
}
|
|
140 |
|
| 139 |
141 |
byte_idx = 0;
|
| 140 |
142 |
byte = 0;
|
| 141 |
|
parity = 1;
|
| 142 |
|
// Normal bits
|
|
143 |
|
|
144 |
// Data bits
|
| 143 |
145 |
} else {
|
| 144 |
|
byte = (((byte >> 1) & 0x3F) | (rval));
|
| 145 |
|
parity ^= rval;
|
|
146 |
byte = (((byte >> 1) & 0x1F) | (cr_buf[i]));
|
| 146 |
147 |
byte_idx++;
|
| 147 |
|
}*/
|
|
148 |
}
|
|
149 |
|
|
150 |
// Goto next bit in cr_buf
|
|
151 |
i--;
|
| 148 |
152 |
}
|
| 149 |
|
//}
|
|
153 |
}
|