AnsweredAssumed Answered

standard crc-16 with 0x8005 polynomial in k64 mcu hardware CRC module

Question asked by wulin weng on Jul 30, 2015
Latest reply on Aug 3, 2015 by Hui_Ma

Hi, I use a K64F120M and have probelms getting correct CRC-16 result using the built in hardware CRC generator.

the software calculation is below:

/* Table of CRC values for high byte */
const uchar auchCRCHi[] = {
0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U,
0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U,
0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U,
0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U,
0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U,
0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U,
0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U,
0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U,
0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U,
0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U,
0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U,
0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U,
0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U,
0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U,
0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U,
0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U,
0x00U, 0xC1U, 0x81U, 0x40U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x01U, 0xC0U, 0x80U, 0x41U, 0x00U, 0xC1U, 0x81U,
0x40U
} ;

/* Table of CRC values for low byte */
const uchar auchCRCLo[] = {
0x00U, 0xC0U, 0xC1U, 0x01U, 0xC3U, 0x03U, 0x02U, 0xC2U, 0xC6U, 0x06U, 0x07U, 0xC7U, 0x05U, 0xC5U, 0xC4U,
0x04U, 0xCCU, 0x0CU, 0x0DU, 0xCDU, 0x0FU, 0xCFU, 0xCEU, 0x0EU, 0x0AU, 0xCAU, 0xCBU, 0x0BU, 0xC9U, 0x09U,
0x08U, 0xC8U, 0xD8U, 0x18U, 0x19U, 0xD9U, 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U, 0x70U, 0xB0U, 0x50U, 0x90U, 0x91U,
0x51U, 0x93U, 0x53U, 0x52U, 0x92U, 0x96U, 0x56U, 0x57U, 0x97U, 0x55U, 0x95U, 0x94U, 0x54U, 0x9CU, 0x5CU,
0x5DU, 0x9DU, 0x5FU, 0x9FU, 0x9EU, 0x5EU, 0x5AU, 0x9AU, 0x9BU, 0x5BU, 0x99U, 0x59U, 0x58U, 0x98U, 0x88U,
0x48U, 0x49U, 0x89U, 0x4BU, 0x8BU, 0x8AU, 0x4AU, 0x4EU, 0x8EU, 0x8FU, 0x4FU, 0x8DU, 0x4DU, 0x4CU, 0x8CU,
0x44U, 0x84U, 0x85U, 0x45U, 0x87U, 0x47U, 0x46U, 0x86U, 0x82U, 0x42U, 0x43U, 0x83U, 0x41U, 0x81U, 0x80U,
0x40U
} ;

uint CRC16(const uchar puchMsg[], uint32_t usDataLen)

{

uchar uchCRCHi = 0xFFU ; /* high byte of CRC initialized */

uchar uchCRCLo = 0xFFU ; /* low byte of f initialized */

        uint result;

 

uchar uIndex ; /* will index into CRC lookup table */

uint index=0U;

  while (usDataLen--) /* pass through message buffer */

{

  uIndex = uchCRCHi ^ puchMsg[index++] ; /* calculate the CRC */

  uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;

  uchCRCLo = auchCRCLo[uIndex] ;

}

        result = ((uint)uchCRCHi << 8 );

        result |= ((uint)uchCRCLo);

return (result);

}

but I can't get the right crc result to match the software in hardware crc module calculation, and the source code used hardware is below:

uint CRC16_H(const uchar puchMsg[], uint32_t usDataLen)

{

    uint result;

 

uchar uIndex ; /* will index into CRC lookup table */

uint index=0U;

CRC_CTRL &= ~CRC_CTRL_TCRC_MASK;

CRC_CTRL |= CRC_CTRL_TOT(3);

CRC_CTRL |= CRC_CTRL_TOTR(3);

CRC_CTRL |= CRC_CTRL_FXOR_MASK;

CRC_GPOLY = 0x8005U;

CRC_CTRL |= CRC_CTRL_WAS_MASK;

CRC_DATA = 0xFFFFU;

CRC_CTRL &= ~CRC_CTRL_WAS_MASK;

   while (usDataLen--) /* pass through message buffer */

{

  CRC_DATA = puchMsg[index++];

}

result = (uint)CRC_DATA;

 

 

return (result);

}

 

Can you help me to set the register for the crc-16 standard to match the software calculation?

BR,

Outcomes