7

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.

Adam Haun
  • 21,331
  • 4
  • 50
  • 91
Rocketmagnet
  • 26,933
  • 17
  • 92
  • 177
  • 1
    I would strongly recommend you watching Ben Easter channel. He explained it so well! Theory: https://www.youtube.com/watch?v=izG7qT0EpBw Practice: https://www.youtube.com/watch?v=sNkERQlK8j8 – andz Apr 21 '21 at 22:38

2 Answers2

10

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

Toby Jaffey
  • 28,796
  • 19
  • 96
  • 150
  • 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 09:08
  • @JobyJaffey, and there you go. Hope you hang around and answer some more questions! – Kortuk Jun 14 '12 at 12:49
  • 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. – Owl Jul 02 '18 at 09:27
  • Do you have an example of how these functions are used and initialised? – Owl Jul 02 '18 at 09:40
1

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.

Craig McQueen
  • 743
  • 1
  • 5
  • 13