This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I need a CRC-8 bit XFP complient CRC piece of code

I need a XFP complient CRC code module. Or I need to validate the one that I got. So any help (pointers to code, pointers to XFP CRC spec, notes on how to validate) would help.
Sean

Parents
  • "...come and be satisified.

    OK Sean. Good job. To your contribution, I'll add another that uses a nibble-oriented feedback table for situations where one is tight on code space (for the table) and can give up a little performance.

    /****************************************************************************
     *
     *     NAME: Crc8()
     *  PURPOSE: Generate 8-bit CRC.
     *
     *  SYNOPSIS:   unsigned char Crc8(unsigned char b, unsigned char initCrc);
     *
     *  INTERFACE PARAMETERS:
     *
     *      Name                 FG-IO  Description
     *      -------------------  -----  -----------------------------------------
     *      b                    F  I   Data byte to CRC (MSBit first).
     *      initCrc              F  I   Initial CRC or CRC accumulator for
     *                                  continuation.
     *      <return value>           O  CRC.
     *
     *  DESCRIPTION:
     *
     *      The function returns the 8-bit CRC after including "b" with the
     *      initial CRC "initCRC".
     *
     ****************************************************************************/
    
    unsigned char Crc8(unsigned char b, unsigned char initCrc)
    {
        static const unsigned char code feedback[16] =
        {
            0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
            0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D
        };
    
    #define CRC initCrc
    
        CRC = (CRC << 4) ^ feedback[((CRC     ) ^ b) >> 4];
        CRC = (CRC << 4) ^ feedback[((CRC >> 4) ^ b) & 0x0F];
        return (CRC);
    
    #undef  CRC
    }

Reply
  • "...come and be satisified.

    OK Sean. Good job. To your contribution, I'll add another that uses a nibble-oriented feedback table for situations where one is tight on code space (for the table) and can give up a little performance.

    /****************************************************************************
     *
     *     NAME: Crc8()
     *  PURPOSE: Generate 8-bit CRC.
     *
     *  SYNOPSIS:   unsigned char Crc8(unsigned char b, unsigned char initCrc);
     *
     *  INTERFACE PARAMETERS:
     *
     *      Name                 FG-IO  Description
     *      -------------------  -----  -----------------------------------------
     *      b                    F  I   Data byte to CRC (MSBit first).
     *      initCrc              F  I   Initial CRC or CRC accumulator for
     *                                  continuation.
     *      <return value>           O  CRC.
     *
     *  DESCRIPTION:
     *
     *      The function returns the 8-bit CRC after including "b" with the
     *      initial CRC "initCRC".
     *
     ****************************************************************************/
    
    unsigned char Crc8(unsigned char b, unsigned char initCrc)
    {
        static const unsigned char code feedback[16] =
        {
            0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
            0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D
        };
    
    #define CRC initCrc
    
        CRC = (CRC << 4) ^ feedback[((CRC     ) ^ b) >> 4];
        CRC = (CRC << 4) ^ feedback[((CRC >> 4) ^ b) & 0x0F];
        return (CRC);
    
    #undef  CRC
    }

Children
No data