1 |
32985a29
|
laforge
|
/* ISO14443A Manchester encoder for OpenPICC
|
2 |
548ec55e
|
(no author)
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
3 |
|
|
*
|
4 |
32985a29
|
laforge
|
* This program is free software; you can redistribute it and/or modify
|
5 |
|
|
* it under the terms of the GNU General Public License as published by
|
6 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
7 |
|
|
* (at your option) any later version.
|
8 |
|
|
*
|
9 |
|
|
* This program is distributed in the hope that it will be useful,
|
10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
|
* GNU General Public License for more details.
|
13 |
|
|
*
|
14 |
|
|
* You should have received a copy of the GNU General Public License
|
15 |
|
|
* along with this program; if not, write to the Free Software
|
16 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
17 |
|
|
*
|
18 |
|
|
*/
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
/*
|
22 |
548ec55e
|
(no author)
|
* Definitions for 106kBps, at sampling clock 1695kHz
|
23 |
|
|
*
|
24 |
|
|
* bit sample pattern for one bit cycle
|
25 |
|
|
* MSB first LSB first hex LSB first
|
26 |
|
|
* Sequence D 1010101000000000 0000000001010101 0x0055
|
27 |
|
|
* Sequence E 0000000010101010 0101010100000000 0x5500
|
28 |
|
|
* Sequence F 1010101010101010 0101010101010101 0x5555
|
29 |
|
|
*
|
30 |
|
|
* Logic 1 Sequence D
|
31 |
|
|
* Logic 0 Sequence E
|
32 |
|
|
* SOF Sequence D
|
33 |
|
|
* EOF Sequence F
|
34 |
|
|
*
|
35 |
|
|
* 212/424/848kBps: BPSK.
|
36 |
|
|
*
|
37 |
|
|
* SOF: 32 subcarrier clocks + bit '0'
|
38 |
|
|
*
|
39 |
|
|
* SOF: hex LSB first: 0x55555555 55555555 + bit '0'
|
40 |
|
|
*
|
41 |
|
|
* EOF: even parity of last byte (!)
|
42 |
|
|
*
|
43 |
|
|
*/
|
44 |
|
|
|
45 |
|
|
#define MANCHESTER_SEQ_D 0x0055
|
46 |
|
|
#define MANCHESTER_SEQ_E 0x5500
|
47 |
|
|
#define MANCHESTER_SEQ_F 0x5555
|
48 |
|
|
|
49 |
373c172a
|
Harald Welte
|
static uint32_t manchester_sample_size(uint8_t frame_bytelen)
|
50 |
548ec55e
|
(no author)
|
{
|
51 |
|
|
/* 16 bits (2 bytes) per bit => 16 bytes samples per data byte,
|
52 |
|
|
* plus 16bit (2 bytes) parity per data byte
|
53 |
|
|
* plus 16bit (2 bytes) SOF plus 16bit (2 bytes) EOF */
|
54 |
|
|
return (frame_bytelen*18) + 2 + 2;
|
55 |
|
|
|
56 |
|
|
/* this results in a maximum samples-per-frame size of 4612 bytes
|
57 |
|
|
* for a 256byte frame */
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
struct manch_enc_state {
|
61 |
|
|
const char *data;
|
62 |
|
|
char *samples;
|
63 |
373c172a
|
Harald Welte
|
uint16_t *samples16;
|
64 |
548ec55e
|
(no author)
|
};
|
65 |
|
|
|
66 |
373c172a
|
Harald Welte
|
static void manchester_enc_byte(struct manch_enc_state *mencs, uint8_t data)
|
67 |
548ec55e
|
(no author)
|
{
|
68 |
|
|
int i;
|
69 |
373c172a
|
Harald Welte
|
uint8_t sum_1 = 0;
|
70 |
548ec55e
|
(no author)
|
|
71 |
|
|
/* append 8 sample blobs, one for each bit */
|
72 |
|
|
for (i = 0; i < 8; i++) {
|
73 |
|
|
if (data & (1 << i)) {
|
74 |
|
|
*(mencs->samples16) = MANCHESTER_SEQ_D;
|
75 |
|
|
sum_1++;
|
76 |
|
|
} else {
|
77 |
|
|
*(mencs->samples16) = MANCHESTER_SEQ_E;
|
78 |
|
|
}
|
79 |
|
|
mencs->samples16++
|
80 |
|
|
}
|
81 |
|
|
/* append odd parity */
|
82 |
|
|
if (sum_1 & 0x01)
|
83 |
|
|
*(mencs->samples16) = MANCHESTER_SEQ_E;
|
84 |
|
|
else
|
85 |
|
|
*(mencs->samples16) = MANCHESTER_SEQ_D;
|
86 |
|
|
mencs->samples16++
|
87 |
|
|
}
|
88 |
|
|
|
89 |
373c172a
|
Harald Welte
|
int manchester_encode(char *sample_buf, uint16_t sample_buf_len,
|
90 |
|
|
const char *data, uint8_t data_len)
|
91 |
548ec55e
|
(no author)
|
{
|
92 |
|
|
int i, enc_size;
|
93 |
|
|
struct manch_enc_state mencs
|
94 |
|
|
|
95 |
|
|
enc_size = manchester_sample_size(data_len);
|
96 |
|
|
|
97 |
|
|
if (sample_buf_len < enc_size)
|
98 |
|
|
return -EINVAL;
|
99 |
|
|
|
100 |
|
|
/* SOF */
|
101 |
|
|
*(mencs.samples16++) = MANCHESTER_SEQ_D;
|
102 |
|
|
|
103 |
|
|
for (i = 0; i < data_len; i++)
|
104 |
|
|
manchester_enc_byte(mencs, data[i]);
|
105 |
|
|
|
106 |
|
|
/* EOF */
|
107 |
|
|
*(mencs.samples16++) = MANCHESTER_SEQ_F;
|
108 |
|
|
|
109 |
|
|
return enc_size;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
#define BPSK_SPEED_212
|
113 |
|
|
|
114 |
|
|
|
115 |
373c172a
|
Harald Welte
|
static uint32_t bpsk_sample_size(uint8_t frame_bytelen)
|
116 |
548ec55e
|
(no author)
|
|
117 |
373c172a
|
Harald Welte
|
int bpsk_encode(char *sample_buf, uint16_t sample_buf_len,
|
118 |
|
|
const char *data, uint8_t data_len)
|
119 |
548ec55e
|
(no author)
|
{
|
120 |
|
|
/* burst of 32 sub carrier cycles */
|
121 |
|
|
memset(sample_buf, 0x55, 8);
|
122 |
|
|
|
123 |
|
|
}
|