summaryrefslogtreecommitdiff
path: root/firmware/src/pcd/main_reqa.c
blob: 9f465697d6fc0f09fc84d31530f7f6b27e32ffea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* main_reqa - OpenPCD firmware for generating an endless loop of
 * ISO 14443-A REQA packets.  Alternatively we can send WUPA, or
 * perform a full ISO14443A anti-collision loop.
 *
 * If a response is received from the PICC, LED1 (Red) will be switched
 * on.  If no valid response has been received within the timeout of the
 * receiver, LED1 (Red) will be switched off.
 *
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <errno.h>
#include <string.h>
#include <lib_AT91SAM7.h>
#include <librfid/rfid_layer2_iso14443a.h>
#include "rc632.h"
#include <os/dbgu.h>
#include <os/led.h>
#include <os/pcd_enumerate.h>
#include <os/trigger.h>

#include "../openpcd.h"

#ifdef WITH_TC
#include "tc.h"
#endif

void _init_func(void)
{
	trigger_init();
	DEBUGPCRF("enabling RC632");
	rc632_init();
#ifdef WITH_TC
	DEBUGPCRF("enabling TC");
	tc_cdiv_init();
#endif
	DEBUGPCRF("turning on RF");
	rc632_turn_on_rf(RAH);
	DEBUGPCRF("initializing 14443A operation");
	rc632_iso14443a_init(RAH);
}

#define MODE_REQA	0x01
#define MODE_WUPA	0x02
#define MODE_ANTICOL	0x03
#define MODE_14443A	0x04

static volatile int mode = MODE_REQA;

static const char frame_14443a[] = { 0x00, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };

static void reg_inc(u_int8_t reg)
{
	u_int8_t val;
	rc632_reg_read(RAH, reg, &val);
	rc632_reg_write(RAH, reg, val++);
	DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
}

static void reg_dec(u_int8_t reg)
{
	u_int8_t val;
	rc632_reg_read(RAH, reg, &val);
	rc632_reg_write(RAH, reg, val--);
	DEBUGPCRF("reg 0x%02x = 0x%02x", reg, val);
}

static u_int8_t ana_out_sel;
static u_int8_t mfout_sel;
static u_int8_t speed_idx;

static void help(void)
{
	DEBUGPCR("r: REQA         w: WUPA        a: ANTICOL\r\n"
		 "A: 14443A       +: inc speed   -: dec speed\r\n"
		 "y: inc cw cond  x: dec cond    c: inc mod cond");
	DEBUGPCR("v: dec mod cond o: dec ana_out p: dec ana_out\r\n"
		 "h: trigger high l: trigger low u: dec MFOUT mode");
	DEBUGPCR("i: inc MFOUT md <: dec cdiv ph >: inc cdiv phase\r\n"
		 "{: dev cdiv     }: inc cdiv");
}

static u_int16_t cdivs[] = { 128, 64, 32, 16 };

int _main_dbgu(char key)
{
	int ret = 0;
	static int cdiv_idx = 0;

	switch (key) {
	case '?':
		help();
		break;
	case 'r':
		mode = MODE_REQA;
		break;
	case 'w':
		mode = MODE_WUPA;
		break;
	case 'A':
		mode = MODE_14443A;
		break;
	case 'a':
		mode = MODE_ANTICOL;
		break;
		/* Those below don't work as long as 
		 * iso14443a_init() is called before
		 * every cycle */
	case 'y':
		reg_inc(RC632_REG_CW_CONDUCTANCE);
		break;
	case 'x':
		reg_dec(RC632_REG_CW_CONDUCTANCE);
		break;
	case 'c':
		reg_inc(RC632_REG_MOD_CONDUCTANCE);
		break;
	case 'v':
		reg_dec(RC632_REG_MOD_CONDUCTANCE);
		break;
	case 'o':
		if (ana_out_sel > 0) {
			ana_out_sel--;
			DEBUGPCR("switching to analog output mode 0x%x\n", ana_out_sel);
			rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, ana_out_sel);
		}
		ret = 1;
		break;
	case 'p':
		if (ana_out_sel < 0xc) {
			ana_out_sel++;
			DEBUGPCR("switching to analog output mode 0x%x\n", ana_out_sel);
			rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, ana_out_sel);
		}
		ret = 1;
		break;
	case 'u':
		if (mfout_sel > 0) {
			mfout_sel--;
			DEBUGPCR("switching to MFOUT mode 0x%x\n", mfout_sel);
			rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
		}
		ret = 1;
		break;
	case 'i':
		if (mfout_sel < 5) {
			mfout_sel++;
			DEBUGPCR("switching to MFOUT mode 0x%x\n", mfout_sel);
			rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
		}
		ret = 1;
		break;
	case 'h':
		AT91F_PIO_SetOutput(AT91C_BASE_PIOA, OPENPCD_PIO_TRIGGER);
		break;
	case 'l':
		AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, OPENPCD_PIO_TRIGGER);
		break;
#ifdef WITH_TC
	case '<':
		tc_cdiv_phase_inc();
		break;
	case '>':
		tc_cdiv_phase_dec();
		break;
	case '{':
		if (cdiv_idx > 0)
			cdiv_idx--;
		tc_cdiv_set_divider(cdivs[cdiv_idx]);
		break;
	case '}':
		if (cdiv_idx < ARRAY_SIZE(cdivs)-1)
			cdiv_idx++;
		tc_cdiv_set_divider(cdivs[cdiv_idx]);
		break;
#endif
	case '-':
		if (speed_idx > 0)
			speed_idx--;
		break;
	case '+':
		if (speed_idx < 3)
			speed_idx++;
		break;
	default:
		return -EINVAL;
	}
	
	return ret;
}

void _main_func(void)
{
	int status;
	struct iso14443a_atqa atqa;
	struct rfid_layer2_handle l2h;
	volatile int i;

	memset(&atqa, 0, sizeof(atqa));

	/* fake layer2 handle initialization */
	memset(&l2h, 0, sizeof(l2h));
	l2h.l2 = &rfid_layer2_iso14443a;
	l2h.priv.iso14443a.state = ISO14443A_STATE_NONE;
	l2h.priv.iso14443a.level = ISO14443A_LEVEL_NONE;

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

	rc632_iso14443a_init(RAH);
	rc632_reg_write(RAH, RC632_REG_TEST_ANA_SELECT, ana_out_sel);
	rc632_reg_write(RAH, RC632_REG_MFOUT_SELECT, mfout_sel);
	for (i = 0; i < 0x3ffff; i++) {}
	//rc632_dump();
#ifdef WITH_TC
	tc_cdiv_print();
#endif

	switch (mode) {
	case MODE_REQA:
		status = rc632_iso14443a_transceive_sf(RAH, ISO14443A_SF_CMD_REQA, &atqa);
		if (status < 0)
			DEBUGPCRF("error during transceive_sf REQA");
		else 
			DEBUGPCRF("received ATQA: %s", hexdump((char *)&atqa, sizeof(atqa)));
		break;
	case MODE_WUPA:
		status = rc632_iso14443a_transceive_sf(RAH, ISO14443A_SF_CMD_WUPA, &atqa);
		if (status < 0)
			DEBUGPCRF("error during transceive_sf WUPA");
		else 
			DEBUGPCRF("received WUPA: %s", hexdump((char *)&atqa, sizeof(atqa)));
		break;
	case MODE_ANTICOL:
		status = rfid_layer2_iso14443a.fn.open(&l2h);
		if (status < 0)
			DEBUGPCR("error during anticol");
		else
			DEBUGPCR("Anticol OK");
		break;
	case MODE_14443A:
		{
			char rx_buf[4];
			int rx_len = sizeof(rx_buf);
			rfid_layer2_iso14443a.fn.setopt(&l2h, RFID_OPT_14443A_SPEED_RX,
					 &speed_idx, sizeof(speed_idx));
			rfid_layer2_iso14443a.fn.setopt(&l2h, RFID_OPT_14443A_SPEED_TX,
					 &speed_idx, sizeof(speed_idx));
			rfid_layer2_iso14443a.fn.transceive(&l2h, RFID_14443A_FRAME_REGULAR, 
					    &frame_14443a, sizeof(frame_14443a),
					    &rx_buf, &rx_len, 1, 0);
		}
		break;
	}

	if (status < 0)
		led_switch(1, 0);
	else 
		led_switch(1, 1);

	led_toggle(2);

}
personal git repositories of Harald Welte. Your mileage may vary