Electronic – Calculating a simple CRC

ccrcethercat

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 \$x^8+x^2+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.

Best Answer

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 int made_table=0;

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

  if (!made_table) {
    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); */
    }
    made_table=1;
  }
}


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
      */
{
  if (!made_table)
    init_crc8();

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

Taken from: http://www.rajivchakravorty.com/source-code/uncertainty/multimedia-sim/html/crc8_8c-source.html

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