Search code examples
csmartcardcrcnxp-microcontroller

What is the difference between CRC-16/CCITT-FALSE and CRC-16/X-25?


I want to implement CRC-16/X-25. I have a code written in C for CRC-16/CCITT_FALSE, but I don't know what changes do I need to make in that code for CRC-16/X-25 implementation?

The code I am using for CRC-16/CCITT-FALSE is given below:

#include  <msp430.h>

#define CRC_POLYNOMIAL      0x1021
#define USHORT_TOPBIT       0x8000

short crc_ret;

// function initialisation

unsigned short crc16(const char *buf, unsigned int buf_len)
{

    unsigned short ret = 0xFFFF;        // Initial Value
    int bit = 8;

    do {
        if (!buf) {
            break;
        }
        unsigned int i = 0;
        for (i=0; i<buf_len; ++i) {
            ret = (unsigned short)(ret ^ ((unsigned short)buf[i] >> 8));
            for (bit = 8; bit > 0; --bit)
            {
                if (ret & USHORT_TOPBIT)
                    ret = (unsigned short)((ret >> 1) ^ CRC_POLYNOMIAL);
                else
                    ret = (unsigned short)(ret >> 1);
            }
        }
    } while (0);
    return ret;
 }

int main(void)
{
  PM5CTL0 &= ~LOCKLPM5;               // Disable the GPIO power-on default high-impedance mode to activate
                                      // previously configured port settings

  char buf[16];


  buf[0] = 0x5A;                    //buf[0], buf[1] and buf[2] are data bytes

  buf[1] = 0xCF;
  buf[2] = 0x00;



  crc_ret = crc16(buf, 3);   // Calling CRC function

}


Solution

  • According to the website below, CRC16/CCITT_FALSE doesn't match your code. The website indicates it's a left shifting (non-reflected) CRC, while your code is right shifting.

    CRC16/X25 is a right shifting (reflected) CRC, and returns ~CRC (CRC xor 0xFFFF). I don't know if this is the same as CRC16/CCITT_X25.

    http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

    You can use the website to compare your results with it's results.

    I started with a simple example to create the two crc16 functions:

    uint16_t crc16_ccitt_false(char* pData, int length)
    {
        int i;
        uint16_t wCrc = 0xffff;
        while (length--) {
            wCrc ^= *(unsigned char *)pData++ << 8;
            for (i=0; i < 8; i++)
                wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
        }
        return wCrc;
    }
    
    uint16_t crc16_x25(char* pData, int length)
    {
        int i;
        uint16_t wCrc = 0xffff;
        while (length--) {
            wCrc ^= *(unsigned char *)pData++ << 0;
            for (i=0; i < 8; i++)
                wCrc = wCrc & 0x0001 ? (wCrc >> 1) ^ 0x8408 : wCrc >> 1;
        }
        return wCrc ^ 0xffff;
    }