1 |
92b5f96c
|
(no author)
|
/* main_reqa - OpenPCD firmware for generating an endless loop of
|
2 |
37907554
|
(no author)
|
* ISO 14443-A REQA packets. Alternatively we can send WUPA, or
|
3 |
|
|
* perform a full ISO14443A anti-collision loop.
|
4 |
92b5f96c
|
(no author)
|
*
|
5 |
|
|
* If a response is received from the PICC, LED1 (Red) will be switched
|
6 |
|
|
* on. If no valid response has been received within the timeout of the
|
7 |
|
|
* receiver, LED1 (Red) will be switched off.
|
8 |
|
|
*
|
9 |
32985a29
|
laforge
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
10 |
|
|
*
|
11 |
|
|
* This program is free software; you can redistribute it and/or modify
|
12 |
|
|
* it under the terms of the GNU General Public License as published by
|
13 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
14 |
|
|
* (at your option) any later version.
|
15 |
|
|
*
|
16 |
|
|
* This program is distributed in the hope that it will be useful,
|
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
* GNU General Public License for more details.
|
20 |
|
|
*
|
21 |
|
|
* You should have received a copy of the GNU General Public License
|
22 |
|
|
* along with this program; if not, write to the Free Software
|
23 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
24 |
|
|
*
|
25 |
92b5f96c
|
(no author)
|
*/
|
26 |
|
|
|
27 |
9d0d7022
|
(no author)
|
#include <errno.h>
|
28 |
|
|
#include <string.h>
|
29 |
6a3cb645
|
(no author)
|
#include <lib_AT91SAM7.h>
|
30 |
9d0d7022
|
(no author)
|
#include <librfid/rfid_layer2_iso14443a.h>
|
31 |
caf50003
|
(no author)
|
#include "rc632.h"
|
32 |
520784c7
|
(no author)
|
#include <os/dbgu.h>
|
33 |
|
|
#include <os/led.h>
|
34 |
|
|
#include <os/pcd_enumerate.h>
|
35 |
|
|
#include <os/trigger.h>
|
36 |
28eb4a57
|
laforge
|
#include <pcd/rc632_highlevel.h>
|
37 |
|
|
|
38 |
|
|
#include <librfid/rfid_reader.h>
|
39 |
|
|
#include <librfid/rfid_layer2.h>
|
40 |
520784c7
|
(no author)
|
|
41 |
|
|
#include "../openpcd.h"
|
42 |
|
|
|
43 |
|
|
#ifdef WITH_TC
|
44 |
06d2a987
|
(no author)
|
#include "tc.h"
|
45 |
520784c7
|
(no author)
|
#endif
|
46 |
caf50003
|
(no author)
|
|
47 |
28eb4a57
|
laforge
|
#define RAH NULL
|
48 |
|
|
|
49 |
|
|
static struct rfid_reader_handle *rh;
|
50 |
|
|
static struct rfid_layer2_handle *l2h;
|
51 |
|
|
|
52 |
caf50003
|
(no author)
|
void _init_func(void)
|
53 |
|
|
{
|
54 |
|
|
trigger_init();
|
55 |
06d2a987
|
(no author)
|
DEBUGPCRF("enabling RC632");
|
56 |
92b5f96c
|
(no author)
|
rc632_init();
|
57 |
520784c7
|
(no author)
|
#ifdef WITH_TC
|
58 |
06d2a987
|
(no author)
|
DEBUGPCRF("enabling TC");
|
59 |
|
|
tc_cdiv_init();
|
60 |
520784c7
|
(no author)
|
#endif
|
61 |
92b5f96c
|
(no author)
|
DEBUGPCRF("turning on RF");
|
62 |
|
|
rc632_turn_on_rf(RAH);
|
63 |
|
|
DEBUGPCRF("initializing 14443A operation");
|
64 |
28eb4a57
|
laforge
|
rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
|
65 |
|
|
l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
|
66 |
caf50003
|
(no author)
|
}
|
67 |
|
|
|
68 |
78c6dcc4
|
(no author)
|
#define MODE_REQA 0x01
|
69 |
|
|
#define MODE_WUPA 0x02
|
70 |
|
|
#define MODE_ANTICOL 0x03
|
71 |
ed96e055
|
(no author)
|
#define MODE_14443A 0x04
|
72 |
78c6dcc4
|
(no author)
|
|
73 |
|
|
static volatile int mode = MODE_REQA;
|
74 |
|
|
|
75 |
28eb4a57
|
laforge
|
static const char frame_14443a[] = {
|
76 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
77 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
78 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
79 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
80 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
81 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
82 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
83 |
|
|
0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
|
84 |
|
|
};
|
85 |
ed96e055
|
(no author)
|
|
86 |
373c172a
|
Harald Welte
|
static void reg_inc(uint8_t reg)
|
87 |
3866f983
|
(no author)
|
{
|
88 |
373c172a
|
Harald Welte
|
uint8_t val;
|
89 |
28eb4a57
|
laforge
|
opcd_rc632_reg_read(RAH, reg, &val);
|
90 |
|
|
opcd_rc632_reg_write(RAH, reg, val++);
|
91 |
3866f983
|
(no author)
|
DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
373c172a
|
Harald Welte
|
static void reg_dec(uint8_t reg)
|
95 |
3866f983
|
(no author)
|
{
|
96 |
373c172a
|
Harald Welte
|
uint8_t val;
|
97 |
28eb4a57
|
laforge
|
opcd_rc632_reg_read(RAH, reg, &val);
|
98 |
|
|
opcd_rc632_reg_write(RAH, reg, val--);
|
99 |
3866f983
|
(no author)
|
DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
373c172a
|
Harald Welte
|
static uint8_t ana_out_sel;
|
103 |
|
|
static uint8_t mfout_sel;
|
104 |
|
|
static uint8_t speed_idx;
|
105 |
6a3cb645
|
(no author)
|
|
106 |
|
|
static void help(void)
|
107 |
|
|
{
|
108 |
|
|
DEBUGPCR("r: REQA w: WUPA a: ANTICOL\r\n"
|
109 |
ed96e055
|
(no author)
|
"A: 14443A +: inc speed -: dec speed\r\n"
|
110 |
6a3cb645
|
(no author)
|
"y: inc cw cond x: dec cond c: inc mod cond");
|
111 |
|
|
DEBUGPCR("v: dec mod cond o: dec ana_out p: dec ana_out\r\n"
|
112 |
06d2a987
|
(no author)
|
"h: trigger high l: trigger low u: dec MFOUT mode");
|
113 |
|
|
DEBUGPCR("i: inc MFOUT md <: dec cdiv ph >: inc cdiv phase\r\n"
|
114 |
|
|
"{: dev cdiv }: inc cdiv");
|
115 |
6a3cb645
|
(no author)
|
}
|
116 |
|
|
|
117 |
373c172a
|
Harald Welte
|
static uint16_t cdivs[] = { 128, 64, 32, 16 };
|
118 |
06d2a987
|
(no author)
|
|
119 |
9d0d7022
|
(no author)
|
int _main_dbgu(char key)
|
120 |
|
|
{
|
121 |
6a3cb645
|
(no author)
|
int ret = 0;
|
122 |
28eb4a57
|
laforge
|
static int cdiv_idx = 2;
|
123 |
6a3cb645
|
(no author)
|
|
124 |
78c6dcc4
|
(no author)
|
switch (key) {
|
125 |
6a3cb645
|
(no author)
|
case '?':
|
126 |
|
|
help();
|
127 |
|
|
break;
|
128 |
78c6dcc4
|
(no author)
|
case 'r':
|
129 |
|
|
mode = MODE_REQA;
|
130 |
|
|
break;
|
131 |
|
|
case 'w':
|
132 |
|
|
mode = MODE_WUPA;
|
133 |
|
|
break;
|
134 |
ed96e055
|
(no author)
|
case 'A':
|
135 |
|
|
mode = MODE_14443A;
|
136 |
|
|
break;
|
137 |
78c6dcc4
|
(no author)
|
case 'a':
|
138 |
|
|
mode = MODE_ANTICOL;
|
139 |
|
|
break;
|
140 |
3866f983
|
(no author)
|
/* Those below don't work as long as
|
141 |
|
|
* iso14443a_init() is called before
|
142 |
|
|
* every cycle */
|
143 |
|
|
case 'y':
|
144 |
|
|
reg_inc(RC632_REG_CW_CONDUCTANCE);
|
145 |
|
|
break;
|
146 |
|
|
case 'x':
|
147 |
|
|
reg_dec(RC632_REG_CW_CONDUCTANCE);
|
148 |
|
|
break;
|
149 |
|
|
case 'c':
|
150 |
|
|
reg_inc(RC632_REG_MOD_CONDUCTANCE);
|
151 |
|
|
break;
|
152 |
|
|
case 'v':
|
153 |
|
|
reg_dec(RC632_REG_MOD_CONDUCTANCE);
|
154 |
|
|
break;
|
155 |
6a3cb645
|
(no author)
|
case 'o':
|
156 |
|
|
if (ana_out_sel > 0) {
|
157 |
|
|
ana_out_sel--;
|
158 |
|
|
DEBUGPCR("switching to analog output mode 0x%x\n", ana_out_sel);
|
159 |
28eb4a57
|
laforge
|
opcd_rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT,
|
160 |
|
|
ana_out_sel);
|
161 |
6a3cb645
|
(no author)
|
}
|
162 |
|
|
ret = 1;
|
163 |
|
|
break;
|
164 |
|
|
case 'p':
|
165 |
|
|
if (ana_out_sel < 0xc) {
|
166 |
|
|
ana_out_sel++;
|
167 |
|
|
DEBUGPCR("switching to analog output mode 0x%x\n", ana_out_sel);
|
168 |
28eb4a57
|
laforge
|
opcd_rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT,
|
169 |
|
|
ana_out_sel);
|
170 |
6a3cb645
|
(no author)
|
}
|
171 |
|
|
ret = 1;
|
172 |
|
|
break;
|
173 |
|
|
case 'u':
|
174 |
|
|
if (mfout_sel > 0) {
|
175 |
|
|
mfout_sel--;
|
176 |
|
|
DEBUGPCR("switching to MFOUT mode 0x%x\n", mfout_sel);
|
177 |
28eb4a57
|
laforge
|
opcd_rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
|
178 |
6a3cb645
|
(no author)
|
}
|
179 |
|
|
ret = 1;
|
180 |
|
|
break;
|
181 |
|
|
case 'i':
|
182 |
|
|
if (mfout_sel < 5) {
|
183 |
|
|
mfout_sel++;
|
184 |
|
|
DEBUGPCR("switching to MFOUT mode 0x%x\n", mfout_sel);
|
185 |
28eb4a57
|
laforge
|
opcd_rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
|
186 |
6a3cb645
|
(no author)
|
}
|
187 |
|
|
ret = 1;
|
188 |
|
|
break;
|
189 |
|
|
case 'h':
|
190 |
|
|
AT91F_PIO_SetOutput(AT91C_BASE_PIOA, OPENPCD_PIO_TRIGGER);
|
191 |
|
|
break;
|
192 |
|
|
case 'l':
|
193 |
|
|
AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, OPENPCD_PIO_TRIGGER);
|
194 |
|
|
break;
|
195 |
520784c7
|
(no author)
|
#ifdef WITH_TC
|
196 |
06d2a987
|
(no author)
|
case '<':
|
197 |
|
|
tc_cdiv_phase_inc();
|
198 |
|
|
break;
|
199 |
|
|
case '>':
|
200 |
|
|
tc_cdiv_phase_dec();
|
201 |
|
|
break;
|
202 |
|
|
case '{':
|
203 |
|
|
if (cdiv_idx > 0)
|
204 |
|
|
cdiv_idx--;
|
205 |
|
|
tc_cdiv_set_divider(cdivs[cdiv_idx]);
|
206 |
|
|
break;
|
207 |
|
|
case '}':
|
208 |
|
|
if (cdiv_idx < ARRAY_SIZE(cdivs)-1)
|
209 |
|
|
cdiv_idx++;
|
210 |
|
|
tc_cdiv_set_divider(cdivs[cdiv_idx]);
|
211 |
|
|
break;
|
212 |
520784c7
|
(no author)
|
#endif
|
213 |
ed96e055
|
(no author)
|
case '-':
|
214 |
|
|
if (speed_idx > 0)
|
215 |
|
|
speed_idx--;
|
216 |
|
|
break;
|
217 |
|
|
case '+':
|
218 |
|
|
if (speed_idx < 3)
|
219 |
|
|
speed_idx++;
|
220 |
|
|
break;
|
221 |
78c6dcc4
|
(no author)
|
default:
|
222 |
|
|
return -EINVAL;
|
223 |
|
|
}
|
224 |
|
|
|
225 |
6a3cb645
|
(no author)
|
return ret;
|
226 |
9d0d7022
|
(no author)
|
}
|
227 |
caf50003
|
(no author)
|
|
228 |
|
|
void _main_func(void)
|
229 |
|
|
{
|
230 |
78c6dcc4
|
(no author)
|
int status;
|
231 |
caf50003
|
(no author)
|
struct iso14443a_atqa atqa;
|
232 |
78c6dcc4
|
(no author)
|
volatile int i;
|
233 |
caf50003
|
(no author)
|
|
234 |
6a3cb645
|
(no author)
|
/* FIXME: why does this only work every second attempt without reset or
|
235 |
|
|
* power-cycle? */
|
236 |
28eb4a57
|
laforge
|
//rc632_turn_off_rf();
|
237 |
78c6dcc4
|
(no author)
|
//rc632_reset();
|
238 |
28eb4a57
|
laforge
|
//rc632_turn_on_rf();
|
239 |
6a3cb645
|
(no author)
|
|
240 |
28eb4a57
|
laforge
|
opcd_rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, ana_out_sel);
|
241 |
|
|
opcd_rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
|
242 |
|
|
for (i = 0; i < 0xfffff; i++) {}
|
243 |
78c6dcc4
|
(no author)
|
//rc632_dump();
|
244 |
520784c7
|
(no author)
|
#ifdef WITH_TC
|
245 |
06d2a987
|
(no author)
|
tc_cdiv_print();
|
246 |
520784c7
|
(no author)
|
#endif
|
247 |
3866f983
|
(no author)
|
|
248 |
78c6dcc4
|
(no author)
|
switch (mode) {
|
249 |
|
|
case MODE_REQA:
|
250 |
28eb4a57
|
laforge
|
status = iso14443a_transceive_sf(l2h, ISO14443A_SF_CMD_REQA, &atqa);
|
251 |
78c6dcc4
|
(no author)
|
if (status < 0)
|
252 |
|
|
DEBUGPCRF("error during transceive_sf REQA");
|
253 |
|
|
else
|
254 |
|
|
DEBUGPCRF("received ATQA: %s", hexdump((char *)&atqa, sizeof(atqa)));
|
255 |
|
|
break;
|
256 |
|
|
case MODE_WUPA:
|
257 |
28eb4a57
|
laforge
|
status = iso14443a_transceive_sf(l2h, ISO14443A_SF_CMD_WUPA, &atqa);
|
258 |
78c6dcc4
|
(no author)
|
if (status < 0)
|
259 |
|
|
DEBUGPCRF("error during transceive_sf WUPA");
|
260 |
|
|
else
|
261 |
|
|
DEBUGPCRF("received WUPA: %s", hexdump((char *)&atqa, sizeof(atqa)));
|
262 |
|
|
break;
|
263 |
|
|
case MODE_ANTICOL:
|
264 |
28eb4a57
|
laforge
|
status = rfid_layer2_open(l2h);
|
265 |
78c6dcc4
|
(no author)
|
if (status < 0)
|
266 |
|
|
DEBUGPCR("error during anticol");
|
267 |
|
|
else
|
268 |
|
|
DEBUGPCR("Anticol OK");
|
269 |
|
|
break;
|
270 |
ed96e055
|
(no author)
|
case MODE_14443A:
|
271 |
|
|
{
|
272 |
|
|
char rx_buf[4];
|
273 |
|
|
int rx_len = sizeof(rx_buf);
|
274 |
28eb4a57
|
laforge
|
rfid_layer2_setopt(l2h, RFID_OPT_14443A_SPEED_RX,
|
275 |
ed96e055
|
(no author)
|
&speed_idx, sizeof(speed_idx));
|
276 |
28eb4a57
|
laforge
|
rfid_layer2_setopt(l2h, RFID_OPT_14443A_SPEED_TX,
|
277 |
ed96e055
|
(no author)
|
&speed_idx, sizeof(speed_idx));
|
278 |
28eb4a57
|
laforge
|
rfid_layer2_transceive(l2h, RFID_14443A_FRAME_REGULAR,
|
279 |
ed96e055
|
(no author)
|
&frame_14443a, sizeof(frame_14443a),
|
280 |
|
|
&rx_buf, &rx_len, 1, 0);
|
281 |
|
|
}
|
282 |
|
|
break;
|
283 |
78c6dcc4
|
(no author)
|
}
|
284 |
caf50003
|
(no author)
|
|
285 |
78c6dcc4
|
(no author)
|
if (status < 0)
|
286 |
92b5f96c
|
(no author)
|
led_switch(1, 0);
|
287 |
78c6dcc4
|
(no author)
|
else
|
288 |
92b5f96c
|
(no author)
|
led_switch(1, 1);
|
289 |
78c6dcc4
|
(no author)
|
|
290 |
5ab655eb
|
(no author)
|
led_toggle(2);
|
291 |
6a3cb645
|
(no author)
|
|
292 |
caf50003
|
(no author)
|
}
|