Revision 267
Reads track 2, messes up on track 1
trunk/cardbox/rs485_int.c | ||
---|---|---|
46 | 46 |
received_byte = 0x0; |
47 | 47 |
|
48 | 48 |
// Set to receive mode |
49 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
49 |
// XXX change this back to off later |
|
50 |
rs485_toggle_transmit(RS485_TX_ON); |
|
50 | 51 |
} |
51 | 52 |
|
52 | 53 |
int8_t rs485_get_byte(uint8_t *output_byte) { |
... | ... | |
64 | 65 |
while (!(UCSR0A & _BV(UDRE0))); |
65 | 66 |
|
66 | 67 |
// Enable writes and send |
67 |
rs485_toggle_transmit(RS485_TX_ON); |
|
68 |
//rs485_toggle_transmit(RS485_TX_ON);
|
|
68 | 69 |
UDR0 = data; |
69 | 70 |
return; |
70 | 71 |
} |
... | ... | |
84 | 85 |
|
85 | 86 |
ISR(USART_TX_vect) { |
86 | 87 |
// Re-enable reads |
87 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
88 |
//rs485_toggle_transmit(RS485_TX_OFF);
|
|
88 | 89 |
} |
trunk/cardbox/cardreader.h | ||
---|---|---|
1 |
#ifndef __CARDREADER_H__ |
|
2 |
#define __CARDREADER_H__ |
|
3 |
|
|
4 |
#include <avr/io.h> |
|
5 |
#include <avr/interrupt.h> |
|
6 |
#include <stdint.h> |
|
7 |
#include "main.h" |
|
8 |
#include "rs485_int.h" |
|
9 |
|
|
10 |
typedef enum { |
|
11 |
CR_NONE, |
|
12 |
CR_GOOD, |
|
13 |
CR_BAD |
|
14 |
} cr_flag_t; |
|
15 |
|
|
16 |
typedef enum { |
|
17 |
CR_IDLE, |
|
18 |
CR_SS, |
|
19 |
CR_DATA |
|
20 |
} cr_state_t; |
|
21 |
|
|
22 |
extern volatile cr_flag_t cr_flag; |
|
23 |
extern volatile uint8_t read_card; |
|
24 |
extern volatile uint8_t cr_buf[512]; |
|
25 |
extern volatile uint8_t cr_buf_idx; |
|
26 |
|
|
27 |
void card_reader_setup(void); |
|
28 |
#endif |
trunk/cardbox/main.c | ||
---|---|---|
16 | 16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com> |
17 | 17 |
* |
18 | 18 |
********/ |
19 |
#include <util/delay.h> |
|
20 |
#include "rs485_int.h" |
|
21 |
#include <avr/interrupt.h> |
|
22 |
#include <avr/io.h> |
|
19 |
#include "main.h" |
|
23 | 20 |
|
24 |
#include <tooltron.h> |
|
25 |
#include "timer.h" |
|
26 |
#include "packet.h" |
|
27 |
|
|
28 | 21 |
/***** Keypad definitions ******/ |
29 | 22 |
#define KEYPORT PORTB |
30 | 23 |
#define KEYPIN PINB |
... | ... | |
46 | 39 |
/** @brief COL4 of the keypad */ |
47 | 40 |
#define COL1 (_BV(PB7)) |
48 | 41 |
|
49 |
/***** LED definitions *****/ |
|
50 |
#define LEDPORT PORTC |
|
51 |
#define LEDDDR DDRC |
|
52 |
#define LED_RED (_BV(PC5)) |
|
53 |
#define LED_YELLOW (_BV(PC4)) |
|
54 |
#define LED_GREEN (_BV(PC3)) |
|
55 |
|
|
56 | 42 |
#define ADDR 2 |
57 | 43 |
|
58 | 44 |
/***** Global variables *****/ |
... | ... | |
177 | 163 |
rs485_init(BAUD9600); |
178 | 164 |
init_pins(); |
179 | 165 |
init_timer(); |
166 |
card_reader_setup(); |
|
180 | 167 |
sei(); |
181 | 168 |
|
169 |
toggle_led(LED_GREEN|LED_YELLOW|LED_RED, OFF); |
|
170 |
read_card = 0; |
|
171 |
rs485_send_byte('s'); |
|
172 |
|
|
182 | 173 |
while(1) { |
174 |
while(1) { |
|
175 |
read_card = 1; |
|
176 |
if (cr_flag != CR_NONE) { |
|
177 |
read_card = 0; |
|
183 | 178 |
|
179 |
//if (cr_flag == CR_GOOD) { |
|
180 |
for (c = 0; c < cr_buf_idx; c++) { |
|
181 |
rs485_send_byte(cr_buf[c]); |
|
182 |
} |
|
183 |
/* |
|
184 |
} else { |
|
185 |
toggle_led(LED_YELLOW|LED_RED, ON); |
|
186 |
_delay_ms(1000); |
|
187 |
toggle_led(LED_YELLOW|LED_RED, OFF); |
|
188 |
}*/ |
|
189 |
|
|
190 |
cr_flag = CR_NONE; |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
|
|
195 |
|
|
184 | 196 |
switch(state) { |
185 | 197 |
case req: |
186 | 198 |
toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF); |
trunk/cardbox/cardreader.c | ||
---|---|---|
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 |
//} |
Also available in: Unified diff