Revision 6fbe093f

View differences:

toolbox/Makefile
32 32
	avr-gcc -S $(FLAGS) $< -o $@
33 33

  
34 34
program: toolbox.hex
35
	avrdude -p $(PART) -c $(PROG) -P usb -B 3 -F -U flash:w:toolbox.hex
35
	avrdude -p $(PART) -c $(PROG) -P usb -B 3 -U flash:w:toolbox.hex
36

  
37
readfuse:
38
	avrdude -p $(PART) -c $(PROG) -P usb -B 3 -U lfuse:r:-:b -U hfuse:r:-:b -U efuse:r:-:b -U lock:r:-:b
36 39

  
37 40
clean:
38 41
	rm -f *.elf *.hex
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;
toolbox/serial.h
1 1
#ifndef SERIAL_H
2 2
#define SERIAL_H
3 3

  
4
#define BAUD_RATE 9600
4
#define BAUD_RATE 19200
5 5

  
6 6
#define RX_BUFFER_SIZE 64
7 7

  

Also available in: Unified diff