Hi there,
I'm doing experiments on CRC module in CC430F5137. I'm comparing Software generated CRC and Hardware (CC430's CRC module) generated CRC, Somehow they are not matching.
Please let me know where I'm wrong.
??
Here is code:
/**************************************************************************
//
// crc16.c - generate a ccitt 16 bit cyclic redundancy check (crc)
//
// The code in this module generates the crc for a block of data.
//
**************************************************************************/
/*
// 16 12 5
// The CCITT CRC 16 polynomial is X + X + X + 1.
// In binary, this is the bit pattern 1 0001 0000 0010 0001, and in hex it
// is 0x11021.
// A 17 bit register is simulated by testing the MSB before shifting
// the data, which affords us the luxury of specifiy the polynomial as a
// 16 bit value, 0x1021.
// Due to the way in which we process the CRC, the bits of the polynomial
// are stored in reverse order. This makes the polynomial 0x8408.
*/
#include<msp430.h>
#include <stdlib.h>
#include <inttypes.h>
#include "CC430F5137.h"
#define POLY 0x8408
/*
// note: when the crc is included in the message, the valid crc is:
// 0xF0B8, before the compliment and byte swap,
// 0x0F47, after compliment, before the byte swap,
// 0x470F, after the compliment and the byte swap.
*/
unsigned int result_crc_SW;
unsigned int result_crc_HW;
/**************************************************************************
//
// crc16() - generate a 16 bit crc
//
//
// PURPOSE
// This routine generates the 16 bit remainder of a block of
// data using the ccitt polynomial generator.
//
// CALLING SEQUENCE
// crc = crc16(data, len);
//
// PARAMETERS
// data <-- address of start of data block
// len <-- length of data block
//
// RETURNED VALUE
// crc16 value. data is calcuated using the 16 bit ccitt polynomial.
//
// NOTES
// The CRC is preset to all 1's to detect errors involving a loss
// of leading zero's.
// The CRC (a 16 bit value) is generated in LSB MSB order.
// Two ways to verify the integrity of a received message
// or block of data:
// 1) Calculate the crc on the data, and compare it to the crc
// calculated previously. The location of the saved crc must be
// known.
/ 2) Append the calculated crc to the end of the data. Now calculate
// the crc of the data and its crc. If the new crc equals the
// value in "crc_ok", the data is valid.
//
// PSEUDO CODE:
// initialize crc (-1)
// DO WHILE count NE zero
// DO FOR each bit in the data byte, from LSB to MSB
// IF (LSB of crc) EOR (LSB of data)
// crc := (crc / 2) EOR polynomial
// ELSE
// crc := (crc / 2)
// FI
// OD
// OD
// 1's compliment and swap bytes in crc
// RETURN crc
//
**************************************************************************/
unsigned short crc16(data_p, length)
char *data_p;
unsigned short length;
{
unsigned char i;
unsigned int data;
unsigned int crc;
crc = 0xffff;
if (length == 0)
return (~crc);
do {
for (i = 0, data = (unsigned int)0xff & *data_p++; i < 8; i++, data >>= 1)
{
if ((crc & 0x0001) ^ (data & 0x0001))
crc = (crc >> 1) ^ POLY;
else
crc >>= 1;
}
} while (--length);
crc = ~crc;
data = crc;
crc = (crc << 8) | (data >> 8 & 0xFF);
return (crc);
}
void main(void)
{
int i;
char arr[10] = {'0','1','2','3','4','5','6','7','8','9'};
// Call func. to calc. CRC-CCITT
result_crc_SW = crc16(&arr[0], 10); // S/w generated CRC
CRCINIRES = 0x0FFFF;
for(i=0; i<10; i++)
{
CRCDI= arr[i];
}
result_crc_HW = CRCINIRES; // H/w generated CRC
}
Thank you,
Rahul