7
\$\begingroup\$

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.

\$\endgroup\$
10
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Thanks! Now that I know it has a proper name, suddenly I can find example code for it on Google. \$\endgroup\$ – Rocketmagnet Jun 13 '12 at 21:34
  • \$\begingroup\$ @joby, glad to see you again! Mind bringing some of the information from those sources over so we are not link rot sensitive? \$\endgroup\$ – Kortuk Jun 13 '12 at 22:04
  • \$\begingroup\$ @Kortuk Fair point, there you go \$\endgroup\$ – Toby Jaffey Jun 14 '12 at 9:08
  • \$\begingroup\$ @JobyJaffey, and there you go. Hope you hang around and answer some more questions! \$\endgroup\$ – Kortuk Jun 14 '12 at 12:49
  • \$\begingroup\$ One problem i have with this code is that the polynomial, GP, isn't used anywhere within the code. So what polynomial is the CRC using to calculate it's CRC table :( DI is used however. \$\endgroup\$ – Owl Jul 2 '18 at 9:27
1
\$\begingroup\$

That sounds like the CRC-8/ROHC algorithm. According to the Catalogue of parametrised CRC algorithms, its parameters are:

width=8 poly=0x07 init=0xff refin=true refout=true xorout=0x00 check=0xd0 name="CRC-8/ROHC"

Note that CRC-8 uses the same polynomial, but uses an initial value of 0x00, not 0xFF. It is also a "non-reflected" algorithm (bits are MSbit first). Its parameters are:

width=8 poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4 name="CRC-8"

These parameters can be used with various tools that can calculate CRCs or generate CRC code, such as Online CRC calculation or pycrc, a free CRC source code generator.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.