1
|
/***************************************************************
|
2
|
*
|
3
|
* OpenPICC - ISO 14443 Layer 2 Type A T/C based receiver code
|
4
|
* Implements a receiver using FDT Timer/Counter (TC2) and the
|
5
|
* FIQ to measure the number of carrier cycles between modulation
|
6
|
* pauses.
|
7
|
*
|
8
|
* The timing measurements are given to the differential miller
|
9
|
* decoder on the fly to interleave reception and decoding. This
|
10
|
* means two things: a) The CPU will be held in an IRQ handler
|
11
|
* with IRQs disabled for the time of reception and b) The frame
|
12
|
* will already have been fully decoded to a iso14443_frame
|
13
|
* structure when reception ends.
|
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 <openpicc.h>
|
36
|
#include <errno.h>
|
37
|
#include <string.h>
|
38
|
|
39
|
#include <task.h>
|
40
|
#include <queue.h>
|
41
|
|
42
|
#include "tc_recv.h"
|
43
|
|
44
|
#include "iso14443a_diffmiller.h"
|
45
|
#include "usb_print.h"
|
46
|
#include "pio_irq.h"
|
47
|
#include "led.h"
|
48
|
#include "cmd.h"
|
49
|
|
50
|
struct tc_recv_handle {
|
51
|
u_int8_t initialized;
|
52
|
u_int8_t pauses_count;
|
53
|
struct diffmiller_state *decoder;
|
54
|
int current, next;
|
55
|
tc_recv_callback_t callback;
|
56
|
iso14443_frame *current_frame;
|
57
|
xQueueHandle rx_queue;
|
58
|
};
|
59
|
|
60
|
static struct tc_recv_handle _tc;
|
61
|
|
62
|
#define BUFSIZE 1024
|
63
|
typedef struct {
|
64
|
u_int32_t count;
|
65
|
u_int32_t data[BUFSIZE];
|
66
|
} fiq_buffer_t;
|
67
|
fiq_buffer_t fiq_buffers[2];
|
68
|
|
69
|
fiq_buffer_t *tc_sniffer_next_buffer_for_fiq = 0;
|
70
|
|
71
|
iso14443_frame rx_frames[TC_RECV_NUMBER_OF_FRAME_BUFFERS];
|
72
|
|
73
|
/* The standard defines EOF as a logical 0 followed by 128 carrier cycles without modulation.
|
74
|
* That means that the frame end is either 20+128+128 carrier cycles after the end of the
|
75
|
* last modulation (if there was a 1 bit before the EOF) or 20+64+128 carrier cycles after
|
76
|
* the last modulation. So the correct REAL_FRAME_END setting would be something like
|
77
|
* 276. However, we can detect that the last bit period (that without modulation) is not a
|
78
|
* valid bit much earlier: if the last data bit was 1 there are (ca.) 20 cycles till the start
|
79
|
* of the EOF. Then there are 128 cycles without modulation. The next bit (were it not part of
|
80
|
* the EOF) would have to be either sequence X (for a 1 bit) or sequence Z (for a 0 bit):
|
81
|
* If it were a 0 bit there would be modulation right away, if it were a 1 bit there would be
|
82
|
* modulation after 64 cycles. So the maximum valid time without modulation that is not signalling
|
83
|
* and EOF is 20+128+64. Define REAL_FRAME_END as that value (plus 20 cycles error margin).
|
84
|
*/
|
85
|
#define REAL_FRAME_END (20+128+64+20)
|
86
|
|
87
|
static int tc_recv_buffer_overruns = 0;
|
88
|
|
89
|
static inline iso14443_frame *get_frame_buffer(tc_recv_handle_t th)
|
90
|
{
|
91
|
if(th->current_frame) return th->current_frame;
|
92
|
unsigned int i; iso14443_frame *result;
|
93
|
for(i=0; i<sizeof(rx_frames)/sizeof(rx_frames[0]); i++) {
|
94
|
if(rx_frames[i].state == FRAME_FREE) {
|
95
|
result = &rx_frames[i];
|
96
|
result->state = FRAME_PENDING;
|
97
|
th->current_frame = result;
|
98
|
return result;
|
99
|
}
|
100
|
}
|
101
|
tc_recv_buffer_overruns++;
|
102
|
return NULL;
|
103
|
}
|
104
|
|
105
|
static portBASE_TYPE handle_frame(iso14443_frame *frame, portBASE_TYPE task_woken)
|
106
|
{
|
107
|
if(_tc.callback) _tc.callback(TC_RECV_CALLBACK_RX_FRAME_ENDED, frame);
|
108
|
if(frame->state != FRAME_FREE) {
|
109
|
task_woken = xQueueSendFromISR(_tc.rx_queue, &frame, task_woken);
|
110
|
}
|
111
|
_tc.current_frame = NULL;
|
112
|
#ifdef PRINT_PERFORMANCE
|
113
|
int old=usb_print_set_default_flush(0);
|
114
|
iso14443a_diffmiller_print_performance(_tc.decoder);
|
115
|
usb_print_set_default_flush(old);
|
116
|
#endif
|
117
|
return task_woken;
|
118
|
}
|
119
|
|
120
|
static portBASE_TYPE handle_buffer(u_int32_t data[], unsigned int count, portBASE_TYPE task_woken)
|
121
|
{
|
122
|
unsigned int offset = 0;
|
123
|
while(offset < count) {
|
124
|
iso14443_frame *rx_frame = get_frame_buffer(&_tc);
|
125
|
if(rx_frame == NULL) return task_woken;
|
126
|
int ret = iso14443a_decode_diffmiller(_tc.decoder, rx_frame, data, &offset, count);
|
127
|
if(ret == 0) {
|
128
|
task_woken = handle_frame(rx_frame, task_woken);
|
129
|
}
|
130
|
}
|
131
|
return task_woken;
|
132
|
}
|
133
|
|
134
|
static inline portBASE_TYPE flush_buffer(fiq_buffer_t *buffer, portBASE_TYPE task_woken)
|
135
|
{
|
136
|
if(buffer->count > 0) {
|
137
|
if(buffer->count >= BUFSIZE) {
|
138
|
usb_print_string_f("Warning: Possible buffer overrun detected\n\r",0);
|
139
|
//overruns++;
|
140
|
}
|
141
|
buffer->count = MIN(buffer->count, BUFSIZE);
|
142
|
task_woken = handle_buffer(buffer->data, buffer->count, task_woken);
|
143
|
buffer->count = 0;
|
144
|
}
|
145
|
return task_woken;
|
146
|
}
|
147
|
|
148
|
#define NEXT_BUFFER(a) ((a+1)%(sizeof(fiq_buffers)/sizeof(fiq_buffers[0])))
|
149
|
|
150
|
static portBASE_TYPE switch_buffers(portBASE_TYPE task_woken)
|
151
|
{
|
152
|
_tc.next = NEXT_BUFFER(_tc.current);
|
153
|
task_woken = flush_buffer( &fiq_buffers[_tc.next] , task_woken);
|
154
|
|
155
|
tc_sniffer_next_buffer_for_fiq = &fiq_buffers[_tc.current=_tc.next];
|
156
|
return task_woken;
|
157
|
}
|
158
|
|
159
|
static portBASE_TYPE tc_recv_irq(u_int32_t pio, portBASE_TYPE task_woken)
|
160
|
{
|
161
|
(void)pio;
|
162
|
/* TODO There should be some emergency exit here to prevent the CPU from
|
163
|
* spinning in the IRQ for excessive amounts of time. (Maximum transmission
|
164
|
* time for 256 Byte frame is something like 21ms.)
|
165
|
*/
|
166
|
while(*AT91C_TC2_CV <= REAL_FRAME_END ||
|
167
|
fiq_buffers[NEXT_BUFFER(_tc.current)].count > 0 ||
|
168
|
fiq_buffers[_tc.current].count > 0)
|
169
|
task_woken = switch_buffers(task_woken);
|
170
|
|
171
|
if(*AT91C_TC2_CV > REAL_FRAME_END) {
|
172
|
iso14443_frame *rx_frame = get_frame_buffer(&_tc);
|
173
|
if(rx_frame == NULL) return task_woken;
|
174
|
int ret = iso14443a_diffmiller_assert_frame_ended(_tc.decoder, rx_frame);
|
175
|
if(ret == 0) {
|
176
|
task_woken = handle_frame(rx_frame, task_woken);
|
177
|
}
|
178
|
}
|
179
|
return task_woken;
|
180
|
}
|
181
|
|
182
|
|
183
|
int tc_recv_init(tc_recv_handle_t *_th, int pauses_count, tc_recv_callback_t callback)
|
184
|
{
|
185
|
if(_tc.initialized) return -EBUSY;
|
186
|
tc_recv_handle_t th = &_tc;
|
187
|
|
188
|
memset(fiq_buffers, 0, sizeof(fiq_buffers));
|
189
|
th->current = th->next = 0;
|
190
|
|
191
|
memset(rx_frames, 0, sizeof(rx_frames));
|
192
|
th->current_frame = NULL;
|
193
|
|
194
|
if(th->rx_queue == NULL) {
|
195
|
th->rx_queue = xQueueCreate(TC_RECV_NUMBER_OF_FRAME_BUFFERS, sizeof(iso14443_frame*));
|
196
|
if(th->rx_queue == NULL)
|
197
|
return -ENOMEM;
|
198
|
}
|
199
|
|
200
|
th->pauses_count = pauses_count;
|
201
|
th->decoder = iso14443a_init_diffmiller(th->pauses_count);
|
202
|
if(!th->decoder) return -EBUSY;
|
203
|
|
204
|
// The change interrupt is going to be handled by the FIQ and our secondary IRQ handler
|
205
|
AT91F_PIO_CfgInput(AT91C_BASE_PIOA, OPENPICC_SSC_DATA);
|
206
|
if( pio_irq_register(OPENPICC_SSC_DATA, &tc_recv_irq) < 0)
|
207
|
return -EBUSY;
|
208
|
pio_irq_enable(OPENPICC_SSC_DATA);
|
209
|
|
210
|
th->initialized = 1;
|
211
|
*_th = th;
|
212
|
|
213
|
th->callback = callback;
|
214
|
if(th->callback) th->callback(TC_RECV_CALLBACK_SETUP, th);
|
215
|
|
216
|
return 0;
|
217
|
}
|
218
|
|
219
|
int tc_recv_receive(tc_recv_handle_t th, iso14443_frame* *frame, unsigned int timeout)
|
220
|
{
|
221
|
if(th == NULL) return -EINVAL;
|
222
|
if(!th->initialized) return -EINVAL;
|
223
|
|
224
|
if(xQueueReceive(th->rx_queue, frame, timeout)){
|
225
|
if(*frame != NULL) return 0;
|
226
|
else return -EINTR;
|
227
|
}
|
228
|
|
229
|
return -ETIMEDOUT;
|
230
|
}
|