Fixed crc issues

This commit is contained in:
agelonch 2014-05-12 13:20:21 +02:00
parent 587ff2c7f8
commit 07528df864
5 changed files with 154 additions and 57 deletions

View file

@ -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);

View file

@ -28,63 +28,152 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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) ));
for (i=0;i<left_shift;i++) {
if (tmp & (0x1<<(long_crc-1)))
tmp=(tmp<<1)^poly;
else
tmp <<= 1;
}
return tmp;
void gen_crc_table(int lorder, unsigned long poly) {
int i, j, ord=(lorder-8);
unsigned long bit, crc;
for (i=0; i<256; i++) {
crc = ((unsigned long)i)<<ord;
for (j=0; j<8; j++) {
bit = crc & crchighbit;
crc<<= 1;
if (bit) crc^= poly;
}
crctab[i] = crc & crcmask;
}
}
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<nbits; m++){
if((rmask & crc) == 0x01 )crc = (crc ^ polynom)>>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 long_crc, unsigned int poly, int paste_word) {
int i,k;
unsigned int data;
int stop;
unsigned int ret;
int i, len8, res8, a;
#ifdef _WITHMALLOC
char *data0, *pter;
cword=crc;
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;
k=0;
stop=0;
while(!stop) {
data=0;
for (i=0;i<long_crc/2;i++) {
if (bufptr[k] && k<len)
data|=(0x1<<(long_crc/2-1-i));
k++;
if (k==len) {
stop=1;
i++;
break;
}
}
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;
cword=(unsigned int) (icrc1((unsigned int) (cword<<long_crc>>long_crc),
data,long_crc,i,poly)<<long_crc)>>long_crc;
}
// Zeroed additional bits
memset((char *)(bufptr+len),0,(32)*sizeof(char));
ret=cword;
if (paste_word) {
cword<<=32-long_crc;
for (i=0;i<long_crc;i++) {
bufptr[i+len]=((cword&(0x1<<31))>>31);
cword<<=1;
}
}
return (ret);
for(i=0; i<len8+a; i++){
pter=(char *)(bufptr+8*i);
data0[i]=(char)(unpack_bits(&pter, 8)&0xFF);
}
// Calculate CRC
pter=data0;
crc=crctable ((unsigned char *)pter, len8+a, long_crc);
// Reverse CRC res8 positions
if(a==1)crc=reversecrcbit(crc, poly, long_crc, res8);
// Add CRC
pter=(char *)(bufptr+len);
pack_bits(crc, &pter, long_crc);
#ifdef _WITHMALLOC
free(data0);
data0=NULL;
#endif
//Return CRC value
return crc;
}

View file

@ -43,9 +43,9 @@ ADD_TEST(viterbi_1000_4 viterbi_test -n 100 -s 1 -l 1000 -k 7 -t -e 4.5)
ADD_EXECUTABLE(crc_test crc_test.c)
TARGET_LINK_LIBRARIES(crc_test lte)
ADD_TEST(crc_24A crc_test -n 5000 -l 24 -p 0x1864CFB -s 1)
ADD_TEST(crc_24B crc_test -n 5000 -l 24 -p 0x1800063 -s 1)
ADD_TEST(crc_16 crc_test -n 5000 -l 16 -p 0x11021 -s 1)
ADD_TEST(crc_8 crc_test -n 5000 -l 8 -p 0x19B -s 1)
ADD_TEST(crc_24A crc_test -n 5001 -l 24 -p 0x1864CFB -s 1)
ADD_TEST(crc_24B crc_test -n 5001 -l 24 -p 0x1800063 -s 1)
ADD_TEST(crc_16 crc_test -n 5001 -l 16 -p 0x11021 -s 1)
ADD_TEST(crc_8 crc_test -n 5001 -l 8 -p 0x19B -s 1)

View file

@ -35,12 +35,11 @@
#include <time.h>
#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);

View file

@ -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}
};