This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/BQ76940: example code of BQ76940

Part Number: BQ76940

Tool/software: Code Composer Studio

Dear expert,

My customer has some questions about BQ76940 CRC example code. Please see the blue bold part below, there will be a direct calculation relationship between CRC and ptr. Does the red bold part as below have the same function as the blue bold part below? (In this CRC example code, the crc is operating independently, and I can't find the XOR relationship between crc and the data to be sent ptr, or is it something I misunderstand?) Thanks for your support.

  • CRC Example code:

unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)

{

            unsigned char i;

            unsigned char crc=0;

            while(len--!=0)

            {

                        for(i=0x80; i!=0; i/=2)

                        {

                                    if((crc & 0x80) != 0)

                                    {

                                                crc *= 2;

                                                crc ^= key;

                                    }

                                    else

                                                crc *= 2;

 

                                    if((*ptr & i)!=0) //  

                                                crc ^= key;

                        }

                        ptr++;   

            }

            return(crc);

}

  • Normal CRC dode:

unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)

{

            unsigned char i;

            unsigned char crc=0;

        while(len--)

        {

            crc ^= *ptr++;  /* Each time XOR with the data that needs to be calculated, and the calculation will point to the next data */  

            for (i=8; i>0; --i)   /* The following calculation process is the same as calculating a byte crc */  

            { 

                if (crc & 0x80)

                    crc = (crc << 1) ^ 0x31;

                else

                    crc = (crc << 1);

            }

        }

 

return (crc); 

}

------------------------------

 

  • Hi Minqi,

    This line is doing an XOR with the key:

    crc ^= key;

    It is equivalent to crc = crc ^ key; 

    Where does the "normal CRC code" come from? I am not sure these functions will return the same results. I tried using both of them on the same data using this online compiler and had different results. https://www.onlinegdb.com/online_c_compiler

    Here is my code I used to test in the online compiler:

    #include <stdio.h>
    unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)
    {
        unsigned char i;
        unsigned char crc=0;
        while(len--!=0)
        {
            for(i=0x80; i!=0; i/=2)
            {
                if((crc & 0x80) != 0)
                {
                    crc *= 2;
                    crc ^= key;
                }
                else
                    crc *= 2;
                if((*ptr & i)!=0) //  
                    crc ^= key;
            }
            ptr++;   
        }
    return(crc);
    }
    
    unsigned char CRC8_Norm(unsigned char *ptr, unsigned char len,unsigned char key)
    {
        unsigned char i;
        unsigned char crc=0;
        while(len--)
        {
            crc ^= *ptr++;  /* Each time XOR with the data that needs to be calculated, and the calculation will point to the next data */  
            for (i=8; i>0; --i)   /* The following calculation process is the same as calculating a byte crc */  
            { 
                if (crc & 0x80)
                    crc = (crc << 1) ^ 0x31;
                else
                    crc = (crc << 1);
            }
        }
    return (crc); 
    }
    
    int main()
    {
        
        unsigned char data1[6];
        data1[0] = 0x31;
        data1[1] = 0x49;
        data1[2] = 0x55;
        unsigned int key = 0x107;
        
        printf("data = 0x%.2x \n", data1[0]);
        
        unsigned char crc1 = CRC8(data1,3,key);
    
        printf("crc1 = 0x%.2x \n", crc1);
        
        unsigned char crc2 = CRC8_Norm(data1,3,key);
    
        printf("crc2 = 0x%.2x \n", crc2);
        return 0;
    }

    Best regards,

    Matt