cancel
Showing results for 
Search instead for 
Did you mean: 

C/C++ Example for CRC Calculation

knut2
Associate II
Posted on March 19, 2013 at 18:37

Hi,

does anybody know, where i could find a c/c++ example for calculating a crc like the hardware based unit in a STM32F10x?

Some links in this forum seems to be obsolete.

knut

#crc-stm32f103-algorithm-example
4 REPLIES 4
Posted on March 19, 2013 at 18:45

https://community.st.com/0D50X00009XkbNMSAZ

The CRC processes 32-bits at a time, applying them in a small-endian fashion. It is not performed byte wise in the manner most online testers use.It can be computed on the STM32 or x86 PC with the following code. The nibble table method is a trade off between speed and space.DWORD Crc32(DWORD Crc, DWORD Data){int i;Crc = Crc ^ Data;for(i=0; i<32; i++)if (Crc & 0x80000000)Crc = (Crc << 1) ^ 0x04C11DB7; // Polynomial used in STM32elseCrc = (Crc << 1);return(Crc);}DWORD Crc32Fast(DWORD Crc, DWORD Data){static const DWORD CrcTable[16] = { // Nibble lookup table for 0x04C11DB7 polynomial0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005,0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD };Crc= Crc ^ Data; // Apply all 32-bits// Process 32-bits, 4 at a time, or 8 roundsCrc = (Crc << 4) ^ CrcTable[Crc >> 28]; // Assumes 32-bit reg, masking index to 4-bitsCrc = (Crc << 4) ^ CrcTable[Crc >> 28]; // 0x04C11DB7 Polynomial used in STM32Crc = (Crc << 4) ^ CrcTable[Crc >> 28];Crc = (Crc << 4) ^ CrcTable[Crc >> 28];Crc = (Crc << 4) ^ CrcTable[Crc >> 28];Crc = (Crc << 4) ^ CrcTable[Crc >> 28];Crc = (Crc << 4) ^ CrcTable[Crc >> 28];Crc = (Crc << 4) ^ CrcTable[Crc >> 28];return(Crc);}{printf('%08X',Crc32(0xFFFFFFFF, 0x12345678)); // 0xDF8A8A2Bprintf('%08X',Crc32Fast(0xFFFFFFFF, 0x12345678)); // 0xDF8A8A2B}

Edit: Fixed DEAD LINK, original post from March 19, 2013

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
knut2
Associate II
Posted on March 20, 2013 at 11:11

Thanks a lot. You are very fast, like your avatar... :)

stmicro2
Associate
Posted on November 12, 2013 at 20:25

https://leventozturk.com/engineering/crc/

kristian faulkner
Associate
Posted on February 14, 2018 at 18:35

Greetings all,

I independently wrote this C# code and verified this is working. Enjoy.

I also want to add that I spent a lot of time working with the Keil debugger --

DO NOT TRUST THE DEBUGGER FOR CRC. Always work out your CRC value using code, not the debug windows. The debug window gives you an incorrect CRC value (e.g. 0x12345678 -> 0x877BB728 and not the correct value of 0xDF8A8A2B).

   

public

static

UInt32

ComputeSTM32Checksum

(

UInt32

[

]

inputData

,

UInt32 initial

=

0xFFFFFFFF

,

UInt32 polynomial

=

0x04C11DB7

)

   

{

      UInt32 crc

=

initial

;

     

foreach

(

UInt32 current

in

inputData

)

     

{

        crc

^

=

current

;

       

// Process all the bits in input data.

       

for

(

uint

bitIndex

=

0

;

(

bitIndex

<

32

)

;

++

bitIndex

)

       

{

         

// If the MSB for CRC == 1

         

if

(

(

crc

&

0x80000000

)

!=

0

)

         

{

            crc

=

(

(

crc

<

<

1

)

^

polynomial

)

;

         

}

         

else

         

{

            crc

<

<=

1

;

         

}

       

}

     

}

     

return

crc

;

   

}

������������������������������������������������

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

�

Tests to check:

uint

result1

=

ComputeSTM32Checksum

(

new

uint

[

]

{

0x12345678

}

)

;

Trace

.

Assert

(

result1

==

0xDF8A8A2B

)

;

uint

result2

=

ComputeSTM32Checksum

(

new

uint

[

]

{

0x8E09BAF6

}

)

;

Trace

.

Assert

(

result2

==

0x5C00CC44

)

;

uint

result3

=

ComputeSTM32Checksum

(

new

uint

[

]

{

0x12345678

,

0x8E09BAF6

}

)

;

Trace

.

Assert

(

result3

==

0x04F1F147

)

;

uint

result4

=

ComputeSTM32Checksum

(

new

uint

[

]

{

0x8E09BAF6

,

0x12345678

}

)

;

Trace

.

Assert

(

result4

==

0x90B2EE2D

)

;

��������

�

�

�

�

�

�

�

�