altos: Software implemenation of CC1111 radio encoding
[fw/altos] / src / drivers / 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 void
22 ao_fec_dump_bytes(uint8_t *bytes, uint8_t len, char *name)
23 {
24         uint8_t i;
25
26         printf ("%s (%d):", name, len);
27         for (i = 0; i < len; i++) {
28                 if ((i & 7) == 0)
29                         printf ("\n\t%02x:", i);
30                 printf(" %02x", bytes[i]);
31         }
32         printf ("\n");
33 }
34
35 static uint16_t inline
36 crc_byte(uint8_t byte, uint16_t crc)
37 {
38         uint8_t bit;
39
40         for (bit = 0; bit < 8; bit++) {
41                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
42                         crc = (crc << 1) ^ 0x8005;
43                 else
44                         crc = (crc << 1);
45                 byte <<= 1;
46         }
47         return crc;
48 }
49
50 uint16_t
51 ao_fec_crc(uint8_t *bytes, uint8_t len)
52 {
53         uint16_t        crc = AO_FEC_CRC_INIT;
54
55         while (len--)
56                 crc = crc_byte(*bytes++, crc);
57         return crc;
58 }
59
60 uint8_t
61 ao_fec_prepare(uint8_t *in, uint8_t len, uint8_t *out)
62 {
63         uint16_t        crc = ao_fec_crc (in, len);
64         uint8_t         i;
65         uint8_t         num_fec;
66
67         /* Copy data */
68         for (i = 0; i < len; i++)
69                 out[i] = in[i];
70
71         /* Append CRC */
72         out[i++] = crc >> 8;
73         out[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                 out[i++] = AO_FEC_TRELLIS_TERMINATOR;
79         return i;
80 }
81
82 static const uint8_t whiten[] = {
83 #include "ao_whiten.h"
84 };
85
86 uint8_t
87 ao_fec_whiten(uint8_t *in, uint8_t len, uint8_t *out)
88 {
89         const uint8_t   *w = whiten;
90
91         while (len--)
92                 *out++ = *in++ ^ *w++;
93 }
94
95 static const uint8_t ao_fec_encode_table[16] = {
96         0, 3, 1, 2,
97         3, 0, 2, 1,
98         3, 0, 2, 1,
99         0, 3, 1, 2
100 };
101
102 uint8_t
103 ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out)
104 {
105         uint16_t        fec = 0, output;
106         uint8_t         byte, bit;
107
108         for (byte = 0; byte < len; byte++) {
109                 fec = (fec & 0x700) | in[byte];
110                 output = 0;
111                 for (bit = 0; bit < 8; bit++) {
112                         output = output << 2 | ao_fec_encode_table[fec >> 7];
113                         fec = (fec << 1) & 0x7ff;
114                 }
115                 out[byte * 2] = output >> 8;
116                 out[byte * 2 + 1] = output;
117         }
118         return len * 2;
119 }
120
121 uint8_t
122 ao_fec_interleave(uint8_t *in, uint8_t len, uint8_t *out)
123 {
124         uint8_t i, j;
125
126         for (i = 0; i < len; i += 4) {
127                 uint32_t        interleaved = 0;
128
129                 for (j = 0; j < 4 * 4; j++) {
130                         interleaved <<= 2;
131                         interleaved |= (in[i + (~j & 0x3)] >> (2 * ((j & 0xc) >> 2))) & 0x03;
132                 }
133                 out[i+0] = interleaved >> 24;
134                 out[i+1] = interleaved >> 16;
135                 out[i+2] = interleaved >> 8;
136                 out[i+3] = interleaved;
137         }
138         return len;
139 }