1 |
6be69f0d
|
henryk
|
/***************************************************************
|
2 |
|
|
*
|
3 |
|
|
* OpenPICC - ISO 14443 Layer 2 Type A PICC transceiver code
|
4 |
|
|
* Manages receiving, sending and parts of framing
|
5 |
|
|
*
|
6 |
|
|
* This does not fully implement layer 2 in that it won't
|
7 |
|
|
* automatically call the Miller decoder or Manchester encoder
|
8 |
|
|
* for you. Instead you'll be given ssc rx buffer pointers and
|
9 |
|
|
* are expected to hand in ssc tx buffer pointers. You've got
|
10 |
|
|
* to call iso14443a_manchester and iso14443a_miller yourself.
|
11 |
|
|
* The reason is that this makes it possible for the layer 3
|
12 |
|
|
* implementation to work on raw samples without en/de-coding
|
13 |
|
|
* time to enable fast responses during anticollision.
|
14 |
|
|
*
|
15 |
|
|
* Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
|
16 |
|
|
*
|
17 |
|
|
***************************************************************
|
18 |
|
|
|
19 |
|
|
This program is free software; you can redistribute it and/or modify
|
20 |
|
|
it under the terms of the GNU General Public License as published by
|
21 |
|
|
the Free Software Foundation; version 2.
|
22 |
|
|
|
23 |
|
|
This program is distributed in the hope that it will be useful,
|
24 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
25 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
26 |
|
|
GNU General Public License for more details.
|
27 |
|
|
|
28 |
|
|
You should have received a copy of the GNU General Public License along
|
29 |
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
30 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
31 |
|
|
|
32 |
|
|
*/
|
33 |
|
|
|
34 |
|
|
#include <FreeRTOS.h>
|
35 |
|
|
#include <board.h>
|
36 |
|
|
#include <task.h>
|
37 |
|
|
#include <errno.h>
|
38 |
|
|
|
39 |
|
|
#include "openpicc.h"
|
40 |
|
|
#include "iso14443_layer2a.h"
|
41 |
ad5b96d5
|
henryk
|
#include "ssc.h"
|
42 |
d3bab6e9
|
henryk
|
#include "ssc_buffer.h"
|
43 |
6be69f0d
|
henryk
|
#include "pll.h"
|
44 |
|
|
#include "tc_fdt.h"
|
45 |
|
|
#include "tc_cdiv.h"
|
46 |
|
|
#include "tc_cdiv_sync.h"
|
47 |
e2e37bea
|
henryk
|
#include "tc_recv.h"
|
48 |
6be69f0d
|
henryk
|
#include "load_modulation.h"
|
49 |
6304718e
|
henryk
|
#include "clock_switch.h"
|
50 |
ad5b96d5
|
henryk
|
#include "pio_irq.h"
|
51 |
|
|
|
52 |
|
|
#include "usb_print.h"
|
53 |
78a0c8a5
|
henryk
|
#include "cmd.h"
|
54 |
ad5b96d5
|
henryk
|
|
55 |
|
|
#define PRINT_DEBUG 0
|
56 |
6be69f0d
|
henryk
|
|
57 |
|
|
static u_int8_t fast_receive;
|
58 |
d3bab6e9
|
henryk
|
static u_int8_t tx_pending=0;
|
59 |
|
|
static u_int8_t rx_pending=0;
|
60 |
|
|
static iso14443_receive_callback_t callback=NULL;
|
61 |
ad5b96d5
|
henryk
|
static ssc_handle_t *ssc;
|
62 |
e2e37bea
|
henryk
|
static tc_recv_handle_t th;
|
63 |
6be69f0d
|
henryk
|
|
64 |
|
|
#ifdef FOUR_TIMES_OVERSAMPLING
|
65 |
|
|
#define RX_DIVIDER 32
|
66 |
|
|
#else
|
67 |
|
|
#define RX_DIVIDER 64
|
68 |
|
|
#endif
|
69 |
|
|
|
70 |
e2e37bea
|
henryk
|
int iso14443_receive(iso14443_receive_callback_t _callback, iso14443_frame **frame, unsigned int timeout)
|
71 |
6be69f0d
|
henryk
|
{
|
72 |
e2e37bea
|
henryk
|
iso14443_frame* _frame = NULL;
|
73 |
6be69f0d
|
henryk
|
|
74 |
d3bab6e9
|
henryk
|
if(rx_pending) {
|
75 |
|
|
return -EALREADY;
|
76 |
|
|
}
|
77 |
|
|
rx_pending=1;
|
78 |
|
|
callback=_callback;
|
79 |
|
|
|
80 |
e2e37bea
|
henryk
|
if(tc_recv_receive(th, &_frame, timeout) == 0) {
|
81 |
6be69f0d
|
henryk
|
|
82 |
e2e37bea
|
henryk
|
if(_frame == NULL) {
|
83 |
6be69f0d
|
henryk
|
/* Can this happen? */
|
84 |
d3bab6e9
|
henryk
|
rx_pending=0;
|
85 |
|
|
callback=NULL;
|
86 |
6be69f0d
|
henryk
|
return -ETIMEDOUT;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
portENTER_CRITICAL();
|
90 |
e2e37bea
|
henryk
|
_frame->state = FRAME_PROCESSING;
|
91 |
6be69f0d
|
henryk
|
portEXIT_CRITICAL();
|
92 |
|
|
|
93 |
d3bab6e9
|
henryk
|
if(callback != NULL && !fast_receive) {
|
94 |
e2e37bea
|
henryk
|
callback(NULL, _frame, 0);
|
95 |
d3bab6e9
|
henryk
|
}
|
96 |
|
|
|
97 |
e2e37bea
|
henryk
|
if(frame != NULL) *frame = _frame;
|
98 |
6be69f0d
|
henryk
|
else {
|
99 |
|
|
portENTER_CRITICAL();
|
100 |
e2e37bea
|
henryk
|
_frame->state = FRAME_FREE;
|
101 |
6be69f0d
|
henryk
|
portEXIT_CRITICAL();
|
102 |
|
|
}
|
103 |
|
|
|
104 |
d3bab6e9
|
henryk
|
rx_pending=0;
|
105 |
|
|
callback=NULL;
|
106 |
e2e37bea
|
henryk
|
return 0;
|
107 |
6be69f0d
|
henryk
|
}
|
108 |
|
|
|
109 |
d3bab6e9
|
henryk
|
/* Note: There is the remote chance of a race condition probability here if
|
110 |
|
|
* a frame start was received right before the timeout for this function
|
111 |
|
|
* expired. In the future one might want to replace this with some safer code
|
112 |
|
|
* (hmm, maybe check TC2_CV?) but for now it's an essential safeguard to prevent
|
113 |
|
|
* a hung receiver when no proper frame end is signalled to iso14443_ssc_callback
|
114 |
|
|
* and therefore the callback never resets the flipflop */
|
115 |
|
|
if(!tx_pending) {
|
116 |
|
|
if(AT91F_PIO_IsInputSet(AT91C_BASE_PIOA, OPENPICC_PIO_FRAME)) tc_cdiv_sync_reset();
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
rx_pending=0;
|
120 |
|
|
callback=NULL;
|
121 |
6be69f0d
|
henryk
|
return -ETIMEDOUT;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
d3bab6e9
|
henryk
|
int iso14443_transmit(ssc_dma_tx_buffer_t *buffer, unsigned int fdt, u_int8_t async, unsigned int timeout)
|
125 |
|
|
{
|
126 |
|
|
if(tx_pending)
|
127 |
|
|
return -EBUSY;
|
128 |
|
|
|
129 |
|
|
tx_pending = 1;
|
130 |
|
|
|
131 |
|
|
/* Immediately set up FDT and clock */
|
132 |
af156e10
|
henryk
|
//clock_switch(CLOCK_SELECT_CARRIER);
|
133 |
d3bab6e9
|
henryk
|
ssc_set_gate(0);
|
134 |
|
|
tc_fdt_set(fdt);
|
135 |
f7a420f2
|
henryk
|
// We'll keep the divider at 8
|
136 |
|
|
//tc_cdiv_set_divider(8); // FIXME Magic hardcoded number
|
137 |
d3bab6e9
|
henryk
|
|
138 |
|
|
if(!async) {
|
139 |
|
|
/* FIXME Implement */
|
140 |
|
|
(void)timeout;
|
141 |
|
|
tx_pending = 0;
|
142 |
|
|
return -EINVAL;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
int ret = ssc_send(ssc, buffer);
|
146 |
|
|
if(ret < 0) {
|
147 |
|
|
tx_pending = 0;
|
148 |
|
|
return ret;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
if(!async) {
|
152 |
|
|
/* FIXME Wait for completion, timeout or abort */
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
return 0;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
2bf5a3ae
|
henryk
|
int iso14443_tx_abort(void)
|
159 |
|
|
{
|
160 |
|
|
int ret = 0;
|
161 |
|
|
taskENTER_CRITICAL();
|
162 |
|
|
ret = ssc_send_abort(ssc);
|
163 |
|
|
tx_pending = 0;
|
164 |
|
|
taskEXIT_CRITICAL();
|
165 |
|
|
return ret;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
6be69f0d
|
henryk
|
int iso14443_wait_for_carrier(unsigned int timeout)
|
169 |
|
|
{
|
170 |
|
|
(void)timeout;
|
171 |
|
|
return 0;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
u_int8_t iso14443_set_fast_receive(u_int8_t enable_fast_receive)
|
175 |
|
|
{
|
176 |
|
|
u_int8_t old_value = fast_receive;
|
177 |
|
|
fast_receive = enable_fast_receive;
|
178 |
|
|
return old_value;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
u_int8_t iso14443_get_fast_receive(void)
|
182 |
|
|
{
|
183 |
|
|
return fast_receive;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
ad5b96d5
|
henryk
|
static void iso14443_ssc_callback(ssc_callback_reason reason, void *data)
|
187 |
6be69f0d
|
henryk
|
{
|
188 |
f7a420f2
|
henryk
|
if( reason == SSC_CALLBACK_SETUP ) {
|
189 |
|
|
// We'll keep the divider at 8
|
190 |
|
|
tc_cdiv_set_divider(8); // FIXME Magic hardcoded number
|
191 |
|
|
}
|
192 |
|
|
|
193 |
e2e37bea
|
henryk
|
if(reason == SSC_CALLBACK_RX_FRAME_BEGIN) {
|
194 |
ad5b96d5
|
henryk
|
/* Busy loop for the frame end */
|
195 |
|
|
int *end_asserted = data, i=0;
|
196 |
|
|
for(i=0; i<96000; i++)
|
197 |
|
|
if(*AT91C_TC2_CV > 2*128) { // FIXME magic number
|
198 |
|
|
*end_asserted = 1;
|
199 |
|
|
if(PRINT_DEBUG) usb_print_string_f("^", 0); // DEBUG OUTPUT
|
200 |
|
|
break;
|
201 |
|
|
}
|
202 |
|
|
return;
|
203 |
|
|
}
|
204 |
d3bab6e9
|
henryk
|
|
205 |
e2e37bea
|
henryk
|
if(reason == SSC_CALLBACK_TX_FRAME_ENDED) {
|
206 |
d3bab6e9
|
henryk
|
tx_pending = 0;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
e2e37bea
|
henryk
|
if( reason == SSC_CALLBACK_RX_FRAME_ENDED && fast_receive ) {
|
210 |
af156e10
|
henryk
|
//clock_switch(CLOCK_SELECT_CARRIER); /* A Tx might be coming up */
|
211 |
d3bab6e9
|
henryk
|
|
212 |
|
|
ssc_dma_rx_buffer_t *buffer = data;
|
213 |
|
|
if(callback != NULL)
|
214 |
e2e37bea
|
henryk
|
callback(buffer, NULL, 1);
|
215 |
d3bab6e9
|
henryk
|
}
|
216 |
|
|
|
217 |
e2e37bea
|
henryk
|
if( (reason == SSC_CALLBACK_RX_FRAME_ENDED && !tx_pending) || reason == SSC_CALLBACK_RX_STARTING
|
218 |
|
|
|| reason == SSC_CALLBACK_TX_FRAME_ENDED ) {
|
219 |
440a95fc
|
henryk
|
/* For regular SSC Rx we'd set the clock to
|
220 |
6304718e
|
henryk
|
// clock_switch(CLOCK_SELECT_PLL);
|
221 |
440a95fc
|
henryk
|
* however, the SSC Rx code is going to go away (at least for 14443-A)
|
222 |
|
|
* and switching clocks messes up the Tx timing, so we do a */
|
223 |
af156e10
|
henryk
|
//clock_switch(CLOCK_SELECT_CARRIER);
|
224 |
d3bab6e9
|
henryk
|
ssc_set_gate(1);
|
225 |
6be69f0d
|
henryk
|
tc_fdt_set(0xff00);
|
226 |
f7a420f2
|
henryk
|
// We'll keep the divider at 8
|
227 |
|
|
//tc_cdiv_set_divider(RX_DIVIDER);
|
228 |
6be69f0d
|
henryk
|
tc_cdiv_sync_reset();
|
229 |
ed093a99
|
henryk
|
#if 0
|
230 |
78a0c8a5
|
henryk
|
int old=usb_print_set_default_flush(0);
|
231 |
|
|
DumpStringToUSB("["); DumpUIntToUSB(reason); DumpStringToUSB("]");
|
232 |
|
|
usb_print_set_default_flush(old);
|
233 |
|
|
#endif
|
234 |
6be69f0d
|
henryk
|
}
|
235 |
|
|
}
|
236 |
|
|
|
237 |
e2e37bea
|
henryk
|
static void iso14443_tc_recv_callback(tc_recv_callback_reason reason, void *data)
|
238 |
ad5b96d5
|
henryk
|
{
|
239 |
e2e37bea
|
henryk
|
if( reason == TC_RECV_CALLBACK_RX_FRAME_ENDED && fast_receive ) {
|
240 |
af156e10
|
henryk
|
//clock_switch(CLOCK_SELECT_CARRIER); /* A Tx might be coming up */
|
241 |
e2e37bea
|
henryk
|
|
242 |
|
|
iso14443_frame *frame = data;
|
243 |
|
|
if(callback != NULL)
|
244 |
|
|
callback(NULL, frame, 1);
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
if( (reason == TC_RECV_CALLBACK_RX_FRAME_ENDED && !tx_pending) ||
|
248 |
|
|
reason == TC_RECV_CALLBACK_SETUP ) {
|
249 |
|
|
/* For T/C Rx we set the clock to */
|
250 |
af156e10
|
henryk
|
//clock_switch(CLOCK_SELECT_CARRIER);
|
251 |
e2e37bea
|
henryk
|
ssc_set_gate(1);
|
252 |
|
|
tc_fdt_set(0xff00);
|
253 |
f7a420f2
|
henryk
|
// We'll keep the divider at 8
|
254 |
|
|
//tc_cdiv_set_divider(RX_DIVIDER);
|
255 |
e2e37bea
|
henryk
|
tc_cdiv_sync_reset();
|
256 |
|
|
}
|
257 |
ad5b96d5
|
henryk
|
}
|
258 |
|
|
|
259 |
6be69f0d
|
henryk
|
int iso14443_layer2a_init(u_int8_t enable_fast_receive)
|
260 |
|
|
{
|
261 |
|
|
pll_init();
|
262 |
|
|
|
263 |
|
|
tc_cdiv_init();
|
264 |
|
|
tc_fdt_init();
|
265 |
|
|
|
266 |
625ffcb3
|
henryk
|
tc_cdiv_sync_init();
|
267 |
|
|
tc_cdiv_sync_enable();
|
268 |
|
|
|
269 |
6304718e
|
henryk
|
clock_switch_init();
|
270 |
6be69f0d
|
henryk
|
load_mod_init();
|
271 |
|
|
|
272 |
|
|
iso14443_set_fast_receive(enable_fast_receive);
|
273 |
ad5b96d5
|
henryk
|
pio_irq_init_once();
|
274 |
|
|
|
275 |
e2e37bea
|
henryk
|
ssc = ssc_open(0, 1, SSC_MODE_14443A, iso14443_ssc_callback);
|
276 |
ad5b96d5
|
henryk
|
if(ssc == NULL)
|
277 |
|
|
return -EIO;
|
278 |
|
|
|
279 |
af156e10
|
henryk
|
int pauses_count;
|
280 |
|
|
if(OPENPICC->features.clock_switching) {
|
281 |
|
|
clock_switch(CLOCK_SELECT_CARRIER);
|
282 |
|
|
pauses_count = 0;
|
283 |
|
|
} else {
|
284 |
|
|
if(OPENPICC->default_clock == CLOCK_SELECT_CARRIER) {
|
285 |
|
|
pauses_count = 0;
|
286 |
|
|
} else {
|
287 |
|
|
return -ENOTSUP;
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
if(tc_recv_init(&th, pauses_count, iso14443_tc_recv_callback) < 0) {
|
291 |
e2e37bea
|
henryk
|
ssc_close(ssc);
|
292 |
|
|
return -EIO;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
d3bab6e9
|
henryk
|
load_mod_level(3);
|
296 |
ad5b96d5
|
henryk
|
|
297 |
6be69f0d
|
henryk
|
return 0;
|
298 |
|
|
}
|