CRC for polynomial 0xedb88320

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CRC for polynomial 0xedb88320

5,091 Views
suhas_prabhu
Contributor I

Hello,

I am facing the issue with CRC32 calculation using hardware CRC functions. I tried with the "0xedb88320" for MK22FN512VLL12. I am not able to get the CRC value right.

Is there any sample code (with hardware configuration values set and working) that can  provide a comparison to calculate CRC32 using hardware and software ? 

I hope i can use the CRC hardware function from K22.

I have a reference table from  

link : //https://publications.qld.gov.au/dataset/8ffa941e-6efc-43fb-a207-202018baf3d2/resource/8c62a217-35db-419f-b625-7db213382ab7/download/hashingalgorithms-v1-6.pdf

unsigned long int CRC32Table[] = { /* CRC polynomial 0xedb88320 */
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535,
0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, ..................

};

Hope to get some help on that.

Regards,

Suhas

Labels (1)
0 Kudos
2 Replies

4,615 Views
mjbcswitzerland
Specialist V

Hi

Here is the CRC32 test code from the uTasker project, including errate 2776 workaround (not be needed for K22), which checks the CRC32 of an Ethernet frame (with CRC32 at end of the input data)

        if (rx_frame->frame_size != 0) {
            unsigned long ulLength = rx_frame->frame_size;
            unsigned long *ptr = (unsigned long *)rx_frame->ptEth;
            POWER_UP_ATOMIC(6, CRC);                                     // power up the CRC module
            CRC_CTRL = (CRC_CTRL_TCRC_32 | CRC_CTRL_TOTR_BITS_BYTES | CRC_CTRL_TOT_BITS_BYTES | CRC_CTRL_FXOR); // mode required for CRC-32 IEEE compatibility
            CRC_GPOLY = 0x04c11db7;                                      // set the polynomial
            CRC_CTRL = (CRC_CTRL_TCRC_32 | CRC_CTRL_WAS | CRC_CTRL_TOTR_BITS_BYTES | CRC_CTRL_TOT_BITS_BYTES | CRC_CTRL_FXOR); // enable write of seed
            CRC_CRC = 0xffffffff;                                        // seed is 0xffffffff
            CRC_CTRL = (CRC_CTRL_TCRC_32 | CRC_CTRL_TOTR_BITS_BYTES | CRC_CTRL_TOT_BITS_BYTES | CRC_CTRL_FXOR); // switch back to data input mode
            while (ulLength >= 4) {                                      // main part using long words
                CRC_CRC = *ptr++;                                        // process long world
                ulLength -= 4;                                           // by bytes at a time - the CRC calculation requires 2 clock cycles
            }
        #if !defined ERRATA_ID_2776
            if (ulLength != 0) {                                         // remaining bytes that didn't fit into a long word
                unsigned char *ptrByte = (unsigned char *)ptr;
                CRC_CRC_HU = *ptrByte++;                                 // write bytes starting with MSB
                ulLength--;
                if (ulLength != 0) {
                    CRC_CRC_HL = *ptrByte++;
                    ulLength--;
                    if (ulLength != 0) {
                        CRC_CRC_LU = *ptrByte++;
                        ulLength--;
                    }
                }
                ptr = (unsigned long *)ptrByte;
            }
        #else                                                            // errata 2776 workaround
            if (ulLength != 0) {                                         // remaining bytes that didn't fit into a long word
            #if !defined _COMPILE_KEIL
                register unsigned long ull = 0;
                register int i;
            #else
                register unsigned long ull;
            #endif
                CRC_CTRL = (CRC_CTRL_TCRC_32 | CRC_CTRL_TOTR_BITS_BYTES | CRC_CTRL_FXOR); // switch to mode without swapping
            #if defined _COMPILE_KEIL
                ull = __rbit(*ptr);                                      // swap all bits of long word
            #else
                // To do - insert asm code for other compilers if required
                //
                for (i = 0; i < 32; i++) {
                    if (*ptr & (0x80000000 >> i)) {
                        ull |= (0x00000001 << i);
                    }
                }
            #endif
                CRC_CRC_LL = (unsigned char)(ull >> 24);                 // swap byte
                ulLength--;
                if (ulLength != 0) {
                    CRC_CRC_LU = (unsigned char)(ull >> 16)              // swap byte
                    ulLength--;
                    if (ulLength != 0) {
                        CRC_CRC_HL = (unsigned char)(ull >> 8);          // swap byte
                        ulLength--;
                    }
                }
            }
        #endif
            if (CRC_CRC == *ptr) {                                       // check the CRC that was calculated with that received at the end of the Ethernet frame
                // CRC-32 matches
            }
            else {
                // CRC-32 mismatch
            }
        }
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Regards

Mark


Complete Kinetis solutions for professional needs, training and support: http://www.utasker.com/kinetis.html
Kinetis K22:
- http://www.utasker.com/kinetis/FRDM-K22F.html
- http://www.utasker.com/kinetis/TWR-K22F120M.html
- http://www.utasker.com/kinetis/BLAZE_K22.html
- http://www.utasker.com/kinetis/tinyK22.html

uTasker: supporting >1'000 registered Kinetis users get products faster and cheaper to market
Request Free emergency remote desk-top consulting at http://www.utasker.com/services.html

Open Source version at https://github.com/uTasker/uTasker-Kinetis

0 Kudos

4,615 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Suhas,
Please refer the description of CRC32 Demystified:

CRC32 0xEDB88320 0x04C11DB7.png

There is crc example in MCUXpresso SDK_2.6.0_MK22FN512xxx12(...SDK_2.6.0_MK22FN512xxx12\boards\frdmk22f\driver_examples\crc)

In that example, we will get checksum32 == checkCrc32(0xcbf43926u).

Please have a try on that example.

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos