root / trunk / cardbox / cardreader.c @ 269
History | View | Annotate | Download (4.2 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 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 | cr_buf_idx = 0;
|
| 77 | |
| 78 | //start_sentinal = 0;
|
| 79 | //stop_sentinal = 0;
|
| 80 | reading = 1;
|
| 81 | } else if (PIND & CR_CL) { |
| 82 | toggle_led(LED_RED, ON); |
| 83 | reading = 0;
|
| 84 | |
| 85 | //if ((stop_sentinal == 1) && (parity_error == 0)) {
|
| 86 | cr_flag = CR_GOOD; |
| 87 | //} else {
|
| 88 | // cr_flag = CR_BAD;
|
| 89 | //}
|
| 90 | return;
|
| 91 | } |
| 92 | |
| 93 | // Read data in on the downtick of the clock
|
| 94 | if (!(PIND & (CR_CLK))) {
|
| 95 | // Read data from card reader and flip since ours inverts
|
| 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 | cr_buf_idx++; |
| 100 | } |
| 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;
|
| 117 | |
| 118 | rs485_send_byte(cr_dict[byte]); |
| 119 | |
| 120 | // Skip parity bit
|
| 121 | i--; |
| 122 | } |
| 123 | i--; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | byte = 0;
|
| 128 | byte_idx = 0;
|
| 129 | parity = 0;
|
| 130 | |
| 131 | while (i >= 0) { |
| 132 | // Parity bit
|
| 133 | if (byte_idx == CR_MAX_IDX) {
|
| 134 | |
| 135 | if (!(cr_buf[i]) != (parity % 2)) { |
| 136 | rs485_send_byte('F');
|
| 137 | } |
| 138 | |
| 139 | rs485_send_byte(cr_dict[byte]); |
| 140 | |
| 141 | // Stop at the stop sentinal
|
| 142 | if (cr_dict[byte] == CR_ES) {
|
| 143 | return;
|
| 144 | } |
| 145 | |
| 146 | byte_idx = 0;
|
| 147 | byte = 0;
|
| 148 | parity = 0;
|
| 149 | |
| 150 | // Data bits
|
| 151 | } else {
|
| 152 | byte = (((byte >> 1) & 0x1F) | (cr_buf[i])); |
| 153 | byte_idx++; |
| 154 | parity += (cr_buf[i] == 0) ? 0 : 1; |
| 155 | } |
| 156 | |
| 157 | // Goto next bit in cr_buf
|
| 158 | i--; |
| 159 | } |
| 160 | } |