nr_crc_byte.c 6.65 KB
Newer Older
Guy De Souza's avatar
Guy De Souza committed
1
2
3
4
5
6
7
8
9
#include "PHY/CODING/nrPolar_tools/nr_polar_defs.h"



/*********************************************************

crc table initialization

*********************************************************/
Guy De Souza's avatar
Guy De Souza committed
10
/*static unsigned int      crc24cTable[256];
Guy De Souza's avatar
Guy De Souza committed
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
static unsigned short    crc11Table[256];
static unsigned char     crc6Table[256];


unsigned int crc24c (unsigned char * inptr, int bitlen)
{

  int octetlen, resbit;
  unsigned int  crc = 0;
  octetlen = bitlen / 8;
  resbit = (bitlen % 8);

  while (octetlen-- > 0) {
    crc = (crc << 8) ^ crc24cTable[(*inptr++) ^ (crc >> 24)];
  }

  if (resbit > 0)
    crc = (crc << resbit) ^ crc24cTable[((*inptr) >> (8 - resbit)) ^ (crc >> (32 - resbit))];

  return crc;
}



unsigned int crc11 (unsigned char * inptr, int bitlen)
{
  int octetlen, resbit;
  unsigned int             crc = 0;
Guy De Souza's avatar
Guy De Souza committed
39
  octetlen = bitlen / 8;        // Change in octets
Guy De Souza's avatar
Guy De Souza committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  resbit = (bitlen % 8);

  while (octetlen-- > 0) {
    crc = (crc << 8) ^ (crc11Table[(*inptr++) ^ (crc >> 24)] << 16);
  }

  if (resbit > 0)
    crc = (crc << resbit) ^ (crc11Table[((*inptr) >> (8 - resbit)) ^ (crc >> (32 - resbit))] << 16);

  return crc;
}

unsigned int crc6 (unsigned char * inptr, int bitlen)
{
  int octetlen, resbit;
  unsigned int crc = 0;
Guy De Souza's avatar
Guy De Souza committed
56
  octetlen = bitlen / 8;        // Change in octets
Guy De Souza's avatar
Guy De Souza committed
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  resbit = (bitlen % 8);

  while (octetlen-- > 0) {
    crc = crc6Table[(*inptr++) ^ (crc >> 24)] << 24;
  }

  if (resbit > 0)
    crc = (crc << resbit) ^ (crc6Table[((*inptr) >> (8 - resbit)) ^ (crc >> (32 - resbit))] << 24);

  return crc;
}

uint8_t check_crc(uint8_t *decoded_bytes, uint16_t len, uint8_t crc_type)
{
  uint8_t crc_len,temp;
  uint16_t oldcrc,crc;

  switch (crc_type) {

  case 0: //CRC24_A:
  case 1: //CRC24_B:
    crc_len=3;
    break;

  case 2: //CRC16:
    crc_len=2;
    break;

  case 3: //CRC8:
    crc_len=1;
    break;

  default:
    crc_len=3;
  }

  // check the CRC
  oldcrc= *((unsigned int *)(&decoded_bytes[(len>>3)-crc_len]));

      switch (crc_type) {

      case 0: //CRC24_A:
        oldcrc&=0x00ffffff;
        crc = crc24a(decoded_bytes,len-24)>>8;
        temp=((uint8_t *)&crc)[2];
        ((uint8_t *)&crc)[2] = ((uint8_t *)&crc)[0];
        ((uint8_t *)&crc)[0] = temp;
        break;

      case 1: //CRC24_B:
        oldcrc&=0x00ffffff;
        crc = crc24b(decoded_bytes,len-24)>>8;
        temp=((uint8_t *)&crc)[2];
        ((uint8_t *)&crc)[2] = ((uint8_t *)&crc)[0];
        ((uint8_t *)&crc)[0] = temp;
        break;

      case 2: //CRC16:
        oldcrc&=0x0000ffff;
        crc = crc16(decoded_bytes,len-16)>>16;
        break;

      case 3: //CRC8:
        oldcrc&=0x000000ff;
        crc = crc6(decoded_bytes,len-8)>>24;
        break;

      default:
        printf("FATAL: Unknown CRC\n");
        return(255);
        break;
      }

      printf("old CRC %x, CRC %x \n",oldcrc,crc);
      return (crc == oldcrc); 
Guy De Souza's avatar
Guy De Souza committed
132
}*/
Guy De Souza's avatar
Guy De Souza committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

uint8_t **crc24c_generator_matrix(uint16_t payloadSizeBits){

	uint8_t crcPolynomialPattern[25] = {1,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,0,0,0,1,0,1,1,1};
	// 1011 0010 1011 0001 0001 0111 D^24 + D^23 + D^21 + D^20 + D^17 + D^15 + D^13 + D^12 + D^8 + D^4 + D^2 + D + 1
	uint8_t crcPolynomialSize = 24;// 24 because crc24c
	uint8_t temp1[crcPolynomialSize], temp2[crcPolynomialSize];

	uint8_t **crc_generator_matrix = malloc(payloadSizeBits * sizeof(uint8_t *));
	if (crc_generator_matrix)
	{
	  for (int i = 0; i < payloadSizeBits; i++)
	  {
		  crc_generator_matrix[i] = malloc(crcPolynomialSize * sizeof(uint8_t));
	  }
	}

	for (int i = 0; i < crcPolynomialSize; i++) crc_generator_matrix[payloadSizeBits-1][i]=crcPolynomialPattern[i+1];

	for (int i = payloadSizeBits-2; i >= 0; i--){

		for (int j = 0; j < crcPolynomialSize-1; j++) temp1[j]=crc_generator_matrix[i+1][j+1];
		temp1[crcPolynomialSize-1]=0;

		for (int j = 0; j < crcPolynomialSize; j++) temp2[j]=crc_generator_matrix[i+1][0]*crcPolynomialPattern[j+1];

		for (int j = 0; j < crcPolynomialSize; j++){
			if(temp1[j]+temp2[j] == 1){
				crc_generator_matrix[i][j]=1;
			} else {
				crc_generator_matrix[i][j]=0;
			}
		}

	}

	return crc_generator_matrix;
}

uint8_t **crc11_generator_matrix(uint16_t payloadSizeBits){

	uint8_t crcPolynomialPattern[12] = {1,1,1,0,0,0,1,0,0,0,0,1};
	// 1110 0010 0001 D^11 + D^10 + D^9 + D^5 + 1
	uint8_t crcPolynomialSize = 11;
	uint8_t temp1[crcPolynomialSize], temp2[crcPolynomialSize];

	uint8_t **crc_generator_matrix = malloc(payloadSizeBits * sizeof(uint8_t *));
	if (crc_generator_matrix)
	{
	  for (int i = 0; i < payloadSizeBits; i++)
	  {
		  crc_generator_matrix[i] = malloc(crcPolynomialSize * sizeof(uint8_t));
	  }
	}

	for (int i = 0; i < crcPolynomialSize; i++) crc_generator_matrix[payloadSizeBits-1][i]=crcPolynomialPattern[i+1];

	for (int i = payloadSizeBits-2; i >= 0; i--){

		for (int j = 0; j < crcPolynomialSize-1; j++) temp1[j]=crc_generator_matrix[i+1][j+1];
		temp1[crcPolynomialSize-1]=0;

		for (int j = 0; j < crcPolynomialSize; j++) temp2[j]=crc_generator_matrix[i+1][0]*crcPolynomialPattern[j+1];

		for (int j = 0; j < crcPolynomialSize; j++){
			if(temp1[j]+temp2[j] == 1){
				crc_generator_matrix[i][j]=1;
			} else {
				crc_generator_matrix[i][j]=0;
			}
		}

	}

	return crc_generator_matrix;
}

uint8_t **crc6_generator_matrix(uint16_t payloadSizeBits){

	uint8_t crcPolynomialPattern[7] = {1,1,0,0,0,0,1};
	// 0110 0001 D^6 + D^5 + 1
	uint8_t crcPolynomialSize = 6;
	uint8_t temp1[crcPolynomialSize], temp2[crcPolynomialSize];

	uint8_t **crc_generator_matrix = malloc(payloadSizeBits * sizeof(uint8_t *));
	if (crc_generator_matrix)
	{
	  for (int i = 0; i < payloadSizeBits; i++)
	  {
		  crc_generator_matrix[i] = malloc(crcPolynomialSize * sizeof(uint8_t));
	  }
	}

	for (int i = 0; i < crcPolynomialSize; i++) crc_generator_matrix[payloadSizeBits-1][i]=crcPolynomialPattern[i+1];

	for (int i = payloadSizeBits-2; i >= 0; i--){

		for (int j = 0; j < crcPolynomialSize-1; j++) temp1[j]=crc_generator_matrix[i+1][j+1];
		temp1[crcPolynomialSize-1]=0;

		for (int j = 0; j < crcPolynomialSize; j++) temp2[j]=crc_generator_matrix[i+1][0]*crcPolynomialPattern[j+1];

		for (int j = 0; j < crcPolynomialSize; j++){
			if(temp1[j]+temp2[j] == 1){
				crc_generator_matrix[i][j]=1;
			} else {
				crc_generator_matrix[i][j]=0;
			}
		}

	}

	return crc_generator_matrix;
}


#ifdef DEBUG_CRC
/*******************************************************************/
/**   Test code
/*******************************************************************/

#include <stdio.h>

main()
{
  unsigned char test[] = "Thebigredfox";
  crcTableInit();
260
261
  //printf("%x\n", crcbit(test, sizeof(test) - 1, poly24a));
  //printf("%x\n", crc24a(test, (sizeof(test) - 1)*8));
Guy De Souza's avatar
Guy De Souza committed
262
263
264
265
266
  printf("%x\n", crcbit(test, sizeof(test) - 1, poly6));
  printf("%x\n", crc6(test, (sizeof(test) - 1)*8));
}
#endif