Revision 9b2b6d91
Moved rfid functions into their own file
Makefile | ||
---|---|---|
2 | 2 |
MCU=attiny1634 |
3 | 3 |
#MCU=atmega2560 |
4 | 4 |
F_CPU=1000000 |
5 |
SRC=main.c serial.c \ |
|
5 |
SRC=main.c serial.c rfid.c \
|
|
6 | 6 |
freemodbus/port/*.c \ |
7 | 7 |
freemodbus/modbus/mb.c \ |
8 | 8 |
freemodbus/modbus/rtu/mbrtu.c \ |
9 | 9 |
freemodbus/modbus/ascii/mbascii.c \ |
10 | 10 |
freemodbus/modbus/functions/*.c |
11 |
HDR=tooltron_mb.h serial.h freemodbus/port/*.h |
|
11 |
HDR=tooltron_mb.h serial.h rfid.h freemodbus/port/*.h
|
|
12 | 12 |
|
13 | 13 |
# TODO we can exclude modbus functions we don't need if the generated hex file |
14 | 14 |
# ends up too large |
main.c | ||
---|---|---|
3 | 3 |
#include "mb.h" |
4 | 4 |
#include "mbport.h" |
5 | 5 |
#include "tooltron_mb.h" |
6 |
#include "serial.h" |
|
7 |
|
|
8 |
#define RFID_OK 1 |
|
9 |
|
|
10 |
static char rfid_read_cmd[] = {'!', 'R', 'W', 1, 32}; |
|
11 |
|
|
12 |
#define RFID_SERNO_SIZE 4 |
|
13 |
static char rfid_serno[RFID_SERNO_SIZE]; |
|
14 |
|
|
15 |
static void rfid_zero_serno() { |
|
16 |
int i; |
|
17 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
18 |
rfid_serno[i] = 0; |
|
19 |
} |
|
20 |
} |
|
21 |
|
|
22 |
static void rfid_read_serno() { |
|
23 |
int i; |
|
24 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
25 |
rfid_serno[i] = serial_read_blocking(); |
|
26 |
} |
|
27 |
} |
|
28 |
|
|
29 |
static char rfid_check_serno() { |
|
30 |
int i; |
|
31 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
32 |
if (rfid_serno[i] != serial_read_blocking()) { |
|
33 |
return 0; |
|
34 |
} |
|
35 |
} |
|
36 |
return 1; |
|
37 |
} |
|
38 |
|
|
39 |
/* |
|
40 |
* When reading, it must read the same value RFID_MIN_OK times. If it |
|
41 |
* encounters RFID_MAX_ERRS read errors first, it will output all 0's. |
|
42 |
*/ |
|
43 |
#define RFID_MAX_ERRS 10 |
|
44 |
#define RFID_MIN_OK 5 |
|
45 |
|
|
46 |
static void rfid_read_safe() { |
|
47 |
char n_err = 0, n_ok = 0; |
|
48 |
while (1) { |
|
49 |
serial_write(rfid_read_cmd, sizeof(rfid_read_cmd)); |
|
50 |
if (serial_read_blocking() == RFID_OK) { |
|
51 |
if (n_ok == 0) { |
|
52 |
rfid_read_serno(); |
|
53 |
} else if (!rfid_check_serno()) { |
|
54 |
rfid_zero_serno(); |
|
55 |
return; |
|
56 |
} |
|
57 |
n_ok++; |
|
58 |
if (n_ok >= RFID_MIN_OK) { |
|
59 |
return; |
|
60 |
} |
|
61 |
} else { |
|
62 |
n_err++; |
|
63 |
if (n_err >= RFID_MAX_ERRS) { |
|
64 |
rfid_zero_serno(); |
|
65 |
return; |
|
66 |
} |
|
67 |
} |
|
68 |
_delay_ms(50); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
static void rfid_read() { |
|
73 |
while (1) { |
|
74 |
serial_write(rfid_read_cmd, sizeof(rfid_read_cmd)); |
|
75 |
if (serial_read_blocking() == RFID_OK) { |
|
76 |
rfid_read_serno(); |
|
77 |
return; |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
6 |
#include "rfid.h" |
|
81 | 7 |
|
82 | 8 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT num, |
83 | 9 |
eMBRegisterMode mode) { |
... | ... | |
98 | 24 |
int i; |
99 | 25 |
switch (addr) { |
100 | 26 |
case 0: |
101 |
// TODO #define addresses as TYPE_NAME, eg INPUT_SERNO1 |
|
102 | 27 |
// TODO test whether the following works as expected (8 or 16 bit buf?) |
103 | 28 |
for (i = 0; i < 2*num; i++) { |
104 | 29 |
reg_buf[i] = i; |
... | ... | |
121 | 46 |
} |
122 | 47 |
|
123 | 48 |
int main() { |
124 |
// init rfid's serial |
|
125 |
serial_init(); |
|
49 |
rfid_init(); |
|
126 | 50 |
|
127 |
// init modbus and register callbacks |
|
128 | 51 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE); |
129 | 52 |
eMBEnable(); |
130 | 53 |
|
rfid.c | ||
---|---|---|
1 |
#include <util/delay.h> |
|
2 |
#include "rfid.h" |
|
3 |
#include "serial.h" |
|
4 |
|
|
5 |
static char read_cmd[] = {'!', 'R', 'W', 1, 32}; |
|
6 |
|
|
7 |
static char serno[RFID_SERNO_SIZE]; |
|
8 |
|
|
9 |
static void zero_serno() { |
|
10 |
int i; |
|
11 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
12 |
serno[i] = 0; |
|
13 |
} |
|
14 |
} |
|
15 |
|
|
16 |
static void read_serno() { |
|
17 |
int i; |
|
18 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
19 |
serno[i] = serial_read_blocking(); |
|
20 |
} |
|
21 |
} |
|
22 |
|
|
23 |
static char check_serno() { |
|
24 |
int i; |
|
25 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
26 |
if (serno[i] != serial_read_blocking()) { |
|
27 |
return 0; |
|
28 |
} |
|
29 |
} |
|
30 |
return 1; |
|
31 |
} |
|
32 |
|
|
33 |
void rfid_init() { |
|
34 |
serial_init(); |
|
35 |
} |
|
36 |
|
|
37 |
void rfid_read_safe() { |
|
38 |
char n_err = 0, n_ok = 0; |
|
39 |
while (1) { |
|
40 |
serial_write(read_cmd, sizeof(read_cmd)); |
|
41 |
if (serial_read_blocking() == RFID_OK) { |
|
42 |
if (n_ok == 0) { |
|
43 |
read_serno(); |
|
44 |
} else if (!check_serno()) { |
|
45 |
zero_serno(); |
|
46 |
return; |
|
47 |
} |
|
48 |
n_ok++; |
|
49 |
if (n_ok >= RFID_MIN_OK) { |
|
50 |
return; |
|
51 |
} |
|
52 |
} else { |
|
53 |
n_err++; |
|
54 |
if (n_err >= RFID_MAX_ERRS) { |
|
55 |
zero_serno(); |
|
56 |
return; |
|
57 |
} |
|
58 |
} |
|
59 |
_delay_ms(40); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
void rfid_read() { |
|
64 |
while (1) { |
|
65 |
serial_write(read_cmd, sizeof(read_cmd)); |
|
66 |
if (serial_read_blocking() == RFID_OK) { |
|
67 |
read_serno(); |
|
68 |
return; |
|
69 |
} |
|
70 |
_delay_ms(40); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
void rfid_get_serno(char *buf) { |
|
75 |
int i; |
|
76 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
77 |
buf[i] = serno[i]; |
|
78 |
} |
|
79 |
} |
rfid.h | ||
---|---|---|
1 |
#ifndef RFID_H |
|
2 |
#define RFID_H |
|
3 |
|
|
4 |
/* First byte back from rfid reader */ |
|
5 |
#define RFID_OK 1 |
|
6 |
|
|
7 |
/* Bytes in serial number on rfid tags */ |
|
8 |
#define RFID_SERNO_SIZE 4 |
|
9 |
|
|
10 |
/* In rfid_read_safe, it must read the same value RFID_MIN_OK times. If it |
|
11 |
* encounters RFID_MAX_ERRS read errors first, it will output all 0's. */ |
|
12 |
#define RFID_MAX_ERRS 10 |
|
13 |
#define RFID_MIN_OK 5 |
|
14 |
|
|
15 |
/* Should be called before anything else */ |
|
16 |
void rfid_init(); |
|
17 |
|
|
18 |
/* Reads the serial number of an rfid tag into an internal buffer, accessbile |
|
19 |
* with rfid_get_serno */ |
|
20 |
void rfid_read(); |
|
21 |
|
|
22 |
/* Attempts to read the serial number multiple times, and only accepts it if it |
|
23 |
* is the same every time. Use rfid_read instead until this one is proven |
|
24 |
* necessary */ |
|
25 |
void rfid_read_safe(); |
|
26 |
|
|
27 |
/* Call this only after calling rfid_read. This will copy the value it read |
|
28 |
* into serno, which should be at least RFID_SERNO_SIZE bytes */ |
|
29 |
void rfid_get_serno(char *serno); |
|
30 |
|
|
31 |
#endif |
Also available in: Unified diff