paths changed
[kk_librfid.git] / firmware / src / picc / iso14443a_manchester.c
blobf9eba1ec2d6b2eea74206eaedc9fa6bded00cb6a
1 /* ISO14443A Manchester encoder for OpenPICC
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
4 * 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.
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.
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
22 * Definitions for 106kBps, at sampling clock 1695kHz
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
30 * Logic 1 Sequence D
31 * Logic 0 Sequence E
32 * SOF Sequence D
33 * EOF Sequence F
35 * 212/424/848kBps: BPSK.
37 * SOF: 32 subcarrier clocks + bit '0'
39 * SOF: hex LSB first: 0x55555555 55555555 + bit '0'
41 * EOF: even parity of last byte (!)
45 #define MANCHESTER_SEQ_D 0x0055
46 #define MANCHESTER_SEQ_E 0x5500
47 #define MANCHESTER_SEQ_F 0x5555
49 static u_int32_t manchester_sample_size(u_int8_t frame_bytelen)
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;
56 /* this results in a maximum samples-per-frame size of 4612 bytes
57 * for a 256byte frame */
60 struct manch_enc_state {
61 const char *data;
62 char *samples;
63 u_int16_t *samples16;
66 static void manchester_enc_byte(struct manch_enc_state *mencs, u_int8_t data)
68 int i;
69 u_int8_t sum_1 = 0;
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;
79 mencs->samples16++
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++
89 int manchester_encode(char *sample_buf, u_int16_t sample_buf_len,
90 const char *data, u_int8_t data_len)
92 int i, enc_size;
93 struct manch_enc_state mencs
95 enc_size = manchester_sample_size(data_len);
97 if (sample_buf_len < enc_size)
98 return -EINVAL;
100 /* SOF */
101 *(mencs.samples16++) = MANCHESTER_SEQ_D;
103 for (i = 0; i < data_len; i++)
104 manchester_enc_byte(mencs, data[i]);
106 /* EOF */
107 *(mencs.samples16++) = MANCHESTER_SEQ_F;
109 return enc_size;
112 #define BPSK_SPEED_212
115 static u_int32_t bpsk_sample_size(u_int8_t frame_bytelen)
117 int bpsk_encode(char *sample_buf, u_int16_t sample_buf_len,
118 const char *data, u_int8_t data_len)
120 /* burst of 32 sub carrier cycles */
121 memset(sample_buf, 0x55, 8);