Initial commit
[kk_librfid.git] / firmware / src / picc / decoder_nrzl.c
blob136a9c80697bde8befe9e8b8ac57afaae31a6ccc
1 /* NRZ-L decoder implementation 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
21 * speed(kbps) 106 212 424 848
22 * etu 128/fc 64/fc 32/fc 16/fc
23 * etu(usec) 9.4 4.7 2.35 1.18
25 * NRZ-L coding with logic level
27 * logic 1: carrier high field amplitude (no modulation)
28 * logic 0: carrier low field amplitude
30 * Character transmission format:
31 * start bit: logic 0
32 * data: eight bits, lsb first
33 * stop bit logic 1
35 * Frame Format:
37 * SOF char [EGT char, ...] EOF
39 * SOF: falling edge, 10..11 etu '0', rising edge in 1etu, 2..3etu '1'
40 * EGT: between 0 and 57uS
41 * EOF: falling edge, 10..11 etu '0', rising edge in 1etu
44 * Sampling
45 * - sample once per bit clock, exactly in the middle of it
46 * - synchronize CARRIER_DIV TC0 to first falling edge
47 * - Configure CARRIER_DIV RA compare (rising edge) to be at
48 * etu/2 carrier clocks.
49 * - problem: SOF 12..14etu length, therefore we cannot specify
50 * SOF as full start condition and then sample with 10bit
51 * frames :(
55 #include <errno.h>
56 #include <sys/types.h>
57 #include <os/dbgu.h>
58 #include <picc/decoder.h>
60 /* currently this code will only work with oversampling_rate == 1 */
61 #define OVERSAMPLING_RATE 1
63 static u_int32_t get_next_bytesample(struct decoder_state *st,
64 u_int8_t *parity_sample)
66 u_int32_t ret = 0;
67 u_int8_t bits_per_sampled_char = st->algo->bits_per_sampled_char;
68 u_int8_t bytesample_mask = st->algo->bytesample_mask;
70 /* FIXME: shift start and stop bit into parity_sample and just
71 * return plain 8-bit data word */
73 /* first part of 10-databit bytesample */
74 ret = (*(st->buf32) >> st->bit_ofs) & bytesample_mask;
76 if (st->bit_ofs > 32 - bits_per_sampled_char) {
77 /* second half of 10-databit bytesample */
78 st->buf32++;
79 ret |= (*(st->buf32) << (32 - st->bit_ofs));
81 st->bit_ofs = (st->bit_ofs + bits_per_sampled_char) % 32;
83 return ret & bytesample_mask;
86 static int nrzl_decode_sample(const u_int32_t sample, u_int8_t *data)
88 *data = (sample >> 1) & 0xff;
90 if (!(sample & 0x01)) {
91 DEBUGPCRF("invalid start bit 0!");
92 return -EIO;
94 if (sample & 0x20) {
95 DEBUGPCRF("invalid stop bit 1!");
96 return -EIO;
99 return 0;
102 static struct decoder_algo nrzl_decoder = {
103 .oversampling_rate = OVERSAMPLING_RATE,
104 .bits_per_sampled_char = 10 * OVERSAMPLING_RATE,
105 .bytesample_mask = 0x3ff,
106 .decode_sample = &nrzl_decode_sample,
107 .get_next_bytesample = &get_next_bytesample,