Project

General

Profile

Revision 6fbe093f

ID6fbe093fe653e18a28c26960fbecd27457ea2617
Parent 88570136
Child 90bb5fa7

Added by Thomas Mullins about 10 years ago

Implemented code for new RFID readers

It's not fully working yet. When the checksum is enabled, it ignores all
responses. Also, it no longer recognizes a missing card.

View differences:

toolbox/rfid.c
4 4
#include "rfid.h"
5 5
#include "serial.h"
6 6

  
7
#define RFID_OK 1
8
static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32};
7
static uint8_t seek_cmd[] = {0xFF, 0x00, 0x01, 0x82, 0x83};
8

  
9
#define FRAME_SIZE(data_size) ((data_size)+4)
10
#define MAX_FRAME_SIZE FRAME_SIZE(RFID_SERNO_SIZE+2)
11

  
12
#define RESP_START    0
13
#define RESP_RESERVED 1
14
#define RESP_LENGTH   2
15
#define RESP_CMD      3
16
#define RESP_DATA     4
17
static uint8_t response[MAX_FRAME_SIZE];
18
static uint8_t resp_idx;
9 19

  
10
static int serno_idx;
11 20
static uint8_t serno[RFID_SERNO_SIZE];
12 21

  
13 22
static void zero_serno() {
......
22 31
}
23 32

  
24 33
void rfid_start_read() {
25
  serno_idx = -1;
34
  resp_idx = 0;
26 35
  serial_flush();
27
  serial_write(read_cmd, sizeof(read_cmd));
36
  serial_write(seek_cmd, sizeof(seek_cmd));
37
}
38

  
39
char parse_response() {
40
  uint8_t sum;
41
  int resp_csum, i;
42

  
43
  if (response[RESP_LENGTH] != RFID_SERNO_SIZE+2
44
      || response[RESP_CMD] != 0x82) {
45
    zero_serno();
46
    return 0;
47
  }
48

  
49
  resp_csum = response[RESP_LENGTH] + RESP_CMD;
50
  sum = 0;
51
  for (i = RESP_LENGTH; i < resp_csum; i++) {
52
    sum += response[i];
53
  }
54
  if (response[resp_csum] != sum) {
55
    //return 0; TODO
56
  }
57

  
58
  memcpy(serno, &response[RESP_DATA+1], RFID_SERNO_SIZE);
59
  return 1;
28 60
}
29 61

  
30 62
char rfid_poll() {
......
32 64

  
33 65
  while ((c = serial_read()) >= 0) {
34 66

  
35
    if (serno_idx < 0) {
36
      if (c != RFID_OK) {
37
        zero_serno();
38
        return 1;
39
      }
40
    } else {
41
      serno[serno_idx] = c;
67
    if (resp_idx < sizeof(response)) {
68
      response[resp_idx] = c;
42 69
    }
70
    resp_idx++;
43 71

  
44
    serno_idx++;
45
    if (serno_idx >= RFID_SERNO_SIZE) {
46
      return 1;
47
    }
72
    if (resp_idx == 1) {
73

  
74
      // restart if the frame start is invalid
75
      if (response[RESP_START] != 0xFF) {
76
        resp_idx = 0;
77
      }
48 78

  
79
    } else if (resp_idx > RESP_LENGTH) {
80

  
81
      // check if we're done with current packet
82
      if (resp_idx >= FRAME_SIZE(response[RESP_LENGTH])) {
83
        resp_idx = 0;
84
        serial_write(seek_cmd, sizeof(seek_cmd));
85
        if (parse_response()) {
86
          return 1;
87
        }
88
      }
89

  
90
    }
49 91
  }
50 92

  
51 93
  return 0;

Also available in: Unified diff