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.

MSP-EXP430FR2355: confused with understanding of CRC16-CCITT implemented in msp430

Part Number: MSP-EXP430FR2355
Other Parts Discussed in Thread: MSP430FR2355

Hello Team,

Actually i'm trying to understand CRC16 examle code implemented in msp430FR2355 and i'm confused.

no online calculated CRC16-CCITT is matching with results obtained from CRC module(CRCDIRB) for example i'm sending string "BVK" getting CRC=27860, but online CRC16 calculators giving result 0xE2E7.

I tried to send "BVK" + CRC to CRC module(CRCDIRB) to get 0 result but i'm getting something else. If i check with online CRC16 calculator with 0xE2E7 i'm getting 0 as expected. Can't understand where the things going wrong. 

So, with CRC module how to check the CRC correctness? any help would be very thankfull.

Below is the code and i have tried by CRC_Str[]="BVK";

#include <msp430.h>

unsigned int CRC_Init = 0xFFFF;
unsigned int i;
unsigned int CRC_Str[]= {0x0fc0, 0x1096, 0x5042, 0x0010, // 16 random 16-bit numbers
0x7ff7, 0xf86a, 0xb58e, 0x7651, // these numbers can be
0x8b88, 0x0679, 0x0123, 0x9599, // modified if desired
0xc58c, 0xd1e2, 0xe144, 0xb691 };
unsigned int CRC_Results;
unsigned int SW_Results;
unsigned int CRC_New;

// Software Algorithm Function Definition
unsigned int CCITT_Update(unsigned int, unsigned int);

int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1OUT &= ~BIT0; // Clear P1.0 output state
P1DIR |= BIT0; // Set P1.0 to output direction

PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings

// Init CRC
CRCINIRES = CRC_Init; // Init CRC with 0xFFFF

for(i=0;i<16;i++)
{
// Input random values into CRC Hardware
CRCDIRB = CRC_Str[i]; // Input data in CRC
__no_operation();
}
CRC_Results = CRCINIRES; // Save results (per CRC-CCITT standard)

CRC_New = CRC_Init; // Put CRC Init value into CRC_New
for(i = 0; i < 16; i++)
{
// Input values into Software algorithm (requires 8-bit inputs)
// Clear upper 8 bits to get lower byte
unsigned int LowByte = (CRC_Str[i] & 0x00FF);
// Shift right 8 bits to get upper byte
unsigned int UpByte = (CRC_Str[i] >> 8);
// First input lower byte
CRC_New = CCITT_Update(CRC_New,LowByte);
// Then input upper byte
CRC_New = CCITT_Update(CRC_New,UpByte);
}
SW_Results = CRC_New;

// Compare data output results
if(CRC_Results==SW_Results) // if data is equal
P1OUT |= BIT0; // set the LED
else
P1OUT &= ~BIT0; // if not, clear LED

while(1); // infinite loop
}

// Software algorithm - CCITT CRC16 code
unsigned int CCITT_Update(unsigned int init, unsigned int input)
{
unsigned int CCITT;
CCITT =(unsigned char)(init >> 8)|(init << 8);
CCITT ^= input;
CCITT ^= (unsigned char)(CCITT & 0xFF) >> 4;
CCITT ^= (CCITT << 8) << 4;
CCITT ^= ((CCITT & 0xFF) << 4) << 1;
return CCITT;
}

  • Hi vamshi,

    This example code include the software CRC and hardware CRC. And compared the two calculation results, if they are the same, it will set P1.0.

    Maybe the software CRC formula you use is different from the one in the code.

    Thanks!

    Best Regards

    Johnson

  • Thank you for the reply,

    but i understood example well that it was checking between hardware and software CRC. but my issue was different let me explain clearly

    1. I tried to check CRC for a string "BVK" i get something which doesn't match with online CRC-CCITT calculator.

    2. I tried to resend the CRC result to CRC module i'm getting again some value not zero.

    3. How can i check CRC of received of 5 bytes (3bytes + CRC)sent  by master to slave.

    4. I assume XOR CRC result with CRC generated by data should give 0. Is it a wrong assumption? then how can i check error?

    5. I want to use CRC8 so can i generate CRC8 with hardware module?

    Below is the code i have tried.

    #include <msp430.h> 
    
    unsigned int CRC_Init = 0xFFFF;
    unsigned char CRC_Str[] =  "BVK" ;
    
    unsigned int i;
    
    unsigned int CRC_Results,CRC_Results1;
    unsigned int SW_Results;
    unsigned int CRC_New;
    
    // Software Algorithm Function Definition
    unsigned int CCITT_Update(unsigned int, unsigned int);
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
    
        P1OUT &= ~BIT0;                         // Clear P1.0 output state
        P1DIR |= BIT0;                          // Set P1.0 to output direction
    
        PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
                              // to activate previously configured port settings
    
        // Init CRC
        CRCINIRES = CRC_Init;                 // Init CRC with 0xFFFF
    
        for (i = 0; i < 3; i++)
        {
            // Input random values into CRC Hardware
            CRCDIRB = CRC_Str[i];             // Input data in CRC
            __no_operation();
        }
        CRC_Results1 = CRCINIRES;             // Save results (per CRC-CCITT standard)
    
        CRCINIRES = CRC_Init;                 // Init CRC with 0xFFFF
        for (i = 0; i < 3; i++)
         {
             // Input random values into CRC Hardware
             CRCDIRB = CRC_Str[i];             // Input data in CRC
             __no_operation();
         }
         CRCDIRB = CRC_Results;
         //CRC_Results ^= CRCINIRES;
         CRC_Results = CRCINIRES;             // Here i should get 0 ???
         CRC_Results = CRC_Results ^ CRC_Results1; 
    
        while (1);                     // infinite loop
    }
    
    

  • In the GCC headers at least CRCDIRB is defined as a word register so writing a byte to it is going to cause trouble. CRCDIRB_L is what I use when adding a byte of data to a CRC.

  • Thank you David, 

    That solved my issue but i still have a confusion about

    * If i send CRC result back to CRC module will i get 0 as a result?

    To check the correctness of string with CRC.

  •      CRCDIRB = CRC_Results;

    Did you mean to

         CRCDIRB = CRC_Results1;

    That said, I don't know if this will produce 0. I personally just compare the results.

  • Thank you Bruce,

    I checked and it is not producing 0. Don't know why?

    Is there any possibility to use that CRC16 to generate CRC8?  

  • Prof Koopman has written quite a bit about CRCs. Google for "crc koopman".

**Attention** This is a public forum