# Calculating a simple CRC

In the datasheet for the ET1200 EtherCAT ASIC (page 94), I am told that I need to calculate a CRC of some of the 16-bit data in its EEPROM. The only description of this CRC is:

Low byte [of word 7] contains remainder of division of word 0 to word 6 as unsigned number divided by the polynomial x8+x²+x+1 (initial value 0xFF).

For some reason, reading the Wikipedia page on Calculating a CRC makes my brain melt. Especially since the example code is written in a special language.

Can someone please just tell me what I need to add to what, and shift where and whatnot? In C preferably.

-

This sounds like CRC8.

/*
* crc8.c
*
* Computes a 8-bit CRC
*
*/

#include <stdio.h>

#define GP  0x107   /* x^8 + x^2 + x + 1 */
#define DI  0x07

static unsigned char crc8_table[256];     /* 8-bit table */

static void init_crc8()
/*
* Should be called before any other crc function.
*/
{
int i,j;
unsigned char crc;

for (i=0; i<256; i++) {
crc = i;
for (j=0; j<8; j++)
crc = (crc << 1) ^ ((crc & 0x80) ? DI : 0);
crc8_table[i] = crc & 0xFF;
/* printf("table[%d] = %d (0x%X)\n", i, crc, crc); */
}
}
}

void crc8(unsigned char *crc, unsigned char m)
/*
* For a byte array whose accumulated crc value is stored in *crc, computes
* resultant crc obtained by appending m to the byte array
*/
{
init_crc8();

*crc = crc8_table[(*crc) ^ m];
*crc &= 0xFF;
}


http://sbs-forum.org/marcom/dc2/20_crc-8_firmware_implementations.pdf

C implementations without lookup table (especially good for the 8-bit CPU optimised function):

http://websvn.hylands.org/filedetails.php?repname=Projects&path=%2Fcommon%2FCrc8.c&sc=1

-
Thanks! Now that I know it has a proper name, suddenly I can find example code for it on Google. –  Rocketmagnet Jun 13 '12 at 21:34
@joby, glad to see you again! Mind bringing some of the information from those sources over so we are not link rot sensitive? –  Kortuk Jun 13 '12 at 22:04
@Kortuk Fair point, there you go –  Toby Jaffey Jun 14 '12 at 9:08
@JobyJaffey, and there you go. Hope you hang around and answer some more questions! –  Kortuk Jun 14 '12 at 12:49