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
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
{
66 static void manchester_enc_byte(struct manch_enc_state
*mencs
, u_int8_t data
)
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
;
77 *(mencs
->samples16
) = MANCHESTER_SEQ_E
;
81 /* append odd parity */
83 *(mencs
->samples16
) = MANCHESTER_SEQ_E
;
85 *(mencs
->samples16
) = MANCHESTER_SEQ_D
;
89 int manchester_encode(char *sample_buf
, u_int16_t sample_buf_len
,
90 const char *data
, u_int8_t data_len
)
93 struct manch_enc_state mencs
95 enc_size
= manchester_sample_size(data_len
);
97 if (sample_buf_len
< enc_size
)
101 *(mencs
.samples16
++) = MANCHESTER_SEQ_D
;
103 for (i
= 0; i
< data_len
; i
++)
104 manchester_enc_byte(mencs
, data
[i
]);
107 *(mencs
.samples16
++) = MANCHESTER_SEQ_F
;
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);