1
|
/* NRZ-L decoder implementation for OpenPICC
|
2
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
3
|
*
|
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.
|
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
|
* 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
|
24
|
*
|
25
|
* NRZ-L coding with logic level
|
26
|
*
|
27
|
* logic 1: carrier high field amplitude (no modulation)
|
28
|
* logic 0: carrier low field amplitude
|
29
|
*
|
30
|
* Character transmission format:
|
31
|
* start bit: logic 0
|
32
|
* data: eight bits, lsb first
|
33
|
* stop bit logic 1
|
34
|
*
|
35
|
* Frame Format:
|
36
|
*
|
37
|
* SOF char [EGT char, ...] EOF
|
38
|
*
|
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
|
42
|
*
|
43
|
*
|
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 :(
|
52
|
*
|
53
|
*/
|
54
|
|
55
|
#include <errno.h>
|
56
|
#include <sys/types.h>
|
57
|
|
58
|
#include "openpicc.h"
|
59
|
#include "dbgu.h"
|
60
|
#include "decoder.h"
|
61
|
|
62
|
/* currently this code will only work with oversampling_rate == 1 */
|
63
|
#define OVERSAMPLING_RATE 1
|
64
|
|
65
|
static u_int32_t get_next_bytesample(struct decoder_state *st,
|
66
|
u_int8_t *parity_sample)
|
67
|
{
|
68
|
u_int32_t ret = 0;
|
69
|
u_int8_t bits_per_sampled_char = st->algo->bits_per_sampled_char;
|
70
|
u_int8_t bytesample_mask = st->algo->bytesample_mask;
|
71
|
|
72
|
/* FIXME: shift start and stop bit into parity_sample and just
|
73
|
* return plain 8-bit data word */
|
74
|
(void)parity_sample;
|
75
|
|
76
|
/* first part of 10-databit bytesample */
|
77
|
ret = (*(st->buf32) >> st->bit_ofs) & bytesample_mask;
|
78
|
|
79
|
if (st->bit_ofs > 32 - bits_per_sampled_char) {
|
80
|
/* second half of 10-databit bytesample */
|
81
|
st->buf32++;
|
82
|
ret |= (*(st->buf32) << (32 - st->bit_ofs));
|
83
|
}
|
84
|
st->bit_ofs = (st->bit_ofs + bits_per_sampled_char) % 32;
|
85
|
|
86
|
return ret & bytesample_mask;
|
87
|
}
|
88
|
|
89
|
static int nrzl_decode_sample(const u_int32_t sample, u_int8_t *data)
|
90
|
{
|
91
|
*data = (sample >> 1) & 0xff;
|
92
|
|
93
|
if (!(sample & 0x01)) {
|
94
|
DEBUGPCRF("invalid start bit 0!");
|
95
|
return -EIO;
|
96
|
}
|
97
|
if (sample & 0x20) {
|
98
|
DEBUGPCRF("invalid stop bit 1!");
|
99
|
return -EIO;
|
100
|
}
|
101
|
|
102
|
return 0;
|
103
|
}
|
104
|
|
105
|
struct decoder_algo nrzl_decoder = {
|
106
|
.oversampling_rate = OVERSAMPLING_RATE,
|
107
|
.bits_per_sampled_char = 10 * OVERSAMPLING_RATE,
|
108
|
.bytesample_mask = 0x3ff,
|
109
|
.decode_sample = &nrzl_decode_sample,
|
110
|
.get_next_bytesample = &get_next_bytesample,
|
111
|
};
|