Revision 2f795968
Fixed card removal detection
Also switched from using seek command to select command with the reader.
Select returns a result immediately, rather than seek which first
returns an "in progress" error then later returns the card (as far as I
can tell).
toolbox/rfid.c | ||
---|---|---|
4 | 4 |
#include "rfid.h" |
5 | 5 |
#include "serial.h" |
6 | 6 |
|
7 |
static uint8_t seek_cmd[] = {0xFF, 0x00, 0x01, 0x82, 0x83}; |
|
7 |
#define MF_SELECT_CMD 0x83 |
|
8 |
|
|
9 |
static uint8_t select_cmd[] = {0xFF, 0, 1, MF_SELECT_CMD, MF_SELECT_CMD + 1}; |
|
8 | 10 |
|
9 | 11 |
#define FRAME_SIZE(data_size) ((data_size)+4) |
10 |
#define MAX_FRAME_SIZE FRAME_SIZE(RFID_SERNO_SIZE+2) |
|
12 |
#define RFID_DATA_SIZE (RFID_SERNO_SIZE+2) |
|
13 |
#define MAX_FRAME_SIZE FRAME_SIZE(RFID_DATA_SIZE) |
|
11 | 14 |
|
12 | 15 |
#define RESP_START 0 |
13 | 16 |
#define RESP_RESERVED 1 |
... | ... | |
33 | 36 |
void rfid_start_read() { |
34 | 37 |
resp_idx = 0; |
35 | 38 |
serial_flush(); |
36 |
serial_write(seek_cmd, sizeof(seek_cmd));
|
|
39 |
serial_write(select_cmd, sizeof(select_cmd));
|
|
37 | 40 |
} |
38 | 41 |
|
39 | 42 |
char parse_response() { |
40 | 43 |
uint8_t sum; |
41 | 44 |
int resp_csum, i; |
42 | 45 |
|
43 |
if (response[RESP_LENGTH] != RFID_SERNO_SIZE+2
|
|
44 |
|| response[RESP_CMD] != 0x82) {
|
|
46 |
if (response[RESP_CMD] != MF_SELECT_CMD) {
|
|
47 |
// invalid response, ignore
|
|
45 | 48 |
zero_serno(); |
46 | 49 |
return 0; |
47 | 50 |
} |
48 | 51 |
|
52 |
if (response[RESP_LENGTH] != RFID_DATA_SIZE) { |
|
53 |
// wrong response length, assume there's no card |
|
54 |
zero_serno(); |
|
55 |
return 1; |
|
56 |
} |
|
57 |
|
|
49 | 58 |
resp_csum = response[RESP_LENGTH] + RESP_CMD; |
50 | 59 |
sum = 0; |
51 | 60 |
for (i = RESP_LENGTH; i < resp_csum; i++) { |
52 | 61 |
sum += response[i]; |
53 | 62 |
} |
54 | 63 |
if (response[resp_csum] != sum) { |
55 |
//return 0; TODO |
|
64 |
zero_serno(); |
|
65 |
return 0; |
|
56 | 66 |
} |
57 | 67 |
|
58 | 68 |
memcpy(serno, &response[RESP_DATA+1], RFID_SERNO_SIZE); |
... | ... | |
80 | 90 |
|
81 | 91 |
// check if we're done with current packet |
82 | 92 |
if (resp_idx >= FRAME_SIZE(response[RESP_LENGTH])) { |
83 |
resp_idx = 0; |
|
84 |
serial_write(seek_cmd, sizeof(seek_cmd)); |
|
93 |
rfid_start_read(); |
|
85 | 94 |
if (parse_response()) { |
86 | 95 |
return 1; |
87 | 96 |
} |
Also available in: Unified diff