summaryrefslogtreecommitdiff
path: root/firmware/src/picc/decoder_nrzl.c
blob: 8307bcd6e92fb0ce7197640f09ea5d034d792b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* NRZ-L decoder implementation for OpenPICC
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * speed(kbps)	106	212	424	848
 * etu		128/fc	64/fc	32/fc	16/fc
 * etu(usec)	9.4	4.7	2.35	1.18
 *
 * NRZ-L coding with logic level
 *
 * logic 1:	carrier high field amplitude (no modulation)
 * logic 0:	carrier low field amplitude
 *
 * Character transmission format:
 *	start bit:	logic 0
 *	data:		eight bits, lsb first
 *	stop bit	logic 1
 *
 * Frame Format:
 *
 * 		SOF char [EGT char, ...] EOF
 *
 * 	SOF: falling edge, 10..11 etu '0', rising edge in 1etu, 2..3etu '1'
 *	EGT: between 0 and 57uS 
 *	EOF: falling edge, 10..11 etu '0', rising edge in 1etu
 *
 *
 * Sampling
 * - sample once per bit clock, exactly in the middle of it
 * - synchronize CARRIER_DIV TC0 to first falling edge
 * - Configure CARRIER_DIV RA compare (rising edge) to be at
 *   etu/2 carrier clocks.
 * - problem: SOF 12..14etu length, therefore we cannot specify
 *            SOF as full start condition and then sample with 10bit
 *            frames :(
 * 
 */

#include <errno.h>
#include <sys/types.h>
#include <os/dbgu.h>
#include <picc/decoder.h>

/* currently this code will only work with oversampling_rate == 1 */
#define OVERSAMPLING_RATE 1

static uint32_t get_next_bytesample(struct decoder_state *st,
				     uint8_t *parity_sample)
{
	uint32_t ret = 0;
	uint8_t bits_per_sampled_char = st->algo->bits_per_sampled_char;
	uint8_t bytesample_mask = st->algo->bytesample_mask;

	/* FIXME: shift start and stop bit into parity_sample and just
	 * return plain 8-bit data word */
	
	/* first part of 10-databit bytesample */
	ret = (*(st->buf32) >> st->bit_ofs) & bytesample_mask;

	if (st->bit_ofs > 32 - bits_per_sampled_char) {
		/* second half of 10-databit bytesample */
		st->buf32++;
		ret |= (*(st->buf32) << (32 - st->bit_ofs));
	}
	st->bit_ofs = (st->bit_ofs + bits_per_sampled_char) % 32;

	return ret & bytesample_mask;
}

static int nrzl_decode_sample(const uint32_t sample, uint8_t *data)
{
	*data = (sample >> 1) & 0xff;

	if (!(sample & 0x01)) {
		DEBUGPCRF("invalid start bit 0!");
		return -EIO;
	}
	if (sample & 0x20) {
		DEBUGPCRF("invalid stop bit 1!");
		return -EIO;
	}

	return 0;
}

static struct decoder_algo nrzl_decoder = {
	.oversampling_rate = OVERSAMPLING_RATE,
	.bits_per_sampled_char = 10 * OVERSAMPLING_RATE,
	.bytesample_mask = 0x3ff,
	.decode_sample = &nrzl_decode_sample,
	.get_next_bytesample = &get_next_bytesample,
};
personal git repositories of Harald Welte. Your mileage may vary