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