Project

General

Profile

Download (7.02 KB) Statistics
| Branch: | Tag: | Revision:
1
/* main_reqa - OpenPCD firmware for generating an endless loop of
2
 * ISO 14443-A REQA packets.  Alternatively we can send WUPA, or
3
 * perform a full ISO14443A anti-collision loop.
4
 *
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
 * (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
 */
26

    
27
#include <errno.h>
28
#include <string.h>
29
#include <lib_AT91SAM7.h>
30
#include <librfid/rfid_layer2_iso14443a.h>
31
#include "rc632.h"
32
#include <os/dbgu.h>
33
#include <os/led.h>
34
#include <os/pcd_enumerate.h>
35
#include <os/trigger.h>
36
#include <pcd/rc632_highlevel.h>
37

    
38
#include <librfid/rfid_reader.h>
39
#include <librfid/rfid_layer2.h>
40

    
41
#include "../openpcd.h"
42

    
43
#ifdef WITH_TC
44
#include "tc.h"
45
#endif
46

    
47
#define RAH NULL
48

    
49
static struct rfid_reader_handle *rh;
50
static struct rfid_layer2_handle *l2h;
51

    
52
void _init_func(void)
53
{
54
	trigger_init();
55
	DEBUGPCRF("enabling RC632");
56
	rc632_init();
57
#ifdef WITH_TC
58
	DEBUGPCRF("enabling TC");
59
	tc_cdiv_init();
60
#endif
61
	DEBUGPCRF("turning on RF");
62
	rc632_turn_on_rf(RAH);
63
	DEBUGPCRF("initializing 14443A operation");
64
	rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
65
	l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
66
}
67

    
68
#define MODE_REQA	0x01
69
#define MODE_WUPA	0x02
70
#define MODE_ANTICOL	0x03
71
#define MODE_14443A	0x04
72

    
73
static volatile int mode = MODE_REQA;
74

    
75
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

    
86
static void reg_inc(uint8_t reg)
87
{
88
	uint8_t val;
89
	opcd_rc632_reg_read(RAH, reg, &val);
90
	opcd_rc632_reg_write(RAH, reg, val++);
91
	DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
92
}
93

    
94
static void reg_dec(uint8_t reg)
95
{
96
	uint8_t val;
97
	opcd_rc632_reg_read(RAH, reg, &val);
98
	opcd_rc632_reg_write(RAH, reg, val--);
99
	DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
100
}
101

    
102
static uint8_t ana_out_sel;
103
static uint8_t mfout_sel;
104
static uint8_t speed_idx;
105

    
106
static void help(void)
107
{
108
	DEBUGPCR("r: REQA         w: WUPA        a: ANTICOL\r\n"
109
		 "A: 14443A       +: inc speed   -: dec speed\r\n"
110
		 "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
		 "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
}
116

    
117
static uint16_t cdivs[] = { 128, 64, 32, 16 };
118

    
119
int _main_dbgu(char key)
120
{
121
	int ret = 0;
122
	static int cdiv_idx = 2;
123

    
124
	switch (key) {
125
	case '?':
126
		help();
127
		break;
128
	case 'r':
129
		mode = MODE_REQA;
130
		break;
131
	case 'w':
132
		mode = MODE_WUPA;
133
		break;
134
	case 'A':
135
		mode = MODE_14443A;
136
		break;
137
	case 'a':
138
		mode = MODE_ANTICOL;
139
		break;
140
		/* 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
	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
			opcd_rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, 
160
					     ana_out_sel);
161
		}
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
			opcd_rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, 
169
					     ana_out_sel);
170
		}
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
			opcd_rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
178
		}
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
			opcd_rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
186
		}
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
#ifdef WITH_TC
196
	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
#endif
213
	case '-':
214
		if (speed_idx > 0)
215
			speed_idx--;
216
		break;
217
	case '+':
218
		if (speed_idx < 3)
219
			speed_idx++;
220
		break;
221
	default:
222
		return -EINVAL;
223
	}
224
	
225
	return ret;
226
}
227

    
228
void _main_func(void)
229
{
230
	int status;
231
	struct iso14443a_atqa atqa;
232
	volatile int i;
233

    
234
	/* FIXME: why does this only work every second attempt without reset or
235
	 * power-cycle? */
236
	//rc632_turn_off_rf();
237
	//rc632_reset();
238
	//rc632_turn_on_rf();
239

    
240
	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
	//rc632_dump();
244
#ifdef WITH_TC
245
	tc_cdiv_print();
246
#endif
247

    
248
	switch (mode) {
249
	case MODE_REQA:
250
		status = iso14443a_transceive_sf(l2h, ISO14443A_SF_CMD_REQA, &atqa);
251
		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
		status = iso14443a_transceive_sf(l2h, ISO14443A_SF_CMD_WUPA, &atqa);
258
		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
		status = rfid_layer2_open(l2h);
265
		if (status < 0)
266
			DEBUGPCR("error during anticol");
267
		else
268
			DEBUGPCR("Anticol OK");
269
		break;
270
	case MODE_14443A:
271
		{
272
			char rx_buf[4];
273
			int rx_len = sizeof(rx_buf);
274
			rfid_layer2_setopt(l2h, RFID_OPT_14443A_SPEED_RX,
275
					 &speed_idx, sizeof(speed_idx));
276
			rfid_layer2_setopt(l2h, RFID_OPT_14443A_SPEED_TX,
277
					 &speed_idx, sizeof(speed_idx));
278
			rfid_layer2_transceive(l2h, RFID_14443A_FRAME_REGULAR, 
279
					    &frame_14443a, sizeof(frame_14443a),
280
					    &rx_buf, &rx_len, 1, 0);
281
		}
282
		break;
283
	}
284

    
285
	if (status < 0)
286
		led_switch(1, 0);
287
	else 
288
		led_switch(1, 1);
289

    
290
	led_toggle(2);
291

    
292
}
(8-8/15)
Add picture from clipboard (Maximum size: 48.8 MB)