summaryrefslogtreecommitdiff
path: root/openpicc/application/iso14443_layer2a.c
blob: 35a97eee34656fcaf98539ab4168afc3faad3206 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***************************************************************
 *
 * OpenPICC - ISO 14443 Layer 2 Type A PICC transceiver code
 * Manages receiving, sending and parts of framing
 * 
 * This does not fully implement layer 2 in that it won't 
 * automatically call the Miller decoder or Manchester encoder
 * for you. Instead you'll be given ssc rx buffer pointers and
 * are expected to hand in ssc tx buffer pointers. You've got
 * to call iso14443a_manchester and iso14443a_miller yourself.
 * The reason is that this makes it possible for the layer 3
 * implementation to work on raw samples without en/de-coding
 * time to enable fast responses during anticollision.
 *
 * Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
 *
 ***************************************************************

    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; version 2.

    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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include <FreeRTOS.h>
#include <board.h>
#include <task.h>
#include <errno.h>

#include "openpicc.h"
#include "iso14443_layer2a.h"
#include "ssc.h"
#include "ssc_buffer.h"
#include "pll.h"
#include "tc_fdt.h"
#include "tc_cdiv.h"
#include "tc_cdiv_sync.h"
#include "tc_recv.h"
#include "load_modulation.h"
#include "clock_switch.h"
#include "pio_irq.h"

#include "usb_print.h"
#include "cmd.h"

#define PRINT_DEBUG 0

static u_int8_t fast_receive;
static u_int8_t tx_pending=0;
static u_int8_t rx_pending=0;
static iso14443_receive_callback_t callback=NULL;
static ssc_handle_t *ssc;
static tc_recv_handle_t th;

#ifdef FOUR_TIMES_OVERSAMPLING
#define RX_DIVIDER 32
#else
#define RX_DIVIDER 64
#endif

int iso14443_receive(iso14443_receive_callback_t _callback, iso14443_frame **frame, unsigned int timeout)
{
	iso14443_frame* _frame = NULL;
	
	if(rx_pending) {
		return -EALREADY;
	}
	rx_pending=1;
	callback=_callback;
	
	if(tc_recv_receive(th, &_frame, timeout) == 0) {
		
		if(_frame == NULL) {
			/* Can this happen? */
			rx_pending=0;
			callback=NULL;
			return -ETIMEDOUT;
		}
		
		portENTER_CRITICAL();
		_frame->state = FRAME_PROCESSING;
		portEXIT_CRITICAL();
		
		if(callback != NULL && !fast_receive) {
			callback(NULL, _frame, 0);
		}
		
		if(frame != NULL) *frame = _frame;
		else {
			portENTER_CRITICAL();
			_frame->state = FRAME_FREE;
			portEXIT_CRITICAL();
		}
		
		rx_pending=0;
		callback=NULL;
		return 0;
	}
	
	/* Note: There is the remote chance of a race condition probability here if
	 * a frame start was received right before the timeout for this function 
	 * expired. In the future one might want to replace this with some safer code
	 * (hmm, maybe check TC2_CV?) but for now it's an essential safeguard to prevent
	 * a hung receiver when no proper frame end is signalled to iso14443_ssc_callback
	 * and therefore the callback never resets the flipflop */
	if(!tx_pending) {
		if(AT91F_PIO_IsInputSet(AT91C_BASE_PIOA, OPENPICC_PIO_FRAME)) tc_cdiv_sync_reset();
	}
	
	rx_pending=0;
	callback=NULL;
	return -ETIMEDOUT;
}

int iso14443_transmit(ssc_dma_tx_buffer_t *buffer, unsigned int fdt, u_int8_t async, unsigned int timeout)
{
	if(tx_pending) 
		return -EBUSY;
	
	tx_pending = 1;
	
	/* Immediately set up FDT and clock */
	//clock_switch(CLOCK_SELECT_CARRIER);
	ssc_set_gate(0);
	tc_fdt_set(fdt);
	// We'll keep the divider at 8
	//tc_cdiv_set_divider(8); // FIXME Magic hardcoded number
	
	if(!async) {
		/* FIXME Implement */
		(void)timeout;
		tx_pending = 0;
		return -EINVAL;
	}
	
	int ret = ssc_send(ssc, buffer);
	if(ret < 0) {
		tx_pending = 0;
		return ret;
	}
	
	if(!async) {
		/* FIXME Wait for completion, timeout or abort */
	}
	
	return 0;
}

int iso14443_tx_abort(void)
{
	int ret = 0;
	taskENTER_CRITICAL();
	ret = ssc_send_abort(ssc);
	tx_pending = 0;
	taskEXIT_CRITICAL();
	return ret;
}

int iso14443_wait_for_carrier(unsigned int timeout)
{
	(void)timeout;
	return 0;
}

u_int8_t iso14443_set_fast_receive(u_int8_t enable_fast_receive)
{
	u_int8_t old_value = fast_receive;
	fast_receive = enable_fast_receive;
	return old_value;
}

u_int8_t iso14443_get_fast_receive(void)
{
	return fast_receive;
}

static void iso14443_ssc_callback(ssc_callback_reason reason, void *data)
{
	if( reason == SSC_CALLBACK_SETUP ) {
		// We'll keep the divider at 8
		tc_cdiv_set_divider(8); // FIXME Magic hardcoded number
	}

	if(reason == SSC_CALLBACK_RX_FRAME_BEGIN) {
		/* Busy loop for the frame end */
		int *end_asserted = data, i=0;
		for(i=0; i<96000; i++) 
			if(*AT91C_TC2_CV > 2*128) { // FIXME magic number
				*end_asserted = 1;
				if(PRINT_DEBUG) usb_print_string_f("^", 0); // DEBUG OUTPUT
				break;
			}
		return;
	}
	
	if(reason == SSC_CALLBACK_TX_FRAME_ENDED) {
		tx_pending = 0;
	}
	
	if( reason == SSC_CALLBACK_RX_FRAME_ENDED && fast_receive ) {
		//clock_switch(CLOCK_SELECT_CARRIER); /* A Tx might be coming up */
		
		ssc_dma_rx_buffer_t *buffer = data;
		if(callback != NULL)
			callback(buffer, NULL, 1);
	}
	
	if( (reason == SSC_CALLBACK_RX_FRAME_ENDED && !tx_pending) || reason == SSC_CALLBACK_RX_STARTING 
			|| reason == SSC_CALLBACK_TX_FRAME_ENDED ) {
		/* For regular SSC Rx we'd set the clock to
		// clock_switch(CLOCK_SELECT_PLL);
		 * however, the SSC Rx code is going to go away (at least for 14443-A)
		 * and switching clocks messes up the Tx timing, so we do a */
		//clock_switch(CLOCK_SELECT_CARRIER);
		ssc_set_gate(1);
		tc_fdt_set(0xff00);
		// We'll keep the divider at 8
		//tc_cdiv_set_divider(RX_DIVIDER);
		tc_cdiv_sync_reset();
#if 0
		int old=usb_print_set_default_flush(0);
		DumpStringToUSB("["); DumpUIntToUSB(reason); DumpStringToUSB("]");
		usb_print_set_default_flush(old);
#endif
	}
}

static void iso14443_tc_recv_callback(tc_recv_callback_reason reason, void *data)
{
	if( reason == TC_RECV_CALLBACK_RX_FRAME_ENDED && fast_receive ) {
		//clock_switch(CLOCK_SELECT_CARRIER); /* A Tx might be coming up */
		
		iso14443_frame *frame = data;
		if(callback != NULL)
			callback(NULL, frame, 1);
	}
	
	if( (reason == TC_RECV_CALLBACK_RX_FRAME_ENDED && !tx_pending) ||
			reason == TC_RECV_CALLBACK_SETUP ) {
		/* For T/C Rx we set the clock to */
		//clock_switch(CLOCK_SELECT_CARRIER);
		ssc_set_gate(1);
		tc_fdt_set(0xff00);
		// We'll keep the divider at 8
		//tc_cdiv_set_divider(RX_DIVIDER);
		tc_cdiv_sync_reset();
	}
}

int iso14443_layer2a_init(u_int8_t enable_fast_receive)
{
	pll_init();
    
	tc_cdiv_init();
	tc_fdt_init();
	
	tc_cdiv_sync_init();
	tc_cdiv_sync_enable();
	
	clock_switch_init();
	load_mod_init();
	
	iso14443_set_fast_receive(enable_fast_receive);
	pio_irq_init_once();
	
	ssc = ssc_open(0, 1, SSC_MODE_14443A, iso14443_ssc_callback);
	if(ssc == NULL)
		return -EIO;
	
	int pauses_count;
	if(OPENPICC->features.clock_switching) {
		clock_switch(CLOCK_SELECT_CARRIER);
		pauses_count = 0;
	} else {
		if(OPENPICC->default_clock == CLOCK_SELECT_CARRIER) {
			pauses_count = 0;
		} else {
			return -ENOTSUP;
		}
	}
	if(tc_recv_init(&th, pauses_count, iso14443_tc_recv_callback) < 0) {
		ssc_close(ssc);
		return -EIO;
	}
	
	load_mod_level(3);
	
	return 0;
}
personal git repositories of Harald Welte. Your mileage may vary