altos: Rename 'core' to 'kernel'
[fw/altos] / src / kernel / ao_fec_tx.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao_fec.h>
19 #include <stdio.h>
20
21 #if AO_FEC_DEBUG
22 void
23 ao_fec_dump_bytes(const uint8_t *bytes, uint16_t len, const char *name)
24 {
25         uint16_t        i;
26
27         printf ("%s (%d):", name, len);
28         for (i = 0; i < len; i++) {
29                 if ((i & 7) == 0)
30                         printf ("\n\t%02x:", i);
31                 printf(" %02x", bytes[i]);
32         }
33         printf ("\n");
34 }
35 #endif
36
37 uint16_t
38 ao_fec_crc(const uint8_t *bytes, uint8_t len)
39 {
40         uint16_t        crc = AO_FEC_CRC_INIT;
41
42         while (len--)
43                 crc = ao_fec_crc_byte(*bytes++, crc);
44         return crc;
45 }
46
47 /*
48  * len is the length of the data; the crc will be
49  * the fist two bytes after that
50  */
51
52 uint8_t
53 ao_fec_check_crc(const uint8_t *bytes, uint8_t len)
54 {
55         uint16_t        computed_crc = ao_fec_crc(bytes, len);
56         uint16_t        received_crc = (bytes[len] << 8) | (bytes[len+1]);
57
58         return computed_crc == received_crc;
59 }
60
61 /*
62  * Compute CRC and trellis-terminator/interleave-pad bytes
63  */
64 static uint8_t
65 ao_fec_prepare(const uint8_t *in, uint8_t len, uint8_t *extra)
66 {
67         uint16_t        crc = ao_fec_crc (in, len);
68         uint8_t         i = 0;
69         uint8_t         num_fec;
70
71         /* Append CRC */
72         extra[i++] = crc >> 8;
73         extra[i++] = crc;
74
75         /* Append FEC -- 1 byte if odd, two bytes if even */
76         num_fec = 2 - (i & 1);
77         while (num_fec--)
78                 extra[i++] = AO_FEC_TRELLIS_TERMINATOR;
79         return i;
80 }
81
82 const uint8_t ao_fec_whiten_table[] = {
83 #include "ao_whiten.h"
84 };
85
86 static const uint8_t ao_fec_encode_table[16] = {
87 /* next 0  1      state */
88         0, 3,   /* 000 */
89         1, 2,   /* 001 */
90         3, 0,   /* 010 */
91         2, 1,   /* 011 */
92         3, 0,   /* 100 */
93         2, 1,   /* 101 */
94         0, 3,   /* 110 */
95         1, 2    /* 111 */
96 };
97
98 uint8_t
99 ao_fec_encode(const uint8_t *in, uint8_t len, uint8_t *out)
100 {
101         uint8_t         extra[AO_FEC_PREPARE_EXTRA];
102         uint8_t         extra_len;
103         uint32_t        encode, interleave;
104         uint8_t         pair, byte, bit;
105         uint16_t        fec = 0;
106         const uint8_t   *whiten = ao_fec_whiten_table;
107
108         extra_len = ao_fec_prepare(in, len, extra);
109         for (pair = 0; pair < len + extra_len; pair += 2) {
110                 encode = 0;
111                 for (byte = 0; byte < 2; byte++) {
112                         if (pair + byte == len)
113                                 in = extra;
114                         fec |= *in++ ^ *whiten++;
115                         for (bit = 0; bit < 8; bit++) {
116                                 encode = encode << 2 | ao_fec_encode_table[fec >> 7];
117                                 fec = (fec << 1) & 0x7ff;
118                         }
119                 }
120
121                 interleave = 0;
122                 for (bit = 0; bit < 4 * 4; bit++) {
123                         uint8_t byte_shift = (bit & 0x3) << 3;
124                         uint8_t bit_shift = (bit & 0xc) >> 1;
125
126                         interleave = (interleave << 2) | ((encode >> (byte_shift + bit_shift)) & 0x3);
127                 }
128                 *out++ = interleave >> 24;
129                 *out++ = interleave >> 16;
130                 *out++ = interleave >> 8;
131                 *out++ = interleave >> 0;
132         }
133         return (len + extra_len) * 2;
134 }