From 07528df864b704a5e9a088965423f9ce866fdf7c Mon Sep 17 00:00:00 2001 From: agelonch Date: Mon, 12 May 2014 13:20:21 +0200 Subject: [PATCH] Fixed crc issues --- lte/include/lte/fec/crc.h | 1 + lte/lib/fec/src/crc.c | 189 +++++++++++++++++++++++--------- lte/lib/fec/test/CMakeLists.txt | 8 +- lte/lib/fec/test/crc_test.c | 12 +- lte/lib/fec/test/crc_test.h | 11 +- 5 files changed, 159 insertions(+), 62 deletions(-) diff --git a/lte/include/lte/fec/crc.h b/lte/include/lte/fec/crc.h index 5cc24ad23..010122973 100644 --- a/lte/include/lte/fec/crc.h +++ b/lte/include/lte/fec/crc.h @@ -36,6 +36,7 @@ #define LTE_CRC8 0x19B +int init_crc(int lorder, unsigned long polynom); unsigned int crc(unsigned int crc, char *bufptr, int len, int long_crc,unsigned int poly, int paste_word); diff --git a/lte/lib/fec/src/crc.c b/lte/lib/fec/src/crc.c index 6360d3537..3cbd1ad11 100644 --- a/lte/lib/fec/src/crc.c +++ b/lte/lib/fec/src/crc.c @@ -28,63 +28,152 @@ #include #include +#include +#include "lte/utils/pack.h" + +#define _WITHMALLOC unsigned int cword; +char *cdata; +const unsigned long crcinit = 0x00000000; //initial CRC value +const unsigned long crcxor = 0x00000000; //final XOR value +unsigned long crcmask; +unsigned long crchighbit; +unsigned long crctab[256]; + + + -unsigned int icrc1(unsigned int crc, unsigned short onech,int long_crc, - int left_shift,unsigned int poly) -{ - int i; - unsigned int tmp=(unsigned int) (crc ^ (onech << (long_crc >> 1) )); +void gen_crc_table(int lorder, unsigned long poly) { - for (i=0;i>long_crc), - data,long_crc,i,poly)<>long_crc; - } - - ret=cword; - if (paste_word) { - cword<<=32-long_crc; - for (i=0;i>31); - cword<<=1; - } - } - return (ret); + +unsigned long crctable (unsigned char* data, unsigned long length, int lorder) { + + // Polynom lorders of 8, 16, 24 or 32. + unsigned long crc = crcinit; + + while (length--) crc = (crc << 8) ^ crctab[ ((crc >> (lorder-8)) & 0xff) ^ *data++]; + + return((crc^crcxor)&crcmask); } +unsigned long reversecrcbit(unsigned int crc, unsigned int polynom, int lorder, int nbits) { + + unsigned long m, rmask=0x1; + + for(m=0; m>1; + else crc = crc >> 1; + } + return((crc^crcxor)&crcmask); +} + + +int init_crc(int lorder, unsigned long polynom){ + + unsigned long polyhighbit; + + // Compute bit masks for whole CRC and CRC high bit + crcmask = ((((unsigned long)1<<(lorder-1))-1)<<1)|1; + polyhighbit=0xFFFFFFFF ^ (crcmask+1); + crchighbit = (unsigned long)1<<(lorder-1); + + // Eliminate highest bit in polynom word + polynom=polynom & polyhighbit; + + // check parameters + if (lorder < 1 || lorder > 32) { + printf("ERROR, invalid order, it must be between 1..32.\n"); + return(0); + } + if (lorder%8 != 0) { + printf("ERROR, invalid order=%d, it must be 8, 16, 24 or 32.\n", lorder); + return(0); + } + if (polynom != (polynom & crcmask)) { + printf("ERROR, invalid polynom.\n"); + return(0); + } + if (crcinit != (crcinit & crcmask)) { + printf("ERROR, invalid crcinit.\n"); + return(0); + } + if (crcxor != (crcxor & crcmask)) { + printf("ERROR, invalid crcxor.\n"); + return(0); + } + // generate lookup table + gen_crc_table(lorder, polynom); + + return(1); +} + +#define MAX_LENGTH 8192 + +unsigned int crc(unsigned int crc, char *bufptr, int len, + int long_crc, unsigned int poly, int paste_word) { + + int i, len8, res8, a; +#ifdef _WITHMALLOC + char *data0, *pter; + + data0 = (char *)malloc(sizeof(char) * (len+long_crc)*2); + if (!data0) { + perror("malloc ERROR: Allocating memory for data pointer in crc() function"); + return(-1); + } +#endif +#ifndef _WITHMALLOC + char data0[MAX_LENGTH], *pter; + + if((((len+long_crc)>>3) + 1) > MAX_LENGTH){ + printf("ERROR: Not enough memory allocated\n"); + return(-1); + } +#endif + //# Pack bits into bytes + len8=(len>>3); + res8=8-(len - (len8<<3)); + if(res8>0)a=1; + else a=0; + + // Zeroed additional bits + memset((char *)(bufptr+len),0,(32)*sizeof(char)); + + for(i=0; i #include "lte.h" - #include "crc_test.h" -int num_bits = 1000, crc_length = 16; -unsigned int crc_poly = 0x11021; -unsigned int seed = 0; +int num_bits = 5000, crc_length = 24; +unsigned int crc_poly = 0x1864CFB; +unsigned int seed = 1; void usage(char *prog) { @@ -81,7 +80,7 @@ int main(int argc, char **argv) { parse_args(argc, argv); - data = malloc(sizeof(char) * (num_bits+crc_length)); + data = malloc(sizeof(char) * (num_bits+crc_length)*2); if (!data) { perror("malloc"); exit(-1); @@ -97,6 +96,9 @@ int main(int argc, char **argv) { data[i] = rand()%2; } + //Initialize crc params and tables + if(!init_crc(crc_length, crc_poly))exit(0); + // generate CRC word crc_word = crc(0, data, num_bits, crc_length, crc_poly, 1); diff --git a/lte/lib/fec/test/crc_test.h b/lte/lib/fec/test/crc_test.h index 926e79426..164032b7b 100644 --- a/lte/lib/fec/test/crc_test.h +++ b/lte/lib/fec/test/crc_test.h @@ -39,10 +39,15 @@ typedef struct { static expected_word_t expected_words[] = { - {5000, 24, LTE_CRC24A, 1, 0x4D0836}, // LTE CRC24A (36.212 Sec 5.1.1) + /* {5000, 24, LTE_CRC24A, 1, 0x4D0836}, // LTE CRC24A (36.212 Sec 5.1.1) {5000, 24, LTE_CRC24B, 1, 0x9B68F8}, // LTE CRC24B - {5000, 16, LTE_CRC16, 1, 0xBFFA}, // LTE CRC16 - {5000, 8, LTE_CRC8, 1, 0xF8}, // LTE CRC8 + {5000, 16, LTE_CRC16, 1, 0xBFFA}, // LTE CRC16: 0xBFFA + {5000, 8, LTE_CRC8, 1, 0xF8}, // LTE CRC8 0xF8 + */ + {5001, 24, LTE_CRC24A, 1, 0x1C5C97}, // LTE CRC24A (36.212 Sec 5.1.1) + {5001, 24, LTE_CRC24B, 1, 0x36D1F0}, // LTE CRC24B + {5001, 16, LTE_CRC16, 1, 0x7FF4}, // LTE CRC16: 0x7FF4 + {5001, 8, LTE_CRC8, 1, 0xF0}, // LTE CRC8 0xF8 {-1, -1, 0, 0, 0} };